From 6406766e5a175c87dfc20c1ff1374c8c8a163517 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Sat, 18 Mar 2023 22:47:02 +1300 Subject: Many and Some --- spec/parcom_spec.cr | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'spec') diff --git a/spec/parcom_spec.cr b/spec/parcom_spec.cr index 86ad93a..870b0a2 100644 --- a/spec/parcom_spec.cr +++ b/spec/parcom_spec.cr @@ -380,5 +380,40 @@ describe Parser do result.value.should eq('b') end end + + describe "#many" do + p = Parser(Char, Char).token('a').many + + it "returns no results if it never succeeds" do + ["", "bb"].each do |s| + tokens = Tokens.from_string(s) + result = p.parse(tokens) + + result.value.empty?.should be_true + result.tokens.should eq(tokens) + end + end + + it "parses as many times as possible" do + ["a", "aaa", "aaaaa"].each do |s| + tokens = Tokens.from_string(s) + result = p.parse(tokens) + + result.value.should eq(s.chars) + result.tokens.empty?.should be_true + end + end + end + + describe "#some" do + p = Parser(Char, Char).token('a').some + + it "fails if it cannot parse at least once" do + ["", "bb"].each do |s| + tokens = Tokens.from_string(s) + expect_raises(ParserFail) { p.parse(tokens) } + end + end + end end -- cgit v1.2.1