From 54263a0cfec5ef1adcd1bf6541abc0275d9a98df Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Sun, 19 Mar 2023 22:13:13 +1300 Subject: first_of --- spec/parcom_spec.cr | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'spec') diff --git a/spec/parcom_spec.cr b/spec/parcom_spec.cr index aae1d1b..af95953 100644 --- a/spec/parcom_spec.cr +++ b/spec/parcom_spec.cr @@ -216,6 +216,36 @@ describe Parser do end end + describe "self.first_of" do + ps = [ + Parser.token('a'), + Parser.token('b'), + Parser.token('c'), + ] + abc = Parser(Char, Char).first_of(ps) + + it "fails to instantiate if array is empty" do + expect_raises(ArgumentError) { Parser.first_of([] of Parser(Char, Char)) } + end + + it "returns the result of the first parser that succeeds" do + {"a", "b", "c"}.each do |s| + tokens = Tokens.from_string(s) + result = abc.parse(tokens) + + result.value.should eq(s[0]) + result.tokens.empty?.should be_true + end + end + + it "fails if none of the parsers succeed" do + {"", "d"}.each do |s| + tokens = Tokens.from_string(s) + expect_raises(ParserFail) { abc.parse(tokens) } + end + end + end + describe "#assert" do p = Parser(Char, Char).any_token.assert { |c| c == 'a' } @@ -621,7 +651,6 @@ describe Parser do end end - # TODO: first_of # TODO: sep_by # TODO: phrase # TODO: peek -- cgit v1.2.1