PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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