From e101e1b6f362b3619220acf5daec2a2d87abd385 Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 23 May 2014 15:07:22 +0200 Subject: [PATCH] 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. --- spec/api/generated-parser-api.spec.js | 50 +++++++++++++++++++ ...c.js => generated-parser-behavior.spec.js} | 33 +----------- spec/index.html | 3 +- 3 files changed, 53 insertions(+), 33 deletions(-) create mode 100644 spec/api/generated-parser-api.spec.js rename spec/api/{generated-parser.spec.js => generated-parser-behavior.spec.js} (97%) diff --git a/spec/api/generated-parser-api.spec.js b/spec/api/generated-parser-api.spec.js new file mode 100644 index 0000000..ad9ff24 --- /dev/null +++ b/spec/api/generated-parser-api.spec.js @@ -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 }); + }); + }); +}); diff --git a/spec/api/generated-parser.spec.js b/spec/api/generated-parser-behavior.spec.js similarity index 97% rename from spec/api/generated-parser.spec.js rename to spec/api/generated-parser-behavior.spec.js index 252b0e1..1a809bc 100644 --- a/spec/api/generated-parser.spec.js +++ b/spec/api/generated-parser-behavior.spec.js @@ -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() { diff --git a/spec/index.html b/spec/index.html index bd0d668..176f368 100644 --- a/spec/index.html +++ b/spec/index.html @@ -15,7 +15,8 @@ - + +