Behavior specs cleanup: Improve action specs

redux
David Majda 10 years ago
parent b623396cb8
commit 3d9600b81b

@ -796,78 +796,102 @@ describe("generated parser behavior", function() {
}); });
describe("action", function() { describe("action", function() {
it("tranforms the expression result by returnung a value", function() { describe("when the expression matches", function() {
it("returns the value returned by the code", function() {
var parser = PEG.buildParser('start = "a" { return 42; }', options); var parser = PEG.buildParser('start = "a" { return 42; }', options);
expect(parser).toParse("a", 42); expect(parser).toParse("a", 42);
}); });
it("is not called when the expression does not match", function() { describe("label variables", function() {
it("can access label variables from a labeled expression", function() {
var parser = PEG.buildParser('start = a:"a" { return a; }', options);
expect(parser).toParse("a", "a");
});
it("can access label variables from a sequence with labeled elements", function() {
var parser = PEG.buildParser( var parser = PEG.buildParser(
'start = "a" { throw "Boom!"; } / "b"', 'start = a:"a" b:"b" c:"c" { return [a, b, c]; }',
options options
); );
expect(parser).toParse("b", "b"); expect(parser).toParse("abc", ["a", "b", "c"]);
});
}); });
it("can use label variables", function() { describe("initializer variables & functions", function() {
var parser = PEG.buildParser('start = a:"a" { return a; }', options); it("can access variables defined in the initializer", function() {
var parser = PEG.buildParser([
'{ var v = 42 }',
'start = "a" { return v; }'
].join("\n"), options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a", 42);
});
it("can access 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("can use the |text| function to get the text matched by the expression", function() { describe("available variables & functions", function() {
it("|parser| contains the parser object", function() {
var parser = PEG.buildParser( var parser = PEG.buildParser(
'start = "a" "b" "c" { return text(); }', 'start = "a" { return parser; }',
options options
); );
expect(parser).toParse("abc", "abc"); expect(parser).toParse("a", parser);
}); });
it("can use the |offset| function to get the current parse position", function() { it("|options| contains options", function() {
var parser = PEG.buildParser( var parser = PEG.buildParser(
'start = "a" ("b" { return offset(); })', 'start = "a" { return options; }',
options options
); );
expect(parser).toParse("ab", ["a", 1]); expect(parser).toParse("a", { a: 42 }, { a: 42 });
}); });
it("can use the |line| and |column| functions to get the current line and column", function() { it("|text| returns text matched by the expression", function() {
var parser = PEG.buildParser(
'start = "a" { return text(); }',
options
);
expect(parser).toParse("a", "a");
});
it("|offset|, |line|, and |column| return parse position, line, and column at the beginning of the expression", function() {
var parser = PEG.buildParser([ var parser = PEG.buildParser([
'{ var result; }', '{ var result; }',
'start = line (nl+ line)* { return result; }', 'start = line (nl+ line)* { return result; }',
'line = thing (" "+ thing)*', 'line = thing (" "+ thing)*',
'thing = digit / mark', 'thing = digit / mark',
'digit = [0-9]', 'digit = [0-9]',
'mark = "x" { result = [line(), column()]; }', 'mark = "x" { result = [offset(), line(), column()]; }',
'nl = ("\\r" / "\\n" / "\\u2028" / "\\u2029")' 'nl = [\\r\\n\\u2028\\u2029]'
].join("\n"), options); ].join("\n"), options);
expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", [7, 5]); expect(parser).toParse("1\n2\n\n3\n\n\n4 5 x", [13, 7, 5]);
/* Non-Unix newlines */ /* Non-Unix newlines */
expect(parser).toParse("1\rx", [2, 1]); // Old Mac expect(parser).toParse("1\rx", [2, 2, 1]); // Old Mac
expect(parser).toParse("1\r\nx", [2, 1]); // Windows expect(parser).toParse("1\r\nx", [3, 2, 1]); // Windows
expect(parser).toParse("1\n\rx", [3, 1]); // mismatched expect(parser).toParse("1\n\rx", [3, 3, 1]); // mismatched
/* Strange newlines */ /* Strange newlines */
expect(parser).toParse("1\u2028x", [2, 1]); // line separator expect(parser).toParse("1\u2028x", [2, 2, 1]); // line separator
expect(parser).toParse("1\u2029x", [2, 1]); // paragraph separator expect(parser).toParse("1\u2029x", [2, 2, 1]); // paragraph separator
}); });
it("can use variables defined in the initializer", function() { it("|expected| terminates parsing and throws an exception", function() {
var parser = PEG.buildParser([
'{ var v = 42 }',
'start = "a" { return v; }'
].join("\n"), options);
expect(parser).toParse("a", 42);
});
it("can use the |expected| function to trigger an error", function() {
var parser = PEG.buildParser( var parser = PEG.buildParser(
'start = "a" { expected("a"); }', 'start = "a" { expected("a"); }',
options options
@ -883,7 +907,7 @@ describe("generated parser behavior", function() {
}); });
}); });
it("can use the |error| function to trigger an error", function() { it("|error| terminates parsing and throws an exception", function() {
var parser = PEG.buildParser( var parser = PEG.buildParser(
'start = "a" { error("a"); }', 'start = "a" { error("a"); }',
options options
@ -898,32 +922,24 @@ describe("generated parser behavior", function() {
column: 1 column: 1
}); });
}); });
});
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("can use the |parser| variable to access the parser object", function() { describe("when the expression doesn't match", function() {
var parser = PEG.buildParser( it("reports match failure", function() {
'start = "a" { return parser; }', var parser = PEG.buildParser('start = "a" { return 42; }', options);
options
);
expect(parser).toParse("a", parser); expect(parser).toFailToParse("b");
}); });
it("can use options passed to the parser", function() { it("doesn't execute the code", function() {
var parser = PEG.buildParser( var parser = PEG.buildParser(
'start = "a" { return options; }', 'start = "a" { throw "Boom!"; } / "b"',
options options
); );
expect(parser).toParse("a", { a: 42 }, { a: 42 }); expect(parser).toParse("b");
});
}); });
}); });

Loading…
Cancel
Save