private bool AssertSerialization(XdmNode assertion, SingleResultDoc result, XPathCompiler xpath, bool debug) { if (IsException()) { return(false); } else { String method = assertion.GetAttributeValue(new QName("method")); if (method == null) { method = "xml"; } String resultString = result.serialization; String comparand = xpath.Evaluate( "if (@file) then " + "if (@encoding) " + "then unparsed-text(resolve-uri(@file, base-uri(.)), @encoding) " + "else unparsed-text(resolve-uri(@file, base-uri(.))) " + "else string(.)", assertion).ToString(); comparand = comparand.Replace("\r\n", "\n"); if (comparand.EndsWith("\n")) { comparand = comparand.Substring(0, comparand.Length - 1); } if (resultString == null) { if (result.value is XdmItem) { resultString = ((XdmItem)result.value).Simplify.ToString(); } else { if (debug) { driver.println("Assert serialization fails: result is a sequence"); } return(false); } } bool isHtml = method.Equals("html") || method.Equals("xhtml"); bool normalize = isHtml; if (!normalize) { String normalizeAtt = assertion.GetAttributeValue(new QName("normalize-space")); normalize = normalizeAtt != null && (normalizeAtt.Trim().Equals("true") || normalizeAtt.Trim().Equals("1")); } if (normalize) { comparand = JWhitespace.collapseWhitespace(comparand).toString(); resultString = JWhitespace.collapseWhitespace(resultString).toString(); } else if (resultString.EndsWith("\n")) { resultString = resultString.Substring(0, resultString.Length - 1); } if (isHtml) { // should really do this only for block-level elements comparand = comparand.Replace(" <", "<"); comparand = comparand.Replace("> ", ">"); resultString = resultString.Replace(" <", "<"); resultString = resultString.Replace("> ", ">"); } if (resultString.Equals(comparand)) { return(true); } else { if (debug) { if (resultString.Length != comparand.Length) { driver.println("Result length " + resultString.Length + "; expected length " + comparand.Length); } int len = Math.Min(resultString.Length, comparand.Length); for (int i = 0; i < len; i++) { if (resultString[1] != comparand[i]) { driver.println("Results differ at index " + i + "(\"" + resultString.Substring(i, (i + 10 > len ? len : i + 10)) + "\") vs (\"" + comparand.Substring(i, (i + 10 > len ? len : i + 10)) + "\")"); break; } } } driver.println("Serialized results differ"); 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); } } }
private bool AssertstringValue(XdmNode assertion, SingleResultDoc result, bool debug) { if (IsException()) { return(false); } else { string resultstring; string assertionstring = assertion.StringValue; if (result.value is XdmItem) { resultstring = ((XdmItem)result.value).Simplify.ToString(); } else { bool first = true; net.sf.saxon.tree.util.FastStringBuffer fsb = new net.sf.saxon.tree.util.FastStringBuffer(256); foreach (XdmItem item in result.value) { if (first) { first = false; } else { fsb.append(' '); } fsb.append(item.Simplify.ToString()); } resultstring = fsb.ToString(); } string normalizeAtt = assertion.GetAttributeValue(new QName("normalize-space")); if (normalizeAtt != null && (normalizeAtt.Trim().Equals("true") || normalizeAtt.Trim().Equals("1"))) { assertionstring = JWhitespace.collapseWhitespace(assertionstring).ToString(); resultstring = JWhitespace.collapseWhitespace(resultstring).ToString(); } if (resultstring.Equals(assertionstring)) { return(true); } else { if (debug) { if (resultstring.Length != assertionstring.Length) { driver.println("Result length " + resultstring.Length + "; expected length " + assertionstring.Length); } int len = Math.Min(resultstring.Length, assertionstring.Length); for (int i = 0; i < len; i++) { if (resultstring[i] != assertionstring[i]) { driver.println("Results differ at index " + i + "(\"" + resultstring.Substring(i, (i + 10 > len ? len : i + 10)) + "\") vs (\"" + assertionstring.Substring(i, (i + 10 > len ? len : i + 10)) + "\")"); break; } } } return(false); } } }