From 14c11b4dfc903652c276f8748fdb0b877507974d Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 22 Apr 2012 11:07:00 +0200 Subject: [PATCH] Jasmine: Convert class matching tests --- spec/generated-parser.spec.js | 55 +++++++++++++++++++++++ test/compiler-test.js | 83 ----------------------------------- 2 files changed, 55 insertions(+), 83 deletions(-) diff --git a/spec/generated-parser.spec.js b/spec/generated-parser.spec.js index cba568e..307d376 100644 --- a/spec/generated-parser.spec.js +++ b/spec/generated-parser.spec.js @@ -153,4 +153,59 @@ describe("generated parser", function() { }); }); }); + + describe("class matching", function() { + varyAll(function(options) { + it("matches empty class correctly", function() { + var parser = PEG.buildParser('start = []', options); + + expect(parser).toFailToParse("a"); + }); + + it("matches class with a character list correctly", 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"); + }); + + it("matches class with a range correctly", 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"); + }); + + it("matches inverted class correctly", function() { + var parser = PEG.buildParser('start = [^a]', options); + + expect(parser).toFailToParse("a"); + expect(parser).toParse("b", "b"); + }); + + it("is case sensitive without the \"i\" flag", function() { + var parser = PEG.buildParser('start = [a]', options); + + 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", "a"); + expect(parser).toParse("A", "A"); + }); + + it("advances position on success", function() { + var parser = PEG.buildParser('start = [a] .', options); + + expect(parser).toParse("ab", ["a", "b"]); + }); + }); + }); }); diff --git a/test/compiler-test.js b/test/compiler-test.js index 52f42b2..9878ff3 100644 --- a/test/compiler-test.js +++ b/test/compiler-test.js @@ -643,89 +643,6 @@ testWithVaryingTrackLineAndColumn("rule references", function(options) { parses(parser, "Python", "Python"); }); -testWithVaryingTrackLineAndColumn("classes", function(options) { - var emptyClassParser = PEG.buildParser('start = []', options); - doesNotParse(emptyClassParser, ""); - doesNotParse(emptyClassParser, "a"); - doesNotParse(emptyClassParser, "ab"); - - var invertedEmptyClassParser = PEG.buildParser('start = [^]', options); - doesNotParse(invertedEmptyClassParser, ""); - parses(invertedEmptyClassParser, "a", "a"); - doesNotParse(invertedEmptyClassParser, "ab"); - - var nonEmptyCaseSensitiveClassParser = PEG.buildParser( - 'start = [ab-d]', - options - ); - parses(nonEmptyCaseSensitiveClassParser, "a", "a"); - parses(nonEmptyCaseSensitiveClassParser, "b", "b"); - parses(nonEmptyCaseSensitiveClassParser, "c", "c"); - parses(nonEmptyCaseSensitiveClassParser, "d", "d"); - doesNotParse(nonEmptyCaseSensitiveClassParser, ""); - doesNotParse(nonEmptyCaseSensitiveClassParser, "A"); - doesNotParse(nonEmptyCaseSensitiveClassParser, "B"); - doesNotParse(nonEmptyCaseSensitiveClassParser, "C"); - doesNotParse(nonEmptyCaseSensitiveClassParser, "D"); - doesNotParse(nonEmptyCaseSensitiveClassParser, "e"); - doesNotParse(nonEmptyCaseSensitiveClassParser, "ab"); - - var invertedNonEmptyCaseSensitiveClassParser = PEG.buildParser( - 'start = [^ab-d]', - options - ); - parses(invertedNonEmptyCaseSensitiveClassParser, "A", "A"); - parses(invertedNonEmptyCaseSensitiveClassParser, "B", "B"); - parses(invertedNonEmptyCaseSensitiveClassParser, "C", "C"); - parses(invertedNonEmptyCaseSensitiveClassParser, "D", "D"); - parses(invertedNonEmptyCaseSensitiveClassParser, "e", "e"); - doesNotParse(invertedNonEmptyCaseSensitiveClassParser, "a", "a"); - doesNotParse(invertedNonEmptyCaseSensitiveClassParser, "b", "b"); - doesNotParse(invertedNonEmptyCaseSensitiveClassParser, "c", "c"); - doesNotParse(invertedNonEmptyCaseSensitiveClassParser, "d", "d"); - doesNotParse(invertedNonEmptyCaseSensitiveClassParser, ""); - doesNotParse(invertedNonEmptyCaseSensitiveClassParser, "ab"); - - var nonEmptyCaseInsensitiveClassParser = PEG.buildParser( - 'start = [ab-d]i', - options - ); - parses(nonEmptyCaseInsensitiveClassParser, "a", "a"); - parses(nonEmptyCaseInsensitiveClassParser, "b", "b"); - parses(nonEmptyCaseInsensitiveClassParser, "c", "c"); - parses(nonEmptyCaseInsensitiveClassParser, "d", "d"); - parses(nonEmptyCaseInsensitiveClassParser, "A", "A"); - parses(nonEmptyCaseInsensitiveClassParser, "B", "B"); - parses(nonEmptyCaseInsensitiveClassParser, "C", "C"); - parses(nonEmptyCaseInsensitiveClassParser, "D", "D"); - doesNotParse(nonEmptyCaseInsensitiveClassParser, ""); - doesNotParse(nonEmptyCaseInsensitiveClassParser, "e"); - doesNotParse(nonEmptyCaseInsensitiveClassParser, "ab"); - - var invertedNonEmptyCaseInsensitiveClassParser = PEG.buildParser( - 'start = [^ab-d]i', - options - ); - parses(invertedNonEmptyCaseInsensitiveClassParser, "e", "e"); - doesNotParse(invertedNonEmptyCaseInsensitiveClassParser, "a", "a"); - doesNotParse(invertedNonEmptyCaseInsensitiveClassParser, "b", "b"); - doesNotParse(invertedNonEmptyCaseInsensitiveClassParser, "c", "c"); - doesNotParse(invertedNonEmptyCaseInsensitiveClassParser, "d", "d"); - doesNotParse(invertedNonEmptyCaseInsensitiveClassParser, "A", "A"); - doesNotParse(invertedNonEmptyCaseInsensitiveClassParser, "B", "B"); - doesNotParse(invertedNonEmptyCaseInsensitiveClassParser, "C", "C"); - doesNotParse(invertedNonEmptyCaseInsensitiveClassParser, "D", "D"); - doesNotParse(invertedNonEmptyCaseInsensitiveClassParser, ""); - doesNotParse(invertedNonEmptyCaseInsensitiveClassParser, "ab"); - - /* - * Test that the parsing position moves forward after successful parsing of - * a class. - */ - var posTestParser = PEG.buildParser('start = [ab-d] [ab-d]', options); - parses(posTestParser, "ab", ["a", "b"]); -}); - testWithVaryingTrackLineAndColumn("cache", function(options) { var grammar = [ '{ var n = 0; }',