Jasmine: Convert tests of parser's "classCharacterRange" rule

redux
David Majda 13 years ago
parent d29a753b8d
commit 5fb59b05f2

@ -61,24 +61,50 @@ describe("PEG.js grammar parser", function() {
}
},
toFailToParse: function() {
var result;
toFailToParse: function(details) {
/*
* Extracted into a function just to silence JSHint complaining about
* creating functions in a loop.
*/
function buildKeyMessage(key, value) {
return function() {
return "Expected " + jasmine.pp(this.actual) + " to fail to parse"
+ (details ? " with details " + jasmine.pp(details) : "") + ", "
+ "but " + jasmine.pp(key) + " "
+ "is " + jasmine.pp(value) + ".";
};
}
var result, key;
try {
result = PEG.parser.parse(this.actual);
this.message = function() {
return "Expected " + jasmine.pp(this.actual) + " to fail to parse, "
return "Expected " + jasmine.pp(this.actual) + " to fail to parse"
+ (details ? " with details " + jasmine.pp(details) : "") + ", "
+ "but it parsed as " + jasmine.pp(result) + ".";
};
return false;
} catch (e) {
this.message = function() {
return "Expected " + jasmine.pp(this.actual) + " to parse, "
+ "but it failed with message "
+ jasmine.pp(e.message) + ".";
};
if (this.isNot) {
this.message = function() {
return "Expected " + jasmine.pp(this.actual) + " to parse, "
+ "but it failed with message "
+ jasmine.pp(e.message) + ".";
};
} else {
if (details) {
for (key in details) {
if (!this.env.equals_(e[key], details[key])) {
this.message = buildKeyMessage(key, e[key]);
return false;
}
}
}
}
return true;
}
@ -86,6 +112,19 @@ describe("PEG.js grammar parser", function() {
});
});
/* Canonical classCharacterRange is "a-d". */
it("parses classCharacterRange", function() {
expect('start = [a-d]').toParseAs(classGrammar([["a", "d"]], "[a-d]"));
expect('start = [a-a]').toParseAs(classGrammar([["a", "a"]], "[a-a]"));
expect('start = [b-a]').toFailToParse({
message: "Invalid character range: b-a."
});
expect('start = [\u0081-\u0080]').toFailToParse({
message: "Invalid character range: \\x81-\\x80."
});
});
/* Canonical classCharacter is "a". */
it("parses classCharacter", function() {
expect('start = [a]').toParseAs(classGrammar(["a"], "[a]"));

@ -431,15 +431,4 @@ test("parses class", function() {
parserParses("start = [a-d]\n", classGrammar(false, [["a", "d"]], "[a-d]"));
});
/* Canonical classCharacterRange is "a-d". */
test("parses classCharacterRange", function() {
parserParses("start = [a-d]", classGrammar(false, [["a", "d"]], "[a-d]"));
parserParses("start = [a-a]", classGrammar(false, [["a", "a"]], "[a-a]"));
parserDoesNotParse("start = [b-a]");
parserDoesNotParseWithMessage(
"start = [b-a]",
"Invalid character range: b-a."
);
});
})();

Loading…
Cancel
Save