From fee58d0806c5c5e30025072513670f7b58b232fc Mon Sep 17 00:00:00 2001 From: David Majda Date: Sat, 24 Jan 2015 20:17:52 +0100 Subject: [PATCH] Behavior specs cleanup: Improve rule specs --- .../generated-parser-behavior.spec.js | 57 +++++++++++++------ 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/spec/behavior/generated-parser-behavior.spec.js b/spec/behavior/generated-parser-behavior.spec.js index 1e8644c..29895c4 100644 --- a/spec/behavior/generated-parser-behavior.spec.js +++ b/spec/behavior/generated-parser-behavior.spec.js @@ -183,37 +183,62 @@ describe("generated parser behavior", function() { }); describe("rule", function() { - var grammar = [ - '{ var n = 0; }', - 'start = (a "b") / (a "c") { return n; }', - 'a = "a" { n++; }' - ].join("\n"); - if (options.cache) { 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); }); } else { - it("does not cache rule match results", function() { - var parser = PEG.buildParser(grammar, options); + it("doesn't cache rule match results", function() { + 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); }); } - describe("named rule", function() { - var parser = PEG.buildParser('start "start" = "a"'); + describe("when the expression matches", function() { + 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).toFailToParse("b"); }); + }); - it("overwrites expected string on failure", function() { - expect(parser).toFailToParse("b", { - expected: [{ type: "other", description: "start" }] + describe("when the expression doesn't match", function() { + describe("without display name", function() { + 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" }] + }); }); }); });