From 548209b48bc23df2ec7b9917f3a02d46783bad1c Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 16 Jan 2015 16:24:42 +0100 Subject: [PATCH] Behavior specs cleanup: Improve simple predicate specs --- .../generated-parser-behavior.spec.js | 72 +++++++++++++------ 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/spec/behavior/generated-parser-behavior.spec.js b/spec/behavior/generated-parser-behavior.spec.js index 3d8e743..6ff2423 100644 --- a/spec/behavior/generated-parser-behavior.spec.js +++ b/spec/behavior/generated-parser-behavior.spec.js @@ -682,44 +682,72 @@ describe("generated parser behavior", function() { }); describe("positive simple predicate", function() { - it("matches correctly", function() { - var parser = PEG.buildParser('start = &"a" "a"', options); + describe("when the expression matches", function() { + it("returns |undefined|", function() { + var parser = PEG.buildParser('start = &"a" "a"', options); - expect(parser).toParse("a", [undefined, "a"]); - expect(parser).toFailToParse("b"); - }); + expect(parser).toParse("a", [undefined, "a"]); + }); - it("does not advance position on success", function() { - var parser = PEG.buildParser('start = &"a" "a"', options); + it("resets parse position", function() { + var parser = PEG.buildParser('start = &"a" "a"', options); - expect(parser).toParse("a", [undefined, "a"]); + expect(parser).toParse("a"); + }); }); - it("does not influence expected strings on failure", function() { - var parser = PEG.buildParser('start = &"a"', options); + describe("when the expression doesn't match", function() { + it("reports match failure", function() { + var parser = PEG.buildParser('start = &"a"', options); + + expect(parser).toFailToParse("b"); + }); + + it("discards any expectations recorded when matching the expression", function() { + var parser = PEG.buildParser('start = "a" / &"b" / "c"', options); - expect(parser).toFailToParse("b", { expected: [] }); + expect(parser).toFailToParse("d", { + expected: [ + { type: "literal", value: "a", description: '"a"' }, + { type: "literal", value: "c", description: '"c"' } + ] + }); + }); }); }); describe("negative simple predicate", function() { - it("matches correctly", function() { - var parser = PEG.buildParser('start = !"a" "b"', options); + describe("when the expression matches", function() { + it("reports match failure", function() { + var parser = PEG.buildParser('start = !"a"', options); - expect(parser).toParse("b", [undefined, "b"]); - expect(parser).toFailToParse("a"); + expect(parser).toFailToParse("a"); + }); }); - it("does not advance position on failure", function() { - var parser = PEG.buildParser('start = !"a" / "a"', options); + describe("when the expression doesn't match", function() { + it("returns |undefined|", function() { + var parser = PEG.buildParser('start = !"a" "b"', options); - expect(parser).toParse("a", "a"); - }); + expect(parser).toParse("b", [undefined, "b"]); + }); + + it("resets parse position", function() { + var parser = PEG.buildParser('start = !"a" "b"', options); - it("does not influence expected strings on failure", function() { - var parser = PEG.buildParser('start = !"a"', options); + expect(parser).toParse("b"); + }); + + it("discards any expectations recorded when matching the expression", function() { + var parser = PEG.buildParser('start = "a" / !"b" / "c"', options); - expect(parser).toFailToParse("a", { expected: [] }); + expect(parser).toFailToParse("b", { + expected: [ + { type: "literal", value: "a", description: '"a"' }, + { type: "literal", value: "c", description: '"c"' } + ] + }); + }); }); });