Specs cleanup: Implement generated parser API specs

The generated parser API specs are mostly extracted from
generated-parser.spec.js, which got renamed to
generated-parser-behavior.spec.js to better reflect its purpose.
redux
David Majda 11 years ago
parent 94c8b08acf
commit e101e1b6f3

@ -0,0 +1,50 @@
describe("generated parser API", function() {
describe("parse", function() {
it("parses input", function() {
var parser = PEG.buildParser('start = "a"');
expect(parser.parse("a")).toBe("a");
});
it("throws an exception on syntax error", function() {
var parser = PEG.buildParser('start = "a"');
expect(function() { parser.parse("b"); }).toThrow();
});
describe("start rule", function() {
var parser = PEG.buildParser([
'a = "x" { return "a"; }',
'b = "x" { return "b"; }',
'c = "x" { return "c"; }'
].join("\n"), { allowedStartRules: ["b", "c"] });
describe("when |startRule| is not set", function() {
it("starts parsing from the first allowed rule", function() {
expect(parser.parse("x")).toBe("b");
});
});
describe("when |startRule| is set to an allowed rule", function() {
it("starts parsing from the specified rule", function() {
expect(parser.parse("x", { startRule: "b" })).toBe("b");
expect(parser.parse("x", { startRule: "c" })).toBe("c");
});
});
describe("when |startRule| is set to a disallowed start rule", function() {
it("throws an exception", function() {
expect(
function() { parser.parse("x", { startRule: "a" }); }
).toThrow();
});
});
});
it("accepts custom options", function() {
var parser = PEG.buildParser('start = "a"');
parser.parse("a", { foo: 42 });
});
});
});

@ -1,4 +1,4 @@
describe("generated parser", function() {
describe("generated parser behavior", function() {
function varyOptimizationOptions(block) {
function clone(object) {
var result = {}, key;
@ -121,37 +121,6 @@ describe("generated parser", function() {
});
});
describe("parse", function() {
var parser = PEG.buildParser([
'a = "x" { return "a"; }',
'b = "x" { return "b"; }',
'c = "x" { return "c"; }'
].join("\n"), { allowedStartRules: ["b", "c"] });
describe("start rule", function() {
describe("without the |startRule| option", function() {
it("uses the first allowed rule", function() {
expect(parser).toParse("x", "b");
});
});
describe("when the |startRule| option specifies allowed rule", function() {
it("uses the specified rule", function() {
expect(parser).toParse("x", { startRule: "b" }, "b");
expect(parser).toParse("x", { startRule: "c" }, "c");
});
});
describe("when the |startRule| option specifies disallowed rule", function() {
it("throws exception", function() {
expect(parser).toFailToParse("x", { startRule: "a" }, {
message: "Can't start parsing from rule \"a\"."
});
});
});
});
});
varyOptimizationOptions(function(options) {
describe("initializer code", function() {
it("runs before the parsing begins", function() {

@ -15,7 +15,8 @@
<script src="unit/compiler/passes/generate-bytecode.spec.js"></script>
<script src="api/pegjs-api.spec.js"></script>
<script src="api/plugin-api.spec.js"></script>
<script src="api/generated-parser.spec.js"></script>
<script src="api/generated-parser-api.spec.js"></script>
<script src="api/generated-parser-behavior.spec.js"></script>
<script>
(function() {
var env = jasmine.getEnv(),

Loading…
Cancel
Save