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