diff options
Diffstat (limited to 'spec/parcom_spec.cr')
| -rw-r--r-- | spec/parcom_spec.cr | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/spec/parcom_spec.cr b/spec/parcom_spec.cr index 9051cb7..713445f 100644 --- a/spec/parcom_spec.cr +++ b/spec/parcom_spec.cr @@ -401,6 +401,15 @@ describe Sequence do expect_raises(ParserException) { p.parse(tokens) } end end + + it "succeeds and returns empty array if parser iterable is empty" do + tokens = TokenStream.from_string("abcd") + empty_p = Sequence.new([] of Parser(Char, Char)) + result = empty_p.parse(tokens) + + result.value.empty?.should be_true + result.tokens.should eq(tokens) + end end end @@ -480,7 +489,46 @@ describe Some do end end -pending Exactly do +describe Exactly do + letter_a = Token.new('a') + tokens = TokenStream.from_string("aaabcd") + + describe "#parse" do + it "tries to parse exactly n of the wrapper parser" do + p = Exactly.new(3, letter_a) + result = p.parse(tokens) + + result.value.should eq("aaa".chars) + result.tokens.should eq(tokens[3..]) + end + + it "always succeeds with an empty array if n < 1" do + p = Exactly.new(0, letter_a) + result = p.parse(tokens) + + result.value.empty?.should be_true + result.tokens.should eq(tokens) + + p = Exactly.new(-42, letter_a) + result = p.parse(tokens) + + result.value.empty?.should be_true + result.tokens.should eq(tokens) + end + + it "does not take extra matching tokens" do + p = Exactly.new(2, letter_a) + result = p.parse(tokens) + + result.value.should eq("aa".chars) + result.tokens.should eq(tokens[2..]) + end + + it "fails if there are not enough matching tokens" do + p = Exactly.new(60, letter_a) + expect_raises(ParserException) { p.parse(tokens) } + end + end end pending AtLeast do |
