aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/parcom_spec.cr31
1 files changed, 30 insertions, 1 deletions
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