davidlee-state-fu 0.3.1 → 0.10.0
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.
- data/README.textile +124 -34
- data/Rakefile +36 -30
- data/lib/no_stdout.rb +1 -1
- data/lib/state-fu.rb +9 -8
- data/lib/state_fu/active_support_lite/array/access.rb +12 -5
- data/lib/state_fu/active_support_lite/array/conversions.rb +10 -4
- data/lib/state_fu/active_support_lite/array/extract_options.rb +5 -4
- data/lib/state_fu/active_support_lite/array/grouping.rb +7 -4
- data/lib/state_fu/active_support_lite/array/random_access.rb +4 -3
- data/lib/state_fu/active_support_lite/array/wrapper.rb +4 -3
- data/lib/state_fu/active_support_lite/array.rb +3 -1
- data/lib/state_fu/active_support_lite/blank.rb +18 -9
- data/lib/state_fu/active_support_lite/cattr_reader.rb +4 -1
- data/lib/state_fu/active_support_lite/keys.rb +8 -3
- data/lib/state_fu/active_support_lite/misc.rb +6 -4
- data/lib/state_fu/active_support_lite/module/delegation.rb +130 -0
- data/lib/state_fu/active_support_lite/module.rb +1 -0
- data/lib/state_fu/active_support_lite/object.rb +5 -2
- data/lib/state_fu/active_support_lite/string.rb +6 -1
- data/lib/state_fu/active_support_lite/symbol.rb +2 -1
- data/lib/state_fu/applicable.rb +41 -0
- data/lib/state_fu/{helper.rb → arrays.rb} +45 -121
- data/lib/state_fu/binding.rb +136 -159
- data/lib/state_fu/core_ext.rb +78 -10
- data/lib/state_fu/event.rb +112 -48
- data/lib/state_fu/exceptions.rb +80 -34
- data/lib/state_fu/executioner.rb +149 -0
- data/lib/state_fu/has_options.rb +16 -0
- data/lib/state_fu/hooks.rb +21 -16
- data/lib/state_fu/interface.rb +80 -83
- data/lib/state_fu/lathe.rb +361 -148
- data/lib/state_fu/logger.rb +122 -45
- data/lib/state_fu/machine.rb +60 -32
- data/lib/state_fu/method_factory.rb +180 -72
- data/lib/state_fu/methodical.rb +17 -0
- data/lib/state_fu/persistence/active_record.rb +6 -1
- data/lib/state_fu/persistence/attribute.rb +1 -0
- data/lib/state_fu/persistence/base.rb +8 -6
- data/lib/state_fu/persistence.rb +94 -23
- data/lib/state_fu/sprocket.rb +26 -11
- data/lib/state_fu/state.rb +8 -27
- data/lib/state_fu/transition.rb +207 -98
- data/lib/state_fu/transition_query.rb +214 -0
- data/lib/state_fu.rb +1 -0
- data/lib/tasks/spec_last.rake +46 -0
- data/lib/tasks/state_fu.rake +57 -0
- data/lib/vizier.rb +61 -61
- data/spec/custom_formatter.rb +49 -0
- data/spec/features/binding_and_transition_helper_mixin_spec.rb +2 -2
- data/spec/features/method_missing_only_once_spec.rb +28 -0
- data/spec/features/not_requirements_spec.rb +83 -46
- data/spec/features/plotter_spec.rb +97 -0
- data/spec/features/shared_log_spec.rb +7 -0
- data/spec/features/singleton_machine_spec.rb +39 -0
- data/spec/features/state_and_array_options_accessor_spec.rb +1 -1
- data/spec/features/{transition_boolean_comparison.rb → transition_boolean_comparison_spec.rb} +29 -18
- data/spec/helper.rb +6 -117
- data/spec/integration/active_record_persistence_spec.rb +18 -4
- data/spec/integration/binding_extension_spec.rb +1 -1
- data/spec/integration/class_accessor_spec.rb +49 -59
- data/spec/integration/event_definition_spec.rb +20 -20
- data/spec/integration/example_01_document_spec.rb +13 -8
- data/spec/integration/example_02_string_spec.rb +3 -2
- data/spec/integration/instance_accessor_spec.rb +16 -19
- data/spec/integration/lathe_extension_spec.rb +2 -2
- data/spec/integration/machine_duplication_spec.rb +59 -37
- data/spec/integration/relaxdb_persistence_spec.rb +6 -3
- data/spec/integration/requirement_reflection_spec.rb +66 -57
- data/spec/integration/state_definition_spec.rb +72 -66
- data/spec/integration/transition_spec.rb +169 -173
- data/spec/spec.opts +5 -3
- data/spec/spec_helper.rb +132 -0
- data/spec/state_fu_spec.rb +870 -0
- data/spec/units/binding_spec.rb +33 -22
- data/spec/units/event_spec.rb +3 -22
- data/spec/units/exceptions_spec.rb +7 -0
- data/spec/units/lathe_spec.rb +7 -7
- data/spec/units/machine_spec.rb +67 -75
- data/spec/units/method_factory_spec.rb +55 -48
- data/spec/units/sprocket_spec.rb +5 -7
- data/spec/units/state_spec.rb +33 -24
- metadata +31 -19
- data/lib/state_fu/active_support_lite/inheritable_attributes.rb +0 -1
- data/lib/state_fu/fu_space.rb +0 -51
- data/lib/state_fu/mock_transition.rb +0 -38
- data/spec/BDD/plotter_spec.rb +0 -115
- data/spec/integration/dynamic_requirement_spec.rb +0 -160
- data/spec/integration/ex_machine_for_accounts_spec.rb +0 -79
- data/spec/integration/sanity_spec.rb +0 -31
- data/spec/units/fu_space_spec.rb +0 -95
|
@@ -6,8 +6,17 @@ describe "Transition requirement reflection" do
|
|
|
6
6
|
|
|
7
7
|
before do
|
|
8
8
|
reset!
|
|
9
|
-
make_pristine_class("Klass")
|
|
10
|
-
|
|
9
|
+
make_pristine_class("Klass") do
|
|
10
|
+
def turban?; false end
|
|
11
|
+
def arrest_warrant?; false end
|
|
12
|
+
def papers_in_order?; true end
|
|
13
|
+
def papers_in_order?; true end
|
|
14
|
+
def money_for_bribe?; true end
|
|
15
|
+
def spacesuit?; true end
|
|
16
|
+
def plane_ticket?; true end
|
|
17
|
+
def fuel?; true end
|
|
18
|
+
end
|
|
19
|
+
@machine = Klass.state_fu_machine do
|
|
11
20
|
state :soviet_russia do
|
|
12
21
|
requires( :papers_in_order?, :on => [:entry, :exit] )
|
|
13
22
|
requires( :money_for_bribe?, :on => [:entry, :exit] )
|
|
@@ -38,29 +47,23 @@ describe "Transition requirement reflection" do
|
|
|
38
47
|
|
|
39
48
|
end # machine
|
|
40
49
|
@obj = Klass.new()
|
|
41
|
-
stub( @obj ).papers_in_order? { true }
|
|
42
|
-
stub( @obj ).money_for_bribe? { true }
|
|
43
|
-
stub( @obj ).no_turban? { true }
|
|
44
|
-
stub( @obj ).no_arrest_warrant? { true }
|
|
45
|
-
stub( @obj ).spacesuit? { true }
|
|
46
|
-
stub( @obj ).plane_ticket? { true }
|
|
47
|
-
stub( @obj ).fuel? { true }
|
|
48
50
|
end # before
|
|
49
51
|
|
|
50
52
|
describe "transition.valid? / transition.requirements_met?" do
|
|
51
53
|
it "should be true if all requirements are met (return truth)" do
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
@obj.
|
|
56
|
-
|
|
54
|
+
#@obj.state_fu.next_states[:moon].entry_requirements.should == [:spacesuit?]
|
|
55
|
+
|
|
56
|
+
#@obj.state_fu.fireable?([:fly_spaceship,:moon]).should == true
|
|
57
|
+
@obj.can_fly_spaceship?(:moon).should == true
|
|
58
|
+
#@obj.fly_spaceship(:moon).requirements_met?.should == true
|
|
59
|
+
#@obj.fly_spaceship(:moon).should be_valid
|
|
57
60
|
end
|
|
58
61
|
|
|
59
62
|
it "should be false if not all requirements are met" do
|
|
60
63
|
stub( @obj ).spacesuit?() { false }
|
|
61
64
|
@obj.state_fu.next_states[:moon].entry_requirements.should == [:spacesuit?]
|
|
62
|
-
@obj.state_fu.
|
|
63
|
-
@obj.
|
|
65
|
+
# @obj.state_fu.evaluate(:spacesuit?).should == false
|
|
66
|
+
@obj.can_fly_spaceship?(:moon).should == false
|
|
64
67
|
@obj.fly_spaceship(:moon).requirements_met?.should == false
|
|
65
68
|
@obj.fly_spaceship(:moon).should_not be_valid
|
|
66
69
|
end
|
|
@@ -68,11 +71,11 @@ describe "Transition requirement reflection" do
|
|
|
68
71
|
|
|
69
72
|
describe "flying from russia to america without one's affairs in order while wearing a turban" do
|
|
70
73
|
before do
|
|
71
|
-
mock( @obj ).us_visa?() { false }
|
|
72
|
-
mock( @obj ).no_turban?() { false }
|
|
73
|
-
mock( @obj ).no_arrest_warrant?() { false }
|
|
74
|
-
mock( @obj ).money_for_bribe?() { false }
|
|
75
|
-
mock( @obj ).papers_in_order?() { false }
|
|
74
|
+
mock( @obj ).us_visa?(anything) { false }
|
|
75
|
+
mock( @obj ).no_turban?(anything) { false }
|
|
76
|
+
mock( @obj ).no_arrest_warrant?(anything) { false }
|
|
77
|
+
mock( @obj ).money_for_bribe?(anything) { false }
|
|
78
|
+
mock( @obj ).papers_in_order?(anything) { false }
|
|
76
79
|
end
|
|
77
80
|
|
|
78
81
|
describe "when no messages are supplied for the requirements" do
|
|
@@ -88,14 +91,15 @@ describe "Transition requirement reflection" do
|
|
|
88
91
|
|
|
89
92
|
describe "given transition.unmet_requirement_messages" do
|
|
90
93
|
it "should return a list of nils" do
|
|
91
|
-
@obj.state_fu.catch_plane(:america).unmet_requirement_messages.should ==
|
|
94
|
+
@obj.state_fu.catch_plane(:america).unmet_requirement_messages.should ==
|
|
95
|
+
[:papers_in_order?, :money_for_bribe?, :no_turban?, :us_visa?, :no_arrest_warrant?]
|
|
92
96
|
end
|
|
93
97
|
end # unmet_requirement_messages
|
|
94
98
|
end
|
|
95
99
|
|
|
96
100
|
describe "when a message is supplied for the money_for_bribe? entry requirement" do
|
|
97
101
|
before do
|
|
98
|
-
Klass.
|
|
102
|
+
Klass.state_fu_machine do
|
|
99
103
|
state :soviet_russia do
|
|
100
104
|
requires( :money_for_bribe?, :message => "This guard is thirsty! Do you have anything to declare?" )
|
|
101
105
|
end
|
|
@@ -114,13 +118,15 @@ describe "Transition requirement reflection" do
|
|
|
114
118
|
|
|
115
119
|
describe "given transition.unmet_requirement_messages" do
|
|
116
120
|
it "should contain a list of nils plus the requirement message for money_for_bribe? as a string" do
|
|
117
|
-
@obj.state_fu.catch_plane(:america).unmet_requirement_messages.should ==
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
121
|
+
@obj.state_fu.catch_plane(:america).unmet_requirement_messages.should ==
|
|
122
|
+
[:papers_in_order?,
|
|
123
|
+
"This guard is thirsty! Do you have anything to declare?",
|
|
124
|
+
:no_turban?,
|
|
125
|
+
:us_visa?,
|
|
126
|
+
:no_arrest_warrant?]
|
|
122
127
|
end
|
|
123
128
|
end
|
|
129
|
+
|
|
124
130
|
end
|
|
125
131
|
end # flying with a turban
|
|
126
132
|
|
|
@@ -131,8 +137,8 @@ describe "Transition requirement reflection" do
|
|
|
131
137
|
|
|
132
138
|
describe "when a message is supplied for the requirement" do
|
|
133
139
|
it "should contain a list of the requirement failure messages as strings" do
|
|
134
|
-
mock( @obj ).spacesuit?() { false }
|
|
135
|
-
mock( @obj ).fuel?() { false }
|
|
140
|
+
mock( @obj ).spacesuit?(anything) { false }
|
|
141
|
+
mock( @obj ).fuel?(anything) { false }
|
|
136
142
|
@obj.state_fu.fly_spaceship(:moon).unmet_requirements.should == [:spacesuit?, :fuel?]
|
|
137
143
|
end
|
|
138
144
|
end
|
|
@@ -154,9 +160,10 @@ describe "Transition requirement reflection" do
|
|
|
154
160
|
messages = t.unmet_requirement_messages
|
|
155
161
|
messages.should be_kind_of( Array )
|
|
156
162
|
messages.length.should == 2
|
|
157
|
-
messages.
|
|
158
|
-
messages.
|
|
159
|
-
messages.
|
|
163
|
+
messages.strings.length.should == 1
|
|
164
|
+
messages.symbols.length.should == 1
|
|
165
|
+
messages.strings.first.should == @msg
|
|
166
|
+
messages.symbols.first.should == :fuel?
|
|
160
167
|
end
|
|
161
168
|
end
|
|
162
169
|
|
|
@@ -177,15 +184,16 @@ describe "Transition requirement reflection" do
|
|
|
177
184
|
messages = t.unmet_requirement_messages
|
|
178
185
|
messages.should be_kind_of( Array )
|
|
179
186
|
messages.length.should == 2
|
|
180
|
-
messages.
|
|
181
|
-
messages.
|
|
182
|
-
messages.
|
|
187
|
+
messages.strings.length.should == 1
|
|
188
|
+
messages.strings.first.should be_kind_of( String )
|
|
189
|
+
messages.strings.first.should == "I am a StateFu::Transition and I fail it"
|
|
190
|
+
messages.symbols.first.should == :fuel?
|
|
183
191
|
end
|
|
184
192
|
end # arity 1
|
|
185
193
|
|
|
186
194
|
describe "when the arity of the proc is 0" do
|
|
187
195
|
before do
|
|
188
|
-
@msg = lambda { "
|
|
196
|
+
@msg = lambda { "No #{t.target.name} for you!" }
|
|
189
197
|
@machine.requirement_messages[:spacesuit?] = @msg
|
|
190
198
|
end
|
|
191
199
|
|
|
@@ -195,52 +203,53 @@ describe "Transition requirement reflection" do
|
|
|
195
203
|
messages = t.unmet_requirement_messages
|
|
196
204
|
messages.should be_kind_of( Array )
|
|
197
205
|
messages.length.should == 2
|
|
198
|
-
messages.
|
|
199
|
-
messages.
|
|
200
|
-
messages.
|
|
206
|
+
messages.strings.length.should == 1
|
|
207
|
+
messages.strings.first.should be_kind_of( String )
|
|
208
|
+
messages.strings.first.should == "No moon for you!"
|
|
209
|
+
messages.symbols.first.should == :fuel?
|
|
201
210
|
end
|
|
202
211
|
end # arity 1
|
|
203
212
|
|
|
204
213
|
end # 1 proc msg of 2
|
|
205
214
|
describe "when a symbol message is defined for one of two unmet_requirements" do
|
|
206
215
|
before do
|
|
207
|
-
|
|
208
|
-
stub( @obj ).fuel?() { false }
|
|
216
|
+
|
|
209
217
|
@machine.requirement_messages[:spacesuit?] = :no_spacesuit_msg_method
|
|
210
218
|
Klass.class_eval do
|
|
211
219
|
attr_accessor :arg
|
|
212
|
-
|
|
213
|
-
def
|
|
214
|
-
|
|
215
|
-
|
|
220
|
+
def spacesuit?; false end
|
|
221
|
+
def fuel?; false end
|
|
222
|
+
|
|
223
|
+
def no_spacesuit_msg_method(t)
|
|
216
224
|
"You can't go to the #{t.target.name} without a spacesuit!"
|
|
217
225
|
end
|
|
226
|
+
|
|
218
227
|
end
|
|
219
228
|
end
|
|
220
229
|
|
|
221
230
|
describe "when there is no named proc on the machine matching the symbol" do
|
|
222
231
|
|
|
223
|
-
it "should call the method on @obj given transition.
|
|
224
|
-
@obj.method( :no_spacesuit_msg_method ).arity.should == 1
|
|
232
|
+
it "should call the method on @obj given transition.evaluate() with the method name" do
|
|
225
233
|
t = @obj.state_fu.fly_spaceship(:moon)
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
x.should =~ /You can't go to the moon/
|
|
234
|
+
@obj.arg.should == nil
|
|
235
|
+
t.unmet_requirement_messages.should == ["You can't go to the moon without a spacesuit!", :fuel?]
|
|
229
236
|
end
|
|
230
237
|
|
|
231
238
|
it "should call t.evaluate_named_proc_or_method(:no_spacesuit_msg_method)" do
|
|
232
239
|
t = @obj.state_fu.fly_spaceship(:moon)
|
|
233
240
|
t.unmet_requirements.length.should == 2
|
|
234
|
-
|
|
241
|
+
stub( t ).evaluate( anything) { false }
|
|
242
|
+
|
|
243
|
+
mock( t ).evaluate(:no_spacesuit_msg_method){ ":)" }
|
|
235
244
|
messages = t.unmet_requirement_messages
|
|
236
|
-
messages.should include(:
|
|
245
|
+
messages.should include( ":)" )
|
|
237
246
|
end
|
|
238
247
|
|
|
239
248
|
it "should call the method on @obj with the name of the symbol, passing it a transition" do
|
|
240
249
|
t = @obj.state_fu.fly_spaceship(:moon)
|
|
241
250
|
t.unmet_requirements.length.should == 2
|
|
242
251
|
messages = t.unmet_requirement_messages
|
|
243
|
-
@obj.arg.should ==
|
|
252
|
+
@obj.arg.should == nil
|
|
244
253
|
end
|
|
245
254
|
|
|
246
255
|
it "should return the result of the method execution as the message" do
|
|
@@ -248,9 +257,9 @@ describe "Transition requirement reflection" do
|
|
|
248
257
|
t.unmet_requirements.length.should == 2
|
|
249
258
|
messages = t.unmet_requirement_messages
|
|
250
259
|
messages.length.should == 2
|
|
251
|
-
messages.
|
|
252
|
-
|
|
253
|
-
messages.
|
|
260
|
+
messages.strings.length.should == 1
|
|
261
|
+
#@obj.arg.should == t
|
|
262
|
+
messages.strings[0].should == "You can't go to the moon without a spacesuit!"
|
|
254
263
|
end
|
|
255
264
|
end # no named proc
|
|
256
265
|
end # symbol message
|
|
@@ -14,37 +14,37 @@ describe "Adding states to a Machine" do
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it "should allow me to call machine() { state(:egg) }" do
|
|
17
|
-
lambda {Klass.
|
|
17
|
+
lambda {Klass.state_fu_machine(){ state :egg } }.should_not raise_error()
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
describe "having called machine() { state(:egg) }" do
|
|
21
21
|
|
|
22
22
|
before(:each) do
|
|
23
|
-
Klass.
|
|
23
|
+
Klass.state_fu_machine(){ state :egg }
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
it "should return [:egg] given machine.state_names" do
|
|
27
|
-
Klass.
|
|
28
|
-
Klass.
|
|
27
|
+
Klass.state_fu_machine.should respond_to(:state_names)
|
|
28
|
+
Klass.state_fu_machine.state_names.should == [:egg]
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it "should return [<StateFu::State @name=:egg>] given machine.states" do
|
|
32
|
-
Klass.
|
|
33
|
-
Klass.
|
|
34
|
-
Klass.
|
|
35
|
-
Klass.
|
|
32
|
+
Klass.state_fu_machine.should respond_to(:states)
|
|
33
|
+
Klass.state_fu_machine.states.length.should == 1
|
|
34
|
+
Klass.state_fu_machine.states.first.should be_kind_of( StateFu::State )
|
|
35
|
+
Klass.state_fu_machine.states.first.name.should == :egg
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
it "should return :egg given machine.states.first.name" do
|
|
39
|
-
Klass.
|
|
40
|
-
Klass.
|
|
41
|
-
Klass.
|
|
42
|
-
Klass.
|
|
39
|
+
Klass.state_fu_machine.should respond_to(:states)
|
|
40
|
+
Klass.state_fu_machine.states.length.should == 1
|
|
41
|
+
Klass.state_fu_machine.states.first.should respond_to(:name)
|
|
42
|
+
Klass.state_fu_machine.states.first.name.should == :egg
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
it "should return a <StateFu::State @name=:egg> given machine.states[:egg]" do
|
|
46
|
-
Klass.
|
|
47
|
-
result = Klass.
|
|
46
|
+
Klass.state_fu_machine.should respond_to(:states)
|
|
47
|
+
result = Klass.state_fu_machine.states[:egg]
|
|
48
48
|
result.should_not be_nil
|
|
49
49
|
result.should be_kind_of( StateFu::State )
|
|
50
50
|
result.name.should == :egg
|
|
@@ -52,22 +52,22 @@ describe "Adding states to a Machine" do
|
|
|
52
52
|
|
|
53
53
|
|
|
54
54
|
it "should allow me to call machine(){ state(:chick) }" do
|
|
55
|
-
lambda {Klass.
|
|
55
|
+
lambda {Klass.state_fu_machine(){ state :chick } }.should_not raise_error()
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
describe "having called machine() { state(:chick) }" do
|
|
59
59
|
before do
|
|
60
|
-
Klass.
|
|
60
|
+
Klass.state_fu_machine() { state :chick }
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
it "should return [:egg] given machine.state_names" do
|
|
64
|
-
Klass.
|
|
65
|
-
Klass.
|
|
64
|
+
Klass.state_fu_machine.should respond_to(:state_names)
|
|
65
|
+
Klass.state_fu_machine.state_names.should == [:egg, :chick]
|
|
66
66
|
end
|
|
67
67
|
|
|
68
68
|
it "should return a <StateFu::State @name=:chick> given machine.states[:egg]" do
|
|
69
|
-
Klass.
|
|
70
|
-
result = Klass.
|
|
69
|
+
Klass.state_fu_machine.should respond_to(:states)
|
|
70
|
+
result = Klass.state_fu_machine.states[:chick]
|
|
71
71
|
result.should_not be_nil
|
|
72
72
|
result.should be_kind_of( StateFu::State )
|
|
73
73
|
result.name.should == :chick
|
|
@@ -79,7 +79,7 @@ describe "Adding states to a Machine" do
|
|
|
79
79
|
|
|
80
80
|
it "should yield the state to the block as |s|" do
|
|
81
81
|
state = nil
|
|
82
|
-
Klass.
|
|
82
|
+
Klass.state_fu_machine() do
|
|
83
83
|
state(:bird) do |s|
|
|
84
84
|
state = s
|
|
85
85
|
end
|
|
@@ -94,14 +94,14 @@ describe "Adding states to a Machine" do
|
|
|
94
94
|
|
|
95
95
|
it "should instance_eval the block as a StateFu::Lathe" do
|
|
96
96
|
lathe = nil
|
|
97
|
-
Klass.
|
|
97
|
+
Klass.state_fu_machine() do
|
|
98
98
|
state(:bird) do
|
|
99
99
|
lathe = self
|
|
100
100
|
end
|
|
101
101
|
end
|
|
102
102
|
lathe.should be_kind_of(StateFu::Lathe)
|
|
103
|
-
lathe.
|
|
104
|
-
lathe.
|
|
103
|
+
lathe.state_or_event.should be_kind_of(StateFu::State)
|
|
104
|
+
lathe.state_or_event.name.should == :bird
|
|
105
105
|
end
|
|
106
106
|
|
|
107
107
|
end
|
|
@@ -109,10 +109,10 @@ describe "Adding states to a Machine" do
|
|
|
109
109
|
describe "calling state(:bird) consecutive times" do
|
|
110
110
|
|
|
111
111
|
it "should yield the same state each time" do
|
|
112
|
-
Klass.
|
|
113
|
-
bird_1 = Klass.
|
|
114
|
-
Klass.
|
|
115
|
-
bird_2 = Klass.
|
|
112
|
+
Klass.state_fu_machine() { state :bird }
|
|
113
|
+
bird_1 = Klass.state_fu_machine.states[:bird]
|
|
114
|
+
Klass.state_fu_machine() { state :bird }
|
|
115
|
+
bird_2 = Klass.state_fu_machine.states[:bird]
|
|
116
116
|
bird_1.should == bird_2
|
|
117
117
|
end
|
|
118
118
|
|
|
@@ -122,56 +122,62 @@ describe "Adding states to a Machine" do
|
|
|
122
122
|
describe "calling machine() { states(:egg, :chick, :bird, :poultry => true) }" do
|
|
123
123
|
|
|
124
124
|
it "should create 3 states" do
|
|
125
|
-
Klass.
|
|
126
|
-
Klass.
|
|
127
|
-
Klass.
|
|
128
|
-
Klass.
|
|
129
|
-
Klass.
|
|
130
|
-
Klass.
|
|
125
|
+
Klass.state_fu_machine().should be_empty
|
|
126
|
+
Klass.state_fu_machine() { states(:egg, :chick, :bird, :poultry => true) }
|
|
127
|
+
Klass.state_fu_machine().state_names().should == [:egg, :chick, :bird]
|
|
128
|
+
Klass.state_fu_machine().states.length.should == 3
|
|
129
|
+
Klass.state_fu_machine().states.map(&:name).should == [:egg, :chick, :bird]
|
|
130
|
+
Klass.state_fu_machine().states().each do |s|
|
|
131
131
|
s.options[:poultry].should be_true
|
|
132
132
|
s.should be_kind_of(StateFu::State)
|
|
133
133
|
end
|
|
134
|
+
end
|
|
134
135
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
136
|
+
describe "merging options" do
|
|
137
|
+
before do
|
|
138
|
+
make_pristine_class('Klass')
|
|
139
|
+
end
|
|
140
|
+
it "should merge options when states are mentioned more than once" do
|
|
141
|
+
# reset!
|
|
142
|
+
machine = Klass.state_fu_machine
|
|
143
|
+
machine.states.length.should == 0
|
|
144
|
+
Klass.state_fu_machine() { states(:egg, :chick, :bird, :poultry => true) }
|
|
145
|
+
machine = Klass.state_fu_machine
|
|
146
|
+
machine.states.length.should == 3
|
|
147
|
+
|
|
148
|
+
# make sure they're the same states
|
|
149
|
+
states_1 = machine.states
|
|
150
|
+
Klass.state_fu_machine(){ states( :egg, :chick, :bird, :covering => 'feathers')}
|
|
151
|
+
states_1.should == machine.states
|
|
152
|
+
|
|
153
|
+
# ensure options were merged
|
|
154
|
+
machine.states().each do |s|
|
|
155
|
+
s.options[:poultry].should be_true
|
|
156
|
+
s.options[:covering].should == 'feathers'
|
|
157
|
+
s.should be_kind_of(StateFu::State)
|
|
153
158
|
end
|
|
154
159
|
end
|
|
155
160
|
end
|
|
156
161
|
end
|
|
162
|
+
end
|
|
157
163
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
164
|
+
describe "adding events inside a state block" do
|
|
165
|
+
before do
|
|
166
|
+
@lambda = lambda{ Klass.state_fu_machine(){ state(:egg){ event(:hatch, :to => :chick) }}}
|
|
167
|
+
end
|
|
162
168
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
169
|
+
it "should not throw an error" do
|
|
170
|
+
@lambda.should_not raise_error
|
|
171
|
+
end
|
|
166
172
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
end
|
|
173
|
+
describe "Klass.state_fu_machine(){ state(:egg){ event(:hatch, :to => :chick) }}}" do
|
|
174
|
+
before() do
|
|
175
|
+
Klass.state_fu_machine(){ state(:egg){ event(:hatch, :to => :chick) }}
|
|
176
|
+
end
|
|
177
|
+
it "should add an event :hatch to the machine" do
|
|
173
178
|
end
|
|
174
179
|
end
|
|
175
|
-
|
|
176
180
|
end
|
|
177
181
|
|
|
182
|
+
|
|
183
|
+
|