summaryrefslogtreecommitdiffstats
path: root/LayoutTests/fast/xpath/xpath-test-pre.js
blob: 68bef2c61af09739c1d461d8e29f7a1f90bb2b1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function checkSnapshot(comment, actual, expected) {
    if (actual.snapshotLength != expected.length) {
        testFailed(comment + " incorrect length (expected " + expected.length + ", actual " + actual.snapshotLength + ")");
        return;
    }
    
    for (i = 0; i < actual.snapshotLength; ++i) {
        if (actual.snapshotItem(i) != expected[i]) {
            testFailed(comment + " item " + i + " incorrect (expected " + expected[i].nodeName + ", actual " + actual.snapshotItem(i).nodeName + ")");
            return;
        }
    }
    
    testPassed(comment);
}

function test(doc, context, expr, expected, nsResolver)
{
    try {
        if (typeof(context) == "string")
            context = doc.evaluate(context, doc.documentElement, nsResolver, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;
        if (typeof(expected) == "object") {
            var result = doc.evaluate(expr, context, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
            checkSnapshot(expr,  result, expected);
        } else {
            var result = doc.evaluate(expr, context, nsResolver, XPathResult.ANY_TYPE, null);
            if (typeof(expected) == "number") {
                if (result.numberValue == expected)
                    testPassed(expr);
                else
                    testFailed(expr + ": expected " + expected + ", actual " + result.numberValue);
            } else if (typeof(expected) == "string") {
                if (result.stringValue == expected)
                    testPassed(expr);
                else
                    testFailed(expr + ": expected '" + expected + "', actual '" + result.stringValue + "'");
            } else if (typeof(expected) == "boolean") {
                if (result.booleanValue == expected)
                    testPassed(expr);
                else
                    testFailed(expr + ": expected '" + expected + "', actual '" + result.booleanValue + "'");
            }
        }
    } catch (ex) {
        testFailed(expr + ": raised " + ex);
    }
}