diff --git a/spec/parser.spec.js b/spec/parser.spec.js index 9cface3..0c89361 100644 --- a/spec/parser.spec.js +++ b/spec/parser.spec.js @@ -1,17 +1,73 @@ describe("PEG.js grammar parser", function() { - it("works", function() { - expect(PEG.parser.parse('start = "a"')).toEqual({ + var trivialGrammar; + + beforeEach(function() { + trivialGrammar = { type: "grammar", initializer: null, rules: [ { - type: "rule", - name: "start", + type: "rule", + name: "start", displayName: null, - expression: { type: "literal", value: "a", ignoreCase: false } + expression: { type: "literal", value: "abcd", ignoreCase: false } } ], startRule: "start" + }; + + this.addMatchers({ + toParseAs: function(expected) { + var result; + + try { + result = PEG.parser.parse(this.actual); + + this.message = function() { + return "Expected " + jasmine.pp(this.actual) + " " + + (this.isNot ? "not " : "") + + "to parse as " + jasmine.pp(expected) + ", " + + "but it parsed as " + jasmine.pp(result) + "."; + }; + + return this.env.equals_(result, expected); + } catch (e) { + this.message = function() { + return "Expected " + jasmine.pp(this.actual) + " " + + "to parse as " + jasmine.pp(expected) + ", " + + "but it failed to parse with message " + + jasmine.pp(e.message) + "."; + }; + + return false; + } + } }); }); + + /* Canonical whitespace is " ". */ + it("parses whitespace", function() { + expect('start =\t"abcd"' ).toParseAs(trivialGrammar); + expect('start =\x0B"abcd"' ).toParseAs(trivialGrammar); // no "\v" in IE + expect('start =\f"abcd"' ).toParseAs(trivialGrammar); + expect('start = "abcd"' ).toParseAs(trivialGrammar); + expect('start =\u00A0"abcd"').toParseAs(trivialGrammar); + expect('start =\uFEFF"abcd"').toParseAs(trivialGrammar); + expect('start =\u1680"abcd"').toParseAs(trivialGrammar); + expect('start =\u180E"abcd"').toParseAs(trivialGrammar); + expect('start =\u2000"abcd"').toParseAs(trivialGrammar); + expect('start =\u2001"abcd"').toParseAs(trivialGrammar); + expect('start =\u2002"abcd"').toParseAs(trivialGrammar); + expect('start =\u2003"abcd"').toParseAs(trivialGrammar); + expect('start =\u2004"abcd"').toParseAs(trivialGrammar); + expect('start =\u2005"abcd"').toParseAs(trivialGrammar); + expect('start =\u2006"abcd"').toParseAs(trivialGrammar); + expect('start =\u2007"abcd"').toParseAs(trivialGrammar); + expect('start =\u2008"abcd"').toParseAs(trivialGrammar); + expect('start =\u2009"abcd"').toParseAs(trivialGrammar); + expect('start =\u200A"abcd"').toParseAs(trivialGrammar); + expect('start =\u202F"abcd"').toParseAs(trivialGrammar); + expect('start =\u205F"abcd"').toParseAs(trivialGrammar); + expect('start =\u3000"abcd"').toParseAs(trivialGrammar); + }); }); diff --git a/test/parser-test.js b/test/parser-test.js index 5452879..eda13ec 100644 --- a/test/parser-test.js +++ b/test/parser-test.js @@ -580,31 +580,4 @@ test("parses eolChar", function() { parserParses('start =\u2029"abcd"', simpleGrammar); }); -/* Canonical whitespace is " ". */ -test("parses whitespace", function() { - parserParses('start =\t"abcd"', simpleGrammar); - /* IE does not recognize "\v". */ - parserParses('start =\x0B"abcd"', simpleGrammar); - parserParses('start =\f"abcd"', simpleGrammar); - parserParses('start = "abcd"', simpleGrammar); - parserParses('start =\u00A0"abcd"', simpleGrammar); - parserParses('start =\uFEFF"abcd"', simpleGrammar); - parserParses('start =\u1680"abcd"', simpleGrammar); - parserParses('start =\u180E"abcd"', simpleGrammar); - parserParses('start =\u2000"abcd"', simpleGrammar); - parserParses('start =\u2001"abcd"', simpleGrammar); - parserParses('start =\u2002"abcd"', simpleGrammar); - parserParses('start =\u2003"abcd"', simpleGrammar); - parserParses('start =\u2004"abcd"', simpleGrammar); - parserParses('start =\u2005"abcd"', simpleGrammar); - parserParses('start =\u2006"abcd"', simpleGrammar); - parserParses('start =\u2007"abcd"', simpleGrammar); - parserParses('start =\u2008"abcd"', simpleGrammar); - parserParses('start =\u2009"abcd"', simpleGrammar); - parserParses('start =\u200A"abcd"', simpleGrammar); - parserParses('start =\u202F"abcd"', simpleGrammar); - parserParses('start =\u205F"abcd"', simpleGrammar); - parserParses('start =\u3000"abcd"', simpleGrammar); -}); - })();