From a03563ad66150d27f4ee4080cb46762fd63dd341 Mon Sep 17 00:00:00 2001 From: Gears JS Date: Mon, 13 Feb 2023 11:52:57 +0000 Subject: [PATCH] chore: Add tests for coroutine --- spec/parsby/combinators_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/parsby/combinators_spec.rb b/spec/parsby/combinators_spec.rb index 3418051..a751d87 100644 --- a/spec/parsby/combinators_spec.rb +++ b/spec/parsby/combinators_spec.rb @@ -487,4 +487,22 @@ def self.parenthesis expect { eof.parse("x") }.to raise_error Parsby::ExpectationFailed end end + + describe "#coroutine" do + it "parses a group defined by a block" do + test_parser = coroutine do |parse| + a_or_b = parse.call choice(lit("a"),lit("b")) + + if a_or_b == "a" + parse.call lit("c") + else + parse.call lit("d") + end + end + + expect(test_parser.parse("ac")).to eq "c" + expect(test_parser.parse("bd")).to eq "d" + expect { test_parser.parse("ad") }.to raise_error Parsby::ExpectationFailed + end + end end