public void CanCompareCollections()
        {
            var ints         = new[] { 1, 2, 3 };
            var otherInts    = new[] { 4, 5, 6 };
            var longerInts   = new[] { 1, 2, 3, 4 };
            var strings      = new[] { "hello", "bonjour", "guten tag", "你好", "aloha" };
            var otherStrings = new[] { "good bye", "au revoir", "auf wiedersehen", "再見", "aloha" };

            var intComparer    = new ModelComparer <int[]>();
            var stringComparer = new ModelComparer <string[]>();

            // We have to use Assert.True/False instead of .Equal/.NotEqual or we won't actually exercise the ModelComparer's collection handling.
            // Also, we use deep copies for some comparisons to make sure the ModelComparer isn't falling back on reference equality.
            Assert.True(intComparer.Equals(ints, ints));
            Assert.True(stringComparer.Equals(strings, strings));

            Assert.True(intComparer.Equals(ints, (int[])ints.Clone()));
            Assert.True(stringComparer.Equals((string[])strings.Clone(), strings));

            Assert.False(intComparer.Equals(ints, otherInts));
            Assert.False(stringComparer.Equals(otherStrings, strings));

            Assert.False(intComparer.Equals(ints, new[] { 7 }));
            Assert.False(stringComparer.Equals(new[] { "who?" }, strings));

            // Test collections of varying lengths.
            Assert.False(intComparer.Equals(ints, longerInts));
            Assert.False(intComparer.Equals(longerInts, ints));
        }
예제 #2
0
        public void NullEqualsEmptyCollection()
        {
            IEnumerable <int> nullCollection = null;
            var emptyArray = new int[0];
            var emptyList  = new List <int>();

            var comparer = new ModelComparer <IEnumerable <int> >();

            Assert.True(comparer.Equals(nullCollection, emptyArray));
            Assert.True(comparer.Equals(emptyArray, nullCollection));
            Assert.True(comparer.Equals(nullCollection, emptyList));
            Assert.True(comparer.Equals(emptyList, nullCollection));
        }
        public void CanCompareDictionaries()
        {
            var x = new Dictionary <string, double>()
            {
                { "pi", 3.14 }, { "e", 2.718 }
            };
            var notX = new Dictionary <string, double>()
            {
                { "m", 13.3 }, { "v", 77 }
            };

            var y = new Dictionary <string, object>()
            {
                { "a", "test" }, { "b", "test2" }
            };
            var notY = new Dictionary <string, object>()
            {
                { "w", "other" }, { "u", "side" }
            };

            var doubleComparer = new ModelComparer <Dictionary <string, double> >();
            var stringComparer = new ModelComparer <Dictionary <string, object> >();

            // We have to use Assert.True/False instead of .Equal/.NotEqual or we won't actually exercise the ModelComparer's collection handling.
            // Also, we use deep copies for some comparisons to make sure the ModelComparer isn't falling back on reference equality.
            Assert.True(doubleComparer.Equals(x, x));
            Assert.True(stringComparer.Equals(y, y));

            Assert.True(doubleComparer.Equals(x, new Dictionary <string, double>(x)));
            Assert.True(stringComparer.Equals(new Dictionary <string, object>(y), y));

            Assert.False(doubleComparer.Equals(x, notX));
            Assert.False(stringComparer.Equals(notY, y));

            Assert.False(doubleComparer.Equals(x, new Dictionary <string, double>()
            {
                { "r", 12.0 }
            }));
            Assert.False(stringComparer.Equals(new Dictionary <string, object>()
            {
                { "c", "other" }
            }, y));
        }
        public void CanCompareDifferentEnumerableTypes()
        {
            var intArray = new[] { 1, 2, 3 };
            var intList  = new List <int>()
            {
                1, 2, 3
            };

            var intComparer = new ModelComparer <IEnumerable <int> >();

            // We have to use Assert.True/False instead of .Equal/.NotEqual or we won't actually exercise the ModelComparer's collection handling.
            Assert.True(intComparer.Equals(intArray, intList));
        }
        public void IEquatableTakesPrecedenceOverIEnumerable()
        {
            var ints  = new BiasedList(1, 2, 3);
            var odds  = new BiasedList(1, 3, 5);
            var evens = new BiasedList(2, 4, 6);

            var comparer = new ModelComparer <BiasedList>();

            Assert.True(comparer.Equals(ints, ints));
            Assert.True(comparer.Equals(ints, new BiasedList(ints)));

            // ints and odds should compare as equal because BiasedList only looks at the first element.
            Assert.True(comparer.Equals(odds, ints));
            Assert.True(comparer.Equals(new BiasedList(odds), ints));

            Assert.False(comparer.Equals(ints, evens));
            Assert.False(comparer.Equals(ints, new BiasedList(evens)));
        }