Changeset 6344
- Timestamp:
- 02/22/08 15:08:28 (9 months ago)
- Files:
-
- trunk/openlayers/tests/Format/test_XML.html (modified) (3 diffs)
- trunk/openlayers/tests/xml_eq.js (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openlayers/tests/Format/test_XML.html
r6131 r6344 44 44 45 45 var format = new OpenLayers.Format.XML(); 46 t.plan(format.xmldom ? 1 1 : 10);46 t.plan(format.xmldom ? 10 : 9); 47 47 48 48 var doc = format.read(text); … … 53 53 t.ok(doc.documentElement, 54 54 "ok to access doc.documentElement"); 55 t.eq(doc.documentElement.nodeName, "ol:root", 56 "doc root has the correct node name"); 57 t.eq(doc.documentElement.childNodes[1].firstChild.nodeValue, "junk2", 58 "second child of doc root has correct child node"); 55 t.xml_eq(doc.documentElement, text, 56 "doc.documentElement correctly read"); 59 57 60 58 // read can also be called on the prototype directly … … 66 64 t.ok(doc.documentElement, 67 65 "ok to access doc.documentElement"); 68 t.eq(doc.documentElement.nodeName, "ol:root", 69 "doc root has the correct node name"); 70 t.eq(doc.documentElement.childNodes[1].firstChild.nodeValue, "junk2", 71 "second child of doc root has correct child node"); 66 t.xml_eq(doc.documentElement, text, 67 "doc.documentElement correctly read"); 72 68 73 69 // where appropriate, make sure doc is loaded into xmldom property 74 70 if(format.xmldom) { 75 t.eq(format.xmldom.documentElement.childNodes[1].firstChild.nodeValue, 76 "junk2", 77 "second child of doc root has correct child node"); 71 t.xml_eq(format.xmldom.documentElement, text, 72 "xmldom.documentElement contains equivalent xml"); 78 73 } 74 75 // test equivalence with different namespace alias 76 var pre1 = 77 "<pre1:parent xmlns:pre1='http://namespace'>" + 78 "<pre1:child1>value2</pre1:child1>" + 79 "<pre1:child2 pre1:attr1='foo'>value2</pre1:child2>" + 80 "<pre1:child3 chicken:attr='hot' xmlns:chicken='http://soup'/>" + 81 "</pre1:parent>"; 82 var pre2 = 83 "<pre2:parent xmlns:pre2='http://namespace'>" + 84 "<pre2:child1>value2</pre2:child1>" + 85 "<pre2:child2 pre2:attr1='foo'>value2</pre2:child2>" + 86 "<pre2:child3 pea:attr='hot' xmlns:pea='http://soup'/>" + 87 "</pre2:parent>"; 88 var doc1 = format.read(pre1); 89 t.xml_eq(doc1.documentElement, pre2, "read correctly sets namespaces"); 90 79 91 } 80 92 trunk/openlayers/tests/xml_eq.js
r6341 r6344 3 3 * Adds a xml_eq method to AnotherWay test objects. 4 4 * 5 * Function: Test.AnotherWay._test_object_t.xml_eq6 * Test if two XML nodes are equivalent. Tests for same node types, same7 * node names, same namespace prefix and URI, same attributes, and8 * recursively tests child nodes.9 *10 * (code)11 * t.xml_eq(got, expected, message);12 * (end)13 *14 * Parameters:15 * got - {DOMElement | String} A DOM node or XML string to test.16 * expected - {DOMElement | String} The expected DOM node or XML string.17 * msg - {String} A message to print with test output.18 *19 5 */ 20 6 21 7 (function() { 22 23 /**24 * Function: stringFormat25 * Given a string with tokens in the form ${token}, return a string26 * with tokens replaced with properties from the given context27 * object. Represent a literal "${" by doubling it, e.g. "${${".28 *29 * Parameters:30 * template - {String} A string with tokens to be replaced. A template31 * has the form "literal ${token}" where the token will be replaced32 * by the value of context["token"].33 * context - {Object} An optional object with properties corresponding34 * to the tokens in the format string. If no context is sent, the35 * window object will be used.36 *37 * Returns:38 * {String} A string with tokens replaced from the context object.39 */40 function formatString(template, context) {41 if(!context) {42 context = window;43 }44 var tokens = template.split("${");45 var item, last;46 for(var i=1; i<tokens.length; i++) {47 item = tokens[i];48 last = item.indexOf("}");49 if(last > 0) {50 tokens[i] = context[item.substring(0, last)] +51 item.substring(++last);52 } else {53 tokens[i] = "${" + item;54 }55 }56 return tokens.join("");57 }58 8 59 9 /** … … 118 68 * got - {Object} 119 69 * expected - {Object} 120 * msg - {String} The message to be thrown. Can be formatted for use with121 * formatString where got and expected (cast to string) will be122 * substituted.70 * msg - {String} The message to be thrown. This message will be appended 71 * with ": got {got} but expected {expected}" where got and expected are 72 * replaced with string representations of the above arguments. 123 73 */ 124 74 function assertEqual(got, expected, msg) { … … 133 83 expected = "null"; 134 84 } 135 if(!msg) {136 msg = "got ${got} but expected ${expected}";137 }138 85 if(got != expected) { 139 throw formatString(msg, {got: got, expected: expected});86 throw msg + ": got " + got + " but expected " + expected; 140 87 } 141 88 } … … 151 98 * got - {DOMElement} 152 99 * expected - {DOMElement} 153 */ 154 function assertElementNodesEqual(got, expected) { 100 * options - {Object} Optional object for configuring test options. Set 101 * 'prefix' property to true in order to compare element and attribute 102 * prefixes (namespace uri always tested). By default, prefixes 103 * are not tested. 104 */ 105 function assertElementNodesEqual(got, expected, options) { 106 var testPrefix = (options && options.prefix === true); 107 155 108 // compare types 156 assertEqual( 157 got.nodeType, expected.nodeType, 158 "Node type mismatch: got ${got} but expected ${expected}" 159 ); 109 assertEqual(got.nodeType, expected.nodeType, "Node type mismatch"); 160 110 161 111 // compare names 162 assertEqual( 163 got.nodeName, expected.nodeName, 164 "Node name mismatch: got ${got} but expected ${expected}" 165 ); 166 112 var gotName = testPrefix ? 113 got.nodeName : got.nodeName.split(":").pop(); 114 var expName = testPrefix ? 115 expected.nodeName : expected.nodeName.split(":").pop(); 116 assertEqual(gotName, expName, "Node name mismatch"); 117 118 // for text nodes compare value 119 if(got.nodeType == 3) { 120 assertEqual( 121 got.nodeValue, expected.nodeValue, "Node value mismatch" 122 ); 123 } 167 124 // for element type nodes compare namespace, attributes, and children 168 if(got.nodeType == 1) {125 else if(got.nodeType == 1) { 169 126 170 127 // test namespace alias and uri 171 128 if(got.prefix || expected.prefix) { 172 assertEqual( 173 got.prefix, expected.prefix, 174 "Bad prefix for " + got.nodeName + 175 ": got ${got} but expected ${expected}" 176 ); 129 if(testPrefix) { 130 assertEqual( 131 got.prefix, expected.prefix, 132 "Bad prefix for " + got.nodeName 133 ); 134 } 177 135 } 178 136 if(got.namespaceURI || expected.namespaceURI) { 179 137 assertEqual( 180 138 got.namespaceURI, expected.namespaceURI, 181 "Bad namespaceURI for " + got.nodeName + 182 ": got ${got} but expected ${expected}" 139 "Bad namespaceURI for " + got.nodeName 183 140 ); 184 141 } … … 187 144 var gotAttrLen = 0; 188 145 var gotAttr = {}; 189 var exp ectedAttrLen = 0;190 var exp ectedAttr = {};191 var ga, ea ;146 var expAttrLen = 0; 147 var expAttr = {}; 148 var ga, ea, gn, en; 192 149 for(var i=0; i<got.attributes.length; ++i) { 193 150 ga = got.attributes[i]; 194 if(ga.name.split(":").shift() != "xmlns") { 195 gotAttr[ga.name] = ga.value; 196 ++gotAttrLen; 151 if(ga.specified === undefined || ga.specified === true) { 152 if(ga.name.split(":").shift() != "xmlns") { 153 gn = testPrefix ? ga.name : ga.name.split(":").pop(); 154 gotAttr[gn] = ga; 155 ++gotAttrLen; 156 } 197 157 } 198 158 } 199 159 for(var i=0; i<expected.attributes.length; ++i) { 200 160 ea = expected.attributes[i]; 201 if(ea.name.split(":").shift() != "xmlns") { 202 expectedAttr[ea.name] = ea.value; 203 ++expectedAttrLen; 161 if(ea.specified === undefined || ea.specified === true) { 162 if(ea.name.split(":").shift() != "xmlns") { 163 en = testPrefix ? ea.name : ea.name.split(":").pop(); 164 expAttr[en] = ea; 165 ++expAttrLen; 166 } 204 167 } 205 168 } 206 169 assertEqual( 207 gotAttrLen, expectedAttrLen, 208 "Attributes length mismatch for " + got.nodeName + 209 ": got ${got} but expected ${expected}" 170 gotAttrLen, expAttrLen, 171 "Attributes length mismatch for " + got.nodeName 210 172 ); 211 173 var gv, ev; 212 174 for(var name in gotAttr) { 175 if(expAttr[name] == undefined) { 176 throw "Attribute name " + gotAttr[name].name + " expected for element " + got.nodeName; 177 } 178 // test attribute namespace 213 179 assertEqual( 214 gotAttr[name], expectedAttr[name], 180 gotAttr[name].namespaceURI, expAttr[name].namespaceURI, 181 "Attribute namespace mismatch for element " + 182 got.nodeName + " attribute name " + gotAttr[name].name 183 ); 184 // test attribute value 185 assertEqual( 186 gotAttr[name].value, expAttr[name].value, 215 187 "Attribute value mismatch for element " + got.nodeName + 216 " attribute " + name + ": got ${got} but expected ${expected}"188 " attribute name " + gotAttr[name].name 217 189 ); 218 190 } … … 221 193 assertEqual( 222 194 got.childNodes.length, expected.childNodes.length, 223 "Children length mismatch for " + got.nodeName + 224 ": got ${got} but expected ${expected}" 195 "Children length mismatch for " + got.nodeName 225 196 ); 226 197 for(var j=0; j<got.childNodes.length; ++j) { … … 230 201 ); 231 202 } catch(err) { 232 throw "Bad child " + j + " for node" + got.nodeName + ": " + err;203 throw "Bad child " + j + " for element " + got.nodeName + ": " + err; 233 204 } 234 205 } … … 237 208 } 238 209 239 // assign test function to AnotherWay test prototype 240 var proto = Test.AnotherWay._test_object_t.prototype; 241 proto.xml_eq = function(got, expected, msg) { 210 /** 211 * Function: Test.AnotherWay._test_object_t.xml_eq 212 * Test if two XML nodes are equivalent. Tests for same node types, same 213 * node names, same namespace URI, same attributes, and recursively 214 * tests child nodes for same criteria. 215 * 216 * (code) 217 * t.xml_eq(got, expected, message); 218 * (end) 219 * 220 * Parameters: 221 * got - {DOMElement | String} A DOM node or XML string to test. 222 * expected - {DOMElement | String} The expected DOM node or XML string. 223 * msg - {String} A message to print with test output. 224 * options - {Object} Optional object for configuring test options. Set 225 * 'prefix' property to true in order to compare element and attribute 226 * prefixes (namespace uri always tested). By default, prefixes 227 * are not tested. 228 */ 229 var proto = Test.AnotherWay._test_object_t.prototype; 230 proto.xml_eq = function(got, expected, msg, options) { 242 231 // convert arguments to nodes if string 243 232 if(typeof got == "string") { … … 245 234 got = createNode(got); 246 235 } catch(err) { 247 this.fail( "got argument could not be converted to an XML node: " + err);236 this.fail(msg + ": got argument could not be converted to an XML node: " + err); 248 237 return; 249 238 } … … 253 242 expected = createNode(expected); 254 243 } catch(err) { 255 this.fail( "expected argument could not be converted to an XML node: " + err);244 this.fail(msg + ": expected argument could not be converted to an XML node: " + err); 256 245 return; 257 246 } … … 260 249 // test nodes for equivalence 261 250 try { 262 assertElementNodesEqual(got, expected );251 assertElementNodesEqual(got, expected, options); 263 252 this.ok(true, msg); 264 253 } catch(err) { 265 this.fail( err);254 this.fail(msg + ": " + err); 266 255 } 267 256 }
