From b61aba7186cb7c616d6c8f9a3b9e6442173cbbf6 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Wed, 8 Mar 2023 20:53:17 +1300 Subject: Implement Optional (sort of) --- spec/parcom_spec.cr | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'spec') diff --git a/spec/parcom_spec.cr b/spec/parcom_spec.cr index d9af3b9..2f3f6ba 100644 --- a/spec/parcom_spec.cr +++ b/spec/parcom_spec.cr @@ -348,7 +348,7 @@ describe Recover do result.value.should eq('t') end - it "succeeds and returns the default value without modifying the input of the wrapped parser fails" do + it "succeeds and returns the default value without modifying the input if the wrapped parser fails" do tokens = TokenStream.from_string("_____") result = p.parse(tokens) @@ -358,7 +358,26 @@ describe Recover do end end -pending Optional do +describe Optional do + p = Optional.new(Token.new('t')) + + describe "#parse" do + it "succeeds and returns the wrapped parser's value if it succeeds" do + tokens = TokenStream.from_string("testing") + result = p.parse(tokens) + + result.tokens.should eq(tokens[1..]) + result.value.should eq('t') + end + + it "succeeds and returns a value of `nil` without modifying the input if the wrapped parser fails" do + tokens = TokenStream.from_string("_____") + result = p.parse(tokens) + + result.tokens.should eq(tokens) + result.value.should be_nil + end + end end pending Tokens do -- cgit v1.2.1