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
This commit is contained in:
+24
-12
@@ -43,7 +43,7 @@
|
|||||||
function test_Format_XML_read(t) {
|
function test_Format_XML_read(t) {
|
||||||
|
|
||||||
var format = new OpenLayers.Format.XML();
|
var format = new OpenLayers.Format.XML();
|
||||||
t.plan(format.xmldom ? 11 : 10);
|
t.plan(format.xmldom ? 10 : 9);
|
||||||
|
|
||||||
var doc = format.read(text);
|
var doc = format.read(text);
|
||||||
t.eq(doc.nodeType, 9,
|
t.eq(doc.nodeType, 9,
|
||||||
@@ -52,10 +52,8 @@
|
|||||||
"doc has the correct node name");
|
"doc has the correct node name");
|
||||||
t.ok(doc.documentElement,
|
t.ok(doc.documentElement,
|
||||||
"ok to access doc.documentElement");
|
"ok to access doc.documentElement");
|
||||||
t.eq(doc.documentElement.nodeName, "ol:root",
|
t.xml_eq(doc.documentElement, text,
|
||||||
"doc root has the correct node name");
|
"doc.documentElement correctly read");
|
||||||
t.eq(doc.documentElement.childNodes[1].firstChild.nodeValue, "junk2",
|
|
||||||
"second child of doc root has correct child node");
|
|
||||||
|
|
||||||
// read can also be called on the prototype directly
|
// read can also be called on the prototype directly
|
||||||
doc = OpenLayers.Format.XML.prototype.read(text);
|
doc = OpenLayers.Format.XML.prototype.read(text);
|
||||||
@@ -65,17 +63,31 @@
|
|||||||
"doc has the correct node name");
|
"doc has the correct node name");
|
||||||
t.ok(doc.documentElement,
|
t.ok(doc.documentElement,
|
||||||
"ok to access doc.documentElement");
|
"ok to access doc.documentElement");
|
||||||
t.eq(doc.documentElement.nodeName, "ol:root",
|
t.xml_eq(doc.documentElement, text,
|
||||||
"doc root has the correct node name");
|
"doc.documentElement correctly read");
|
||||||
t.eq(doc.documentElement.childNodes[1].firstChild.nodeValue, "junk2",
|
|
||||||
"second child of doc root has correct child node");
|
|
||||||
|
|
||||||
// where appropriate, make sure doc is loaded into xmldom property
|
// where appropriate, make sure doc is loaded into xmldom property
|
||||||
if(format.xmldom) {
|
if(format.xmldom) {
|
||||||
t.eq(format.xmldom.documentElement.childNodes[1].firstChild.nodeValue,
|
t.xml_eq(format.xmldom.documentElement, text,
|
||||||
"junk2",
|
"xmldom.documentElement contains equivalent xml");
|
||||||
"second child of doc root has correct child node");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test equivalence with different namespace alias
|
||||||
|
var pre1 =
|
||||||
|
"<pre1:parent xmlns:pre1='http://namespace'>" +
|
||||||
|
"<pre1:child1>value2</pre1:child1>" +
|
||||||
|
"<pre1:child2 pre1:attr1='foo'>value2</pre1:child2>" +
|
||||||
|
"<pre1:child3 chicken:attr='hot' xmlns:chicken='http://soup'/>" +
|
||||||
|
"</pre1:parent>";
|
||||||
|
var pre2 =
|
||||||
|
"<pre2:parent xmlns:pre2='http://namespace'>" +
|
||||||
|
"<pre2:child1>value2</pre2:child1>" +
|
||||||
|
"<pre2:child2 pre2:attr1='foo'>value2</pre2:child2>" +
|
||||||
|
"<pre2:child3 pea:attr='hot' xmlns:pea='http://soup'/>" +
|
||||||
|
"</pre2:parent>";
|
||||||
|
var doc1 = format.read(pre1);
|
||||||
|
t.xml_eq(doc1.documentElement, pre2, "read correctly sets namespaces");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_Format_XML_write(t) {
|
function test_Format_XML_write(t) {
|
||||||
|
|||||||
+87
-98
@@ -2,59 +2,9 @@
|
|||||||
* File: xml_eq.js
|
* File: xml_eq.js
|
||||||
* Adds a xml_eq method to AnotherWay test objects.
|
* 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() {
|
||||||
|
|
||||||
/**
|
|
||||||
* 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<tokens.length; i++) {
|
|
||||||
item = tokens[i];
|
|
||||||
last = item.indexOf("}");
|
|
||||||
if(last > 0) {
|
|
||||||
tokens[i] = context[item.substring(0, last)] +
|
|
||||||
item.substring(++last);
|
|
||||||
} else {
|
|
||||||
tokens[i] = "${" + item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tokens.join("");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function: createNode
|
* Function: createNode
|
||||||
@@ -117,9 +67,9 @@
|
|||||||
* Parameters:
|
* Parameters:
|
||||||
* got - {Object}
|
* got - {Object}
|
||||||
* expected - {Object}
|
* expected - {Object}
|
||||||
* msg - {String} The message to be thrown. Can be formatted for use with
|
* msg - {String} The message to be thrown. This message will be appended
|
||||||
* formatString where got and expected (cast to string) will be
|
* with ": got {got} but expected {expected}" where got and expected are
|
||||||
* substituted.
|
* replaced with string representations of the above arguments.
|
||||||
*/
|
*/
|
||||||
function assertEqual(got, expected, msg) {
|
function assertEqual(got, expected, msg) {
|
||||||
if(got === undefined) {
|
if(got === undefined) {
|
||||||
@@ -132,11 +82,8 @@
|
|||||||
} else if (expected === null) {
|
} else if (expected === null) {
|
||||||
expected = "null";
|
expected = "null";
|
||||||
}
|
}
|
||||||
if(!msg) {
|
|
||||||
msg = "got ${got} but expected ${expected}";
|
|
||||||
}
|
|
||||||
if(got != expected) {
|
if(got != expected) {
|
||||||
throw formatString(msg, {got: got, expected: expected});
|
throw msg + ": got " + got + " but expected " + expected;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,78 +97,102 @@
|
|||||||
* Parameters:
|
* Parameters:
|
||||||
* got - {DOMElement}
|
* got - {DOMElement}
|
||||||
* expected - {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
|
// compare types
|
||||||
assertEqual(
|
assertEqual(got.nodeType, expected.nodeType, "Node type mismatch");
|
||||||
got.nodeType, expected.nodeType,
|
|
||||||
"Node type mismatch: got ${got} but expected ${expected}"
|
|
||||||
);
|
|
||||||
|
|
||||||
// compare names
|
// compare names
|
||||||
assertEqual(
|
var gotName = testPrefix ?
|
||||||
got.nodeName, expected.nodeName,
|
got.nodeName : got.nodeName.split(":").pop();
|
||||||
"Node name mismatch: got ${got} but expected ${expected}"
|
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
|
// for element type nodes compare namespace, attributes, and children
|
||||||
if(got.nodeType == 1) {
|
else if(got.nodeType == 1) {
|
||||||
|
|
||||||
// test namespace alias and uri
|
// test namespace alias and uri
|
||||||
if(got.prefix || expected.prefix) {
|
if(got.prefix || expected.prefix) {
|
||||||
assertEqual(
|
if(testPrefix) {
|
||||||
got.prefix, expected.prefix,
|
assertEqual(
|
||||||
"Bad prefix for " + got.nodeName +
|
got.prefix, expected.prefix,
|
||||||
": got ${got} but expected ${expected}"
|
"Bad prefix for " + got.nodeName
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(got.namespaceURI || expected.namespaceURI) {
|
if(got.namespaceURI || expected.namespaceURI) {
|
||||||
assertEqual(
|
assertEqual(
|
||||||
got.namespaceURI, expected.namespaceURI,
|
got.namespaceURI, expected.namespaceURI,
|
||||||
"Bad namespaceURI for " + got.nodeName +
|
"Bad namespaceURI for " + got.nodeName
|
||||||
": got ${got} but expected ${expected}"
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// compare attributes - disregard xmlns given namespace handling above
|
// compare attributes - disregard xmlns given namespace handling above
|
||||||
var gotAttrLen = 0;
|
var gotAttrLen = 0;
|
||||||
var gotAttr = {};
|
var gotAttr = {};
|
||||||
var expectedAttrLen = 0;
|
var expAttrLen = 0;
|
||||||
var expectedAttr = {};
|
var expAttr = {};
|
||||||
var ga, ea;
|
var ga, ea, gn, en;
|
||||||
for(var i=0; i<got.attributes.length; ++i) {
|
for(var i=0; i<got.attributes.length; ++i) {
|
||||||
ga = got.attributes[i];
|
ga = got.attributes[i];
|
||||||
if(ga.name.split(":").shift() != "xmlns") {
|
if(ga.specified === undefined || ga.specified === true) {
|
||||||
gotAttr[ga.name] = ga.value;
|
if(ga.name.split(":").shift() != "xmlns") {
|
||||||
++gotAttrLen;
|
gn = testPrefix ? ga.name : ga.name.split(":").pop();
|
||||||
|
gotAttr[gn] = ga;
|
||||||
|
++gotAttrLen;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(var i=0; i<expected.attributes.length; ++i) {
|
for(var i=0; i<expected.attributes.length; ++i) {
|
||||||
ea = expected.attributes[i];
|
ea = expected.attributes[i];
|
||||||
if(ea.name.split(":").shift() != "xmlns") {
|
if(ea.specified === undefined || ea.specified === true) {
|
||||||
expectedAttr[ea.name] = ea.value;
|
if(ea.name.split(":").shift() != "xmlns") {
|
||||||
++expectedAttrLen;
|
en = testPrefix ? ea.name : ea.name.split(":").pop();
|
||||||
|
expAttr[en] = ea;
|
||||||
|
++expAttrLen;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assertEqual(
|
assertEqual(
|
||||||
gotAttrLen, expectedAttrLen,
|
gotAttrLen, expAttrLen,
|
||||||
"Attributes length mismatch for " + got.nodeName +
|
"Attributes length mismatch for " + got.nodeName
|
||||||
": got ${got} but expected ${expected}"
|
|
||||||
);
|
);
|
||||||
var gv, ev;
|
var gv, ev;
|
||||||
for(var name in gotAttr) {
|
for(var name in gotAttr) {
|
||||||
|
if(expAttr[name] == undefined) {
|
||||||
|
throw "Attribute name " + gotAttr[name].name + " expected for element " + got.nodeName;
|
||||||
|
}
|
||||||
|
// test attribute namespace
|
||||||
assertEqual(
|
assertEqual(
|
||||||
gotAttr[name], expectedAttr[name],
|
gotAttr[name].namespaceURI, expAttr[name].namespaceURI,
|
||||||
|
"Attribute namespace mismatch for element " +
|
||||||
|
got.nodeName + " attribute name " + gotAttr[name].name
|
||||||
|
);
|
||||||
|
// test attribute value
|
||||||
|
assertEqual(
|
||||||
|
gotAttr[name].value, expAttr[name].value,
|
||||||
"Attribute value mismatch for element " + got.nodeName +
|
"Attribute value mismatch for element " + got.nodeName +
|
||||||
" attribute " + name + ": got ${got} but expected ${expected}"
|
" attribute name " + gotAttr[name].name
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// compare children
|
// compare children
|
||||||
assertEqual(
|
assertEqual(
|
||||||
got.childNodes.length, expected.childNodes.length,
|
got.childNodes.length, expected.childNodes.length,
|
||||||
"Children length mismatch for " + got.nodeName +
|
"Children length mismatch for " + got.nodeName
|
||||||
": got ${got} but expected ${expected}"
|
|
||||||
);
|
);
|
||||||
for(var j=0; j<got.childNodes.length; ++j) {
|
for(var j=0; j<got.childNodes.length; ++j) {
|
||||||
try {
|
try {
|
||||||
@@ -229,22 +200,40 @@
|
|||||||
got.childNodes[j], expected.childNodes[j]
|
got.childNodes[j], expected.childNodes[j]
|
||||||
);
|
);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
throw "Bad child " + j + " for node " + got.nodeName + ": " + err;
|
throw "Bad child " + j + " for element " + got.nodeName + ": " + err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// assign test function to AnotherWay test prototype
|
/**
|
||||||
var proto = Test.AnotherWay._test_object_t.prototype;
|
* Function: Test.AnotherWay._test_object_t.xml_eq
|
||||||
proto.xml_eq = function(got, expected, msg) {
|
* Test if two XML nodes are equivalent. Tests for same node types, same
|
||||||
|
* node names, same namespace URI, same attributes, and recursively
|
||||||
|
* tests child nodes for same criteria.
|
||||||
|
*
|
||||||
|
* (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.
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
var proto = Test.AnotherWay._test_object_t.prototype;
|
||||||
|
proto.xml_eq = function(got, expected, msg, options) {
|
||||||
// convert arguments to nodes if string
|
// convert arguments to nodes if string
|
||||||
if(typeof got == "string") {
|
if(typeof got == "string") {
|
||||||
try {
|
try {
|
||||||
got = createNode(got);
|
got = createNode(got);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
this.fail("got argument could not be converted to an XML node: " + err);
|
this.fail(msg + ": got argument could not be converted to an XML node: " + err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -252,17 +241,17 @@
|
|||||||
try {
|
try {
|
||||||
expected = createNode(expected);
|
expected = createNode(expected);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
this.fail("expected argument could not be converted to an XML node: " + err);
|
this.fail(msg + ": expected argument could not be converted to an XML node: " + err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// test nodes for equivalence
|
// test nodes for equivalence
|
||||||
try {
|
try {
|
||||||
assertElementNodesEqual(got, expected);
|
assertElementNodesEqual(got, expected, options);
|
||||||
this.ok(true, msg);
|
this.ok(true, msg);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
this.fail(err);
|
this.fail(msg + ": " + err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user