File size: 2,693 Bytes
4cadbaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * a node.js test runner that executes all conformance
 * tests and outputs results to console.
 * Process returns zero on success, non-zero on failure.
 */

const   fs = require('fs'),
      path = require('path'),
jsonselect = require('../jsonselect.js'),
       sys = require('sys');

var pathToTests = path.join(__dirname, "tests");

// a map: document nametest name -> list of se
var numTests = 0;
var numPassed = 0;
var tests = {};

function runOneSync(name, selname, p) {
    var testDocPath = path.join(p, name + ".json");
    var selDocPath = path.join(p, name + '_' +
                               selname + ".selector");
    var outputDocPath = selDocPath.replace(/selector$/, "output");

    // take `obj`, apply `sel, get `got`, is it what we `want`? 
    var obj = JSON.parse(fs.readFileSync(testDocPath));
    var want = String(fs.readFileSync(outputDocPath)).trim();
    var got = "";
    var sel = String(fs.readFileSync(selDocPath)).trim();

    try {
        jsonselect.forEach(sel, obj, function(m) {
            got += JSON.stringify(m, undefined, 4) + "\n";
        });
    } catch(e) {
        got = e.toString();
        if (want.trim() != got.trim()) throw e;
    }
    if (want.trim() != got.trim()) throw "mismatch";
}


function runTests() {
    console.log("Running Tests:"); 
    for (var l in tests) {
        for (var d in tests[l]) {
            console.log("  level " + l + " tests against \"" + d + ".json\":");
            for (var i = 0; i < tests[l][d].length; i++) {
                sys.print("    " + tests[l][d][i][0] + ": ");
                try {
                    runOneSync(d, tests[l][d][i][0], tests[l][d][i][1]);
                    numPassed++;
                    console.log("pass");
                } catch (e) {
                    console.log("fail (" + e.toString() + ")");
                }
            }
        }
    }
    console.log(numPassed + "/" + numTests + " passed");
    process.exit(numPassed == numTests ? 0 : 1);
}

// discover all tests
var pathToTests = path.join(__dirname, "tests");

fs.readdirSync(pathToTests).forEach(function(subdir) {
    var p = path.join(pathToTests, subdir);
    if (!fs.statSync(p).isDirectory()) return;
    var l = /^level_([\d+])$/.exec(subdir);
    if (!l) return;
    l = l[1];
    var files = fs.readdirSync(p);
    for (var i = 0; i < files.length; i++) {
        var f = files[i];
        var m = /^([A-Za-z]+)_(.+)\.selector$/.exec(f);
        if (m) {
            if (!tests.hasOwnProperty(l)) tests[l] = [];
            if (!tests[l].hasOwnProperty(m[1])) tests[l][m[1]] = [];
            numTests++;
            tests[l][m[1]].push([m[2], p]);
        }
    }
});
runTests();