From fa42329b313d29e6b9efc36ddd806ddcd001b962 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Fri, 22 Feb 2008 20:08:28 +0000 Subject: [PATCH] Modifying xml_eq so that prefixes for element and attribute nodes are not tested by default. Namespace URI is always tested for both. If you also want to confirm that prefixes are equal, test with options.prefix true. Modifying tests for the XML format to use xml_eq. (see #1383) git-svn-id: http://svn.openlayers.org/trunk/openlayers@6344 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- tests/Format/test_XML.html | 36 +++++--- tests/xml_eq.js | 185 +++++++++++++++++-------------------- 2 files changed, 111 insertions(+), 110 deletions(-) diff --git a/tests/Format/test_XML.html b/tests/Format/test_XML.html index 37067ab6ff..aff544e134 100644 --- a/tests/Format/test_XML.html +++ b/tests/Format/test_XML.html @@ -43,7 +43,7 @@ function test_Format_XML_read(t) { var format = new OpenLayers.Format.XML(); - t.plan(format.xmldom ? 11 : 10); + t.plan(format.xmldom ? 10 : 9); var doc = format.read(text); t.eq(doc.nodeType, 9, @@ -52,10 +52,8 @@ "doc has the correct node name"); t.ok(doc.documentElement, "ok to access doc.documentElement"); - t.eq(doc.documentElement.nodeName, "ol:root", - "doc root has the correct node name"); - t.eq(doc.documentElement.childNodes[1].firstChild.nodeValue, "junk2", - "second child of doc root has correct child node"); + t.xml_eq(doc.documentElement, text, + "doc.documentElement correctly read"); // read can also be called on the prototype directly doc = OpenLayers.Format.XML.prototype.read(text); @@ -65,17 +63,31 @@ "doc has the correct node name"); t.ok(doc.documentElement, "ok to access doc.documentElement"); - t.eq(doc.documentElement.nodeName, "ol:root", - "doc root has the correct node name"); - t.eq(doc.documentElement.childNodes[1].firstChild.nodeValue, "junk2", - "second child of doc root has correct child node"); + t.xml_eq(doc.documentElement, text, + "doc.documentElement correctly read"); // where appropriate, make sure doc is loaded into xmldom property if(format.xmldom) { - t.eq(format.xmldom.documentElement.childNodes[1].firstChild.nodeValue, - "junk2", - "second child of doc root has correct child node"); + t.xml_eq(format.xmldom.documentElement, text, + "xmldom.documentElement contains equivalent xml"); } + + // test equivalence with different namespace alias + var pre1 = + "" + + "value2" + + "value2" + + "" + + ""; + var pre2 = + "" + + "value2" + + "value2" + + "" + + ""; + var doc1 = format.read(pre1); + t.xml_eq(doc1.documentElement, pre2, "read correctly sets namespaces"); + } function test_Format_XML_write(t) { diff --git a/tests/xml_eq.js b/tests/xml_eq.js index c375e09828..47ab56559e 100644 --- a/tests/xml_eq.js +++ b/tests/xml_eq.js @@ -2,59 +2,9 @@ * File: xml_eq.js * Adds a xml_eq method to AnotherWay test objects. * - * Function: Test.AnotherWay._test_object_t.xml_eq - * Test if two XML nodes are equivalent. Tests for same node types, same - * node names, same namespace prefix and URI, same attributes, and - * recursively tests child nodes. - * - * (code) - * t.xml_eq(got, expected, message); - * (end) - * - * Parameters: - * got - {DOMElement | String} A DOM node or XML string to test. - * expected - {DOMElement | String} The expected DOM node or XML string. - * msg - {String} A message to print with test output. - * */ (function() { - - /** - * Function: stringFormat - * Given a string with tokens in the form ${token}, return a string - * with tokens replaced with properties from the given context - * object. Represent a literal "${" by doubling it, e.g. "${${". - * - * Parameters: - * template - {String} A string with tokens to be replaced. A template - * has the form "literal ${token}" where the token will be replaced - * by the value of context["token"]. - * context - {Object} An optional object with properties corresponding - * to the tokens in the format string. If no context is sent, the - * window object will be used. - * - * Returns: - * {String} A string with tokens replaced from the context object. - */ - function formatString(template, context) { - if(!context) { - context = window; - } - var tokens = template.split("${"); - var item, last; - for(var i=1; i 0) { - tokens[i] = context[item.substring(0, last)] + - item.substring(++last); - } else { - tokens[i] = "${" + item; - } - } - return tokens.join(""); - } /** * Function: createNode @@ -117,9 +67,9 @@ * Parameters: * got - {Object} * expected - {Object} - * msg - {String} The message to be thrown. Can be formatted for use with - * formatString where got and expected (cast to string) will be - * substituted. + * msg - {String} The message to be thrown. This message will be appended + * with ": got {got} but expected {expected}" where got and expected are + * replaced with string representations of the above arguments. */ function assertEqual(got, expected, msg) { if(got === undefined) { @@ -132,11 +82,8 @@ } else if (expected === null) { expected = "null"; } - if(!msg) { - msg = "got ${got} but expected ${expected}"; - } if(got != expected) { - throw formatString(msg, {got: got, expected: expected}); + throw msg + ": got " + got + " but expected " + expected; } } @@ -150,78 +97,102 @@ * Parameters: * got - {DOMElement} * expected - {DOMElement} + * options - {Object} Optional object for configuring test options. Set + * 'prefix' property to true in order to compare element and attribute + * prefixes (namespace uri always tested). By default, prefixes + * are not tested. */ - function assertElementNodesEqual(got, expected) { + function assertElementNodesEqual(got, expected, options) { + var testPrefix = (options && options.prefix === true); + // compare types - assertEqual( - got.nodeType, expected.nodeType, - "Node type mismatch: got ${got} but expected ${expected}" - ); + assertEqual(got.nodeType, expected.nodeType, "Node type mismatch"); // compare names - assertEqual( - got.nodeName, expected.nodeName, - "Node name mismatch: got ${got} but expected ${expected}" - ); + var gotName = testPrefix ? + got.nodeName : got.nodeName.split(":").pop(); + var expName = testPrefix ? + expected.nodeName : expected.nodeName.split(":").pop(); + assertEqual(gotName, expName, "Node name mismatch"); + // for text nodes compare value + if(got.nodeType == 3) { + assertEqual( + got.nodeValue, expected.nodeValue, "Node value mismatch" + ); + } // for element type nodes compare namespace, attributes, and children - if(got.nodeType == 1) { + else if(got.nodeType == 1) { // test namespace alias and uri if(got.prefix || expected.prefix) { - assertEqual( - got.prefix, expected.prefix, - "Bad prefix for " + got.nodeName + - ": got ${got} but expected ${expected}" - ); + if(testPrefix) { + assertEqual( + got.prefix, expected.prefix, + "Bad prefix for " + got.nodeName + ); + } } if(got.namespaceURI || expected.namespaceURI) { assertEqual( got.namespaceURI, expected.namespaceURI, - "Bad namespaceURI for " + got.nodeName + - ": got ${got} but expected ${expected}" + "Bad namespaceURI for " + got.nodeName ); } // compare attributes - disregard xmlns given namespace handling above var gotAttrLen = 0; var gotAttr = {}; - var expectedAttrLen = 0; - var expectedAttr = {}; - var ga, ea; + var expAttrLen = 0; + var expAttr = {}; + var ga, ea, gn, en; for(var i=0; i