public void TestSimpleClassHierarchy()
        {
            var leftObject = new Element()
            {
                Name = "root",
                Content = new Element()
                {
                    Name = "element1",
                    Content = new Element()
                        {
                            Name = "element2",
                        }
                }
            };

            var rightObject = new Element()
            {
                Name = "root",
                Content = new Element()
                {
                    Name = "element1",
                    Content = new Element()
                    {
                        Name = "element2",
                    }
                }
            };

            ObjectGraphFactory factory = new PublicPropertyObjectGraphFactory();
            ObjectComparer comparer = new ObjectComparer(factory);
            bool match = comparer.Compare(leftObject, rightObject).None();

            Assert.True(match, "objects did not match");
        }
        public void ComparePropertyThrows()
        {
            Element leftObject = new Element()
            {
                Content = new TypeWithPropertyThatThrows(),
            };

            Element rightObject = new Element()
            {
                Content = new TypeWithPropertyThatThrows(),
            };

            ObjectGraphFactory factory = new PublicPropertyObjectGraphFactory();
            ObjectComparer comparer = new ObjectComparer(factory);
            bool match = comparer.Compare(leftObject, rightObject).None();

            ObjectToConsole(leftObject, factory);

            Assert.True(match, "objects did not match");
        }
        public void CompareTypesDoNotMatch()
        {
            var leftObject = new Element()
            {
                Content = 32,
            };

            var rightObject = new Element()
            {
                Content = "stringvalue",
            };

            ObjectGraphFactory factory = new PublicPropertyObjectGraphFactory();
            ObjectComparer comparer = new ObjectComparer(factory);
            IEnumerable<ObjectComparisonMismatch> mismatches = comparer.Compare(leftObject, rightObject);
            bool match = mismatches.None();

            string[] expectedMismatches = new string[]
            {
                "ObjectTypesDoNotMatch:Left=Element.Content(32) Right=Element.Content(stringvalue)",
            };

            string[] actualMismatches = StringFromMismatches(mismatches);

            Assert.False(match);
            Assert.True(actualMismatches.Length == expectedMismatches.Length);
            for (int index = 0; index < expectedMismatches.Length; index++)
            {
                Assert.Equal(expectedMismatches[index], actualMismatches[index]);
            }
        }
        public void CompareObjectWithSharedNodes()
        {
            Element sharedLeftObject = new Element() { Name = "shared" };
            Element leftObject = new Element()
            {
                Name = "RootElement",
                Content = new List<Element>()
                {
                   sharedLeftObject,
                   sharedLeftObject,
                },
            };

            Element sharedRightObject = new Element() { Name = "shared" };
            Element rightObject = new Element()
            {
                Name = "RootElement",
                Content = new List<Element>()
                {
                   sharedRightObject,
                   sharedRightObject,
                },
            };

            ObjectGraphFactory factory = new PublicPropertyObjectGraphFactory();
            ObjectComparer comparer = new ObjectComparer(factory);
            bool match = comparer.Compare(leftObject, rightObject).None();

            Assert.True(match, "object with loop did not match");
        }
        public void CompareObjectWithLoopInSecondLevel()
        {
            Element leftObject = new Element()
            {
                Name = "RootElement",
                Content = new Element()
                {
                    Name = "ChildElement"
                }
            };

            // child points to root
            ((Element)leftObject.Content).Content = leftObject;

            Element rightObject = new Element()
            {
                Name = "RootElement",
                Content = new Element()
                {
                    Name = "ChildElement"
                }
            };

            // child points to parent
            ((Element)rightObject.Content).Content = rightObject;

            ObjectGraphFactory factory = new PublicPropertyObjectGraphFactory();
            ObjectComparer comparer = new ObjectComparer(factory);
            bool match = comparer.Compare(leftObject, rightObject).None();

            Assert.True(match, "object with loop did not match");
        }
        public void CompareMissingNodes()
        {
            var leftObject = new Element()
            {
                Name = "Content1",
                Content = new Element()
                {
                    Name = "OnlyOnLeft",
                }
            };

            var rightObject = new Element()
            {
                Name = "Content1"
            };

            ObjectGraphFactory factory = new PublicPropertyObjectGraphFactory();
            ObjectComparer comparer = new ObjectComparer(factory);
            IEnumerable<ObjectComparisonMismatch> mismatches = comparer.Compare(leftObject, rightObject);
            bool match = mismatches.None();
            Assert.False(match);
        }
        public void CompareIncorrectChildCount()
        {
            var leftObject = new Element()
            {
                Content = new string[]
                {
                    "String1",
                    "String2",
                },
            };

            var rightObject = new Element()
            {
                Content = new string[]
                {
                    "String1",
                },
            };

            ObjectGraphFactory factory = new PublicPropertyObjectGraphFactory();
            ObjectComparer comparer = new ObjectComparer(factory);
            IEnumerable<ObjectComparisonMismatch> mismatches = comparer.Compare(leftObject, rightObject);
            bool match = mismatches.None();

            string[] expectedMismatches = new string[]
            {
               "RightNodeHasFewerChildren:Left=Element.Content(System.String[]) Right=Element.Content(System.String[])",
               "MissingRightNode:Left=Element.Content.IEnumerable1(String2) Right=Null(Null)"
            };

            string[] actualMismatches = StringFromMismatches(mismatches);

            Assert.False(match);
            Assert.True(actualMismatches.Length == expectedMismatches.Length);
            for (int index = 0; index < expectedMismatches.Length; index++)
            {
                Assert.Equal(expectedMismatches[index], actualMismatches[index]);
            }
        }
        public void CompareIEnumerable()
        {
            Element leftObject = new Element()
            {
                Name = "root",
                Content = new List<string>
                {
                    "hello1",
                    "hello2",
                    "hello3"
                }
            };

            Element rightObject = new Element()
            {
                Name = "root",
                Content = new List<string>
                {
                    "hello1",
                    "hello2",
                    "hello3"
                }
            };

            ObjectGraphFactory factory = new PublicPropertyObjectGraphFactory();
            ObjectComparer comparer = new ObjectComparer(factory);
            bool match = comparer.Compare(leftObject, rightObject).None();

            Assert.True(match, "objects did not match");
        }