Behavior specs cleanup: Improve character class specs

redux
David Majda 9 years ago
parent 54191fbf12
commit 74be12c657

@ -282,62 +282,74 @@ describe("generated parser behavior", function() {
}); });
describe("character class", function() { describe("character class", function() {
it("matches empty class correctly", function() { describe("matching", function() {
var parser = PEG.buildParser('start = []', options); 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() { it("matches classes with a character list", function() {
var parser = PEG.buildParser('start = [abc]', options); var parser = PEG.buildParser('start = [abc]', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a");
expect(parser).toParse("b", "b"); expect(parser).toParse("b");
expect(parser).toParse("c", "c"); expect(parser).toParse("c");
expect(parser).toFailToParse("d"); expect(parser).toFailToParse("d");
}); });
it("matches class with a range correctly", function() { it("matches classes with a character range", function() {
var parser = PEG.buildParser('start = [a-c]', options); var parser = PEG.buildParser('start = [a-c]', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a");
expect(parser).toParse("b", "b"); expect(parser).toParse("b");
expect(parser).toParse("c", "c"); expect(parser).toParse("c");
expect(parser).toFailToParse("d"); expect(parser).toFailToParse("d");
}); });
it("matches inverted class correctly", function() { it("matches inverted classes", function() {
var parser = PEG.buildParser('start = [^a]', options); var parser = PEG.buildParser('start = [^a]', options);
expect(parser).toFailToParse("a"); expect(parser).toFailToParse("a");
expect(parser).toParse("b", "b"); 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() { expect(parser).toParse("a");
var parser = PEG.buildParser('start = [a]', options); expect(parser).toFailToParse("A");
});
expect(parser).toParse("a", "a"); it("is case insensitive with the \"i\" flag", function() {
expect(parser).toFailToParse("A"); 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() { describe("when it matches", function() {
var parser = PEG.buildParser('start = [a]i', options); 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() { it("advances parse position past the matched character", function() {
var parser = PEG.buildParser('start = [a] .', options); var parser = PEG.buildParser('start = [a] .', options);
expect(parser).toParse("ab", ["a", "b"]); expect(parser).toParse("ab");
});
}); });
it("sets expectation correctly on failure", function() { describe("when it doesn't match", function() {
var parser = PEG.buildParser('start = [a]', options); it("reports match failure and records an expectation of type \"class\"", function() {
var parser = PEG.buildParser('start = [a]', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "class", value: "[a]", description: "[a]" }] expected: [{ type: "class", value: "[a]", description: "[a]" }]
});
}); });
}); });
}); });

Loading…
Cancel
Save