-
Notifications
You must be signed in to change notification settings - Fork 7
/
test-helpers.js
62 lines (53 loc) · 2.3 KB
/
test-helpers.js
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
"use strict";
var esvalid = require("./");
var assert = require("assert");
var STMT = {type: "EmptyStatement"};
var BLOCK = {type: "BlockStatement", body: []};
var EXPR = {type: "Literal", value: null};
var NUM = {type: "Literal", value: 0};
var STR = {type: "Literal", value: "a"};
var ID = {type: "Identifier", name: "a"};
var CATCH = {type: "CatchClause", param: ID, body: BLOCK};
// wrap a statement in a program
function wrapProgram(n) { return {type: "Program", body: [n]}; }
// wrap a statement in an iteration statement
function wrapIter(n) { return {type: "WhileStatement", test: {type: "Literal", value: true}, body: n}; }
// wrap zero or more statements in a function expression
function FE() { return {type: "FunctionExpression", params: [], body: {type: "BlockStatement", body: arguments}}; }
// wrap zero or more statements in a function declaration
function FD() { return {type: "FunctionDeclaration", id: ID, params: [], body: {type: "BlockStatement", body: arguments}}; }
// wrap a statement in a labeledstatement
function label(l, n) { return {type: "LabeledStatement", label: {type: "Identifier", name: l}, body: n}; }
// wrap an expression in an expressionstatement
function exprStmt(e) { return {type: "ExpressionStatement", expression: e}; }
function validStmt(x, msg) { assert.ok(esvalid.isValid(wrapProgram(x)), msg); }
function invalidStmt(n, x, msg) {
assert.ok(!esvalid.isValid(wrapProgram(x)), msg);
var errors = esvalid.errors(wrapProgram(x));
errors.forEach(function(e) { assert.notEqual(e.node, null, msg); });
assert.equal(n, errors.length, msg);
}
function validExpr(x, msg) { assert.ok(esvalid.isValidExpression(x), msg); }
function invalidExpr(n, x, msg) {
assert.ok(!esvalid.isValidExpression(x), msg);
var errors = esvalid.errors(wrapProgram(exprStmt(x)));
errors.forEach(function(e) { assert.notEqual(e.node, null, msg); });
assert.equal(n, errors.length, msg);
}
exports.STMT = STMT;
exports.BLOCK = BLOCK;
exports.EXPR = EXPR;
exports.NUM = NUM;
exports.STR = STR;
exports.ID = ID;
exports.CATCH = CATCH;
exports.wrapProgram = wrapProgram;
exports.wrapIter = wrapIter;
exports.FE = FE;
exports.FD = FD;
exports.label = label;
exports.exprStmt = exprStmt;
exports.validExpr = validExpr;
exports.invalidExpr = invalidExpr;
exports.validStmt = validStmt;
exports.invalidStmt = invalidStmt;