hsume2-state_machine 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. data/CHANGELOG.rdoc +413 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +717 -0
  4. data/Rakefile +77 -0
  5. data/examples/AutoShop_state.png +0 -0
  6. data/examples/Car_state.png +0 -0
  7. data/examples/TrafficLight_state.png +0 -0
  8. data/examples/Vehicle_state.png +0 -0
  9. data/examples/auto_shop.rb +11 -0
  10. data/examples/car.rb +19 -0
  11. data/examples/merb-rest/controller.rb +51 -0
  12. data/examples/merb-rest/model.rb +28 -0
  13. data/examples/merb-rest/view_edit.html.erb +24 -0
  14. data/examples/merb-rest/view_index.html.erb +23 -0
  15. data/examples/merb-rest/view_new.html.erb +13 -0
  16. data/examples/merb-rest/view_show.html.erb +17 -0
  17. data/examples/rails-rest/controller.rb +43 -0
  18. data/examples/rails-rest/migration.rb +11 -0
  19. data/examples/rails-rest/model.rb +23 -0
  20. data/examples/rails-rest/view_edit.html.erb +25 -0
  21. data/examples/rails-rest/view_index.html.erb +23 -0
  22. data/examples/rails-rest/view_new.html.erb +14 -0
  23. data/examples/rails-rest/view_show.html.erb +17 -0
  24. data/examples/traffic_light.rb +7 -0
  25. data/examples/vehicle.rb +31 -0
  26. data/init.rb +1 -0
  27. data/lib/state_machine.rb +448 -0
  28. data/lib/state_machine/alternate_machine.rb +79 -0
  29. data/lib/state_machine/assertions.rb +36 -0
  30. data/lib/state_machine/branch.rb +224 -0
  31. data/lib/state_machine/callback.rb +236 -0
  32. data/lib/state_machine/condition_proxy.rb +94 -0
  33. data/lib/state_machine/error.rb +13 -0
  34. data/lib/state_machine/eval_helpers.rb +86 -0
  35. data/lib/state_machine/event.rb +304 -0
  36. data/lib/state_machine/event_collection.rb +139 -0
  37. data/lib/state_machine/extensions.rb +149 -0
  38. data/lib/state_machine/initializers.rb +4 -0
  39. data/lib/state_machine/initializers/merb.rb +1 -0
  40. data/lib/state_machine/initializers/rails.rb +25 -0
  41. data/lib/state_machine/integrations.rb +110 -0
  42. data/lib/state_machine/integrations/active_model.rb +502 -0
  43. data/lib/state_machine/integrations/active_model/locale.rb +11 -0
  44. data/lib/state_machine/integrations/active_model/observer.rb +45 -0
  45. data/lib/state_machine/integrations/active_model/versions.rb +31 -0
  46. data/lib/state_machine/integrations/active_record.rb +424 -0
  47. data/lib/state_machine/integrations/active_record/locale.rb +20 -0
  48. data/lib/state_machine/integrations/active_record/versions.rb +143 -0
  49. data/lib/state_machine/integrations/base.rb +91 -0
  50. data/lib/state_machine/integrations/data_mapper.rb +392 -0
  51. data/lib/state_machine/integrations/data_mapper/observer.rb +210 -0
  52. data/lib/state_machine/integrations/data_mapper/versions.rb +62 -0
  53. data/lib/state_machine/integrations/mongo_mapper.rb +272 -0
  54. data/lib/state_machine/integrations/mongo_mapper/locale.rb +4 -0
  55. data/lib/state_machine/integrations/mongo_mapper/versions.rb +110 -0
  56. data/lib/state_machine/integrations/mongoid.rb +357 -0
  57. data/lib/state_machine/integrations/mongoid/locale.rb +4 -0
  58. data/lib/state_machine/integrations/mongoid/versions.rb +18 -0
  59. data/lib/state_machine/integrations/sequel.rb +428 -0
  60. data/lib/state_machine/integrations/sequel/versions.rb +36 -0
  61. data/lib/state_machine/machine.rb +1873 -0
  62. data/lib/state_machine/machine_collection.rb +87 -0
  63. data/lib/state_machine/matcher.rb +123 -0
  64. data/lib/state_machine/matcher_helpers.rb +54 -0
  65. data/lib/state_machine/node_collection.rb +157 -0
  66. data/lib/state_machine/path.rb +120 -0
  67. data/lib/state_machine/path_collection.rb +90 -0
  68. data/lib/state_machine/state.rb +271 -0
  69. data/lib/state_machine/state_collection.rb +112 -0
  70. data/lib/state_machine/transition.rb +458 -0
  71. data/lib/state_machine/transition_collection.rb +244 -0
  72. data/lib/tasks/state_machine.rake +1 -0
  73. data/lib/tasks/state_machine.rb +27 -0
  74. data/test/files/en.yml +17 -0
  75. data/test/files/switch.rb +11 -0
  76. data/test/functional/alternate_state_machine_test.rb +122 -0
  77. data/test/functional/state_machine_test.rb +993 -0
  78. data/test/test_helper.rb +4 -0
  79. data/test/unit/assertions_test.rb +40 -0
  80. data/test/unit/branch_test.rb +890 -0
  81. data/test/unit/callback_test.rb +701 -0
  82. data/test/unit/condition_proxy_test.rb +328 -0
  83. data/test/unit/error_test.rb +43 -0
  84. data/test/unit/eval_helpers_test.rb +222 -0
  85. data/test/unit/event_collection_test.rb +358 -0
  86. data/test/unit/event_test.rb +985 -0
  87. data/test/unit/integrations/active_model_test.rb +1097 -0
  88. data/test/unit/integrations/active_record_test.rb +2021 -0
  89. data/test/unit/integrations/base_test.rb +99 -0
  90. data/test/unit/integrations/data_mapper_test.rb +1909 -0
  91. data/test/unit/integrations/mongo_mapper_test.rb +1611 -0
  92. data/test/unit/integrations/mongoid_test.rb +1591 -0
  93. data/test/unit/integrations/sequel_test.rb +1523 -0
  94. data/test/unit/integrations_test.rb +61 -0
  95. data/test/unit/invalid_event_test.rb +20 -0
  96. data/test/unit/invalid_parallel_transition_test.rb +18 -0
  97. data/test/unit/invalid_transition_test.rb +77 -0
  98. data/test/unit/machine_collection_test.rb +599 -0
  99. data/test/unit/machine_test.rb +3043 -0
  100. data/test/unit/matcher_helpers_test.rb +37 -0
  101. data/test/unit/matcher_test.rb +155 -0
  102. data/test/unit/node_collection_test.rb +217 -0
  103. data/test/unit/path_collection_test.rb +266 -0
  104. data/test/unit/path_test.rb +485 -0
  105. data/test/unit/state_collection_test.rb +310 -0
  106. data/test/unit/state_machine_test.rb +31 -0
  107. data/test/unit/state_test.rb +924 -0
  108. data/test/unit/transition_collection_test.rb +2102 -0
  109. data/test/unit/transition_test.rb +1541 -0
  110. metadata +207 -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
+ ignite_transition = 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