forked from hukl/erlectricity
/
receiver_spec.rb
103 lines (88 loc) · 2.46 KB
/
receiver_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
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
load File.dirname(__FILE__) + '/test_helper.rb'
def simple_receiver_and_port(*terms, &block)
port = FakePort.new(*terms)
receiver = if block
Erlectricity::Receiver.new(port, &block)
else
Erlectricity::Receiver.new(port) do |f|
f.when Erl.any do
:matched
end
end
end
end
context "When a receiver is passed a message that matches two match blocks it" do
setup do
@port = FakePort.new([:foo, :foo])
@receiver = Erlectricity::Receiver.new(@port) do |f|
f.when([:foo, :foo]) do
:first
end
f.when([:foo, Erl.any]) do
:second
end
end
end
specify "should run the first matching receiver's block" do
@receiver.run.should == :first
end
end
context "A receiver" do
specify "should return the result of the match block when finished" do
simple_receiver_and_port(:foo).run.should == :matched
simple_receiver_and_port(:bar).run.should == :matched
simple_receiver_and_port(:bar, :baz).run.should == :matched
end
specify "should process another message if the matched block returns the results of receive_loop" do
recv = simple_receiver_and_port(:foo, :bar, :baz) do |f|
f.when(:bar) { }
f.when(Erl.any) { f.receive_loop }
end
recv.run
recv.port.terms.should == [:baz]
end
specify "should properly nest" do
@port = FakePort.new(:foo, :bar, :baz)
@receiver = Erlectricity::Receiver.new(@port) do |f|
f.when(:foo) do
f.receive do |g|
g.when(:bar){ :ok }
end
f.receive_loop
end
f.when(:baz) do
:done
end
end
@receiver.run.should == :done
@port.terms.should == []
end
specify "should queue up skipped results and restore them when a match happens" do
@port = FakePort.new(:foo, :baz, :bar)
@receiver = Erlectricity::Receiver.new(@port) do |f|
f.when(:foo) do
f.receive do |g|
g.when(:bar){ :ok }
end
f.receive_loop
end
f.when(:baz) do
:done
end
end
@receiver.run.should == :done
@port.terms.should == []
end
specify "should expose bindings to the matched block" do
@port = FakePort.new(:foo, :bar, :baz)
results = []
@receiver = Erlectricity::Receiver.new(@port) do |f|
f.when(Erl.atom) do |bindinated|
results << bindinated
f.receive_loop
end
end
@receiver.run.should == nil
results.should == [:foo, :bar, :baz]
end
end