diff options
Diffstat (limited to 'spec/parcom_spec.cr')
| -rw-r--r-- | spec/parcom_spec.cr | 83 |
1 files changed, 75 insertions, 8 deletions
diff --git a/spec/parcom_spec.cr b/spec/parcom_spec.cr index 05fd12d..14e232e 100644 --- a/spec/parcom_spec.cr +++ b/spec/parcom_spec.cr @@ -80,7 +80,7 @@ describe Result do end describe Parser do - describe "Parser.pure" do + describe "self.pure" do v = 'a' p = Parser(Char, Char).pure(v) tokens = Tokens.from_string("____") @@ -95,14 +95,14 @@ describe Parser do end end - describe "Parser.flunk" do + describe "self.flunk" do p = Parser(Char, Char).flunk it "always fails" do expect_raises(ParserFail) { p.parse(Tokens.from_string("arbitrary")) } end end - describe "Parser.any_token" do + describe "self.any_token" do p = Parser(Char, Nil).any_token it "parses the first token in the input stream" do @@ -119,7 +119,7 @@ describe Parser do end end - describe "Parser.eof" do + describe "self.eof" do p = Parser(Char, Nil).eof it "succeeds with nil if the input stream is empty" do @@ -136,7 +136,7 @@ describe Parser do end end - describe "Parser.satisfy" do + describe "self.satisfy" do p = Parser(Char, Char).satisfy { |c| c == 'a' } it "succeeds if the token passes the predicate" do @@ -153,6 +153,23 @@ describe Parser do end end + describe "self.token" do + a = Parser(Char, Char).token('a') + + it "succeeds with the given token if it is at the start" do + tokens = Tokens.from_string("abc") + result = a.parse(tokens) + + result.value.should eq('a') + result.tokens.should eq(tokens[1..]) + end + + it "fails if the given token is not present" do + tokens = Tokens.from_string("bbc") + expect_raises(ParserFail) { a.parse(tokens) } + end + end + describe "#assert" do p = Parser(Char, Char).any_token.assert { |c| c == 'a' } @@ -169,8 +186,58 @@ describe Parser do expect_raises(ParserFail) { p.parse(tokens) } end end -end -#pending Basic do -##end + describe "#map" do + p = Parser(Char, Char).any_token + p_mapped = p.map { |c| c.ord } + tokens = Tokens.from_string("abcd") + + normal_result = p.parse(tokens) + mapped_result = p_mapped.parse(tokens) + + it "parses the same amount from the input" do + mapped_result.tokens.should eq(normal_result.tokens) + end + + it "changes the value of the result" do + mapped_result.value.should eq('a'.ord) + end + end + + describe "#|" do + a = Parser(Char, Char).token('a') + b = Parser(Char, Char).token('b') + p = a | b + + it "succeeds with the first value if both parsers succeed" do + p2 = a | Parser(Char, Char).pure('z') + tokens = Tokens.from_string("abcd") + result = p2.parse(tokens) + + result.value.should eq('a') + result.tokens.should eq(tokens[1..]) + end + + it "succeeds with the first value if only the first parser succeeds" do + tokens = Tokens.from_string("abcd") + result = p.parse(tokens) + + result.value.should eq('a') + result.tokens.should eq(tokens[1..]) + end + + it "succeeds with the second value if only the second parser succeeds" do + tokens = Tokens.from_string("bbcd") + result = p.parse(tokens) + + result.value.should eq('b') + result.tokens.should eq(tokens[1..]) + end + + it "fails if both parsers fail" do + tokens = Tokens.from_string("cbcd") + expect_raises(ParserFail) { p.parse(tokens) } + end + end +end |
