Permalink
Browse files

Jasmine: Convert initializer tests

  • Loading branch information...
1 parent f5f40f6 commit e9f7255d47d9e2eeda49d29f529a55ab4ffd60d4 @dmajda dmajda committed Apr 22, 2012
Showing with 67 additions and 38 deletions.
  1. +67 −0 spec/generated-parser.spec.js
  2. +0 −38 test/compiler-test.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"',
View
@@ -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; }',

0 comments on commit e9f7255

Please sign in to comment.