blob: 4713694c0d5f746595c0770a96db7cb9e10a6cb0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
require "../spec_helper"
include Parcom
describe "Text surrounded by whitespace", tags: "example" do
ws_char = Parser(Char, Char).satisfy(&.whitespace?)
normal_char = Parser(Char, Char).satisfy { |c| !c.whitespace? }
word = normal_char.some.map(&.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
|