james 0.1.1-universal-darwin-10 → 0.2.0-universal-darwin-10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,156 +0,0 @@
1
- # encoding: utf-8
2
- #
3
- require File.expand_path '../../../../lib/james/state_api', __FILE__
4
- require File.expand_path '../../../../lib/james/state_internals', __FILE__
5
-
6
- describe James::State do
7
-
8
- before(:all) do
9
- @context = stub :context,
10
- :inspect => 'some_context'
11
- class << @context
12
-
13
- def state_for name
14
- {
15
- :next_state1 => :some_state_object1,
16
- :next_state2 => :some_state_object2,
17
- :next_state3 => :some_state_object3,
18
- }[name]
19
- end
20
-
21
- end
22
- end
23
-
24
- context 'with no transitions or into or exit' do
25
- let(:state) do
26
- described_class.new :some_name, @context do
27
- # Nothing to see here.
28
- end
29
- end
30
- describe 'phrases' do
31
- it { state.phrases.should == [] }
32
- end
33
- describe 'to_s' do
34
- it { state.to_s.should == 'James::State(some_name, some_context, {})' }
35
- end
36
- describe 'next_for' do
37
- it { state.next_for('non-existent').should == nil }
38
- end
39
- describe 'expand' do
40
- it do
41
- state.expand([:a, :b] => 1).should == { :a => 1, :b => 1 }
42
- end
43
- it do
44
- state.expand(:a => 1).should == { :a => 1 }
45
- end
46
- end
47
- describe '__into__' do
48
- it 'is called' do
49
- state.__into__.should == nil
50
- end
51
- end
52
- describe '__exit__' do
53
- it 'is conditionally called' do
54
- state.__exit__.should == nil
55
- end
56
- end
57
- end
58
-
59
- context 'of the context' do
60
- let(:state) do
61
- described_class.new :some_name, @context do
62
- hear 'transition one' => :next_state1
63
- into { self }
64
- exit { self }
65
- end
66
- end
67
- describe '__into__' do
68
- it 'is called' do
69
- state.__into__.should == @context
70
- end
71
- end
72
- describe '__exit__' do
73
- it 'is conditionally called' do
74
- state.__exit__.should == @context
75
- end
76
- end
77
- end
78
-
79
- context 'with a returning transition' do
80
- let(:state) do
81
- described_class.new :some_name, @context do
82
- hear 'transition one' => lambda { "I do this and return to :some_name" }
83
- end
84
- end
85
- describe 'phrases' do
86
- it { state.phrases.should == ['transition one'] }
87
- end
88
- end
89
-
90
- context 'with 1 transition and into and exit' do
91
- let(:state) do
92
- described_class.new :some_name, @context do
93
- hear 'transition one' => :next_state1
94
- into { "hi there" }
95
- exit { "good bye" }
96
- end
97
- end
98
- describe 'phrases' do
99
- it { state.phrases.should == ['transition one'] }
100
- end
101
- describe 'to_s' do
102
- it { state.to_s.should == 'James::State(some_name, some_context, {"transition one"=>:next_state1})' }
103
- end
104
- describe 'next_for' do
105
- it { state.next_for('transition one').should == :some_state_object1 }
106
- it { state.next_for('non-existent').should == nil }
107
- end
108
- describe '__into__' do
109
- it 'is called' do
110
- state.__into__.should == 'hi there'
111
- end
112
- end
113
- describe '__exit__' do
114
- it 'is conditionally called' do
115
- state.__exit__.should == 'good bye'
116
- end
117
- end
118
- end
119
-
120
- context 'with multiple transition and separate hears' do
121
- let(:state) do
122
- described_class.new :some_name, @context do
123
- hear 'transition one' => :next_state1,
124
- 'transition two' => :next_state2
125
- hear 'transition three' => :next_state3
126
- end
127
- end
128
- describe 'phrases' do
129
- it { state.phrases.should == ['transition one', 'transition two', 'transition three'] }
130
- end
131
- describe 'to_s' do
132
- it { state.to_s.should == 'James::State(some_name, some_context, {"transition one"=>:next_state1, "transition two"=>:next_state2, "transition three"=>:next_state3})' }
133
- end
134
- it { state.next_for('transition two').should == :some_state_object2 }
135
- it { state.next_for('transition three').should == :some_state_object3 }
136
- it { state.next_for('non-existent').should == nil }
137
- end
138
-
139
- context 'with self-transitions' do
140
- some_proc = ->(){ "Going back to where I came from" }
141
- let(:state) do
142
- described_class.new :some_name, @context do
143
- hear 'transition one' => some_proc
144
- end
145
- end
146
- describe 'phrases' do
147
- it { state.phrases.should == ['transition one'] }
148
- end
149
- describe 'to_s' do
150
- it { state.to_s.should == "James::State(some_name, some_context, {\"transition one\"=>#{some_proc}})" }
151
- end
152
- it { state.next_for('transition one').should == some_proc }
153
- it { state.next_for('non-existent').should == nil }
154
- end
155
-
156
- end
@@ -1,101 +0,0 @@
1
- # encoding: utf-8
2
- #
3
- require File.expand_path '../../../../lib/james/visitor', __FILE__
4
-
5
- describe James::Visitor do
6
-
7
- let(:initial) { stub :state }
8
- let(:timer) { stub :timer }
9
- let(:visitor) { described_class.new initial, timer }
10
-
11
- describe 'reset' do
12
- it 'works' do
13
- expect { visitor.reset }.to_not raise_error
14
- end
15
- # it 'calls methods in order' do
16
- # timer.should_receive(:stop).once.with
17
- # visitor.should_receive(:current=).once.with initial
18
- #
19
- # visitor.reset
20
- # end
21
- it 'survives a functional test' do
22
- next_state = stub :next_state
23
- initial.stub! :next_for => next_state
24
-
25
- visitor.current.should == initial
26
- visitor.transition :some_phrase
27
- visitor.current.should == next_state
28
-
29
- visitor.reset
30
-
31
- visitor.current.should == initial
32
- end
33
- end
34
-
35
- describe 'current' do
36
- it { visitor.current.should == initial }
37
- end
38
-
39
- describe 'enter' do
40
- it 'calls enter on the state' do
41
- initial.should_receive(:__into__).once
42
-
43
- visitor.enter
44
- end
45
- it 'returns the result' do
46
- initial.stub! :__into__ => 'some text'
47
-
48
- visitor.enter.should == 'some text'
49
- end
50
- it 'yields the result' do
51
- initial.stub! :__into__ => 'some text'
52
-
53
- visitor.enter do |text|
54
- text.should == 'some text'
55
- end
56
- end
57
- end
58
-
59
- describe 'exit' do
60
- it 'calls enter on the state' do
61
- initial.should_receive(:__exit__).once.with
62
-
63
- visitor.exit
64
- end
65
- it 'returns the result' do
66
- initial.stub! :__exit__ => 'some text'
67
-
68
- visitor.exit.should == 'some text'
69
- end
70
- it 'yields the result' do
71
- initial.stub! :__exit__ => 'some text'
72
-
73
- visitor.exit do |text|
74
- text.should == 'some text'
75
- end
76
- end
77
- end
78
-
79
- describe 'transition' do
80
- it 'sets the current state' do
81
- initial.stub! :next_for => :some_state
82
-
83
- visitor.transition 'some phrase'
84
-
85
- visitor.current.should == :some_state
86
- end
87
- end
88
-
89
- describe 'hear' do
90
- it 'calls methods in order' do
91
- visitor.should_receive(:hears?).once.ordered.with(:some_phrase).and_return true
92
- # timer.should_receive(:restart).once.ordered.with
93
- visitor.should_receive(:exit).once.ordered.with
94
- visitor.should_receive(:transition).once.ordered.with :some_phrase
95
- # visitor.should_receive(:check).once.ordered.with
96
- visitor.should_receive(:enter).once.ordered.with
97
-
98
- visitor.hear :some_phrase
99
- end
100
- end
101
- end
@@ -1,64 +0,0 @@
1
- # encoding: utf-8
2
- #
3
- require File.expand_path '../../../../lib/james/visitors', __FILE__
4
-
5
- describe James::Visitors do
6
-
7
- let(:first) { stub :first }
8
- let(:second) { stub :second }
9
- let(:visitors) { described_class.new first, second }
10
-
11
- describe 'hear' do
12
- context 'the first has it' do
13
- before(:each) do
14
- first.stub! :hear => :something
15
- second.stub! :hear => nil
16
- end
17
- it 'works' do
18
- second.stub! :reset
19
-
20
- visitors.hear 'some phrase'
21
- end
22
- it 'calls the second never' do
23
- first.should_receive(:hear).once.and_return true
24
- second.should_receive(:hear).never
25
- second.should_receive(:reset).once
26
-
27
- visitors.hear 'some phrase'
28
- end
29
- end
30
- context 'the second has it' do
31
- before(:each) do
32
- first.stub! :hear => nil
33
- second.stub! :hear => :something
34
- end
35
- it 'works' do
36
- visitors.hear 'some phrase'
37
- end
38
- it 'calls the first hear first' do
39
- first.should_receive(:hear).once.ordered.with 'some phrase'
40
- second.should_receive(:hear).once.ordered.with 'some phrase'
41
-
42
- visitors.hear 'some phrase'
43
- end
44
- end
45
- end
46
-
47
- describe 'expects' do
48
- context 'chainable' do
49
- before(:each) do
50
- first.stub! :expects => [:a, :b], :chainable? => true
51
- second.stub! :expects => [:c, :d, :e], :chainable? => true
52
- end
53
- it { visitors.expects.should == [:c, :d, :e, :a, :b] }
54
- end
55
- context 'not chainable' do
56
- before(:each) do
57
- first.stub! :expects => [:a, :b], :chainable? => false
58
- second.stub! :expects => [:c, :d, :e], :chainable? => false
59
- end
60
- it { visitors.expects.should == [:a, :b] }
61
- end
62
- end
63
-
64
- end