Split-off |PEG.compiler.checks| tests

redux
David Majda 14 years ago
parent b15eb0bb5f
commit 1b75a7b9b3

@ -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);

@ -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() { test("buildParser allows custom start rule", function() {
var parser = PEG.buildParser('s = "abcd"', "s"); var parser = PEG.buildParser('s = "abcd"', "s");
parses(parser, "abcd", "abcd"); parses(parser, "abcd", "abcd");

@ -7,6 +7,7 @@
<script src="vendor/qunit/qunit.js"></script> <script src="vendor/qunit/qunit.js"></script>
<script src="../lib/peg.js"></script> <script src="../lib/peg.js"></script>
<script src="compiler-test.js"></script> <script src="compiler-test.js"></script>
<script src="checks-test.js"></script>
<script src="parser-test.js"></script> <script src="parser-test.js"></script>
</head> </head>
<body> <body>

Loading…
Cancel
Save