public void CallsFactoriesFromTheFactoryMap()
        {
            var o = new NamedTypeWithAttributedProperty
            {
                Name  = "Ralph",
                Value = new TypeWithAttributedProperty
                {
                    PropertyWithTestAttribute    = "TestValue",
                    PropertyWithoutTestAttribute = "ShouldBeIgnored"
                }
            };

            var map = new ObjectGraphFactoryMap(true);

            map[typeof(TypeWithAttributedProperty)] = new ExtractAttributeObjectGraphFactory();

            var factory = new PublicPropertyObjectGraphFactory();
            var graph   = factory.CreateObjectGraph(o, map);

            var expected =
                @"RootObjectValue = 'Microsoft.Test.AcceptanceTests.ObjectComparison.ObjectGraphFactoryTests+NamedTypeWithAttributedProperty' Type=Microsoft.Test.AcceptanceTests.ObjectComparison.ObjectGraphFactoryTests+NamedTypeWithAttributedProperty
    ValueValue = 'Microsoft.Test.AcceptanceTests.ObjectComparison.TypeWithAttributedProperty' Type=Microsoft.Test.AcceptanceTests.ObjectComparison.TypeWithAttributedProperty
        PropertyWithTestAttributeValue = 'TestValue' Type=System.String
    NameValue = 'Ralph' Type=System.String";
            var actual = TestHelpers.StringFromGraph(graph);

            Assert.Equal(expected, actual.Trim());
        }
        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);
        }
Exemple #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);
                }
            }
        }
Exemple #4
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 override GraphNode CreateObjectGraph(object value, ObjectGraphFactoryMap factoryMap = null)
 {
     return(new GraphNode
     {
         Name = value == null ? "null" : value.GetType().Name,
         ObjectValue = value
     });
 }
        public override GraphNode CreateObjectGraph(object value, ObjectGraphFactoryMap factoryMap = null)
        {
            // Queue of pending nodes
            Queue <GraphNode> pendingQueue = new Queue <GraphNode>();

            // Dictionary of < object hashcode, node > - to lookup already visited objects
            Dictionary <int, GraphNode> visitedObjects = new Dictionary <int, GraphNode>();

            // Build the root node and enqueue it
            GraphNode root = new GraphNode()
            {
                Name        = "RootObject",
                ObjectValue = value,
            };

            pendingQueue.Enqueue(root);

            while (pendingQueue.Count != 0)
            {
                GraphNode currentNode = pendingQueue.Dequeue();
                object    nodeData    = currentNode.ObjectValue;
                Type      nodeType    = currentNode.ObjectType;

                // If we have reached a leaf node -
                // no more processing is necessary
                if (nodeData == null || nodeData.GetType().IsPrimitive)
                {
                    continue;
                }

                // Handle loops by checking the visted objects
                if (visitedObjects.Keys.Contains(nodeData.GetHashCode()))
                {
                    // Caused by a cycle - we have alredy seen this node so
                    // use the existing node instead of creating a new one
                    GraphNode prebuiltNode = visitedObjects[nodeData.GetHashCode()];
                    currentNode.Children.Add(prebuiltNode);
                    continue;
                }
                else
                {
                    visitedObjects.Add(nodeData.GetHashCode(), currentNode);
                }

                // Extract and add child nodes for current object //
                Collection <GraphNode> childNodes = GetChildNodes(nodeData);
                foreach (GraphNode childNode in childNodes)
                {
                    childNode.Parent = currentNode;
                    currentNode.Children.Add(childNode);

                    pendingQueue.Enqueue(childNode);
                }
            }

            return(root);
        }
Exemple #7
0
        public void CanRemoveFactoryExactMatch()
        {
            var map = new ObjectGraphFactoryMap(true);

            map.Add(typeof(Type), new PublicPropertyObjectGraphFactory());
            Assert.True(map.ContainsKey(typeof(Type)));

            map.Remove(typeof(Type));
            Assert.False(map.ContainsKey(typeof(Type)));
        }
Exemple #8
0
        public void CanAddFactoryExactMatch()
        {
            var map = new ObjectGraphFactoryMap(true);

            map.Add(typeof(Type), new PublicPropertyObjectGraphFactory());

            var factory = map[typeof(Type)];

            Assert.Equal(typeof(PublicPropertyObjectGraphFactory), factory.GetType());
        }
Exemple #9
0
        public void DoesNotThrowOnMultipleNonAmbiguousMatch()
        {
            var map = new ObjectGraphFactoryMap(false);

            map.Add(typeof(ICollection), new GraphFactory1());
            map.Add(typeof(ISerializable), new GraphFactory1());

            Assert.Equal(2, map.Keys.Count);
            Assert.Equal(typeof(GraphFactory1), map[typeof(Dictionary <,>)].GetType());
        }
Exemple #10
0
        public void ThrowsOnAmbiguousMatch()
        {
            var map = new ObjectGraphFactoryMap(false);

            map.Add(typeof(ICollection), new GraphFactory1());
            map.Add(typeof(ISerializable), new GraphFactory2());

            Assert.Equal(2, map.Keys.Count);
            Assert.Throws <ArgumentException>(() => map[typeof(Dictionary <,>)]);
        }
Exemple #11
0
        public void CanRemoveNonGenericTypeDefinitionFactoryExactMatch()
        {
            var map = new ObjectGraphFactoryMap(true);

            map.Add(typeof(List <string>), new PublicPropertyObjectGraphFactory());
            Assert.True(map.ContainsKey(typeof(List <>)));

            map.Remove(typeof(List <>));
            Assert.False(map.ContainsKey(typeof(Type)));
            Assert.Equal(0, map.Values.Count);
        }
Exemple #12
0
        public void CanAddNonGenericTypeDefinitionFactoryExactMatch()
        {
            var map = new ObjectGraphFactoryMap(true);

            map.Add(typeof(Func <int>), new PublicPropertyObjectGraphFactory());

            Assert.True(map.ContainsKey(typeof(Func <>)));
            Assert.True(map.ContainsKey(typeof(Func <Type>)));
            Assert.Equal(1, map.Values.Count);

            var factory = map[typeof(Func <>)];

            Assert.Equal(typeof(PublicPropertyObjectGraphFactory), factory.GetType());
        }
Exemple #13
0
        public void CanAddFactoryForInterfaceNonExactMatch()
        {
            var map = new ObjectGraphFactoryMap(false);

            map.Add(typeof(IEnumerable), new GraphFactory1());

            Assert.Equal(1, map.Keys.Count);

            var f = map[typeof(IEnumerable)];

            Assert.Equal(typeof(GraphFactory1), f.GetType());

            f = map[typeof(ICollection <string>)];
            Assert.Equal(typeof(GraphFactory1), f.GetType());

            f = map[typeof(IDictionary <,>)];
            Assert.Equal(typeof(GraphFactory1), f.GetType());

            Assert.Throws <KeyNotFoundException>(() => map[typeof(object)]);
        }
Exemple #14
0
        public void CanAddFactoryNonExactMatch()
        {
            var map = new ObjectGraphFactoryMap(false);

            map.Add(typeof(MemberInfo), new PublicPropertyObjectGraphFactory());
            map.Add(typeof(object), new GraphFactory2());
            map.Add(typeof(MethodInfo), new GraphFactory1());

            Assert.Equal(3, map.Keys.Count);

            var f = map[typeof(MemberInfo)];

            Assert.Equal(typeof(PublicPropertyObjectGraphFactory), f.GetType());
            f = map[typeof(ConstructorInfo)];
            Assert.Equal(typeof(PublicPropertyObjectGraphFactory), f.GetType());
            f = map[typeof(MethodInfo)];
            Assert.Equal(typeof(GraphFactory1), f.GetType());
            f = map[typeof(AttributeTargets)];
            Assert.Equal(typeof(GraphFactory2), f.GetType());
            f = map[typeof(Predicate <object>)];
            Assert.Equal(typeof(GraphFactory2), f.GetType());
        }
 public override GraphNode CreateObjectGraph(object value, ObjectGraphFactoryMap factoryMap = null)
 {
     created = new Dictionary <Sample, GraphNode>();
     return(CreateObjectGraph((Sample)value, null));
 }
Exemple #16
0
 public override GraphNode CreateObjectGraph(object value, ObjectGraphFactoryMap factoryMap = null)
 {
     throw new NotImplementedException();
 }