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();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var  left  = factory.CreateObjectGraph(leftObject);
            var  right = factory.CreateObjectGraph(rightObject);
            bool match = comparer.Compare(left, right);

            Assert.True(match, "object with loop did not match");
        }
Пример #2
0
        public static void CompareObjects(ObjectComparisonCmdletBase cmdlet, object referenceObject, object[] differenceObjects)
        {
            // left is null
            if (null == referenceObject)
            {
                cmdlet.WriteObject(cmdlet, false);
                return;
            }

            // right is null
            if (null == differenceObjects)
            {
                cmdlet.WriteObject(cmdlet, false);
                return;
            }

            if (null != differenceObjects && 0 < differenceObjects.Length)
            {
                ObjectGraphFactory    factory        = new PublicPropertyObjectGraphFactory();
                ObjectGraphFactoryMap map            = new ObjectGraphFactoryMap(true);
                GraphNode             referenceGraph = factory.CreateObjectGraph(referenceObject, map);

                foreach (var differenceObject in differenceObjects)
                {
                    cmdlet.WriteVerbose(cmdlet, "comparing " + referenceObject + " and " + differenceObject);

                    GraphNode           differenceGraph = factory.CreateObjectGraph(differenceObject, map);
                    ObjectGraphComparer comparer        = new ObjectGraphComparer();
                    bool result = comparer.Compare(referenceGraph, differenceGraph);

                    cmdlet.WriteObject(cmdlet, result);
                }
            }
        }
Пример #3
0
        public static void CompareObjects(ObjectComparisonCmdletBase cmdlet, object referenceObject, object[] differenceObjects)
        {
            // left is null
            if (null == referenceObject) {
                cmdlet.WriteObject(cmdlet, false);
                return;
            }
            
            // right is null
            if (null == differenceObjects) {
                cmdlet.WriteObject(cmdlet, false);
                return;
            }
            
            if (null != differenceObjects && 0 < differenceObjects.Length) {
                
                ObjectGraphFactory factory = new PublicPropertyObjectGraphFactory();
                ObjectGraphFactoryMap map = new ObjectGraphFactoryMap(true);
                GraphNode referenceGraph = factory.CreateObjectGraph(referenceObject, map);
                
                foreach (var differenceObject in differenceObjects) {
                    
                    cmdlet.WriteVerbose(cmdlet, "comparing " + referenceObject + " and " + differenceObject);

                    GraphNode differenceGraph = factory.CreateObjectGraph(differenceObject, map);
                    ObjectGraphComparer comparer = new ObjectGraphComparer();
                    bool result = comparer.Compare(referenceGraph, differenceGraph);
                    
                    cmdlet.WriteObject(cmdlet, result);
                }
            }
        }
        public void BasicTypesDiffer()
        {
            BasicTypes leftObject = new BasicTypes()
            {
                BoolPrimitive   = false,
                BytePrimitive   = byte.MinValue,
                CharValue       = char.MinValue,
                DoublePrimitive = double.MinValue,
                FloatPrimitive  = float.MinValue,
                IntPrimitive    = int.MinValue,
                LongPrimitive   = long.MinValue,
                ShortPrimitive  = short.MinValue,
                StringPrimitive = "some string",
                TimeSpanValue   = TimeSpan.MinValue,
            };

            BasicTypes rightObject = leftObject.Clone();

            ObjectGraphFactory  factory  = new PublicPropertyObjectGraphFactory();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var  left  = factory.CreateObjectGraph(leftObject);
            var  right = factory.CreateObjectGraph(rightObject);
            bool match = comparer.Compare(left, right);

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

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

            ObjectGraphFactory  factory  = new PublicPropertyObjectGraphFactory();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var left  = factory.CreateObjectGraph(leftObject);
            var right = factory.CreateObjectGraph(rightObject);
            IEnumerable <ObjectComparisonMismatch> mismatches;
            bool match = comparer.Compare(left, right, out mismatches);

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

            string[] actualMismatches = TestHelpers.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]);
            }
        }
Пример #6
0
        public void HandlesPropertiesWithNullValues()
        {
            // There was a bug that PublicPropertyObjectGraphFactory threw
            // NullReferenceException when
            //   1. factoryMap is not null
            //   2. Property value is null
            // This test case is to make sure that this bug has been fixed and
            // does not come back

            var left  = typeof(string);
            var right = typeof(string);

            var fac      = new PublicPropertyObjectGraphFactory();
            var comparer = new ObjectGraphComparer();

            var factoryMap = new ObjectGraphFactoryMap(false);

            factoryMap.Add(typeof(MethodBase), new StubGraphFactory());
            factoryMap.Add(typeof(Assembly), new StubGraphFactory());

            var leftNode  = fac.CreateObjectGraph(left, factoryMap); // With StubFactory
            var rightNode = fac.CreateObjectGraph(right);            // Without StubFactory

            bool noDifferences = comparer.Compare(leftNode, rightNode);

            Assert.False(noDifferences);

            var leftChildrenCount  = leftNode.GetNodesInDepthFirstOrder().Count();
            var rightChildrenCount = rightNode.GetNodesInDepthFirstOrder().Count();

            // Make sure that we reduced size of the object graph by using StubFactory
            Assert.True(leftChildrenCount < rightChildrenCount);
        }
        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();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var  left  = factory.CreateObjectGraph(leftObject);
            var  right = factory.CreateObjectGraph(rightObject);
            bool match = comparer.Compare(left, right);

            Assert.True(match, "objects did not match");
        }
Пример #8
0
        public void CompareObjects()
        {
            // Create two objects with differing property values
            Person leftObject = new Person()
            {
                Name   = "Person1",
                Age    = 15,
                Parent = new Person()
                {
                    Name = "ParentOfPerson1",
                }
            };

            Person rightObject = new Person()
            {
                Name   = "Person2",
                Age    = 15,
                Parent = new Person()
                {
                    Name = "ParentOfPerson2",
                }
            };

            // Create the object graph factory
            ObjectGraphFactory factory = new PublicPropertyObjectGraphFactory();
            var leftGraph  = factory.CreateObjectGraph(leftObject);
            var rightGraph = factory.CreateObjectGraph(rightObject);

            // Create a comparer using the factory
            ObjectGraphComparer comparer = new ObjectGraphComparer();

            // Compare the objects
            IEnumerable <ObjectComparisonMismatch> mismatches;
            bool match = comparer.Compare(leftGraph, rightGraph, out mismatches);

            // Validate the mismatches when the objects do not match
            string[] expectedMismatches = new string[]
            {
                "ObjectValuesDoNotMatch:Left=RootObject.Name(Person1) Right=RootObject.Name(Person2)",
                "ObjectValuesDoNotMatch:Left=RootObject.Parent.Name(ParentOfPerson1) Right=RootObject.Parent.Name(ParentOfPerson2)",
            };

            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 CompareIncorrectChildCount()
        {
            var leftObject = new Element()
            {
                Content = new string[]
                {
                    "String1",
                    "String2",
                },
            };

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

            ObjectGraphFactory  factory  = new PublicPropertyObjectGraphFactory();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var left  = factory.CreateObjectGraph(leftObject);
            var right = factory.CreateObjectGraph(rightObject);
            IEnumerable <ObjectComparisonMismatch> mismatches;
            bool match = comparer.Compare(left, right, out mismatches);

            string[] expectedMismatches = new string[]
            {
                "RightNodeHasFewerChildren:Left=RootObject.Content(System.String[]) Right=RootObject.Content(System.String[])",
                "MissingRightNode:Left=RootObject.Content.IEnumerable1(String2) Right=Null(Null)",
                "ObjectValuesDoNotMatch:Left=RootObject.Content.Length(2) Right=RootObject.Content.Length(1)",
                "ObjectValuesDoNotMatch:Left=RootObject.Content.LongLength(2) Right=RootObject.Content.LongLength(1)",
            };

            string[] actualMismatches = TestHelpers.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]);
            }
        }
Пример #10
0
        public static void CompareObjects(ObjectComparisonCmdletBase cmdlet, object referenceObject, object[] differenceObjects)
        {
            // left is null

            // right is null

            if (null != differenceObjects && 0 < differenceObjects.Length) {

                ObjectGraphFactory factory = new PublicPropertyObjectGraphFactory();
                GraphNode referenceGraph = factory.CreateObjectGraph(referenceObject, null);

                foreach (var differenceObject in differenceObjects) {

                    GraphNode differenceGraph = factory.CreateObjectGraph(differenceObject, null);
                    ObjectGraphComparer comparer = new ObjectGraphComparer();
                    bool result = comparer.Compare(referenceGraph, differenceGraph);

                    cmdlet.WriteObject(cmdlet, result);
                }
            }
        }
Пример #11
0
        public void CustomFactoryMismatch()
        {
            TypeWithAttributedProperty leftObject = new TypeWithAttributedProperty()
            {
                PropertyWithoutTestAttribute = "Should not be compared",
                PropertyWithTestAttribute    = "Should be compared",
            };

            TypeWithAttributedProperty rightObject = new TypeWithAttributedProperty()
            {
                PropertyWithoutTestAttribute = "Should not be compared - so this is different",
                PropertyWithTestAttribute    = "Should be compared - and should fail because its different",
            };

            ExtractAttributeObjectGraphFactory factory = new ExtractAttributeObjectGraphFactory();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var left  = factory.CreateObjectGraph(leftObject);
            var right = factory.CreateObjectGraph(rightObject);

            Assert.False(comparer.Compare(left, right), "Custom compare passed when it should have failed");
        }
        public void CompareMissingNodes()
        {
            var leftObject = new Element()
            {
                Name    = "Content1",
                Content = new Element()
                {
                    Name = "OnlyOnLeft",
                }
            };

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

            ObjectGraphFactory  factory  = new PublicPropertyObjectGraphFactory();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var left  = factory.CreateObjectGraph(leftObject);
            var right = factory.CreateObjectGraph(rightObject);
            IEnumerable <ObjectComparisonMismatch> mismatches;
            bool match = comparer.Compare(left, right, out mismatches);

            string[] expectedMismatches = new string[]
            {
                "ObjectValuesDoNotMatch:Left=RootObject.Content(Microsoft.Test.AcceptanceTests.ObjectComparison.Element) Right=RootObject.Content(Null)",
                "MissingRightNode:Left=RootObject.Content.Content(Null) Right=Null(Null)",
                "MissingRightNode:Left=RootObject.Content.Name(OnlyOnLeft) Right=Null(Null)",
            };

            string[] actualMismatches = TestHelpers.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 ComparePropertyThrows()
        {
            Element leftObject = new Element()
            {
                Content = new TypeWithPropertyThatThrows(),
            };

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

            ObjectGraphFactory  factory  = new PublicPropertyObjectGraphFactory();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var  left  = factory.CreateObjectGraph(leftObject);
            var  right = factory.CreateObjectGraph(rightObject);
            bool match = comparer.Compare(left, right);

            TestHelpers.ObjectToConsole(leftObject, factory);

            Assert.True(match, "objects did not match");
        }
        public void CompareDictionary()
        {
            Dictionary <int, string> leftObject = new Dictionary <int, string>()
            {
                { 10, "Hello" },
                { 20, "World" },
            };

            Dictionary <int, string> rightObject = new Dictionary <int, string>()
            {
                { 10, "Hello" },
                { 20, "World" },
            };

            ObjectGraphFactory  factory  = new PublicPropertyObjectGraphFactory();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var  left  = factory.CreateObjectGraph(leftObject);
            var  right = factory.CreateObjectGraph(rightObject);
            bool match = comparer.Compare(left, right);

            Assert.True(match, "Dictionary<int, string> did not match");
        }
        public void CompareList()
        {
            List <string> leftObject = new List <string>()
            {
                "Hello",
                "World",
            };

            List <string> rightObject = new List <string>()
            {
                "Hello",
                "World",
            };

            ObjectGraphFactory  factory  = new PublicPropertyObjectGraphFactory();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var  left  = factory.CreateObjectGraph(leftObject);
            var  right = factory.CreateObjectGraph(rightObject);
            bool match = comparer.Compare(left, right);

            Assert.True(match, "List<string> did not match");
        }
        public void CompareBasicTypesMinValues()
        {
            BasicTypes leftObject = new BasicTypes()
            {
                BoolPrimitive   = false,
                BytePrimitive   = byte.MinValue,
                CharValue       = char.MinValue,
                DoublePrimitive = double.MinValue,
                FloatPrimitive  = float.MinValue,
                IntPrimitive    = int.MinValue,
                LongPrimitive   = long.MinValue,
                ShortPrimitive  = short.MinValue,
                StringPrimitive = "some string",
                TimeSpanValue   = TimeSpan.MinValue,
            };

            BasicTypes rightObject = new BasicTypes()
            {
                BoolPrimitive   = true,
                BytePrimitive   = byte.MaxValue,
                CharValue       = char.MaxValue,
                DoublePrimitive = double.MaxValue,
                FloatPrimitive  = float.MaxValue,
                IntPrimitive    = int.MaxValue,
                LongPrimitive   = long.MaxValue,
                ShortPrimitive  = short.MaxValue,
                StringPrimitive = string.Empty,
                TimeSpanValue   = TimeSpan.MaxValue,
            };

            ObjectGraphFactory  factory  = new PublicPropertyObjectGraphFactory();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var  left  = factory.CreateObjectGraph(leftObject);
            var  right = factory.CreateObjectGraph(rightObject);
            bool match = comparer.Compare(left, right);

            Assert.False(match, "Basic types matched when they should not");
        }
        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();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var  left  = factory.CreateObjectGraph(leftObject);
            var  right = factory.CreateObjectGraph(rightObject);
            bool match = comparer.Compare(left, right);

            Assert.True(match, "object with loop did not match");
        }
        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();
            ObjectGraphComparer comparer = new ObjectGraphComparer();
            var  left  = factory.CreateObjectGraph(leftObject);
            var  right = factory.CreateObjectGraph(rightObject);
            bool match = comparer.Compare(left, right);

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