spree-state_machine 2.0.0.beta1
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.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +12 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +502 -0
- data/Gemfile +3 -0
- data/LICENSE +20 -0
- data/README.md +1246 -0
- data/Rakefile +20 -0
- data/examples/AutoShop_state.png +0 -0
- data/examples/Car_state.png +0 -0
- data/examples/Gemfile +5 -0
- data/examples/Gemfile.lock +14 -0
- data/examples/TrafficLight_state.png +0 -0
- data/examples/Vehicle_state.png +0 -0
- data/examples/auto_shop.rb +13 -0
- data/examples/car.rb +21 -0
- data/examples/doc/AutoShop.html +2856 -0
- data/examples/doc/AutoShop_state.png +0 -0
- data/examples/doc/Car.html +919 -0
- data/examples/doc/Car_state.png +0 -0
- data/examples/doc/TrafficLight.html +2230 -0
- data/examples/doc/TrafficLight_state.png +0 -0
- data/examples/doc/Vehicle.html +7921 -0
- data/examples/doc/Vehicle_state.png +0 -0
- data/examples/doc/_index.html +136 -0
- data/examples/doc/class_list.html +47 -0
- data/examples/doc/css/common.css +1 -0
- data/examples/doc/css/full_list.css +55 -0
- data/examples/doc/css/style.css +322 -0
- data/examples/doc/file_list.html +46 -0
- data/examples/doc/frames.html +13 -0
- data/examples/doc/index.html +136 -0
- data/examples/doc/js/app.js +205 -0
- data/examples/doc/js/full_list.js +173 -0
- data/examples/doc/js/jquery.js +16 -0
- data/examples/doc/method_list.html +734 -0
- data/examples/doc/top-level-namespace.html +105 -0
- data/examples/merb-rest/controller.rb +51 -0
- data/examples/merb-rest/model.rb +28 -0
- data/examples/merb-rest/view_edit.html.erb +24 -0
- data/examples/merb-rest/view_index.html.erb +23 -0
- data/examples/merb-rest/view_new.html.erb +13 -0
- data/examples/merb-rest/view_show.html.erb +17 -0
- data/examples/rails-rest/controller.rb +43 -0
- data/examples/rails-rest/migration.rb +7 -0
- data/examples/rails-rest/model.rb +23 -0
- data/examples/rails-rest/view__form.html.erb +34 -0
- data/examples/rails-rest/view_edit.html.erb +6 -0
- data/examples/rails-rest/view_index.html.erb +25 -0
- data/examples/rails-rest/view_new.html.erb +5 -0
- data/examples/rails-rest/view_show.html.erb +19 -0
- data/examples/traffic_light.rb +9 -0
- data/examples/vehicle.rb +33 -0
- data/lib/state_machine/assertions.rb +36 -0
- data/lib/state_machine/branch.rb +225 -0
- data/lib/state_machine/callback.rb +236 -0
- data/lib/state_machine/core.rb +7 -0
- data/lib/state_machine/core_ext/class/state_machine.rb +5 -0
- data/lib/state_machine/core_ext.rb +2 -0
- data/lib/state_machine/error.rb +13 -0
- data/lib/state_machine/eval_helpers.rb +87 -0
- data/lib/state_machine/event.rb +257 -0
- data/lib/state_machine/event_collection.rb +141 -0
- data/lib/state_machine/extensions.rb +149 -0
- data/lib/state_machine/graph.rb +92 -0
- data/lib/state_machine/helper_module.rb +17 -0
- data/lib/state_machine/initializers/rails.rb +25 -0
- data/lib/state_machine/initializers.rb +4 -0
- data/lib/state_machine/integrations/active_model/locale.rb +11 -0
- data/lib/state_machine/integrations/active_model/observer.rb +33 -0
- data/lib/state_machine/integrations/active_model/observer_update.rb +42 -0
- data/lib/state_machine/integrations/active_model/versions.rb +31 -0
- data/lib/state_machine/integrations/active_model.rb +585 -0
- data/lib/state_machine/integrations/active_record/locale.rb +20 -0
- data/lib/state_machine/integrations/active_record/versions.rb +123 -0
- data/lib/state_machine/integrations/active_record.rb +525 -0
- data/lib/state_machine/integrations/base.rb +100 -0
- data/lib/state_machine/integrations.rb +121 -0
- data/lib/state_machine/machine.rb +2287 -0
- data/lib/state_machine/machine_collection.rb +74 -0
- data/lib/state_machine/macro_methods.rb +522 -0
- data/lib/state_machine/matcher.rb +123 -0
- data/lib/state_machine/matcher_helpers.rb +54 -0
- data/lib/state_machine/node_collection.rb +222 -0
- data/lib/state_machine/path.rb +120 -0
- data/lib/state_machine/path_collection.rb +90 -0
- data/lib/state_machine/state.rb +297 -0
- data/lib/state_machine/state_collection.rb +112 -0
- data/lib/state_machine/state_context.rb +138 -0
- data/lib/state_machine/transition.rb +470 -0
- data/lib/state_machine/transition_collection.rb +245 -0
- data/lib/state_machine/version.rb +3 -0
- data/lib/state_machine/yard/handlers/base.rb +32 -0
- data/lib/state_machine/yard/handlers/event.rb +25 -0
- data/lib/state_machine/yard/handlers/machine.rb +344 -0
- data/lib/state_machine/yard/handlers/state.rb +25 -0
- data/lib/state_machine/yard/handlers/transition.rb +47 -0
- data/lib/state_machine/yard/handlers.rb +12 -0
- data/lib/state_machine/yard/templates/default/class/html/setup.rb +30 -0
- data/lib/state_machine/yard/templates/default/class/html/state_machines.erb +12 -0
- data/lib/state_machine/yard/templates.rb +3 -0
- data/lib/state_machine/yard.rb +8 -0
- data/lib/state_machine.rb +8 -0
- data/lib/yard-state_machine.rb +2 -0
- data/state_machine.gemspec +22 -0
- data/test/files/en.yml +17 -0
- data/test/files/switch.rb +15 -0
- data/test/functional/state_machine_test.rb +1066 -0
- data/test/test_helper.rb +7 -0
- data/test/unit/assertions_test.rb +40 -0
- data/test/unit/branch_test.rb +969 -0
- data/test/unit/callback_test.rb +704 -0
- data/test/unit/error_test.rb +43 -0
- data/test/unit/eval_helpers_test.rb +270 -0
- data/test/unit/event_collection_test.rb +398 -0
- data/test/unit/event_test.rb +1196 -0
- data/test/unit/graph_test.rb +98 -0
- data/test/unit/helper_module_test.rb +17 -0
- data/test/unit/integrations/active_model_test.rb +1245 -0
- data/test/unit/integrations/active_record_test.rb +2551 -0
- data/test/unit/integrations/base_test.rb +104 -0
- data/test/unit/integrations_test.rb +71 -0
- data/test/unit/invalid_event_test.rb +20 -0
- data/test/unit/invalid_parallel_transition_test.rb +18 -0
- data/test/unit/invalid_transition_test.rb +115 -0
- data/test/unit/machine_collection_test.rb +603 -0
- data/test/unit/machine_test.rb +3395 -0
- data/test/unit/matcher_helpers_test.rb +37 -0
- data/test/unit/matcher_test.rb +155 -0
- data/test/unit/node_collection_test.rb +362 -0
- data/test/unit/path_collection_test.rb +266 -0
- data/test/unit/path_test.rb +485 -0
- data/test/unit/state_collection_test.rb +352 -0
- data/test/unit/state_context_test.rb +441 -0
- data/test/unit/state_machine_test.rb +31 -0
- data/test/unit/state_test.rb +1101 -0
- data/test/unit/transition_collection_test.rb +2168 -0
- data/test/unit/transition_test.rb +1558 -0
- metadata +264 -0
@@ -0,0 +1,485 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
class PathByDefaultTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@klass = Class.new
|
6
|
+
@machine = StateMachine::Machine.new(@klass)
|
7
|
+
@object = @klass.new
|
8
|
+
|
9
|
+
@path = StateMachine::Path.new(@object, @machine)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_should_have_an_object
|
13
|
+
assert_equal @object, @path.object
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_have_a_machine
|
17
|
+
assert_equal @machine, @path.machine
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_not_have_walked_anywhere
|
21
|
+
assert_equal [], @path
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_should_not_have_a_from_name
|
25
|
+
assert_nil @path.from_name
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_have_no_from_states
|
29
|
+
assert_equal [], @path.from_states
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_not_have_a_to_name
|
33
|
+
assert_nil @path.to_name
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_should_have_no_to_states
|
37
|
+
assert_equal [], @path.to_states
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_should_have_no_events
|
41
|
+
assert_equal [], @path.events
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_should_not_be_able_to_walk_anywhere
|
45
|
+
walked = false
|
46
|
+
@path.walk { walked = true }
|
47
|
+
assert_equal false, walked
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_should_not_be_complete
|
51
|
+
assert_equal false, @path.complete?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class PathTest < Test::Unit::TestCase
|
56
|
+
def setup
|
57
|
+
@klass = Class.new
|
58
|
+
@machine = StateMachine::Machine.new(@klass)
|
59
|
+
@object = @klass.new
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_should_raise_exception_if_invalid_option_specified
|
63
|
+
exception = assert_raise(ArgumentError) {StateMachine::Path.new(@object, @machine, :invalid => true)}
|
64
|
+
assert_equal 'Invalid key(s): invalid', exception.message
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class PathWithoutTransitionsTest < Test::Unit::TestCase
|
69
|
+
def setup
|
70
|
+
@klass = Class.new
|
71
|
+
@machine = StateMachine::Machine.new(@klass)
|
72
|
+
@machine.state :parked, :idling
|
73
|
+
@machine.event :ignite
|
74
|
+
|
75
|
+
@object = @klass.new
|
76
|
+
|
77
|
+
@path = StateMachine::Path.new(@object, @machine)
|
78
|
+
@path.concat([
|
79
|
+
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
|
80
|
+
])
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_should_not_be_able_to_walk_anywhere
|
84
|
+
walked = false
|
85
|
+
@path.walk { walked = true }
|
86
|
+
assert_equal false, walked
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class PathWithTransitionsTest < Test::Unit::TestCase
|
91
|
+
def setup
|
92
|
+
@klass = Class.new
|
93
|
+
@machine = StateMachine::Machine.new(@klass)
|
94
|
+
@machine.state :parked, :idling, :first_gear
|
95
|
+
@machine.event :ignite, :shift_up
|
96
|
+
|
97
|
+
@object = @klass.new
|
98
|
+
@object.state = 'parked'
|
99
|
+
|
100
|
+
@path = StateMachine::Path.new(@object, @machine)
|
101
|
+
@path.concat([
|
102
|
+
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
|
103
|
+
@shift_up_transition = StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)
|
104
|
+
])
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_should_enumerate_transitions
|
108
|
+
assert_equal [@ignite_transition, @shift_up_transition], @path
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_should_have_a_from_name
|
112
|
+
assert_equal :parked, @path.from_name
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_should_have_from_states
|
116
|
+
assert_equal [:parked, :idling], @path.from_states
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_should_have_a_to_name
|
120
|
+
assert_equal :first_gear, @path.to_name
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_should_have_to_states
|
124
|
+
assert_equal [:idling, :first_gear], @path.to_states
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_should_have_events
|
128
|
+
assert_equal [:ignite, :shift_up], @path.events
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_should_not_be_able_to_walk_anywhere
|
132
|
+
walked = false
|
133
|
+
@path.walk { walked = true }
|
134
|
+
assert_equal false, walked
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_should_be_complete
|
138
|
+
assert_equal true, @path.complete?
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
class PathWithDuplicatesTest < Test::Unit::TestCase
|
143
|
+
def setup
|
144
|
+
@klass = Class.new
|
145
|
+
@machine = StateMachine::Machine.new(@klass)
|
146
|
+
@machine.state :parked, :idling
|
147
|
+
@machine.event :park, :ignite
|
148
|
+
|
149
|
+
@object = @klass.new
|
150
|
+
@object.state = 'parked'
|
151
|
+
|
152
|
+
@path = StateMachine::Path.new(@object, @machine)
|
153
|
+
@path.concat([
|
154
|
+
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
|
155
|
+
@park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked),
|
156
|
+
@ignite_again_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
|
157
|
+
])
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_should_not_include_duplicates_in_from_states
|
161
|
+
assert_equal [:parked, :idling], @path.from_states
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_should_not_include_duplicates_in_to_states
|
165
|
+
assert_equal [:idling, :parked], @path.to_states
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_should_not_include_duplicates_in_events
|
169
|
+
assert_equal [:ignite, :park], @path.events
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
class PathWithAvailableTransitionsTest < Test::Unit::TestCase
|
174
|
+
def setup
|
175
|
+
@klass = Class.new
|
176
|
+
@machine = StateMachine::Machine.new(@klass)
|
177
|
+
@machine.state :parked, :idling, :first_gear
|
178
|
+
@machine.event :ignite
|
179
|
+
@machine.event :shift_up do
|
180
|
+
transition :idling => :first_gear
|
181
|
+
end
|
182
|
+
@machine.event :park do
|
183
|
+
transition :idling => :parked
|
184
|
+
end
|
185
|
+
|
186
|
+
@object = @klass.new
|
187
|
+
@object.state = 'parked'
|
188
|
+
|
189
|
+
@path = StateMachine::Path.new(@object, @machine)
|
190
|
+
@path.concat([
|
191
|
+
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
|
192
|
+
])
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_should_not_be_complete
|
196
|
+
assert !@path.complete?
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_should_walk_each_available_transition
|
200
|
+
paths = []
|
201
|
+
@path.walk {|path| paths << path}
|
202
|
+
|
203
|
+
assert_equal [
|
204
|
+
[@ignite_transition, StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)],
|
205
|
+
[@ignite_transition, StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)]
|
206
|
+
], paths
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_should_yield_path_instances_when_walking
|
210
|
+
@path.walk do |path|
|
211
|
+
assert_instance_of StateMachine::Path, path
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_should_not_modify_current_path_after_walking
|
216
|
+
@path.walk {}
|
217
|
+
assert_equal [@ignite_transition], @path
|
218
|
+
end
|
219
|
+
|
220
|
+
def test_should_not_modify_object_after_walking
|
221
|
+
@path.walk {}
|
222
|
+
assert_equal 'parked', @object.state
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
class PathWithGuardedTransitionsTest < Test::Unit::TestCase
|
227
|
+
def setup
|
228
|
+
@klass = Class.new
|
229
|
+
@machine = StateMachine::Machine.new(@klass)
|
230
|
+
@machine.state :parked, :idling
|
231
|
+
@machine.event :ignite
|
232
|
+
@machine.event :shift_up do
|
233
|
+
transition :idling => :first_gear, :if => lambda {false}
|
234
|
+
end
|
235
|
+
|
236
|
+
@object = @klass.new
|
237
|
+
@object.state = 'parked'
|
238
|
+
end
|
239
|
+
|
240
|
+
def test_should_not_walk_transitions_if_guard_enabled
|
241
|
+
path = StateMachine::Path.new(@object, @machine)
|
242
|
+
path.concat([
|
243
|
+
StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
|
244
|
+
])
|
245
|
+
|
246
|
+
paths = []
|
247
|
+
path.walk {|next_path| paths << next_path}
|
248
|
+
|
249
|
+
assert_equal [], paths
|
250
|
+
end
|
251
|
+
|
252
|
+
def test_should_not_walk_transitions_if_guard_disabled
|
253
|
+
path = StateMachine::Path.new(@object, @machine, :guard => false)
|
254
|
+
path.concat([
|
255
|
+
ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
|
256
|
+
])
|
257
|
+
|
258
|
+
paths = []
|
259
|
+
path.walk {|next_path| paths << next_path}
|
260
|
+
|
261
|
+
assert_equal [
|
262
|
+
[ignite_transition, StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)]
|
263
|
+
], paths
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
class PathWithEncounteredTransitionsTest < Test::Unit::TestCase
|
268
|
+
def setup
|
269
|
+
@klass = Class.new
|
270
|
+
@machine = StateMachine::Machine.new(@klass)
|
271
|
+
@machine.state :parked, :idling, :first_gear
|
272
|
+
@machine.event :ignite do
|
273
|
+
transition :parked => :idling
|
274
|
+
end
|
275
|
+
@machine.event :park do
|
276
|
+
transition :idling => :parked
|
277
|
+
end
|
278
|
+
|
279
|
+
@object = @klass.new
|
280
|
+
@object.state = 'parked'
|
281
|
+
|
282
|
+
@path = StateMachine::Path.new(@object, @machine)
|
283
|
+
@path.concat([
|
284
|
+
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
|
285
|
+
@park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)
|
286
|
+
])
|
287
|
+
end
|
288
|
+
|
289
|
+
def test_should_be_complete
|
290
|
+
assert_equal true, @path.complete?
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_should_not_be_able_to_walk
|
294
|
+
walked = false
|
295
|
+
@path.walk { walked = true }
|
296
|
+
assert_equal false, walked
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
class PathWithUnreachedTargetTest < Test::Unit::TestCase
|
301
|
+
def setup
|
302
|
+
@klass = Class.new
|
303
|
+
@machine = StateMachine::Machine.new(@klass)
|
304
|
+
@machine.state :parked, :idling
|
305
|
+
@machine.event :ignite do
|
306
|
+
transition :parked => :idling
|
307
|
+
end
|
308
|
+
|
309
|
+
@object = @klass.new
|
310
|
+
@object.state = 'parked'
|
311
|
+
|
312
|
+
@path = StateMachine::Path.new(@object, @machine, :target => :parked)
|
313
|
+
@path.concat([
|
314
|
+
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
|
315
|
+
])
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_should_not_be_complete
|
319
|
+
assert_equal false, @path.complete?
|
320
|
+
end
|
321
|
+
|
322
|
+
def test_should_not_be_able_to_walk
|
323
|
+
walked = false
|
324
|
+
@path.walk { walked = true }
|
325
|
+
assert_equal false, walked
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
class PathWithReachedTargetTest < Test::Unit::TestCase
|
330
|
+
def setup
|
331
|
+
@klass = Class.new
|
332
|
+
@machine = StateMachine::Machine.new(@klass)
|
333
|
+
@machine.state :parked, :idling
|
334
|
+
@machine.event :ignite do
|
335
|
+
transition :parked => :idling
|
336
|
+
end
|
337
|
+
@machine.event :park do
|
338
|
+
transition :idling => :parked
|
339
|
+
end
|
340
|
+
|
341
|
+
@object = @klass.new
|
342
|
+
@object.state = 'parked'
|
343
|
+
|
344
|
+
@path = StateMachine::Path.new(@object, @machine, :target => :parked)
|
345
|
+
@path.concat([
|
346
|
+
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
|
347
|
+
@park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)
|
348
|
+
])
|
349
|
+
end
|
350
|
+
|
351
|
+
def test_should_be_complete
|
352
|
+
assert_equal true, @path.complete?
|
353
|
+
end
|
354
|
+
|
355
|
+
def test_should_not_be_able_to_walk
|
356
|
+
walked = false
|
357
|
+
@path.walk { walked = true }
|
358
|
+
assert_equal false, walked
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
class PathWithAvailableTransitionsAfterReachingTargetTest < Test::Unit::TestCase
|
363
|
+
def setup
|
364
|
+
@klass = Class.new
|
365
|
+
@machine = StateMachine::Machine.new(@klass)
|
366
|
+
@machine.state :parked, :idling
|
367
|
+
@machine.event :ignite do
|
368
|
+
transition :parked => :idling
|
369
|
+
end
|
370
|
+
@machine.event :shift_up do
|
371
|
+
transition :parked => :first_gear
|
372
|
+
end
|
373
|
+
@machine.event :park do
|
374
|
+
transition [:idling, :first_gear] => :parked
|
375
|
+
end
|
376
|
+
|
377
|
+
@object = @klass.new
|
378
|
+
@object.state = 'parked'
|
379
|
+
|
380
|
+
@path = StateMachine::Path.new(@object, @machine, :target => :parked)
|
381
|
+
@path.concat([
|
382
|
+
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
|
383
|
+
@park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)
|
384
|
+
])
|
385
|
+
end
|
386
|
+
|
387
|
+
def test_should_be_complete
|
388
|
+
assert_equal true, @path.complete?
|
389
|
+
end
|
390
|
+
|
391
|
+
def test_should_be_able_to_walk
|
392
|
+
paths = []
|
393
|
+
@path.walk {|path| paths << path}
|
394
|
+
assert_equal [
|
395
|
+
[@ignite_transition, @park_transition, StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear)]
|
396
|
+
], paths
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
class PathWithDeepTargetTest < Test::Unit::TestCase
|
401
|
+
def setup
|
402
|
+
@klass = Class.new
|
403
|
+
@machine = StateMachine::Machine.new(@klass)
|
404
|
+
@machine.state :parked, :idling
|
405
|
+
@machine.event :ignite do
|
406
|
+
transition :parked => :idling
|
407
|
+
end
|
408
|
+
@machine.event :shift_up do
|
409
|
+
transition :parked => :first_gear
|
410
|
+
end
|
411
|
+
@machine.event :park do
|
412
|
+
transition [:idling, :first_gear] => :parked
|
413
|
+
end
|
414
|
+
|
415
|
+
@object = @klass.new
|
416
|
+
@object.state = 'parked'
|
417
|
+
|
418
|
+
@path = StateMachine::Path.new(@object, @machine, :target => :parked)
|
419
|
+
@path.concat([
|
420
|
+
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
|
421
|
+
@park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked),
|
422
|
+
@shift_up_transition = StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear)
|
423
|
+
])
|
424
|
+
end
|
425
|
+
|
426
|
+
def test_should_not_be_complete
|
427
|
+
assert_equal false, @path.complete?
|
428
|
+
end
|
429
|
+
|
430
|
+
def test_should_be_able_to_walk
|
431
|
+
paths = []
|
432
|
+
@path.walk {|path| paths << path}
|
433
|
+
assert_equal [
|
434
|
+
[@ignite_transition, @park_transition, @shift_up_transition, StateMachine::Transition.new(@object, @machine, :park, :first_gear, :parked)]
|
435
|
+
], paths
|
436
|
+
end
|
437
|
+
end
|
438
|
+
|
439
|
+
class PathWithDeepTargetReachedTest < Test::Unit::TestCase
|
440
|
+
def setup
|
441
|
+
@klass = Class.new
|
442
|
+
@machine = StateMachine::Machine.new(@klass)
|
443
|
+
@machine.state :parked, :idling
|
444
|
+
@machine.event :ignite do
|
445
|
+
transition :parked => :idling
|
446
|
+
end
|
447
|
+
@machine.event :shift_up do
|
448
|
+
transition :parked => :first_gear
|
449
|
+
end
|
450
|
+
@machine.event :park do
|
451
|
+
transition [:idling, :first_gear] => :parked
|
452
|
+
end
|
453
|
+
|
454
|
+
@object = @klass.new
|
455
|
+
@object.state = 'parked'
|
456
|
+
|
457
|
+
@path = StateMachine::Path.new(@object, @machine, :target => :parked)
|
458
|
+
@path.concat([
|
459
|
+
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
|
460
|
+
@park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked),
|
461
|
+
@shift_up_transition = StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear),
|
462
|
+
@park_transition_2 = StateMachine::Transition.new(@object, @machine, :park, :first_gear, :parked)
|
463
|
+
])
|
464
|
+
end
|
465
|
+
|
466
|
+
def test_should_be_complete
|
467
|
+
assert_equal true, @path.complete?
|
468
|
+
end
|
469
|
+
|
470
|
+
def test_should_not_be_able_to_walk
|
471
|
+
walked = false
|
472
|
+
@path.walk { walked = true }
|
473
|
+
assert_equal false, walked
|
474
|
+
end
|
475
|
+
|
476
|
+
def test_should_not_be_able_to_walk_with_available_transitions
|
477
|
+
@machine.event :park do
|
478
|
+
transition :parked => same
|
479
|
+
end
|
480
|
+
|
481
|
+
walked = false
|
482
|
+
@path.walk { walked = true }
|
483
|
+
assert_equal false, walked
|
484
|
+
end
|
485
|
+
end
|