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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
require 'spec_helper'
require 'timeout'
require 'parslet'
describe Parslet do
def not_parse
raise_error(Parslet::ParseFailed)
end
include Parslet
extend Parslet
def src(str); Parslet::Source.new str; end
let(:context) { Parslet::Atoms::Context.new }
describe "match('[abc]')" do
attr_reader :parslet
before(:each) do
@parslet = match('[abc]')
end
it "should parse {a,b,c}" do
parslet.parse('a')
parslet.parse('b')
parslet.parse('c')
end
it "should not parse d" do
cause = catch_failed_parse {
parslet.parse('d')
}
cause.to_s.should == "Failed to match [abc] at line 1 char 1."
end
it "should print as [abc]" do
parslet.inspect.should == "[abc]"
end
end
describe "match(['[a]').repeat(3)" do
attr_reader :parslet
before(:each) do
@parslet = match('[a]').repeat(3)
end
context "when failing on input 'aa'" do
let!(:cause) {
catch_failed_parse { parslet.parse('aa') }
}
it "should have a relevant cause" do
cause.to_s.should == "Expected at least 3 of [a] at line 1 char 1."
end
it "should have a tree with 2 nodes" do
cause.children.should have(1).elements
end
end
it "should succeed on 'aaa'" do
parslet.parse('aaa')
end
it "should succeed on many 'a'" do
parslet.parse('a'*100)
end
it "should inspect as [a]{3, }" do
parslet.inspect.should == "[a]{3, }"
end
end
describe "str('foo')" do
attr_reader :parslet
before(:each) do
@parslet = str('foo')
end
it "should parse 'foo'" do
parslet.parse('foo')
end
it "should not parse 'bar'" do
cause = catch_failed_parse { parslet.parse('bar') }
cause.to_s.should ==
"Expected \"foo\", but got \"bar\" at line 1 char 1."
end
it "should inspect as 'foo'" do
parslet.inspect.should == "'foo'"
end
end
describe "str('foo').maybe" do
let(:parslet) { str('foo').maybe }
it "should parse a foo" do
parslet.parse('foo')
end
it "should leave pos untouched if there is no foo" do
source = src('bar')
parslet.apply(source, context)
source.pos.charpos.should == 0
end
it "should inspect as 'foo'?" do
parslet.inspect.should == "'foo'?"
end
context "when parsing 'foo'" do
subject { parslet.parse('foo') }
it { should == 'foo' }
end
context "when parsing ''" do
subject { parslet.parse('') }
it { should == '' }
end
end
describe "str('foo') >> str('bar')" do
let(:parslet) { str('foo') >> str('bar') }
context "when it fails on input 'foobaz'" do
let!(:cause) {
catch_failed_parse { parslet.parse('foobaz') }
}
it "should not parse 'foobaz'" do
cause.to_s.should == "Failed to match sequence ('foo' 'bar') at line 1 char 4."
end
it "should have 2 nodes in error tree" do
cause.should have(1).children
end
end
it "should parse 'foobar'" do
parslet.parse('foobar')
end
it "should inspect as ('foo' 'bar')" do
parslet.inspect.should == "'foo' 'bar'"
end
end
describe "str('foo') | str('bar')" do
attr_reader :parslet
before(:each) do
@parslet = str('foo') | str('bar')
end
context "when failing on input 'baz'" do
let!(:cause) {
catch_failed_parse { parslet.parse('baz') }
}
it "should have a sensible cause" do
cause.to_s.should == "Expected one of ['foo', 'bar'] at line 1 char 1."
end
it "should have an error tree with 3 nodes" do
cause.should have(2).children
end
end
it "should accept 'foo'" do
parslet.parse('foo')
end
it "should accept 'bar'" do
parslet.parse('bar')
end
it "should inspect as ('foo' / 'bar')" do
parslet.inspect.should == "'foo' / 'bar'"
end
end
describe "str('foo').present? (positive lookahead)" do
attr_reader :parslet
before(:each) do
@parslet = str('foo').present?
end
it "should inspect as &'foo'" do
parslet.inspect.should == "&'foo'"
end
context "when fed 'foo'" do
it "should parse" do
success, _ = parslet.apply(src('foo'), context)
success.should == true
end
it "should not change input position" do
source = src('foo')
parslet.apply(source, context)
source.pos.charpos.should == 0
end
end
context "when fed 'bar'" do
it "should not parse" do
lambda { parslet.parse('bar') }.should not_parse
end
end
describe "<- #parse" do
it "should return nil" do
parslet.apply(src('foo'), context).should == [true, nil]
end
end
end
describe "str('foo').absent? (negative lookahead)" do
attr_reader :parslet
before(:each) do
@parslet = str('foo').absent?
end
it "should inspect as !'foo'" do
parslet.inspect.should == "!'foo'"
end
context "when fed 'bar'" do
it "should parse" do
parslet.apply(src('bar'), context).should == [true, nil]
end
it "should not change input position" do
source = src('bar')
parslet.apply(source, context)
source.pos.charpos.should == 0
end
end
context "when fed 'foo'" do
it "should not parse" do
lambda { parslet.parse('foo') }.should not_parse
end
end
end
describe "non greedy matcher combined with greedy matcher (possible loop)" do
attr_reader :parslet
before(:each) do
# repeat will always succeed, since it has a minimum of 0. It will not
# modify input position in that case. absent? will, depending on
# implementation, match as much as possible and call its inner element
# again. This leads to an infinite loop. This example tests for the
# absence of that loop.
@parslet = str('foo').repeat.maybe
end
it "should not loop infinitely" do
lambda {
timeout(1) { parslet.parse('bar') }
}.should raise_error(Parslet::ParseFailed)
end
end
describe "any" do
attr_reader :parslet
before(:each) do
@parslet = any
end
it "should match" do
parslet.parse('.')
end
it "should consume one char" do
source = src('foo')
parslet.apply(source, context)
source.pos.charpos.should == 1
end
end
describe "eof behaviour" do
context "when the pattern just doesn't consume the input" do
let (:parslet) { any }
it "should fail the parse" do
cause = catch_failed_parse { parslet.parse('..') }
cause.to_s.should == "Don't know what to do with \".\" at line 1 char 2."
end
end
context "when the pattern doesn't match the input" do
let (:parslet) { (str('a')).repeat(1) }
attr_reader :exception
before(:each) do
begin
parslet.parse('a.')
rescue => @exception
end
end
it "raises Parslet::ParseFailed" do
# ParseFailed here, because the input doesn't match the parser grammar.
exception.should be_kind_of(Parslet::ParseFailed)
end
it "has the correct error message" do
exception.message.should == \
"Extra input after last repetition at line 1 char 2."
end
end
end
describe "<- #as(name)" do
context "str('foo').as(:bar)" do
it "should return :bar => 'foo'" do
str('foo').as(:bar).parse('foo').should == { :bar => 'foo' }
end
end
context "match('[abc]').as(:name)" do
it "should return :name => 'b'" do
match('[abc]').as(:name).parse('b').should == { :name => 'b' }
end
end
context "match('[abc]').repeat.as(:name)" do
it "should return collated result ('abc')" do
match('[abc]').repeat.as(:name).
parse('abc').should == { :name => 'abc' }
end
end
context "(str('a').as(:a) >> str('b').as(:b)).as(:c)" do
it "should return a hash of hashes" do
(str('a').as(:a) >> str('b').as(:b)).as(:c).
parse('ab').should == {
:c => {
:a => 'a',
:b => 'b'
}
}
end
end
context "(str('a').as(:a) >> str('ignore') >> str('b').as(:b))" do
it "should correctly flatten (leaving out 'ignore')" do
(str('a').as(:a) >> str('ignore') >> str('b').as(:b)).
parse('aignoreb').should ==
{
:a => 'a',
:b => 'b'
}
end
end
context "(str('a') >> str('ignore') >> str('b')) (no .as(...))" do
it "should return simply the original string" do
(str('a') >> str('ignore') >> str('b')).
parse('aignoreb').should == 'aignoreb'
end
end
context "str('a').as(:a) >> str('b').as(:a)" do
attr_reader :parslet
before(:each) do
@parslet = str('a').as(:a) >> str('b').as(:a)
end
it "should issue a warning that a key is being overwritten in merge" do
flexmock(parslet).
should_receive(:warn).once
parslet.parse('ab').should == { :a => 'b' }
end
it "should return :a => 'b'" do
flexmock(parslet).
should_receive(:warn)
parslet.parse('ab').should == { :a => 'b' }
end
end
context "str('a').absent?" do
it "should return something in merge, even though it is nil" do
(str('a').absent? >> str('b').as(:b)).
parse('b').should == {:b => 'b'}
end
end
context "str('a').as(:a).repeat" do
it "should return an array of subtrees" do
str('a').as(:a).repeat.
parse('aa').should == [{:a=>'a'}, {:a=>'a'}]
end
end
end
describe "<- #flatten(val)" do
def call(val)
dummy = str('a')
flexmock(dummy, :warn => nil)
dummy.flatten(val)
end
[
# In absence of named subtrees: ----------------------------------------
# Sequence or Repetition
[ [:sequence, 'a', 'b'], 'ab' ],
[ [:repetition, 'a', 'a'], 'aa' ],
# Nested inside another node
[ [:sequence, [:sequence, 'a', 'b']], 'ab' ],
# Combined with lookahead (nil)
[ [:sequence, nil, 'a'], 'a' ],
# Including named subtrees ---------------------------------------------
# Atom: A named subtree
[ {:a=>'a'}, {:a=>'a'} ],
# Composition of subtrees
[ [:sequence, {:a=>'a'},{:b=>'b'}], {:a=>'a',:b=>'b'} ],
# Mixed subtrees :sequence of :repetition yields []
[ [:sequence, [:repetition, {:a => 'a'}], {:a => 'a'} ], [{:a=>'a'}, {:a=>'a'}]],
[ [:sequence, {:a => 'a'},[:repetition, {:a => 'a'}] ], [{:a=>'a'}, {:a=>'a'}]],
[ [:sequence, [:repetition, {:a => 'a'}],[:repetition, {:a => 'a'}] ], [{:a=>'a'}, {:a=>'a'}]],
# Repetition
[ [:repetition, [:repetition, {:a=>'a'}], [:repetition, {:a=>'a'}]],
[{:a => 'a'}, {:a => 'a'}]],
[ [:repetition, {:a=>'a'}, 'a', {:a=>'a'}], [{:a=>'a'}, {:a=>'a'}]],
[ [:repetition, {:a=>'a'}, [:repetition, {:b=>'b'}]], [{:a=>'a'}] ],
# Some random samples --------------------------------------------------
[ [:sequence, {:a => :b, :b => :c}], {:a=>:b, :b=>:c} ],
[ [:sequence, {:a => :b}, 'a', {:c=>:d}], {:a => :b, :c=>:d} ],
[ [:repetition, {:a => :b}, 'a', {:c=>:d}], [{:a => :b}, {:c=>:d}] ],
[ [:sequence, {:a => :b}, {:a=>:d}], {:a => :d} ],
[ [:sequence, {:a=>:b}, [:sequence, [:sequence, "\n", nil]]], {:a=>:b} ],
[ [:sequence, nil, " "], ' ' ],
].each do |input, output|
it "should transform #{input.inspect} to #{output.inspect}" do
call(input).should == output
end
end
end
describe "combinations thereof (regression)" do
success=[
[(str('a').repeat >> str('b').repeat), 'aaabbb']
].each do |(parslet, input)|
describe "#{parslet.inspect} applied to #{input.inspect}" do
it "should parse successfully" do
parslet.parse(input)
end
end
end
inspection=[
[str('a'), "'a'" ],
[(str('a') | str('b')).maybe, "('a' / 'b')?" ],
[(str('a') >> str('b')).maybe, "('a' 'b')?" ],
[str('a').maybe.maybe, "'a'??" ],
[(str('a')>>str('b')).maybe.maybe, "('a' 'b')??" ],
[(str('a') >> (str('b') | str('c'))), "'a' ('b' / 'c')"],
[str('a') >> str('b').repeat, "'a' 'b'{0, }" ],
[(str('a')>>str('b')).repeat, "('a' 'b'){0, }" ]
].each do |(parslet, inspect_output)|
context "regression for #{parslet.inspect}" do
it "should inspect correctly as #{inspect_output}" do
parslet.inspect.should == inspect_output
end
end
end
end
end
|