Jasmine: Convert literal matching tests

redux
David Majda 12 years ago
parent feddd10190
commit ec48742032

@ -0,0 +1,140 @@
describe("generated parser", function() {
function vary(names, block) {
var values = {
trackLineAndColumn: [false, true],
cache: [false, true]
};
function varyStep(names, options) {
var clonedOptions = {}, key, name, i;
if (names.length === 0) {
/*
* We have to clone the options so that the block can save them safely
* (e.g. by capturing in a closure) without the risk that they will be
* changed later.
*/
for (key in options) {
clonedOptions[key] = options[key];
}
describe(
"with options " + jasmine.pp(clonedOptions),
function() { block(clonedOptions); }
);
} else {
name = names[0];
for (i = 0; i < values[name].length; i++) {
options[name] = values[name][i];
varyStep(names.slice(1), options);
}
}
}
varyStep(names, {});
}
function varyAll(block) {
vary(["cache", "trackLineAndColumn"], block);
}
beforeEach(function() {
this.addMatchers({
toParse: function(input, expected) {
var result;
try {
result = this.actual.parse(input);
this.message = function() {
return "Expected " + jasmine.pp(input) + " "
+ (this.isNot ? "not " : "")
+ "to parse as " + jasmine.pp(expected) + ", "
+ "but it parsed as " + jasmine.pp(result) + ".";
};
return this.env.equals_(result, expected);
} catch (e) {
this.message = function() {
return "Expected " + jasmine.pp(input) + " "
+ "to parse as " + jasmine.pp(expected) + ", "
+ "but it failed to parse with message "
+ jasmine.pp(e.message) + ".";
};
return false;
}
},
toFailToParse: function(input) {
var result;
try {
result = this.actual.parse(input);
this.message = function() {
return "Expected " + jasmine.pp(input) + " to fail to parse, "
+ "but it parsed as " + jasmine.pp(result) + ".";
};
return false;
} catch (e) {
this.message = function() {
return "Expected " + jasmine.pp(input) + " to parse, "
+ "but it failed with message "
+ jasmine.pp(e.message) + ".";
};
return true;
}
}
});
});
describe("literal matching", function() {
varyAll(function(options) {
it("matches empty literal correctly", function() {
var parser = PEG.buildParser('start = ""', options);
expect(parser).toParse("", "");
});
it("matches one-character literal correctly", function() {
var parser = PEG.buildParser('start = "a"', options);
expect(parser).toParse("a", "a");
expect(parser).toFailToParse("b");
});
it("matches multiple-character literal correctly", function() {
var parser = PEG.buildParser('start = "abcd"', options);
expect(parser).toParse("abcd", "abcd");
expect(parser).toFailToParse("ebcd");
expect(parser).toFailToParse("afcd");
expect(parser).toFailToParse("abgd");
expect(parser).toFailToParse("abch");
});
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"]);
});
});
});
});

@ -7,6 +7,7 @@
<script src="vendor/jasmine/jasmine-html.js"></script>
<script src="../lib/peg.js"></script>
<script src="parser.spec.js"></script>
<script src="generated-parser.spec.js"></script>
<script>
(function() {
var env = jasmine.getEnv(),

@ -643,50 +643,6 @@ testWithVaryingTrackLineAndColumn("rule references", function(options) {
parses(parser, "Python", "Python");
});
testWithVaryingTrackLineAndColumn("literals", function(options) {
var zeroCharParser = PEG.buildParser('start = ""', options);
parses(zeroCharParser, "", "");
doesNotParse(zeroCharParser, "a");
var oneCharCaseSensitiveParser = PEG.buildParser('start = "a"', options);
parses(oneCharCaseSensitiveParser, "a", "a");
doesNotParse(oneCharCaseSensitiveParser, "");
doesNotParse(oneCharCaseSensitiveParser, "A");
doesNotParse(oneCharCaseSensitiveParser, "b");
var multiCharCaseSensitiveParser = PEG.buildParser('start = "abcd"', options);
parses(multiCharCaseSensitiveParser, "abcd", "abcd");
doesNotParse(multiCharCaseSensitiveParser, "");
doesNotParse(multiCharCaseSensitiveParser, "abc");
doesNotParse(multiCharCaseSensitiveParser, "abcde");
doesNotParse(multiCharCaseSensitiveParser, "ABCD");
doesNotParse(multiCharCaseSensitiveParser, "efgh");
var oneCharCaseInsensitiveParser = PEG.buildParser('start = "a"i', options);
parses(oneCharCaseInsensitiveParser, "a", "a");
parses(oneCharCaseInsensitiveParser, "A", "A");
doesNotParse(oneCharCaseInsensitiveParser, "");
doesNotParse(oneCharCaseInsensitiveParser, "b");
var multiCharCaseInsensitiveParser = PEG.buildParser(
'start = "abcd"i',
options
);
parses(multiCharCaseInsensitiveParser, "abcd", "abcd");
parses(multiCharCaseInsensitiveParser, "ABCD", "ABCD");
doesNotParse(multiCharCaseInsensitiveParser, "");
doesNotParse(multiCharCaseInsensitiveParser, "abc");
doesNotParse(multiCharCaseInsensitiveParser, "abcde");
doesNotParse(multiCharCaseInsensitiveParser, "efgh");
/*
* Test that the parsing position moves forward after successful parsing of
* a literal.
*/
var posTestParser = PEG.buildParser('start = "a" "b"', options);
parses(posTestParser, "ab", ["a", "b"]);
});
testWithVaryingTrackLineAndColumn("anys", function(options) {
var parser = PEG.buildParser('start = .', options);
parses(parser, "a", "a");

Loading…
Cancel
Save