Behavior specs cleanup: Improve literal specs

redux
David Majda 10 years ago
parent 2faff0000e
commit 54191fbf12

@ -220,50 +220,58 @@ describe("generated parser behavior", function() {
}); });
describe("literal", function() { describe("literal", function() {
it("matches empty literal correctly", function() { describe("matching", function() {
it("matches empty literals", function() {
var parser = PEG.buildParser('start = ""', options); var parser = PEG.buildParser('start = ""', options);
expect(parser).toParse("", ""); expect(parser).toParse("");
}); });
it("matches one-character literal correctly", function() { it("matches one-character literals", function() {
var parser = PEG.buildParser('start = "a"', options); var parser = PEG.buildParser('start = "a"', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a");
expect(parser).toFailToParse("b"); expect(parser).toFailToParse("b");
}); });
it("matches multi-character literal correctly", function() { it("matches multi-character literals", function() {
var parser = PEG.buildParser('start = "abcd"', options); var parser = PEG.buildParser('start = "abcd"', options);
expect(parser).toParse("abcd", "abcd"); expect(parser).toParse("abcd");
expect(parser).toFailToParse("ebcd"); expect(parser).toFailToParse("efgh");
expect(parser).toFailToParse("afcd");
expect(parser).toFailToParse("abgd");
expect(parser).toFailToParse("abch");
}); });
it("is case sensitive without the \"i\" flag", function() { it("is case sensitive without the \"i\" flag", function() {
var parser = PEG.buildParser('start = "a"', options); var parser = PEG.buildParser('start = "a"', options);
expect(parser).toParse("a", "a"); expect(parser).toParse("a");
expect(parser).toFailToParse("A"); expect(parser).toFailToParse("A");
}); });
it("is case insensitive with the \"i\" flag", function() { it("is case insensitive with the \"i\" flag", function() {
var parser = PEG.buildParser('start = "a"i', options); var parser = PEG.buildParser('start = "a"i', options);
expect(parser).toParse("a");
expect(parser).toParse("A");
});
});
describe("when it matches", function() {
it("returns the matched text", 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 text", 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() {
it("reports match failure and records an expectation of type \"literal\"", function() {
var parser = PEG.buildParser('start = "a"', options); var parser = PEG.buildParser('start = "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
@ -271,6 +279,7 @@ describe("generated parser behavior", function() {
}); });
}); });
}); });
});
describe("character class", function() { describe("character class", function() {
it("matches empty class correctly", function() { it("matches empty class correctly", function() {

Loading…
Cancel
Save