Behavior specs cleanup: Improve rule specs

redux
David Majda 10 years ago
parent fa70e70ddf
commit fee58d0806

@ -183,37 +183,62 @@ describe("generated parser behavior", function() {
}); });
describe("rule", function() { describe("rule", function() {
var grammar = [
'{ var n = 0; }',
'start = (a "b") / (a "c") { return n; }',
'a = "a" { n++; }'
].join("\n");
if (options.cache) { if (options.cache) {
it("caches rule match results", function() { it("caches rule match results", function() {
var parser = PEG.buildParser(grammar, options); var parser = PEG.buildParser([
'{ var n = 0; }',
'start = (a "b") / (a "c") { return n; }',
'a = "a" { n++; }'
].join("\n"), options);
expect(parser).toParse("ac", 1); expect(parser).toParse("ac", 1);
}); });
} else { } else {
it("does not cache rule match results", function() { it("doesn't cache rule match results", function() {
var parser = PEG.buildParser(grammar, options); var parser = PEG.buildParser([
'{ var n = 0; }',
'start = (a "b") / (a "c") { return n; }',
'a = "a" { n++; }'
].join("\n"), options);
expect(parser).toParse("ac", 2); expect(parser).toParse("ac", 2);
}); });
} }
describe("named rule", function() { describe("when the expression matches", function() {
var parser = PEG.buildParser('start "start" = "a"'); it("returns its match result", function() {
var parser = PEG.buildParser('start = "a"');
it("delegates to the expression", function() {
expect(parser).toParse("a", "a"); expect(parser).toParse("a", "a");
expect(parser).toFailToParse("b");
}); });
});
it("overwrites expected string on failure", function() { describe("when the expression doesn't match", function() {
expect(parser).toFailToParse("b", { describe("without display name", function() {
expected: [{ type: "other", description: "start" }] it("reports match failure and doesn't record any expectation", function() {
var parser = PEG.buildParser('start = "a"');
expect(parser).toFailToParse("b", {
expected: [{ type: "literal", value: "a", description: '"a"' }]
});
});
});
describe("with display name", function() {
it("reports match failure and records an expectation of type \"other\"", function() {
var parser = PEG.buildParser('start "start" = "a"');
expect(parser).toFailToParse("b", {
expected: [{ type: "other", description: "start" }]
});
});
it("discards any expectations recorded when matching the expression", function() {
var parser = PEG.buildParser('start "start" = "a"');
expect(parser).toFailToParse("b", {
expected: [{ type: "other", description: "start" }]
});
}); });
}); });
}); });

Loading…
Cancel
Save