PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

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