private bool assertPermutation(XdmNode assertion, SingleResultDoc result, XPathCompiler assertXpc) { // TODO: extend this to handle nodes (if required) if (IsException()) { return(false); } else { return(true); try { int expectedItems = 0; HashSet <string> expected = new HashSet <string>(); XPathSelector s = assertXpc.Compile("(" + assertion.StringValue + ")").Load(); s.SetVariable(new QName("result"), result.value); // not used, but we declared it JCodepointCollator collator = JCodepointCollator.getInstance(); //JXPathContext context = new JXPathContextMajor(stringValue.EMPTY_string, assertXpc.getUnderlyingStaticContext().getConfiguration()); foreach (XdmItem item in s) { expectedItems++; XdmValue value = (XdmValue)item.Simplify; // value.Simplify. /* Object comparable = value.isNaN() ? * AtomicSortComparer.COLLATION_KEY_NaN : * value.getXPathComparable(false, collator, context); * expected.Add(comparable);*/ } int actualItems = 0; foreach (XdmItem item in GetPrincipalResult()) { actualItems++; XdmValue value = (XdmValue)item.Simplify; /*Object comparable = value.isNaN() ? * AtomicSortComparer.COLLATION_KEY_NaN : * value.getXPathComparable(false, collator, context); */ // if (!expected.Contains(comparable)) { return(false); // } } return(actualItems == expectedItems); } catch (DynamicError) { return(false); } } }
private bool assertXml(XdmNode assertion, SingleResultDoc result, XPathCompiler assertXpc, XPathCompiler catalogXpc, bool debug) { if (IsException()) { return(false); } else { string normalizeAtt = assertion.GetAttributeValue(new QName("normalize-space")); bool normalize = normalizeAtt != null && ("true".Equals(normalizeAtt.Trim()) || "1".Equals(normalizeAtt.Trim())); string ignoreAtt = assertion.GetAttributeValue(new QName("ignore-prefixes")); bool ignorePrefixes = ignoreAtt != null && ("true".Equals(ignoreAtt.Trim()) || "1".Equals(ignoreAtt.Trim())); string xmlVersion = assertion.GetAttributeValue(new QName("xml-version")); bool xml11 = "1.1".Equals(xmlVersion); string comparand = catalogXpc.Evaluate("if (@file) then unparsed-text(resolve-uri(@file, base-uri(.))) else string(.)", assertion).ToString(); if (comparand.StartsWith("<?xml")) { int index = comparand.IndexOf("?>"); comparand = comparand.Substring(index + 2); } comparand = comparand.Trim(); comparand = comparand.Replace("\r\n", "\n"); if (normalize) { comparand = JWhitespace.collapseWhitespace(comparand).ToString(); } if (comparand.Equals(Serialize(assertXpc.Processor, result))) { return(true); } DocumentBuilder builder = assertXpc.Processor.NewDocumentBuilder(); if (xml11) { assertXpc.Processor.SetProperty(JFeatureKeys.XML_VERSION, "1.1"); } StringReader reader = new StringReader((xml11 ? "<?xml version='1.1'?>" : "") + "<z>" + comparand + "</z>"); builder.BaseUri = assertion.BaseUri; XdmNode expected = builder.Build(reader); int flag = 0; flag |= JDeepEqual.INCLUDE_COMMENTS; flag |= JDeepEqual.INCLUDE_PROCESSING_INSTRUCTIONS; flag |= JDeepEqual.EXCLUDE_VARIETY; if (!ignorePrefixes) { flag |= JDeepEqual.INCLUDE_NAMESPACES; flag |= JDeepEqual.INCLUDE_PREFIXES; } flag |= JDeepEqual.COMPARE_STRING_VALUES; if (debug) { flag |= JDeepEqual.WARNING_IF_FALSE; } try { JSequenceIterator iter0; if (result == null) { System.Console.WriteLine("Result value is null"); return(false); } XdmValue value = result.value; if (value == null) { System.Console.WriteLine("Result value is null (perhaps serialized?)"); return(false); } if (value.Count == 1 && value.Simplify is XdmNode && ((XdmNode)value.Simplify).NodeKind == System.Xml.XmlNodeType.Document) { iter0 = ((XdmNode)value.Simplify).Implementation.iterateAxis(JAxisInfo.CHILD); } else { iter0 = value.Unwrap().iterate(); } JSequenceIterator iter1 = expected.Implementation.iterateAxis(JAxisInfo.CHILD).next().iterateAxis(JAxisInfo.CHILD); bool success = JDeepEqual.deepEquals( iter0, iter1, new JGenericAtomicComparer(JCodepointCollator.getInstance(), null), assertXpc.Processor.Implementation.getConversionContext(), flag); // if necessary try again ignoring whitespace nodes if (!success) { iter0 = iter0.getAnother(); iter1 = iter1.getAnother(); // deep-equals with the EXCLUDE_WHITESPACE flag doesn't ignore top-level whitespace, so we // need to filter that out ourselves iter0 = new JItemMappingIterator(iter0, new RemoveWhitespace()); iter1 = new JItemMappingIterator(iter1, new RemoveWhitespace()); success = JDeepEqual.deepEquals( iter0, iter1, new JGenericAtomicComparer(JCodepointCollator.getInstance(), null), assertXpc.Processor.Implementation.getConversionContext(), flag | JDeepEqual.EXCLUDE_WHITESPACE_TEXT_NODES); if (success) { comment = "OK after ignoring whitespace text"; } } if (!success) { System.Console.WriteLine("assert-xml comparison failed"); if (debug) { System.Console.WriteLine("assert-xml comparison failed"); System.Console.WriteLine("Reference results:"); System.Console.WriteLine(expected.ToString()); System.Console.WriteLine("Actual results:"); //System.Console.WriteLine(result.serialization); System.Console.WriteLine(value.ToString()); } } return(success); } catch (DynamicError e) { Console.WriteLine(e.StackTrace); return(false); } } }