From 74be12c6572881815b1a3963555f718aea8c120a Mon Sep 17 00:00:00 2001 From: David Majda Date: Mon, 12 Jan 2015 16:23:08 +0100 Subject: [PATCH] Behavior specs cleanup: Improve character class specs --- .../generated-parser-behavior.spec.js | 90 +++++++++++-------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/spec/behavior/generated-parser-behavior.spec.js b/spec/behavior/generated-parser-behavior.spec.js index 1119b52..ec1ed42 100644 --- a/spec/behavior/generated-parser-behavior.spec.js +++ b/spec/behavior/generated-parser-behavior.spec.js @@ -282,62 +282,74 @@ describe("generated parser behavior", function() { }); describe("character class", function() { - it("matches empty class correctly", function() { - var parser = PEG.buildParser('start = []', options); + describe("matching", function() { + it("matches empty classes", function() { + var parser = PEG.buildParser('start = []', options); - expect(parser).toFailToParse("a"); - }); + expect(parser).toFailToParse("a"); + }); - it("matches class with a character list correctly", function() { - var parser = PEG.buildParser('start = [abc]', options); + it("matches classes with a character list", function() { + var parser = PEG.buildParser('start = [abc]', options); - expect(parser).toParse("a", "a"); - expect(parser).toParse("b", "b"); - expect(parser).toParse("c", "c"); - expect(parser).toFailToParse("d"); - }); + expect(parser).toParse("a"); + expect(parser).toParse("b"); + expect(parser).toParse("c"); + expect(parser).toFailToParse("d"); + }); - it("matches class with a range correctly", function() { - var parser = PEG.buildParser('start = [a-c]', options); + it("matches classes with a character range", function() { + var parser = PEG.buildParser('start = [a-c]', options); - expect(parser).toParse("a", "a"); - expect(parser).toParse("b", "b"); - expect(parser).toParse("c", "c"); - expect(parser).toFailToParse("d"); - }); + expect(parser).toParse("a"); + expect(parser).toParse("b"); + expect(parser).toParse("c"); + expect(parser).toFailToParse("d"); + }); - it("matches inverted class correctly", function() { - var parser = PEG.buildParser('start = [^a]', options); + it("matches inverted classes", function() { + var parser = PEG.buildParser('start = [^a]', options); - expect(parser).toFailToParse("a"); - expect(parser).toParse("b", "b"); - }); + expect(parser).toFailToParse("a"); + expect(parser).toParse("b"); + }); + + it("is case sensitive without the \"i\" flag", function() { + var parser = PEG.buildParser('start = [a]', options); - it("is case sensitive without the \"i\" flag", function() { - var parser = PEG.buildParser('start = [a]', options); + expect(parser).toParse("a"); + expect(parser).toFailToParse("A"); + }); - expect(parser).toParse("a", "a"); - expect(parser).toFailToParse("A"); + it("is case insensitive with the \"i\" flag", function() { + var parser = PEG.buildParser('start = [a]i', options); + + expect(parser).toParse("a"); + expect(parser).toParse("A"); + }); }); - it("is case insensitive with the \"i\" flag", function() { - var parser = PEG.buildParser('start = [a]i', options); + describe("when it matches", function() { + it("returns the matched character", function() { + var parser = PEG.buildParser('start = [a]', options); - expect(parser).toParse("a", "a"); - expect(parser).toParse("A", "A"); - }); + expect(parser).toParse("a", "a"); + }); - it("advances position on success", function() { - var parser = PEG.buildParser('start = [a] .', options); + it("advances parse position past the matched character", function() { + var parser = PEG.buildParser('start = [a] .', options); - expect(parser).toParse("ab", ["a", "b"]); + expect(parser).toParse("ab"); + }); }); - it("sets expectation correctly on failure", function() { - var parser = PEG.buildParser('start = [a]', options); + describe("when it doesn't match", function() { + it("reports match failure and records an expectation of type \"class\"", function() { + var parser = PEG.buildParser('start = [a]', options); - expect(parser).toFailToParse("b", { - expected: [{ type: "class", value: "[a]", description: "[a]" }] + expect(parser).toFailToParse("b", { + expected: [{ type: "class", value: "[a]", description: "[a]" }] + }); }); }); });