|
| 1 | +var tests = []; |
| 2 | + |
| 3 | +exports.test = function(code, ast, options) { |
| 4 | + tests.push({code: code, ast: ast, options: options}); |
| 5 | +}; |
| 6 | +exports.testFail = function(code, message, options) { |
| 7 | + tests.push({code: code, error: message, options: options}); |
| 8 | +}; |
| 9 | +exports.testAssert = function(code, assert, options) { |
| 10 | + tests.push({code: code, assert: assert, options: options}); |
| 11 | +}; |
| 12 | + |
| 13 | +exports.runTests = function(config, callback) { |
| 14 | + var parse = config.parse; |
| 15 | + |
| 16 | + for (var i = 0; i < tests.length; ++i) { |
| 17 | + var test = tests[i]; |
| 18 | + if (config.filter && !config.filter(test)) continue; |
| 19 | + try { |
| 20 | + var testOpts = test.options || {locations: true}; |
| 21 | + var expected = {}; |
| 22 | + if (expected.onComment = testOpts.onComment) { |
| 23 | + testOpts.onComment = [] |
| 24 | + } |
| 25 | + if (expected.onToken = testOpts.onToken) { |
| 26 | + testOpts.onToken = []; |
| 27 | + } |
| 28 | + testOpts.plugins = { jsx: true }; |
| 29 | + var ast = parse(test.code, testOpts); |
| 30 | + if (test.error) { |
| 31 | + if (config.loose) { |
| 32 | + callback("ok", test.code); |
| 33 | + } else { |
| 34 | + callback("fail", test.code, "Expected error message: " + test.error + "\nBut parsing succeeded."); |
| 35 | + } |
| 36 | + } |
| 37 | + else if (test.assert) { |
| 38 | + var error = test.assert(ast); |
| 39 | + if (error) callback("fail", test.code, |
| 40 | + "\n Assertion failed:\n " + error); |
| 41 | + else callback("ok", test.code); |
| 42 | + } else { |
| 43 | + var mis = misMatch(test.ast, ast); |
| 44 | + for (var name in expected) { |
| 45 | + if (mis) break; |
| 46 | + if (expected[name]) { |
| 47 | + mis = misMatch(expected[name], testOpts[name]); |
| 48 | + testOpts[name] = expected[name]; |
| 49 | + } |
| 50 | + } |
| 51 | + if (mis) callback("fail", test.code, mis); |
| 52 | + else callback("ok", test.code); |
| 53 | + } |
| 54 | + } catch(e) { |
| 55 | + if (!(e instanceof SyntaxError)) { |
| 56 | + throw e; |
| 57 | + } |
| 58 | + if (test.error) { |
| 59 | + if (e.message == test.error) callback("ok", test.code); |
| 60 | + else callback("fail", test.code, |
| 61 | + "Expected error message: " + test.error + "\nGot error message: " + e.message); |
| 62 | + } else { |
| 63 | + callback("error", test.code, e.stack || e.toString()); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +function ppJSON(v) { return v instanceof RegExp ? v.toString() : JSON.stringify(v, null, 2); } |
| 70 | +function addPath(str, pt) { |
| 71 | + if (str.charAt(str.length-1) == ")") |
| 72 | + return str.slice(0, str.length-1) + "/" + pt + ")"; |
| 73 | + return str + " (" + pt + ")"; |
| 74 | +} |
| 75 | + |
| 76 | +var misMatch = exports.misMatch = function(exp, act) { |
| 77 | + if (!exp || !act || (typeof exp != "object") || (typeof act != "object")) { |
| 78 | + if (exp !== act) return ppJSON(exp) + " !== " + ppJSON(act); |
| 79 | + } else if (exp instanceof RegExp || act instanceof RegExp) { |
| 80 | + var left = ppJSON(exp), right = ppJSON(act); |
| 81 | + if (left !== right) return left + " !== " + right; |
| 82 | + } else if (exp.splice) { |
| 83 | + if (!act.slice) return ppJSON(exp) + " != " + ppJSON(act); |
| 84 | + if (act.length != exp.length) return "array length mismatch " + exp.length + " != " + act.length; |
| 85 | + for (var i = 0; i < act.length; ++i) { |
| 86 | + var mis = misMatch(exp[i], act[i]); |
| 87 | + if (mis) return addPath(mis, i); |
| 88 | + } |
| 89 | + } else { |
| 90 | + for (var prop in exp) { |
| 91 | + var mis = misMatch(exp[prop], act[prop]); |
| 92 | + if (mis) return addPath(mis, prop); |
| 93 | + } |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +function mangle(ast) { |
| 98 | + if (typeof ast != "object" || !ast) return; |
| 99 | + if (ast.slice) { |
| 100 | + for (var i = 0; i < ast.length; ++i) mangle(ast[i]); |
| 101 | + } else { |
| 102 | + var loc = ast.start && ast.end && {start: ast.start, end: ast.end}; |
| 103 | + if (loc) { delete ast.start; delete ast.end; } |
| 104 | + for (var name in ast) if (ast.hasOwnProperty(name)) mangle(ast[name]); |
| 105 | + if (loc) ast.loc = loc; |
| 106 | + } |
| 107 | +} |
0 commit comments