From 1a9792afb92e227adcb564a051b678faa9fe2036 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Thu, 9 Mar 2023 16:52:30 +1300 Subject: Implement Exactly + extra test for Sequence --- spec/parcom_spec.cr | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'spec') 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 -- cgit v1.2.1