/
predicate_spec.rb
52 lines (46 loc) · 1.54 KB
/
predicate_spec.rb
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
require 'spec_helper'
describe Parslet::Atoms::Predicate do
include Parslet
context "str('foo').pred { ... }" do
before(:each) { flexmock(self).should_receive(:pred => true).by_default }
let(:predicate) { described_class.new(str('foo')) { |m| pred(m) } }
describe "#to_s" do
subject { predicate.to_s }
it { should == "'foo' &{ .. }" }
end
describe "#error_tree" do
before(:each) { predicate.parse('bar') rescue nil }
it "should return a valid error tree" do
predicate.error_tree
end
end
context "when the predicate returns false" do
before(:each) { flexmock(self).should_receive(:pred => false) }
it "should not match 'foo'" do
predicate.should_not parse('foo')
end
end
context "when the predicate returns true" do
before(:each) { flexmock(self).should_receive(:pred => true) }
it "should not match 'foo'" do
predicate.should parse('foo')
end
end
end
context "str('a').as(:b).repeat.pred { ... }" do
# before(:each) { flexmock(self).should_receive(:pred => true).by_default }
let(:parslet) { str('a').as(:b).repeat }
let(:predicate) { described_class.new(parslet) { |m| pred(m) } }
context "when fed 'aa', the block" do
def pred(m)
p m
end
it "should receive :b => 'aa' as argument" do
flexmock(self).
should_receive(:pred).with([{:b=>"a"}, {:b=>"a"}]).
and_return(true).once
predicate.parse('aa')
end
end
end
end