From e9f7255d47d9e2eeda49d29f529a55ab4ffd60d4 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 22 Apr 2012 16:45:28 +0200 Subject: [PATCH] Jasmine: Convert initializer tests --- spec/generated-parser.spec.js | 67 +++++++++++++++++++++++++++++++++++ test/compiler-test.js | 38 -------------------- 2 files changed, 67 insertions(+), 38 deletions(-) diff --git a/spec/generated-parser.spec.js b/spec/generated-parser.spec.js index c8efd75..4c58ef5 100644 --- a/spec/generated-parser.spec.js +++ b/spec/generated-parser.spec.js @@ -91,6 +91,19 @@ describe("generated parser", function() { }); }); + describe("initializer code", function() { + varyAll(function(options) { + it("runs before the parsing begins", function() { + var parser = PEG.buildParser([ + '{ var result = 42; }', + 'start = "a" { return result }' + ].join("\n"), options); + + expect(parser).toParse("a", 42); + }); + }); + }); + describe("choice matching", function() { varyAll(function(options) { it("matches correctly", function() { @@ -227,6 +240,24 @@ describe("generated parser", function() { expect(parser).toParse("1\u2029x", [2, 1]); // paragraph separator }); } + + it("can use variables defined in the initializer", function() { + var parser = PEG.buildParser([ + '{ var v = 42 }', + 'start = "a" &{ return v === 42; }' + ].join("\n"), options); + + expect(parser).toParse("a", ["a", ""]); + }); + + it("can use functions defined in the initializer", function() { + var parser = PEG.buildParser([ + '{ function f() { return 42; } }', + 'start = "a" &{ return f() === 42; }' + ].join("\n"), options); + + expect(parser).toParse("a", ["a", ""]); + }); }); }); @@ -286,6 +317,24 @@ describe("generated parser", function() { expect(parser).toParse("1\u2029x", [2, 1]); // paragraph separator }); } + + it("can use variables defined in the initializer", function() { + var parser = PEG.buildParser([ + '{ var v = 42 }', + 'start = "a" !{ return v !== 42; }' + ].join("\n"), options); + + expect(parser).toParse("a", ["a", ""]); + }); + + it("can use functions defined in the initializer", function() { + var parser = PEG.buildParser([ + '{ function f() { return 42; } }', + 'start = "a" !{ return f() !== 42; }' + ].join("\n"), options); + + expect(parser).toParse("a", ["a", ""]); + }); }); }); @@ -387,6 +436,24 @@ describe("generated parser", function() { }); } + it("can use variables defined in the initializer", function() { + var parser = PEG.buildParser([ + '{ var v = 42 }', + 'start = "a" { return v; }' + ].join("\n"), options); + + expect(parser).toParse("a", 42); + }); + + it("can use functions defined in the initializer", function() { + var parser = PEG.buildParser([ + '{ function f() { return 42; } }', + 'start = "a" { return f(); }' + ].join("\n"), options); + + expect(parser).toParse("a", 42); + }); + it("does not advance position when the expression matches but the action returns |null|", function() { var parser = PEG.buildParser( 'start = "a" { return null; } / "a"', diff --git a/test/compiler-test.js b/test/compiler-test.js index a03a0d0..3ff968b 100644 --- a/test/compiler-test.js +++ b/test/compiler-test.js @@ -13,44 +13,6 @@ function testWithVaryingTrackLineAndColumn(name, callback) { ); } -testWithVaryingTrackLineAndColumn("initializer", function(options) { - var variableInActionParser = PEG.buildParser( - '{ a = 42; }; start = "a" { return a; }', - options - ); - parses(variableInActionParser, "a", 42); - - var functionInActionParser = PEG.buildParser( - '{ function f() { return 42; } }; start = "a" { return f(); }', - options - ); - parses(functionInActionParser, "a", 42); - - var variableInSemanticAndParser = PEG.buildParser( - '{ a = 42; }; start = "a" &{ return a === 42; }', - options - ); - parses(variableInSemanticAndParser, "a", ["a", ""]); - - var functionInSemanticAndParser = PEG.buildParser( - '{ function f() { return 42; } }; start = "a" &{ return f() === 42; }', - options - ); - parses(functionInSemanticAndParser, "a", ["a", ""]); - - var variableInSemanticNotParser = PEG.buildParser( - '{ a = 42; }; start = "a" !{ return a !== 42; }', - options - ); - parses(variableInSemanticNotParser, "a", ["a", ""]); - - var functionInSemanticNotParser = PEG.buildParser( - '{ function f() { return 42; } }; start = "a" !{ return f() !== 42; }', - options - ); - parses(functionInSemanticNotParser, "a", ["a", ""]); -}); - testWithVaryingTrackLineAndColumn("cache", function(options) { var grammar = [ '{ var n = 0; }',