From ffd90a8c9ec2b39bab2d1cd242c5986b4513172f Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 6 Mar 2016 17:12:28 +0100 Subject: [PATCH] Label scope specs: Add negative specs (sequences) So far, semantic predicate and action specs which verified scope of labels from containing or outer sequences exercised only cases where label variables were defined. This commit adds also some negative cases. The idea comes from @Mingun. --- .../generated-parser-behavior.spec.js | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/spec/behavior/generated-parser-behavior.spec.js b/spec/behavior/generated-parser-behavior.spec.js index b68b5e6..ed40b02 100644 --- a/spec/behavior/generated-parser-behavior.spec.js +++ b/spec/behavior/generated-parser-behavior.spec.js @@ -445,6 +445,24 @@ describe("generated parser behavior", function() { expect(parser).toParse("a"); }); + + it("cannot access variable defined by labeled predicate element", function() { + var parser = PEG.buildParser( + 'start = "a" b:&{ return b === undefined; } "c"', + options + ); + + expect(parser).toFailToParse("ac"); + }); + + it("cannot access variables defined by following labeled elements", function() { + var parser = PEG.buildParser( + 'start = &{ return a === "a"; } a:"a"', + options + ); + + expect(parser).toFailToParse("a"); + }); }); describe("in outer sequence", function() { @@ -456,6 +474,24 @@ describe("generated parser behavior", function() { expect(parser).toParse("ab"); }); + + it("cannot access variable defined by labeled predicate element", function() { + var parser = PEG.buildParser( + 'start = "a" b:("b" &{ return b === undefined; }) "c"', + options + ); + + expect(parser).toFailToParse("abc"); + }); + + it("cannot access variables defined by following labeled elements", function() { + var parser = PEG.buildParser( + 'start = ("a" &{ return b === "b"; }) b:"b"', + options + ); + + expect(parser).toFailToParse("ab"); + }); }); }); @@ -554,6 +590,24 @@ describe("generated parser behavior", function() { expect(parser).toParse("a"); }); + + it("cannot access variable defined by labeled predicate element", function() { + var parser = PEG.buildParser( + 'start = "a" b:!{ return b !== undefined; } "c"', + options + ); + + expect(parser).toFailToParse("ac"); + }); + + it("cannot access variables defined by following labeled elements", function() { + var parser = PEG.buildParser( + 'start = !{ return a !== "a"; } a:"a"', + options + ); + + expect(parser).toFailToParse("a"); + }); }); describe("in outer sequence", function() { @@ -565,6 +619,24 @@ describe("generated parser behavior", function() { expect(parser).toParse("ab"); }); + + it("cannot access variable defined by labeled predicate element", function() { + var parser = PEG.buildParser( + 'start = "a" b:("b" !{ return b !== undefined; }) "c"', + options + ); + + expect(parser).toFailToParse("abc"); + }); + + it("cannot access variables defined by following labeled elements", function() { + var parser = PEG.buildParser( + 'start = ("a" !{ return b !== "b"; }) b:"b"', + options + ); + + expect(parser).toFailToParse("ab"); + }); }); }); @@ -870,6 +942,24 @@ describe("generated parser behavior", function() { expect(parser).toParse("ab", ["a", "a"]); }); + + it("cannot access variable defined by labeled action element", function() { + var parser = PEG.buildParser( + 'start = "a" b:("b" { return b; }) c:"c"', + options + ); + + expect(parser).toFailToParse("abc"); + }); + + it("cannot access variables defined by following labeled elements", function() { + var parser = PEG.buildParser( + 'start = ("a" { return b; }) b:"b"', + options + ); + + expect(parser).toFailToParse("ab"); + }); }); });