Jasmine: Convert |computeParams| compiler pass tests

redux
David Majda 12 years ago
parent 4edc9982cc
commit eaf2af8e7b

@ -0,0 +1,200 @@
describe("compiler pass |computeParams|", function() {
function pass(ast) {
PEG.compiler.passes.computeVarNames(ast);
PEG.compiler.passes.computeParams(ast);
}
function ruleDetails(details) { return { rules: [details] }; }
function expressionDetails(details) {
return ruleDetails({ expression: details });
}
function innerExpressionDetails(details) {
return expressionDetails({ expression: details });
}
describe("basic cases", function() {
it("computes params for an action", function() {
expect(pass).toChangeAST('start = a:"a" { }', expressionDetails({
params: { a: "result0" }
}));
});
it("computes params for a semantic and", function() {
expect(pass).toChangeAST('start = a:"a" &{ }', expressionDetails({
elements: [
{},
{ params: { a: "result0" } }
]
}));
});
it("computes params for a semantic not", function() {
expect(pass).toChangeAST('start = a:"a" !{ }', expressionDetails({
elements: [
{},
{ params: { a: "result0" } }
]
}));
});
});
describe("recursive walk", function() {
it("computes params for a choice", function() {
expect(pass).toChangeAST(
'start = a:"a" { } / "b" / "c"',
expressionDetails({
alternatives: [{ params: { a: "result0" } }, {}, {}]
})
);
expect(pass).toChangeAST(
'start = "a" / "b" / c:"c" { }',
expressionDetails({
alternatives: [{}, {}, { params: { c: "result0" } }]
})
);
});
it("computes params for a sequence", function() {
expect(pass).toChangeAST(
'start = (a:"a" { }) "b" "c"',
expressionDetails({
elements: [{ params: { a: "result0" } }, {}, {}]
})
);
expect(pass).toChangeAST(
'start = "a" "b" (c:"c" { })',
expressionDetails({
elements: [{}, {}, { params: { c: "result2" } }]
})
);
});
it("computes params for a labeled", function() {
expect(pass).toChangeAST('start = a:(b:"b" { })', innerExpressionDetails({
params: { b: "result0" }
}));
});
it("computes params for a simple and", function() {
expect(pass).toChangeAST('start = &(a:"a" { })', innerExpressionDetails({
params: { a: "result0" }
}));
});
it("computes params for a simple not", function() {
expect(pass).toChangeAST('start = &(a:"a" { })', innerExpressionDetails({
params: { a: "result0" }
}));
});
it("computes params for an optional", function() {
expect(pass).toChangeAST('start = (a:"a" { })?', innerExpressionDetails({
params: { a: "result0" }
}));
});
it("computes params for a zero or more", function() {
expect(pass).toChangeAST('start = (a:"a" { })*', innerExpressionDetails({
params: { a: "result1" }
}));
});
it("computes params for a one or more", function() {
expect(pass).toChangeAST('start = (a:"a" { })+', innerExpressionDetails({
params: { a: "result1" }
}));
});
it("computes params for an action", function() {
expect(pass).toChangeAST('start = (a:"a" { }) { }', innerExpressionDetails({
params: { a: "result0" }
}));
});
});
describe("scoping", function() {
it("creates a new scope for a choice", function() {
expect(pass).toChangeAST(
'start = (a:"a" / b:"b" / c:"c") { }',
expressionDetails({ params: {} })
);
});
it("does not create a new scope for a sequence", function() {
expect(pass).toChangeAST(
'start = a:"a" b:"b" c:"c" { }',
expressionDetails({
params: { a: "result0[0]", b: "result0[1]", c: "result0[2]" }
})
);
expect(pass).toChangeAST(
'start = a:"a" (b:"b" c:"c" d:"d") e:"e"{ }',
expressionDetails({
params: {
a: "result0[0]",
b: "result0[1][0]",
c: "result0[1][1]",
d: "result0[1][2]",
e: "result0[2]"
}
})
);
/*
* Regression tests for a bug where e.g. resultVar names like |result10|
* were incorrectly treated as names derived from |result1|, leading to
* incorrect substitution.
*/
expect(pass).toChangeAST(
'start = ("a" "b" "c" "d" "e" "f" "g" "h" "i" j:"j" { })*',
innerExpressionDetails({
params: { j: "result1[9]" } // Buggy code put "result1[0]0" here.
})
);
});
it("creates a new scope for a labeled", function() {
expect(pass).toChangeAST('start = a:(b:"b") { }', expressionDetails({
params: { a: "result0"}
}));
});
it("creates a new scope for a simple and", function() {
expect(pass).toChangeAST('start = &(a:"a") { }', expressionDetails({
params: {}
}));
});
it("creates a new scope for a simple not", function() {
expect(pass).toChangeAST('start = !(a:"a") { }', expressionDetails({
params: {}
}));
});
it("creates a new scope for an optional", function() {
expect(pass).toChangeAST('start = (a:"a")? { }', expressionDetails({
params: {}
}));
});
it("creates a new scope for a zero or more", function() {
expect(pass).toChangeAST('start = (a:"a")* { }', expressionDetails({
params: {}
}));
});
it("creates a new scope for a one or more", function() {
expect(pass).toChangeAST('start = (a:"a")+ { }', expressionDetails({
params: {}
}));
});
it("creates a new scope for an action", function() {
expect(pass).toChangeAST('start = (a:"a" { }) { }', expressionDetails({
params: {}
}));
});
});
});

@ -22,61 +22,6 @@ describe("compiler pass |computeVarNames|", function() {
function ruleDetails(details) { return { rules: [details] }; }
beforeEach(function() {
this.addMatchers({
toChangeAST: function(grammar, details) {
function matchDetails(value, details) {
function isArray(value) {
return Object.prototype.toString.apply(value) === "[object Array]";
}
function isObject(value) {
return value !== null && typeof value === "object";
}
var i, key;
if (isArray(details)) {
if (!isArray(value)) { return false; }
if (value.length !== details.length) { return false; }
for (i = 0; i < details.length; i++) {
if (!matchDetails(value[i], details[i])) { return false; }
}
return true;
} else if (isObject(details)) {
if (!isObject(value)) { return false; }
for (key in details) {
if (!(key in value)) { return false; }
if (!matchDetails(value[key], details[key])) { return false; }
}
return true;
} else {
return value === details;
}
}
var ast = PEG.parser.parse(grammar);
this.actual(ast);
this.message = function() {
return "Expected the pass "
+ (this.isNot ? "not " : "")
+ "to change the AST " + jasmine.pp(ast) + " "
+ "to match " + jasmine.pp(details) + ", "
+ "but it " + (this.isNot ? "did" : "didn't") + ".";
};
return matchDetails(ast, details);
}
});
});
it("computes variable names for a choice", function() {
expect(pass).toChangeAST('start = &"a" / &"b" / &"c"', ruleDetails({
resultVars: ["result0"],

@ -1 +1,58 @@
PEG = require("../lib/peg.js");
if (typeof module !== "undefined") {
PEG = require("../lib/peg.js");
}
beforeEach(function() {
this.addMatchers({
toChangeAST: function(grammar, details) {
function matchDetails(value, details) {
function isArray(value) {
return Object.prototype.toString.apply(value) === "[object Array]";
}
function isObject(value) {
return value !== null && typeof value === "object";
}
var i, key;
if (isArray(details)) {
if (!isArray(value)) { return false; }
if (value.length !== details.length) { return false; }
for (i = 0; i < details.length; i++) {
if (!matchDetails(value[i], details[i])) { return false; }
}
return true;
} else if (isObject(details)) {
if (!isObject(value)) { return false; }
for (key in details) {
if (!(key in value)) { return false; }
if (!matchDetails(value[key], details[key])) { return false; }
}
return true;
} else {
return value === details;
}
}
var ast = PEG.parser.parse(grammar);
this.actual(ast);
this.message = function() {
return "Expected the pass "
+ (this.isNot ? "not " : "")
+ "to change the AST " + jasmine.pp(ast) + " "
+ "to match " + jasmine.pp(details) + ", "
+ "but it " + (this.isNot ? "did" : "didn't") + ".";
};
return matchDetails(ast, details);
}
});
});

@ -6,11 +6,13 @@
<script src="vendor/jasmine/jasmine.js"></script>
<script src="vendor/jasmine/jasmine-html.js"></script>
<script src="../lib/peg.js"></script>
<script src="helpers.js"></script>
<script src="parser.spec.js"></script>
<script src="generated-parser.spec.js"></script>
<script src="compiler/passes/report-missing-rules.spec.js"></script>
<script src="compiler/passes/report-left-recursion.spec.js"></script>
<script src="compiler/passes/compute-var-names.spec.js"></script>
<script src="compiler/passes/compute-params.spec.js"></script>
<script>
(function() {
var env = jasmine.getEnv(),

@ -1,167 +0,0 @@
(function() {
module("PEG.compiler.passes.computeParams");
test("computes params", function() {
function extractNode(node) { return node; }
function extractExpression(node) { return node.expression; }
var cases = [
/* Bacics */
{
grammar: 'start = a:"a" { }',
extractor: extractNode,
params: { a: "result0" }
},
{
grammar: 'start = a:"a" &{ }',
extractor: function(node) { return node.elements[1]; },
params: { a: "result0" }
},
{
grammar: 'start = a:"a" !{ }',
extractor: function(node) { return node.elements[1]; },
params: { a: "result0" }
},
/* Recursive walk */
{
grammar: 'start = a:"a" { } / "b" / "c"',
extractor: function(node) { return node.alternatives[0]; },
params: { a: "result0" }
},
{
grammar: 'start = "a" / "b" / c:"c" { }',
extractor: function(node) { return node.alternatives[2]; },
params: { c: "result0" }
},
{
grammar: 'start = (a:"a" { }) "b" "c"',
extractor: function(node) { return node.elements[0]; },
params: { a: "result0" }
},
{
grammar: 'start = "a" "b" (c:"c" { })',
extractor: function(node) { return node.elements[2]; },
params: { c: "result2" }
},
{
grammar: 'start = a:(b:"b" { })',
extractor: extractExpression,
params: { b: "result0" }
},
{
grammar: 'start = &(a:"a" { })',
extractor: extractExpression,
params: { a: "result0" }
},
{
grammar: 'start = !(a:"a" { })',
extractor: extractExpression,
params: { a: "result0" }
},
{
grammar: 'start = (a:"a" { })?',
extractor: extractExpression,
params: { a: "result0" }
},
{
grammar: 'start = (a:"a" { })*',
extractor: extractExpression,
params: { a: "result1" }
},
{
grammar: 'start = (a:"a" { })+',
extractor: extractExpression,
params: { a: "result1" }
},
{
grammar: 'start = (a:"a" { }) { }',
extractor: extractExpression,
params: { a: "result0" }
},
/* Scoping */
{
grammar: 'start = (a:"a" / b:"b" / c:"c") { }',
extractor: extractNode,
params: { }
},
{
grammar: 'start = a:(b:"b") { }',
extractor: extractNode,
params: { a: "result0" }
},
{
grammar: 'start = &(a:"a") { }',
extractor: extractNode,
params: { }
},
{
grammar: 'start = !(a:"a") { }',
extractor: extractNode,
params: { }
},
{
grammar: 'start = (a:"a")? { }',
extractor: extractNode,
params: { }
},
{
grammar: 'start = (a:"a")* { }',
extractor: extractNode,
params: { }
},
{
grammar: 'start = (a:"a")+ { }',
extractor: extractNode,
params: { }
},
{
grammar: 'start = (a:"a" { }) { }',
extractor: extractNode,
params: { }
},
/* Sequences */
{
grammar: 'start = a:"a" b:"b" c:"c" { }',
extractor: extractNode,
params: { a: "result0[0]", b: "result0[1]", c: "result0[2]" }
},
{
grammar: 'start = a:"a" (b:"b" c:"c" d:"d") e:"e"{ }',
extractor: extractNode,
params: {
a: "result0[0]",
b: "result0[1][0]",
c: "result0[1][1]",
d: "result0[1][2]",
e: "result0[2]"
}
},
/*
* Regression tests for a bug where e.g. resultVar names like |result10|
* were incorrectly treated as names derived from |result1|, leading to
* incorrect substitution.
*/
{
grammar: 'start = ("a" "b" "c" "d" "e" "f" "g" "h" "i" j:"j" { })*',
extractor: extractExpression,
params: { j: "result1[9]" } // Buggy code put "result1[0]0" here.
}
];
for (var i = 0; i < cases.length; i++) {
var ast = PEG.parser.parse(cases[i].grammar);
PEG.compiler.passes.computeVarNames(ast);
PEG.compiler.passes.computeParams(ast);
deepEqual(
cases[i].extractor(ast.rules[0].expression).params,
cases[i].params
);
}
});
})();

@ -8,7 +8,6 @@
<script src="vendor/qunit/qunit.js"></script>
<script src="helpers.js"></script>
<script src="compiler/passes/remove-proxy-rules-test.js"></script>
<script src="compiler/passes/compute-params-test.js"></script>
</head>
<body>
<h1 id="qunit-header">PEG.js Test Suite</h1>

@ -75,8 +75,7 @@ QUnit.done(function(details) {
[
"helpers.js",
"compiler/passes/remove-proxy-rules-test.js",
"compiler/passes/compute-params-test.js"
"compiler/passes/remove-proxy-rules-test.js"
].forEach(function(file) {
eval("with (QUnit) {" + fs.readFileSync(__dirname + "/" + file, "utf8") + "}");
});

Loading…
Cancel
Save