aboutsummaryrefslogtreecommitdiff
path: root/spec/practical/words_spec.cr
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-20 23:12:40 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-20 23:12:40 +1300
commitcdd4b58aa58320a1a0dfd259b7a1301f3ef60998 (patch)
treef625d93303bb62e492b83b5c510fb35065d6a49a /spec/practical/words_spec.cr
parent3f9f26ddbaa2cc89f7482b61c5d45134fecd71af (diff)
Separate files for practical tests + BF parser/interpreter finished
Diffstat (limited to 'spec/practical/words_spec.cr')
-rw-r--r--spec/practical/words_spec.cr39
1 files changed, 39 insertions, 0 deletions
diff --git a/spec/practical/words_spec.cr b/spec/practical/words_spec.cr
new file mode 100644
index 0000000..5155881
--- /dev/null
+++ b/spec/practical/words_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
+