/// <summary> /// Helper for testing parity with the TreeComparer /// </summary> /// <param name="root1"></param> /// <param name="root2"></param> /// <param name="v1CompareResults"></param> public static void XamlCompareParity(Object root1, Object root2, CompareResult v1CompareResults) { GraphCompareResults compareResults = ObjectGraphComparer.XamlCompare(root1, root2); bool traceGraphs = false; // If results dont match between the two comparers // if ((compareResults.Passed == true && v1CompareResults == CompareResult.Different) || (compareResults.Passed == false && v1CompareResults == CompareResult.Equivalent)) { //Log.TraceInternal("Results do not match between XamlTreeComparer and ObjectGraphComparer"); traceGraphs = true; } // If it was a failure or the compares dont match // if (v1CompareResults == CompareResult.Different || traceGraphs == true) { string tmpName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); //Log.TraceInternal("Logging object graphs - " + tmpName + "*.xml"); ObjectGraph.Serialize(ObjectGraphWalker.Create(root1), tmpName + "root1.xml"); ObjectGraph.Serialize(ObjectGraphWalker.Create(root2), tmpName + "root2.xml"); ObjectGraph.Serialize(compareResults.ResultGraph, tmpName + "result.xml"); //Log.TraceInternal("Exceptions are as follows:"); foreach (CompareError error in compareResults.Errors) { //Log.TraceInternal(string.Format("Node: {0} Message= {1}", error.Node1.QualifiedName, error.Error.Message)); } } }
public static GraphCompareResults Compare(ObjectGraph root1, ObjectGraph root2, List <Object> Ignores) { // Do the comparison here // List <IGraphNode> nodes1 = root1.Decendants; List <IGraphNode> nodes2 = root2.Decendants; GraphCompareResults compareResults = new GraphCompareResults(); ObjectGraph compareResultGraph = root1.Clone(); List <IGraphNode> resultTreeNodes = compareResultGraph.Decendants; if (nodes1.Count != nodes2.Count) { CompareError error = new CompareError((ObjectGraph)nodes1[0], (ObjectGraph)nodes2[0], new Exception("Number of nodes do not match")); resultTreeNodes[0].SetValue(ObjectGraphComparer.CompareErrorProperty, error); compareResults.Errors.Add(error); } for (int i = 0; i < nodes1.Count; i++) { ObjectGraph node1 = (ObjectGraph)nodes1[i]; var nodelist = from node in nodes2 where node1.QualifiedName.Equals(node.QualifiedName) select node; List <IGraphNode> matchingNodes = nodelist.ToList <IGraphNode>(); if (matchingNodes.Count == 0) { CompareError error = new CompareError(node1, null, new Exception("Node not present in second tree")); compareResults.Errors.Add(error); resultTreeNodes[i].SetValue(ObjectGraphComparer.CompareErrorProperty, error); continue; } if (matchingNodes.Count > 1) { CompareError error = new CompareError(node1, null, new Exception("more than one match for this node in second tree")); compareResults.Errors.Add(error); resultTreeNodes[i].SetValue(ObjectGraphComparer.CompareErrorProperty, error); continue; } ObjectGraph node2 = (ObjectGraph)matchingNodes[0]; CompareError error1 = ObjectGraphComparer.CompareNodes(node1, node2); if (error1 != null) { compareResults.Errors.Add(error1); resultTreeNodes[i].SetValue(ObjectGraphComparer.CompareErrorProperty, error1); } } compareResults.Passed = compareResults.Errors.Count == 0 ? true : false; compareResults.ResultGraph = compareResultGraph; return(compareResults); }