aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-19 23:36:35 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-19 23:36:35 +1300
commitf8d0926dc6906faa7ed8b3ba06ee9423b1534329 (patch)
treeaf5d994d664a6376e715dd1fc7018942ebcd6f26
parentb9ea5e3bf3d534d66002f9c59b09bc04aaafa19b (diff)
Implement tests for practical uses
-rw-r--r--spec/practical_spec.cr39
1 files changed, 39 insertions, 0 deletions
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
+