diff options
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/parcom_spec.cr | 26 |
1 files changed, 26 insertions, 0 deletions
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) |
