From 112e4122d040784f11d4fb55a65b0e596d1b4527 Mon Sep 17 00:00:00 2001 From: David Majda Date: Mon, 30 Apr 2012 10:28:01 +0200 Subject: [PATCH] Jasmine: Convert remaining error reporting tests --- spec/generated-parser.spec.js | 142 ++++++++++++++++++++++++++++++++ test/compiler-test.js | 147 ---------------------------------- 2 files changed, 142 insertions(+), 147 deletions(-) diff --git a/spec/generated-parser.spec.js b/spec/generated-parser.spec.js index 9db3c6a..b58f38d 100644 --- a/spec/generated-parser.spec.js +++ b/spec/generated-parser.spec.js @@ -173,6 +173,18 @@ describe("generated parser", function() { expect(parser).toParse("ac", 2); }); } + + it("does not overwrite expected string on failure when not named", function() { + var parser = PEG.buildParser('start = [0-9]', options); + + expect(parser).toFailToParse("a", { expected: ["[0-9]"] }); + }); + + it("overwrites expected string on failure when named", function() { + var parser = PEG.buildParser('start "digit" = [0-9]', options); + + expect(parser).toFailToParse("a", { expected: ["digit"] }); + }); }); }); @@ -236,6 +248,12 @@ describe("generated parser", function() { expect(parser).toParse("a", ["", "a"]); }); + + it("does not influence expected strings on failure", function() { + var parser = PEG.buildParser('start = &"a"', options); + + expect(parser).toFailToParse("b", { expected: [] }); + }); }); }); @@ -253,6 +271,12 @@ describe("generated parser", function() { expect(parser).toParse("a", "a"); }); + + it("does not influence expected strings on failure", function() { + var parser = PEG.buildParser('start = !"a"', options); + + expect(parser).toFailToParse("a", { expected: [] }); + }); }); }); @@ -596,6 +620,12 @@ describe("generated parser", function() { expect(parser).toParse("ab", ["a", "b"]); }); + + it("sets expected string correctly on failure", function() { + var parser = PEG.buildParser('start = "a"', options); + + expect(parser).toFailToParse("b", { expected: ['"a"'] }); + }); }); }); @@ -612,6 +642,12 @@ describe("generated parser", function() { expect(parser).toParse("ab", ["a", "b"]); }); + + it("sets expected string correctly on failure", function() { + var parser = PEG.buildParser('start = .', options); + + expect(parser).toFailToParse("", { expected: ['any character'] }); + }); }); }); @@ -667,11 +703,117 @@ describe("generated parser", function() { expect(parser).toParse("ab", ["a", "b"]); }); + + it("sets expected string correctly on failure", function() { + var parser = PEG.buildParser('start = [a]', options); + + expect(parser).toFailToParse("b", { expected: ["[a]"] }); + }); }); }); describe("error reporting", function() { varyAll(function(options) { + describe("behavior", function() { + it("reports only the rightmost error", function() { + var parser = PEG.buildParser('start = "a" "b" / "a" "c" "d"', options); + + expect(parser).toFailToParse("ace", { offset: 2, expected: ['"d"'] }); + }); + }); + + describe("expected strings reporting", function() { + it("reports expected strings correctly with no alternative", function() { + var parser = PEG.buildParser('start = ', options); + + expect(parser).toFailToParse("a", { expected: [] }); + }); + + it("reports expected strings correctly with one alternative", function() { + var parser = PEG.buildParser('start = "a"', options); + + expect(parser).toFailToParse("b", { expected: ['"a"'] }); + }); + + it("reports expected strings correctly with multiple alternatives", function() { + var parser = PEG.buildParser('start = "a" / "b" / "c"', options); + + expect(parser).toFailToParse("d", { + expected: ['"a"', '"b"', '"c"'] + }); + }); + + it("removes duplicates from expected strings", function() { + var parser = PEG.buildParser('start = "a" / "a"', options); + + expect(parser).toFailToParse("b", { expected: ['"a"'] }); + }); + + it("sorts expected strings", function() { + var parser = PEG.buildParser('start = "c" / "b" / "a"', options); + + expect(parser).toFailToParse("d", { + expected: ['"a"', '"b"', '"c"'] + }); + }); + }); + + describe("found string reporting", function() { + it("reports found string correctly at the end of input", function() { + var parser = PEG.buildParser('start = "a"', options); + + expect(parser).toFailToParse("", { found: null }); + }); + + it("reports found string correctly in the middle of input", function() { + var parser = PEG.buildParser('start = "a"', options); + + expect(parser).toFailToParse("b", { found: "b" }); + }); + }); + + describe("message building", function() { + it("builds message correctly with no alternative", function() { + var parser = PEG.buildParser('start = ', options); + + expect(parser).toFailToParse("a", { + message: 'Expected end of input but "a" found.' + }); + }); + + it("builds message correctly with one alternative", function() { + var parser = PEG.buildParser('start = "a"', options); + + expect(parser).toFailToParse("b", { + message: 'Expected "a" but "b" found.' + }); + }); + + it("builds message correctly with multiple alternatives", function() { + var parser = PEG.buildParser('start = "a" / "b" / "c"', options); + + expect(parser).toFailToParse("d", { + message: 'Expected "a", "b" or "c" but "d" found.' + }); + }); + + it("builds message correctly at the end of input", function() { + var parser = PEG.buildParser('start = "a"', options); + + expect(parser).toFailToParse("", { + message: 'Expected "a" but end of input found.' + }); + }); + + it("builds message correctly in the middle of input", function() { + var parser = PEG.buildParser('start = "a"', options); + + expect(parser).toFailToParse("b", { + message: 'Expected "a" but "b" found.' + }); + }); + }); + describe("position reporting", function() { it("reports position correctly with invalid input", function() { var parser = PEG.buildParser('start = "a"', options); diff --git a/test/compiler-test.js b/test/compiler-test.js index e28300d..70b5baa 100644 --- a/test/compiler-test.js +++ b/test/compiler-test.js @@ -13,151 +13,4 @@ function testWithVaryingTrackLineAndColumn(name, callback) { ); } -testWithVaryingTrackLineAndColumn("error details", function(options) { - var literalParser = PEG.buildParser('start = "abcd"', options); - doesNotParseWithDetails( - literalParser, - "", - ["\"abcd\""], - null, - 'Expected "abcd" but end of input found.' - ); - doesNotParseWithDetails( - literalParser, - "efgh", - ["\"abcd\""], - "e", - 'Expected "abcd" but "e" found.' - ); - doesNotParseWithDetails( - literalParser, - "abcde", - [], - "e", - 'Expected end of input but "e" found.' - ); - - var classParser = PEG.buildParser('start = [a-d]', options); - doesNotParseWithDetails( - classParser, - "", - ["[a-d]"], - null, - 'Expected [a-d] but end of input found.' - ); - var negativeClassParser = PEG.buildParser('start = [^a-d]', options); - doesNotParseWithDetails( - negativeClassParser, - "", - ["[^a-d]"], - null, - 'Expected [^a-d] but end of input found.' - ); - - var anyParser = PEG.buildParser('start = .', options); - doesNotParseWithDetails( - anyParser, - "", - ["any character"], - null, - 'Expected any character but end of input found.' - ); - - var namedRuleWithLiteralParser = PEG.buildParser( - 'start "digit" = [0-9]', - options - ); - doesNotParseWithDetails( - namedRuleWithLiteralParser, - "a", - ["digit"], - "a", - 'Expected digit but "a" found.' - ); - - var namedRuleWithAnyParser = PEG.buildParser('start "whatever" = .', options); - doesNotParseWithDetails( - namedRuleWithAnyParser, - "", - ["whatever"], - null, - 'Expected whatever but end of input found.' - ); - - var namedRuleWithNamedRuleParser = PEG.buildParser([ - 'start "digits" = digit+', - 'digit "digit" = [0-9]' - ].join("\n"), options); - doesNotParseWithDetails( - namedRuleWithNamedRuleParser, - "", - ["digits"], - null, - 'Expected digits but end of input found.' - ); - - var choiceParser1 = PEG.buildParser('start = "a" / "b" / "c"', options); - doesNotParseWithDetails( - choiceParser1, - "def", - ["\"a\"", "\"b\"", "\"c\""], - "d", - 'Expected "a", "b" or "c" but "d" found.' - ); - - var choiceParser2 = PEG.buildParser('start = "a" "b" "c" / "a"', options); - doesNotParseWithDetails( - choiceParser2, - "abd", - ["\"c\""], - "d", - 'Expected "c" but "d" found.' - ); - - var simpleNotParser = PEG.buildParser('start = !"a" "b"', options); - doesNotParseWithDetails( - simpleNotParser, - "c", - ["\"b\""], - "c", - 'Expected "b" but "c" found.' - ); - - var simpleAndParser = PEG.buildParser('start = &"a" [a-b]', options); - doesNotParseWithDetails( - simpleAndParser, - "c", - [], - "c", - 'Expected end of input but "c" found.' - ); - - var emptyParser = PEG.buildParser('start = ', options); - doesNotParseWithDetails( - emptyParser, - "something", - [], - "s", - 'Expected end of input but "s" found.' - ); - - var duplicateErrorParser = PEG.buildParser('start = "a" / "a"', options); - doesNotParseWithDetails( - duplicateErrorParser, - "", - ["\"a\""], - null, - 'Expected "a" but end of input found.' - ); - - var unsortedErrorsParser = PEG.buildParser('start = "b" / "a"', options); - doesNotParseWithDetails( - unsortedErrorsParser, - "", - ["\"a\"", "\"b\""], - null, - 'Expected "a" or "b" but end of input found.' - ); -}); - })();