From 5b94e65c29f141b40b4d0333a1de6968d1670b1e Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Thu, 9 Mar 2023 16:20:10 +1300 Subject: Implement Sequence --- spec/parcom_spec.cr | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'spec/parcom_spec.cr') diff --git a/spec/parcom_spec.cr b/spec/parcom_spec.cr index 57c5024..9051cb7 100644 --- a/spec/parcom_spec.cr +++ b/spec/parcom_spec.cr @@ -378,6 +378,32 @@ describe Optional do end end +describe Sequence do + # HACK: ps has to be declared this way due to contravariance + # https://crystal-lang.org/reference/1.7/syntax_and_semantics/inheritance.html#covariance-and-contravariance + ps = [] of Parser(Char, Char) + ps = ps + "abcd".chars.map { |c| Token.new(c) } + p = Sequence.new(ps) + + describe "#parse" do + it "runs each wrapped parser in order, returns each result" do + tokens = TokenStream.from_string("abcd") + result = p.parse(tokens) + + result.value.should eq("abcd".chars) + result.tokens.empty?.should be_true + end + + it "fails if any of the wrapped parsers fail" do + fail_strings = ["", "abed", "bbcd", "abce"] + fail_strings.each do |s| + tokens = TokenStream.from_string(s) + expect_raises(ParserException) { p.parse(tokens) } + end + end + end +end + describe Tokens do p = Tokens.new("test".chars) -- cgit v1.2.1