fluent-nhibernate /src/FluentNHibernate/Testing/Values/List.cs

Language C# Lines 120
MD5 Hash 3122808af5f11b655fb9d783e9cf688c
Repository https://github.com/Autobase/fluent-nhibernate.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Iesi.Collections;
using Iesi.Collections.Generic;
using FluentNHibernate.Utils;

namespace FluentNHibernate.Testing.Values
{
    public class List<T, TListElement> : Property<T, IEnumerable<TListElement>>
    {
        private readonly IEnumerable<TListElement> _expected;
        private Action<T, Accessor, IEnumerable<TListElement>> _valueSetter;

        public List(Accessor property, IEnumerable<TListElement> value)
            : base(property, value)
        {
            _expected = value;
        }

        public override Action<T, Accessor, IEnumerable<TListElement>> ValueSetter
        {
            get
            {
                if (_valueSetter != null)
                {
                    return _valueSetter;
                }

                return (target, propertyAccessor, value) =>
                {
                    object collection;

                    // sorry guys - create an instance of the collection type because we can't rely
                    // on the user to pass in the correct collection type (especially if they're using
                    // an interface). I've tried to create the common ones, but I'm sure this won't be
                    // infallible.
                    if (propertyAccessor.PropertyType.IsAssignableFrom(typeof(Iesi.Collections.Generic.ISet<TListElement>)))
                    {
                        collection = new HashedSet<TListElement>(Expected.ToList());
                    }
                    else if (propertyAccessor.PropertyType.IsAssignableFrom(typeof(ISet)))
                    {
                        collection = new HashedSet((ICollection)Expected);
                    }
                    else if (propertyAccessor.PropertyType.IsArray)
                    {
                        collection = Array.CreateInstance(typeof(TListElement), Expected.Count());
                        Array.Copy((Array)Expected, (Array)collection, Expected.Count());
                    }
                    else
                    {
                        collection = new List<TListElement>(Expected);
                    }

                    propertyAccessor.SetValue(target, collection);
                };
            }
            set { _valueSetter = value; }
        }

        protected IEnumerable<TListElement> Expected
        {
            get { return _expected; }
        }

        public override void CheckValue(object target)
        {
            var actual = PropertyAccessor.GetValue(target) as IEnumerable;
            AssertGenericListMatches(actual, Expected);
        }

        private void AssertGenericListMatches(IEnumerable actualEnumerable, IEnumerable<TListElement> expectedEnumerable)
        {
            if (actualEnumerable == null)
            {
                throw new ArgumentNullException("actualEnumerable",
                    "Actual and expected are not equal (actual was null).");
            }
            if (expectedEnumerable == null)
            {
                throw new ArgumentNullException("expectedEnumerable",
                    "Actual and expected are not equal (expected was null).");
            }

            List<object> actualList = new List<object>();
            foreach (var item in actualEnumerable)
            {
                actualList.Add(item);
            }

            var expectedList = expectedEnumerable.ToList();

            if (actualList.Count != expectedList.Count)
            {
                throw new ApplicationException(String.Format("Actual count ({0}) does not equal expected count ({1})", actualList.Count, expectedList.Count));
            }

            var equalsFunc = (EntityEqualityComparer != null)
                ? new Func<object, object, bool>((a, b) => EntityEqualityComparer.Equals(a, b))
                : new Func<object, object, bool>(Equals);

            for (var i = 0; i < actualList.Count; i++)
            {
                if (equalsFunc(actualList[i], expectedList[i]))
                {
                    continue;
                }

                var message = String.Format("Expected '{0}' but got '{1}' at position {2}",
                    expectedList[i],
                    actualList[i],
                    i);

                throw new ApplicationException(message);
            }
        }
    }
}
Back to Top