private IEnumerable <ObjectComparisonMismatch> CompareObjects(object leftObject, object rightObject) { var mismatches = new List <ObjectComparisonMismatch>(); GraphNode leftRoot = ObjectGraphFactory.CreateObjectGraph(leftObject); GraphNode rightRoot = ObjectGraphFactory.CreateObjectGraph(rightObject); var leftNodes = new List <GraphNode>(leftRoot.GetNodesInDepthFirstOrder()); var rightNodes = new List <GraphNode>(rightRoot.GetNodesInDepthFirstOrder()); FlagNodesThatShouldBeIgnored(leftNodes); // For each node in the left tree, search for the // node in the right tree and compare them foreach (GraphNode leftNode in leftNodes) { if (leftNode.Ignore) { continue; } GraphNode rightNode = rightNodes.Where(node => leftNode.QualifiedName == node.QualifiedName) .DefaultIfEmpty(null) .FirstOrDefault(); ObjectComparisonMismatch mismatch = CompareNodes(leftNode, rightNode); if (mismatch != null) { mismatches.Add(mismatch); } } return(mismatches); }
private bool CompareObjects(object leftObject, object rightObject, out List <ObjectComparisonMismatch> mismatches) { mismatches = new List <ObjectComparisonMismatch>(); // Get the graph from the objects GraphNode leftRoot = this.ObjectGraphFactory.CreateObjectGraph(leftObject); GraphNode rightRoot = this.ObjectGraphFactory.CreateObjectGraph(rightObject); // Get the nodes in breadth first order List <GraphNode> leftNodes = new List <GraphNode>(leftRoot.GetNodesInDepthFirstOrder()); List <GraphNode> rightNodes = new List <GraphNode>(rightRoot.GetNodesInDepthFirstOrder()); // For each node in the left tree, search for the // node in the right tree and compare them for (int i = 0; i < leftNodes.Count; i++) { GraphNode leftNode = leftNodes[i]; var nodelist = from node in rightNodes where leftNode.QualifiedName.Equals(node.QualifiedName) select node; List <GraphNode> matchingNodes = nodelist.ToList <GraphNode>(); if (matchingNodes.Count != 1) { ObjectComparisonMismatch mismatch = new ObjectComparisonMismatch(leftNode, null, ObjectComparisonMismatchType.MissingRightNode); mismatches.Add(mismatch); continue; } GraphNode rightNode = matchingNodes[0]; // Compare the nodes ObjectComparisonMismatch nodesMismatch = CompareNodes(leftNode, rightNode); if (nodesMismatch != null) { mismatches.Add(nodesMismatch); } } bool passed = mismatches.Count == 0 ? true : false; return(passed); }
private static string StringFromGraph(GraphNode graph) { StringBuilder stringBuilder = new StringBuilder(); IEnumerable<GraphNode> nodes = graph.GetNodesInDepthFirstOrder(); foreach (GraphNode node in nodes) { string type = "Null"; if (node.ObjectValue != null) { type = node.ObjectType.FullName; } stringBuilder.AppendLine("".PadLeft(node.Depth*4) + node.Name + "Value = '" + node.ObjectValue + "'" + " Type=" + type); } return stringBuilder.ToString(); }