aboutsummaryrefslogtreecommitdiff
path: root/spec/practical/words_spec.cr
blob: 35a41133d5e02f5f1e8b8713b82c254e203459a6 (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" 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