aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-13 13:47:22 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-13 13:47:22 +1300
commitbbae80606ce3c355f2ccb6cda95410e4b39fe551 (patch)
tree9492630c7c1438737364465ef7c079580ca98db8 /spec
parente62e42306a1012776b6b9824116dad601be8f557 (diff)
Introduce practical test section
Diffstat (limited to 'spec')
-rw-r--r--spec/parcom_spec.cr23
1 files changed, 23 insertions, 0 deletions
diff --git a/spec/parcom_spec.cr b/spec/parcom_spec.cr
index dde6f21..feea102 100644
--- a/spec/parcom_spec.cr
+++ b/spec/parcom_spec.cr
@@ -721,3 +721,26 @@ describe SepBy do
end
end
+describe "Practical use" do
+ describe "Use case: text surrounded by whitespace" do
+ space = Satisfy(Char).new { |c| c.whitespace? }
+ non_space = Satisfy(Char).new { |c| !c.whitespace? }
+
+ # TODO: Figure out why mapping on this parser breaks
+ # initialization of `body`.
+ word_chars = Some.new(non_space)
+ ws = Some.new(space)
+
+ bookend = Optional.new(ws)
+ body = SepBy.new(word_chars, ws)
+ tokenizer = (bookend >> body << bookend).map do |arrs|
+ arrs.map { |chars| chars.join }
+ end
+
+ tokens = Tokens.from_string("test number 1 ")
+ result = tokenizer.parse(tokens)
+ result.value.should eq(["test", "number", "1"])
+ result.tokens.empty?.should be_true
+ end
+end
+