From 1b75a7b9b33ca8afb7e696c28e40c330780003b2 Mon Sep 17 00:00:00 2001 From: David Majda Date: Wed, 18 Aug 2010 21:15:48 +0200 Subject: [PATCH] Split-off |PEG.compiler.checks| tests --- test/checks-test.js | 58 +++++++++++++++++++++++++++++++++++++++++++ test/compiler-test.js | 53 --------------------------------------- test/index.html | 1 + 3 files changed, 59 insertions(+), 53 deletions(-) create mode 100644 test/checks-test.js diff --git a/test/checks-test.js b/test/checks-test.js new file mode 100644 index 0000000..7ff1b9e --- /dev/null +++ b/test/checks-test.js @@ -0,0 +1,58 @@ +(function(global) { + +module("PEG.compiler.checks"); + +test("reports missing referenced rules", function() { + var grammars = [ + 'start = missing', + 'start = missing / "a" / "b"', + 'start = "a" / "b" / missing', + 'start = missing "a" "b"', + 'start = "a" "b" missing', + 'start = label:missing', + 'start = &missing', + 'start = !missing', + 'start = missing?', + 'start = missing*', + 'start = missing+', + 'start = missing { }' + ]; + + for (var i = 0; i < grammars.length; i++) { + throws( + function() { PEG.buildParser(grammars[i]); }, + PEG.GrammarError, + { message: "Referenced rule \"missing\" does not exist." } + ); + } +}); + +test("reports left recursion", function() { + var grammars = [ + /* Direct */ + 'start = start', + 'start = start / "a" / "b"', + 'start = "a" / "b" / start', + 'start = start "a" "b"', + 'start = label:start', + 'start = &start', + 'start = !start', + 'start = start?', + 'start = start*', + 'start = start+', + 'start = start { }', + + /* Indirect */ + 'start = stop; stop = start' + ]; + + for (var i = 0; i < grammars.length; i++) { + throws( + function() { PEG.buildParser(grammars[i]); }, + PEG.GrammarError, + { message: "Left recursion detected for rule \"start\"." } + ); + } +}); + +})(this); diff --git a/test/compiler-test.js b/test/compiler-test.js index dbf92ad..fbb9c2e 100644 --- a/test/compiler-test.js +++ b/test/compiler-test.js @@ -66,59 +66,6 @@ test("buildParser reports syntax errors in the grammar", function() { ); }); -test("buildParser reports missing referenced rules", function() { - var grammars = [ - 'start = missing', - 'start = missing / "a" / "b"', - 'start = "a" / "b" / missing', - 'start = missing "a" "b"', - 'start = "a" "b" missing', - 'start = label:missing', - 'start = &missing', - 'start = !missing', - 'start = missing?', - 'start = missing*', - 'start = missing+', - 'start = missing { }' - ]; - - for (var i = 0; i < grammars.length; i++) { - throws( - function() { PEG.buildParser(grammars[i]); }, - PEG.GrammarError, - { message: "Referenced rule \"missing\" does not exist." } - ); - } -}); - -test("buildParser reports left recursion", function() { - var grammars = [ - /* Direct */ - 'start = start', - 'start = start / "a" / "b"', - 'start = "a" / "b" / start', - 'start = start "a" "b"', - 'start = label:start', - 'start = &start', - 'start = !start', - 'start = start?', - 'start = start*', - 'start = start+', - 'start = start { }', - - /* Indirect */ - 'start = stop; stop = start' - ]; - - for (var i = 0; i < grammars.length; i++) { - throws( - function() { PEG.buildParser(grammars[i]); }, - PEG.GrammarError, - { message: "Left recursion detected for rule \"start\"." } - ); - } -}); - test("buildParser allows custom start rule", function() { var parser = PEG.buildParser('s = "abcd"', "s"); parses(parser, "abcd", "abcd"); diff --git a/test/index.html b/test/index.html index 78748a1..1271ea5 100644 --- a/test/index.html +++ b/test/index.html @@ -7,6 +7,7 @@ +