Behavior specs cleanup: Improve literal specs

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

@ -220,54 +220,63 @@ describe("generated parser behavior", function() {
}); });
describe("literal", function() { describe("literal", function() {
it("matches empty literal correctly", function() { describe("matching", function() {
var parser = PEG.buildParser('start = ""', options); it("matches empty literals", function() {
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", "a"); expect(parser).toParse("a");
expect(parser).toParse("A", "A"); expect(parser).toParse("A");
});
}); });
it("advances position on success", function() { describe("when it matches", function() {
var parser = PEG.buildParser('start = "a" .', options); it("returns the matched text", function() {
var parser = PEG.buildParser('start = "a"', options);
expect(parser).toParse("ab", ["a", "b"]); expect(parser).toParse("a", "a");
});
it("advances parse position past the matched text", function() {
var parser = PEG.buildParser('start = "a" .', options);
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 \"literal\"", function() {
var parser = PEG.buildParser('start = "a"', options);
expect(parser).toFailToParse("b", { expect(parser).toFailToParse("b", {
expected: [{ type: "literal", value: "a", description: '"a"' }] expected: [{ type: "literal", value: "a", description: '"a"' }]
});
}); });
}); });
}); });

Loading…
Cancel
Save