PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FluentNHibernate/Testing/Values/List.cs

http://github.com/jagregory/fluent-nhibernate
C# | 112 lines | 92 code | 16 blank | 4 comment | 14 complexity | 0b25b9b4894c0e85eb94c2ba49850137 MD5 | raw file
Possible License(s): BSD-3-Clause, CC-BY-SA-3.0, Apache-2.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using FluentNHibernate.Utils;
  6. namespace FluentNHibernate.Testing.Values
  7. {
  8. public class List<T, TListElement> : Property<T, IEnumerable<TListElement>>
  9. {
  10. private readonly IEnumerable<TListElement> _expected;
  11. private Action<T, Accessor, IEnumerable<TListElement>> _valueSetter;
  12. public List(Accessor property, IEnumerable<TListElement> value)
  13. : base(property, value)
  14. {
  15. _expected = value;
  16. }
  17. public override Action<T, Accessor, IEnumerable<TListElement>> ValueSetter
  18. {
  19. get
  20. {
  21. if (_valueSetter != null)
  22. {
  23. return _valueSetter;
  24. }
  25. return (target, propertyAccessor, value) =>
  26. {
  27. object collection;
  28. // sorry guys - create an instance of the collection type because we can't rely
  29. // on the user to pass in the correct collection type (especially if they're using
  30. // an interface). I've tried to create the common ones, but I'm sure this won't be
  31. // infallible.
  32. if (propertyAccessor.PropertyType.IsAssignableFrom(typeof(ISet<TListElement>)))
  33. {
  34. collection = new HashSet<TListElement>(Expected.ToList());
  35. }
  36. else if (propertyAccessor.PropertyType.IsArray)
  37. {
  38. collection = Array.CreateInstance(typeof(TListElement), Expected.Count());
  39. Array.Copy((Array)Expected, (Array)collection, Expected.Count());
  40. }
  41. else
  42. {
  43. collection = new List<TListElement>(Expected);
  44. }
  45. propertyAccessor.SetValue(target, collection);
  46. };
  47. }
  48. set { _valueSetter = value; }
  49. }
  50. protected IEnumerable<TListElement> Expected
  51. {
  52. get { return _expected; }
  53. }
  54. public override void CheckValue(object target)
  55. {
  56. var actual = PropertyAccessor.GetValue(target) as IEnumerable;
  57. AssertGenericListMatches(actual, Expected);
  58. }
  59. private void AssertGenericListMatches(IEnumerable actualEnumerable, IEnumerable<TListElement> expectedEnumerable)
  60. {
  61. if (actualEnumerable == null)
  62. {
  63. throw new ArgumentNullException("actualEnumerable",
  64. "Actual and expected are not equal (actual was null).");
  65. }
  66. if (expectedEnumerable == null)
  67. {
  68. throw new ArgumentNullException("expectedEnumerable",
  69. "Actual and expected are not equal (expected was null).");
  70. }
  71. List<object> actualList = new List<object>();
  72. foreach (var item in actualEnumerable)
  73. {
  74. actualList.Add(item);
  75. }
  76. var expectedList = expectedEnumerable.ToList();
  77. if (actualList.Count != expectedList.Count)
  78. {
  79. throw new ApplicationException(String.Format("Actual count ({0}) does not equal expected count ({1})", actualList.Count, expectedList.Count));
  80. }
  81. var equalsFunc = (EntityEqualityComparer != null) ? ((a, b) => EntityEqualityComparer.Equals(a, b)): new Func<object, object, bool>(Equals);
  82. for (var i = 0; i < actualList.Count; i++)
  83. {
  84. if (equalsFunc(actualList[i], expectedList[i]))
  85. {
  86. continue;
  87. }
  88. var message = String.Format("Expected '{0}' but got '{1}' at position {2}",
  89. expectedList[i],
  90. actualList[i],
  91. i);
  92. throw new ApplicationException(message);
  93. }
  94. }
  95. }
  96. }