public void CompareSomeTypeWithCollectionsInstances()
        {
            var instance1 = new SomeTypeWithCollections {
                ComplexItems =
                {
                    new ComplexType {
                        Text = "a", Value = 1
                    },
                    new ComplexType {
                        Text = "b", Value = 2
                    },
                }
            };

            var instance2 = new SomeTypeWithCollections {
                ComplexItems =
                {
                    new ComplexType {
                        Text = "a", Value = 1
                    },
                    new ComplexType {
                        Text = "c", Value = 2
                    },
                }
            };

            var sut = DeepEqualityComparer.CreateConfiguration().SetIgnoreIndexer(true).CreateEqualityComparer();

            var result = sut.Equals(instance1, instance2);

            Assert.That(result, Is.False);
        }
Ejemplo n.º 2
0
        public void Test()
        {
            var comparer = DeepEqualityComparer.CreateConfiguration()
                           .SetLoggingTextWriter(Console.Out, false)
                           .CreateEqualityComparer();

            var v1 = new Foo(1)
            {
                "x"
            };
            var v2 = new Foo(1)
            {
                "x"
            };
            var v3 = new Foo(1)
            {
                "x", "y"
            };
            var v4 = new Foo(1)
            {
                "y"
            };
            var v5 = new Foo(2)
            {
                "x"
            };

            Assert.IsTrue(comparer.Equals(v1, v2));
            Assert.IsFalse(comparer.Equals(v1, v3));
            Assert.IsFalse(comparer.Equals(v1, v4));
            Assert.IsFalse(comparer.Equals(v1, v4));
            Assert.IsFalse(comparer.Equals(v1, v5)); // fails
        }
        public void ImplicitConversionToIComparer()
        {
            var configuration = DeepEqualityComparer.CreateConfiguration();

            DeepEqualityComparer comparer = configuration;

            Assert.That(1, Is.EqualTo(2).Using(comparer));
        }
Ejemplo n.º 4
0
        public void TreatNullAsEmptyString()
        {
            var comparer = DeepEqualityComparer
                           .CreateConfiguration()
                           .TreatNullAsEmptyString(true)
                           .CreateEqualityComparer();

            Assert.That(comparer.Equals(null, string.Empty), Is.True);
            Assert.That(comparer.Equals(string.Empty, null), Is.True);
        }
Ejemplo n.º 5
0
        public void IgnoreIndexer()
        {
            var so1 = new SomeObjectWithIndexer();
            var so2 = new SomeObjectWithIndexer();

            var comparer = DeepEqualityComparer.CreateConfiguration()
                           .SetIgnoreIndexer(true)
                           .CreateEqualityComparer();

            Assert.That(so1, Is.EqualTo(so2).Using(comparer));
        }
Ejemplo n.º 6
0
        public void ConfigureStringComparison()
        {
            var text1 = "abcd";
            var text2 = "ABCD";

            var comparer =
                DeepEqualityComparer.CreateConfiguration()
                .ConfigureStringComparison(StringComparison.OrdinalIgnoreCase)
                .CreateEqualityComparer();

            Assert.That(text1, Is.EqualTo(text2).Using(comparer));
        }
Ejemplo n.º 7
0
        public void CompareStringWithDifferentCasingAndConfigureStringComparison()
        {
            var text1 = "abcd";
            var text2 = "ABCD";

            var comparer =
                DeepEqualityComparer.CreateConfiguration()
                .ConfigureStringComparison(StringComparison.OrdinalIgnoreCase)
                .CreateEqualityComparer();

            Assert.That(comparer.Equals(text1, text2), Is.True);
        }
Ejemplo n.º 8
0
        public void TreatNullAsEmptyString()
        {
            var so1 = new SomeObject {
                Number = 12, Text = string.Empty
            };
            var so2 = new SomeObject {
                Number = 12, Text = null
            };

            var comparer = DeepEqualityComparer.CreateConfiguration().TreatNullAsEmptyString(true).CreateEqualityComparer();

            Assert.That(so1, Is.EqualTo(so2).Using(comparer));
        }
Ejemplo n.º 9
0
        public void IgnorePropertiesByName()
        {
            var so1 = new SomeObject {
                Number = 12, Text = "abc"
            };
            var so2 = new SomeObject {
                Number = 12, Text = "abcd"
            };

            var comparer = DeepEqualityComparer
                           .CreateConfiguration()
                           .IgnorePropertyByName("Text")
                           .CreateEqualityComparer();

            Assert.That(so1, Is.EqualTo(so2).Using(comparer));
        }
        public void CompareNotEqualInstances()
        {
            var instance1 = new SomeType {
                Text = "T", Number = 12
            };
            var instance2 = new SomeType {
                Text = "X", Number = 12
            };

            var sut = DeepEqualityComparer
                      .CreateConfiguration()
                      .IgnorePropertyByType(typeof(string))
                      .CreateEqualityComparer();

            Assert.That(instance1, Is.EqualTo(instance2).Using(sut));
        }
        public void CompareWithoutTypeSpecificComparer()
        {
            var so1 = new SomeClass {
                Text  = "Shibby",
                Value = 25,
            };

            var so2 = new SomeClass {
                Text  = "Shibby",
                Value = -25,
            };

            var comparer = DeepEqualityComparer.CreateConfiguration().CreateEqualityComparer();

            Assert.That(so1, Is.Not.EqualTo(so2).Using(comparer));
        }
Ejemplo n.º 12
0
        public void TreatNullAsOtherStringIsNotEqual()
        {
            var item1 = new SampleData();
            var item2 = new SampleData()
            {
                Text = "X"
            };

            var sut = DeepEqualityComparer.CreateConfiguration()
                      .TreatNullAs <string>("Y")
                      .CreateEqualityComparer();

            var result = sut.Equals(item1, item2);

            Assert.That(result, Is.False);
        }
Ejemplo n.º 13
0
        public void LogToTextWriter()
        {
            var so1 = new SomeObject {
                Number = 12, Text = "abc"
            };
            var so2 = new SomeObject {
                Number = 12, Text = "abc"
            };

            var textWriter = new StringWriter();

            var comparer = DeepEqualityComparer
                           .CreateConfiguration()
                           .SetLoggingTextWriter(textWriter)
                           .CreateEqualityComparer();

            Assert.That(so1, Is.EqualTo(so2).Using(comparer));
        }
        public void CompareWithTypeSpecificComparer()
        {
            var so1 = new SomeClass {
                Text  = "Shibby",
                Value = 25,
            };

            var so2 = new SomeClass {
                Text  = "Shibby",
                Value = -25,
            };

            var comparer = DeepEqualityComparer.CreateConfiguration()
                           .RegisterEqualityComparerForType(typeof(int), new AbsIntegerEqualityComparer())
                           .CreateEqualityComparer();

            Assert.That(so1, Is.EqualTo(so2).Using(comparer));
        }
        public void LogOnlyNotEqualItems()
        {
            var instance1 = new SomeType {
                Text = "T", Number = 12
            };
            var instance2 = new SomeType {
                Text = "X", Number = 12
            };

            var textWriter = new StringWriter();

            var comparer = DeepEqualityComparer.CreateConfiguration().SetLoggingTextWriter(textWriter, true).CreateEqualityComparer();

            Assert.That(instance1, Is.Not.EqualTo(instance2).Using(comparer));

            var result = textWriter.ToString();

            string expected = @"(root): not equal - x: deepequalitycomparer.tests.CompareWithLoggingTests+SomeType y: deepequalitycomparer.tests.CompareWithLoggingTests+SomeType
  Text: not equal - x: X y: T
";

            Assert.That(result, Is.EqualTo(expected));
        }