From f8d0926dc6906faa7ed8b3ba06ee9423b1534329 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Sun, 19 Mar 2023 23:36:35 +1300 Subject: Implement tests for practical uses --- spec/practical_spec.cr | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 spec/practical_spec.cr (limited to 'spec') diff --git a/spec/practical_spec.cr b/spec/practical_spec.cr new file mode 100644 index 0000000..9dc8ed5 --- /dev/null +++ b/spec/practical_spec.cr @@ -0,0 +1,39 @@ +require "./spec_helper" + +include Parcom + +describe "Text surrounded by whitespace" do + ws_char = Parser(Char, Char).satisfy { |c| c.whitespace? } + normal_char = Parser(Char, Char).satisfy { |c| !c.whitespace? } + + word = normal_char.some.map { |cs| cs.join } + ws = ws_char.some + + words = ws.optional >> word.sep_by(ws) << ws.optional + + good_strings = { + "test with no trailing whitespace", + " test with whitespace in the front", + "test with whitespace in the back", + " test surrounded by whitespace ", + } + + good_strings.each do |s| + tokens = Tokens.from_string(s) + result = words.parse(tokens) + + result.value.should eq(s.strip.split(/\s+/)) + result.tokens.empty?.should be_true + end + + bad_strings = { + "", + " \t \n ", + } + + bad_strings.each do |s| + tokens = Tokens.from_string(s) + expect_raises(ParserFail) { words.parse(tokens) } + end +end + -- cgit v1.2.1