diff --git a/spec/parser.spec.js b/spec/parser.spec.js index 80122d3..0393999 100644 --- a/spec/parser.spec.js +++ b/spec/parser.spec.js @@ -61,24 +61,50 @@ describe("PEG.js grammar parser", function() { } }, - toFailToParse: function() { - var result; + toFailToParse: function(details) { + /* + * Extracted into a function just to silence JSHint complaining about + * creating functions in a loop. + */ + function buildKeyMessage(key, value) { + return function() { + return "Expected " + jasmine.pp(this.actual) + " to fail to parse" + + (details ? " with details " + jasmine.pp(details) : "") + ", " + + "but " + jasmine.pp(key) + " " + + "is " + jasmine.pp(value) + "."; + }; + } + + var result, key; try { result = PEG.parser.parse(this.actual); this.message = function() { - return "Expected " + jasmine.pp(this.actual) + " to fail to parse, " + return "Expected " + jasmine.pp(this.actual) + " to fail to parse" + + (details ? " with details " + jasmine.pp(details) : "") + ", " + "but it parsed as " + jasmine.pp(result) + "."; }; return false; } catch (e) { - this.message = function() { - return "Expected " + jasmine.pp(this.actual) + " to parse, " - + "but it failed with message " - + jasmine.pp(e.message) + "."; - }; + if (this.isNot) { + this.message = function() { + return "Expected " + jasmine.pp(this.actual) + " to parse, " + + "but it failed with message " + + jasmine.pp(e.message) + "."; + }; + } else { + if (details) { + for (key in details) { + if (!this.env.equals_(e[key], details[key])) { + this.message = buildKeyMessage(key, e[key]); + + return false; + } + } + } + } return true; } @@ -86,6 +112,19 @@ describe("PEG.js grammar parser", function() { }); }); + /* Canonical classCharacterRange is "a-d". */ + it("parses classCharacterRange", function() { + expect('start = [a-d]').toParseAs(classGrammar([["a", "d"]], "[a-d]")); + expect('start = [a-a]').toParseAs(classGrammar([["a", "a"]], "[a-a]")); + + expect('start = [b-a]').toFailToParse({ + message: "Invalid character range: b-a." + }); + expect('start = [\u0081-\u0080]').toFailToParse({ + message: "Invalid character range: \\x81-\\x80." + }); + }); + /* Canonical classCharacter is "a". */ it("parses classCharacter", function() { expect('start = [a]').toParseAs(classGrammar(["a"], "[a]")); diff --git a/test/parser-test.js b/test/parser-test.js index 9108618..89ad285 100644 --- a/test/parser-test.js +++ b/test/parser-test.js @@ -431,15 +431,4 @@ test("parses class", function() { parserParses("start = [a-d]\n", classGrammar(false, [["a", "d"]], "[a-d]")); }); -/* Canonical classCharacterRange is "a-d". */ -test("parses classCharacterRange", function() { - parserParses("start = [a-d]", classGrammar(false, [["a", "d"]], "[a-d]")); - parserParses("start = [a-a]", classGrammar(false, [["a", "a"]], "[a-a]")); - parserDoesNotParse("start = [b-a]"); - parserDoesNotParseWithMessage( - "start = [b-a]", - "Invalid character range: b-a." - ); -}); - })();