1function checkSnapshot(comment, actual, expected) { 2 if (actual.snapshotLength != expected.length) { 3 testFailed(comment + " incorrect length (expected " + expected.length + ", actual " + actual.snapshotLength + ")"); 4 return; 5 } 6 7 for (i = 0; i < actual.snapshotLength; ++i) { 8 if (actual.snapshotItem(i) != expected[i]) { 9 testFailed(comment + " item " + i + " incorrect (expected " + expected[i].nodeName + ", actual " + actual.snapshotItem(i).nodeName + ")"); 10 return; 11 } 12 } 13 14 testPassed(comment); 15} 16 17function test(doc, context, expr, expected, nsResolver) 18{ 19 try { 20 if (typeof(context) == "string") 21 context = doc.evaluate(context, doc.documentElement, nsResolver, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue; 22 if (typeof(expected) == "object") { 23 var result = doc.evaluate(expr, context, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 24 checkSnapshot(expr, result, expected); 25 } else { 26 var result = doc.evaluate(expr, context, nsResolver, XPathResult.ANY_TYPE, null); 27 if (typeof(expected) == "number") { 28 if (result.numberValue == expected) 29 testPassed(expr); 30 else 31 testFailed(expr + ": expected " + expected + ", actual " + result.numberValue); 32 } else if (typeof(expected) == "string") { 33 if (result.stringValue == expected) 34 testPassed(expr); 35 else 36 testFailed(expr + ": expected '" + expected + "', actual '" + result.stringValue + "'"); 37 } else if (typeof(expected) == "boolean") { 38 if (result.booleanValue == expected) 39 testPassed(expr); 40 else 41 testFailed(expr + ": expected '" + expected + "', actual '" + result.booleanValue + "'"); 42 } 43 } 44 } catch (ex) { 45 testFailed(expr + ": raised " + ex); 46 } 47} 48