state_machine 0.9.4 → 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.
Files changed (68) hide show
  1. data/CHANGELOG.rdoc +20 -0
  2. data/LICENSE +1 -1
  3. data/README.rdoc +74 -4
  4. data/Rakefile +3 -3
  5. data/lib/state_machine.rb +51 -24
  6. data/lib/state_machine/{guard.rb → branch.rb} +34 -40
  7. data/lib/state_machine/callback.rb +13 -18
  8. data/lib/state_machine/error.rb +13 -0
  9. data/lib/state_machine/eval_helpers.rb +3 -0
  10. data/lib/state_machine/event.rb +67 -30
  11. data/lib/state_machine/event_collection.rb +20 -3
  12. data/lib/state_machine/extensions.rb +3 -3
  13. data/lib/state_machine/integrations.rb +7 -0
  14. data/lib/state_machine/integrations/active_model.rb +149 -59
  15. data/lib/state_machine/integrations/active_model/versions.rb +30 -0
  16. data/lib/state_machine/integrations/active_record.rb +74 -148
  17. data/lib/state_machine/integrations/active_record/locale.rb +0 -7
  18. data/lib/state_machine/integrations/active_record/versions.rb +149 -0
  19. data/lib/state_machine/integrations/base.rb +64 -0
  20. data/lib/state_machine/integrations/data_mapper.rb +50 -39
  21. data/lib/state_machine/integrations/data_mapper/observer.rb +47 -12
  22. data/lib/state_machine/integrations/data_mapper/versions.rb +62 -0
  23. data/lib/state_machine/integrations/mongo_mapper.rb +37 -64
  24. data/lib/state_machine/integrations/mongo_mapper/locale.rb +4 -0
  25. data/lib/state_machine/integrations/mongo_mapper/versions.rb +102 -0
  26. data/lib/state_machine/integrations/mongoid.rb +297 -0
  27. data/lib/state_machine/integrations/mongoid/locale.rb +4 -0
  28. data/lib/state_machine/integrations/mongoid/versions.rb +18 -0
  29. data/lib/state_machine/integrations/sequel.rb +99 -55
  30. data/lib/state_machine/integrations/sequel/versions.rb +40 -0
  31. data/lib/state_machine/machine.rb +273 -136
  32. data/lib/state_machine/machine_collection.rb +21 -13
  33. data/lib/state_machine/node_collection.rb +6 -1
  34. data/lib/state_machine/path.rb +120 -0
  35. data/lib/state_machine/path_collection.rb +90 -0
  36. data/lib/state_machine/state.rb +28 -9
  37. data/lib/state_machine/state_collection.rb +1 -1
  38. data/lib/state_machine/transition.rb +65 -6
  39. data/lib/state_machine/transition_collection.rb +1 -1
  40. data/test/files/en.yml +8 -0
  41. data/test/functional/state_machine_test.rb +15 -2
  42. data/test/unit/branch_test.rb +890 -0
  43. data/test/unit/callback_test.rb +9 -36
  44. data/test/unit/error_test.rb +43 -0
  45. data/test/unit/event_collection_test.rb +67 -33
  46. data/test/unit/event_test.rb +165 -38
  47. data/test/unit/integrations/active_model_test.rb +103 -3
  48. data/test/unit/integrations/active_record_test.rb +90 -43
  49. data/test/unit/integrations/base_test.rb +87 -0
  50. data/test/unit/integrations/data_mapper_test.rb +105 -44
  51. data/test/unit/integrations/mongo_mapper_test.rb +261 -64
  52. data/test/unit/integrations/mongoid_test.rb +1529 -0
  53. data/test/unit/integrations/sequel_test.rb +33 -49
  54. data/test/unit/integrations_test.rb +4 -0
  55. data/test/unit/invalid_event_test.rb +15 -2
  56. data/test/unit/invalid_parallel_transition_test.rb +18 -0
  57. data/test/unit/invalid_transition_test.rb +72 -2
  58. data/test/unit/machine_collection_test.rb +55 -61
  59. data/test/unit/machine_test.rb +388 -26
  60. data/test/unit/node_collection_test.rb +14 -4
  61. data/test/unit/path_collection_test.rb +266 -0
  62. data/test/unit/path_test.rb +485 -0
  63. data/test/unit/state_collection_test.rb +30 -0
  64. data/test/unit/state_test.rb +82 -35
  65. data/test/unit/transition_collection_test.rb +48 -44
  66. data/test/unit/transition_test.rb +198 -41
  67. metadata +111 -74
  68. data/test/unit/guard_test.rb +0 -909
@@ -28,6 +28,7 @@ class StateCollectionTest < Test::Unit::TestCase
28
28
  @states << @nil = StateMachine::State.new(@machine, nil)
29
29
  @states << @parked = StateMachine::State.new(@machine, :parked)
30
30
  @states << @idling = StateMachine::State.new(@machine, :idling)
31
+ @machine.states.concat(@states)
31
32
 
32
33
  @object = @klass.new
33
34
  end
@@ -40,6 +41,10 @@ class StateCollectionTest < Test::Unit::TestCase
40
41
  assert_equal @parked, @states[:parked]
41
42
  end
42
43
 
44
+ def test_should_index_by_qualified_name
45
+ assert_equal @parked, @states[:parked, :qualified_name]
46
+ end
47
+
43
48
  def test_should_index_by_value
44
49
  assert_equal @parked, @states['parked', :value]
45
50
  end
@@ -79,6 +84,25 @@ class StateCollectionTest < Test::Unit::TestCase
79
84
  end
80
85
  end
81
86
 
87
+ class StateCollectionWithNamespaceTest < Test::Unit::TestCase
88
+ def setup
89
+ @klass = Class.new
90
+ @machine = StateMachine::Machine.new(@klass, :namespace => 'vehicle')
91
+ @states = StateMachine::StateCollection.new(@machine)
92
+
93
+ @states << @state = StateMachine::State.new(@machine, :parked)
94
+ @machine.states.concat(@states)
95
+ end
96
+
97
+ def test_should_index_by_name
98
+ assert_equal @state, @states[:parked, :name]
99
+ end
100
+
101
+ def test_should_index_by_qualified_name
102
+ assert_equal @state, @states[:vehicle_parked, :qualified_name]
103
+ end
104
+ end
105
+
82
106
  class StateCollectionWithCustomStateValuesTest < Test::Unit::TestCase
83
107
  def setup
84
108
  @klass = Class.new
@@ -86,6 +110,7 @@ class StateCollectionWithCustomStateValuesTest < Test::Unit::TestCase
86
110
  @states = StateMachine::StateCollection.new(@machine)
87
111
 
88
112
  @states << @state = StateMachine::State.new(@machine, :parked, :value => 1)
113
+ @machine.states.concat(@states)
89
114
 
90
115
  @object = @klass.new
91
116
  @object.state = 1
@@ -112,6 +137,7 @@ class StateCollectionWithStateMatchersTest < Test::Unit::TestCase
112
137
  @states = StateMachine::StateCollection.new(@machine)
113
138
 
114
139
  @states << @state = StateMachine::State.new(@machine, :parked, :if => lambda {|value| !value.nil?})
140
+ @machine.states.concat(@states)
115
141
 
116
142
  @object = @klass.new
117
143
  @object.state = 1
@@ -138,6 +164,7 @@ class StateCollectionWithInitialStateTest < Test::Unit::TestCase
138
164
 
139
165
  @states << @parked = StateMachine::State.new(@machine, :parked)
140
166
  @states << @idling = StateMachine::State.new(@machine, :idling)
167
+ @machine.states.concat(@states)
141
168
 
142
169
  @parked.initial = true
143
170
  end
@@ -175,6 +202,7 @@ class StateCollectionWithStateBehaviorsTest < Test::Unit::TestCase
175
202
 
176
203
  @states << @parked = StateMachine::State.new(@machine, :parked)
177
204
  @states << @idling = StateMachine::State.new(@machine, :idling)
205
+ @machine.states.concat(@states)
178
206
 
179
207
  @idling.context do
180
208
  def speed
@@ -212,6 +240,7 @@ class StateCollectionWithEventTransitionsTest < Test::Unit::TestCase
212
240
 
213
241
  @states << @parked = StateMachine::State.new(@machine, :parked)
214
242
  @states << @idling = StateMachine::State.new(@machine, :idling)
243
+ @machine.states.concat(@states)
215
244
 
216
245
  @machine.event :ignite do
217
246
  transition :to => :idling
@@ -249,6 +278,7 @@ class StateCollectionWithTransitionCallbacksTest < Test::Unit::TestCase
249
278
 
250
279
  @states << @parked = StateMachine::State.new(@machine, :parked)
251
280
  @states << @idling = StateMachine::State.new(@machine, :idling)
281
+ @machine.states.concat(@states)
252
282
 
253
283
  @machine.before_transition :to => :idling, :do => lambda {}
254
284
  end
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
3
3
  class StateByDefaultTest < Test::Unit::TestCase
4
4
  def setup
5
5
  @machine = StateMachine::Machine.new(Class.new)
6
- @state = StateMachine::State.new(@machine, :parked)
6
+ @machine.states << @state = StateMachine::State.new(@machine, :parked)
7
7
  end
8
8
 
9
9
  def test_should_have_a_machine
@@ -43,7 +43,7 @@ end
43
43
  class StateTest < Test::Unit::TestCase
44
44
  def setup
45
45
  @machine = StateMachine::Machine.new(Class.new)
46
- @state = StateMachine::State.new(@machine, :parked)
46
+ @machine.states << @state = StateMachine::State.new(@machine, :parked)
47
47
  end
48
48
 
49
49
  def test_should_raise_exception_if_invalid_option_specified
@@ -87,7 +87,7 @@ class StateWithoutNameTest < Test::Unit::TestCase
87
87
  def setup
88
88
  @klass = Class.new
89
89
  @machine = StateMachine::Machine.new(@klass)
90
- @state = StateMachine::State.new(@machine, nil)
90
+ @machine.states << @state = StateMachine::State.new(@machine, nil)
91
91
  end
92
92
 
93
93
  def test_should_have_a_nil_name
@@ -121,7 +121,7 @@ class StateWithNameTest < Test::Unit::TestCase
121
121
  def setup
122
122
  @klass = Class.new
123
123
  @machine = StateMachine::Machine.new(@klass)
124
- @state = StateMachine::State.new(@machine, :parked)
124
+ @machine.states << @state = StateMachine::State.new(@machine, :parked)
125
125
  end
126
126
 
127
127
  def test_should_have_a_name
@@ -158,7 +158,7 @@ class StateWithNilValueTest < Test::Unit::TestCase
158
158
  def setup
159
159
  @klass = Class.new
160
160
  @machine = StateMachine::Machine.new(@klass)
161
- @state = StateMachine::State.new(@machine, :parked, :value => nil)
161
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :value => nil)
162
162
  end
163
163
 
164
164
  def test_should_have_a_name
@@ -187,7 +187,7 @@ class StateWithSymbolicValueTest < Test::Unit::TestCase
187
187
  def setup
188
188
  @klass = Class.new
189
189
  @machine = StateMachine::Machine.new(@klass)
190
- @state = StateMachine::State.new(@machine, :parked, :value => :parked)
190
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :value => :parked)
191
191
  end
192
192
 
193
193
  def test_should_use_custom_value
@@ -213,7 +213,7 @@ class StateWithIntegerValueTest < Test::Unit::TestCase
213
213
  def setup
214
214
  @klass = Class.new
215
215
  @machine = StateMachine::Machine.new(@klass)
216
- @state = StateMachine::State.new(@machine, :parked, :value => 1)
216
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :value => 1)
217
217
  end
218
218
 
219
219
  def test_should_use_custom_value
@@ -241,7 +241,7 @@ class StateWithLambdaValueTest < Test::Unit::TestCase
241
241
  @args = nil
242
242
  @machine = StateMachine::Machine.new(@klass)
243
243
  @value = lambda {|*args| @args = args; :parked}
244
- @state = StateMachine::State.new(@machine, :parked, :value => @value)
244
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :value => @value)
245
245
  end
246
246
 
247
247
  def test_should_use_evaluated_value_by_default
@@ -328,7 +328,7 @@ class StateWithMatcherTest < Test::Unit::TestCase
328
328
  @klass = Class.new
329
329
  @args = nil
330
330
  @machine = StateMachine::Machine.new(@klass)
331
- @state = StateMachine::State.new(@machine, :parked, :if => lambda {|value| value == 1})
331
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :if => lambda {|value| value == 1})
332
332
  end
333
333
 
334
334
  def test_should_not_match_actual_value
@@ -344,7 +344,7 @@ class StateWithHumanNameTest < Test::Unit::TestCase
344
344
  def setup
345
345
  @klass = Class.new
346
346
  @machine = StateMachine::Machine.new(@klass)
347
- @state = StateMachine::State.new(@machine, :parked, :human_name => 'stopped')
347
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :human_name => 'stopped')
348
348
  end
349
349
 
350
350
  def test_should_use_custom_human_name
@@ -356,7 +356,7 @@ class StateWithDynamicHumanNameTest < Test::Unit::TestCase
356
356
  def setup
357
357
  @klass = Class.new
358
358
  @machine = StateMachine::Machine.new(@klass)
359
- @state = StateMachine::State.new(@machine, :parked, :human_name => lambda {|state, object| ['stopped', object]})
359
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :human_name => lambda {|state, object| ['stopped', object]})
360
360
  end
361
361
 
362
362
  def test_should_use_custom_human_name
@@ -379,7 +379,7 @@ end
379
379
  class StateInitialTest < Test::Unit::TestCase
380
380
  def setup
381
381
  @machine = StateMachine::Machine.new(Class.new)
382
- @state = StateMachine::State.new(@machine, :parked, :initial => true)
382
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :initial => true)
383
383
  end
384
384
 
385
385
  def test_should_be_initial
@@ -391,7 +391,7 @@ end
391
391
  class StateNotInitialTest < Test::Unit::TestCase
392
392
  def setup
393
393
  @machine = StateMachine::Machine.new(Class.new)
394
- @state = StateMachine::State.new(@machine, :parked, :initial => false)
394
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :initial => false)
395
395
  end
396
396
 
397
397
  def test_should_not_be_initial
@@ -403,7 +403,7 @@ end
403
403
  class StateFinalTest < Test::Unit::TestCase
404
404
  def setup
405
405
  @machine = StateMachine::Machine.new(Class.new)
406
- @state = StateMachine::State.new(@machine, :parked)
406
+ @machine.states << @state = StateMachine::State.new(@machine, :parked)
407
407
  end
408
408
 
409
409
  def test_should_be_final_without_input_transitions
@@ -430,7 +430,7 @@ end
430
430
  class StateNotFinalTest < Test::Unit::TestCase
431
431
  def setup
432
432
  @machine = StateMachine::Machine.new(Class.new)
433
- @state = StateMachine::State.new(@machine, :parked)
433
+ @machine.states << @state = StateMachine::State.new(@machine, :parked)
434
434
  end
435
435
 
436
436
  def test_should_not_be_final_with_outgoing_whitelist_transitions
@@ -460,6 +460,9 @@ end
460
460
 
461
461
  class StateWithConflictingHelpersTest < Test::Unit::TestCase
462
462
  def setup
463
+ require 'stringio'
464
+ @original_stderr, $stderr = $stderr, StringIO.new
465
+
463
466
  @klass = Class.new do
464
467
  def parked?
465
468
  0
@@ -470,18 +473,62 @@ class StateWithConflictingHelpersTest < Test::Unit::TestCase
470
473
  @object = @klass.new
471
474
  end
472
475
 
473
- def test_should_not_redefine_state_predicate
476
+ def test_should_not_override_state_predicate
474
477
  assert_equal 0, @object.parked?
475
478
  end
476
479
 
477
- def test_should_allow_super_chaining
480
+ def test_should_not_allow_super_chaining
478
481
  @klass.class_eval do
479
482
  def parked?
480
483
  super ? 1 : 0
481
484
  end
482
485
  end
483
486
 
484
- assert_equal 0, @object.parked?
487
+ assert_raise(NoMethodError) { @object.parked? }
488
+ end
489
+
490
+ def test_should_output_warning
491
+ assert_equal "#parked? is already defined, use #state?(:parked) instead.\n", $stderr.string
492
+ end
493
+
494
+ def teardown
495
+ $stderr = @original_stderr
496
+ end
497
+ end
498
+
499
+ class EventWithConflictingMachineTest < Test::Unit::TestCase
500
+ def setup
501
+ require 'stringio'
502
+ @original_stderr, $stderr = $stderr, StringIO.new
503
+
504
+ @klass = Class.new
505
+ @state_machine = StateMachine::Machine.new(@klass, :state)
506
+ @state_machine.states << @state = StateMachine::State.new(@state_machine, :parked)
507
+ end
508
+
509
+ def test_should_output_warning_if_using_different_attribute
510
+ @status_machine = StateMachine::Machine.new(@klass, :status)
511
+ @status_machine.states << @state = StateMachine::State.new(@status_machine, :parked)
512
+
513
+ assert_equal "State :parked for :status is already defined in :state\n", $stderr.string
514
+ end
515
+
516
+ def test_should_not_output_warning_if_using_same_attribute
517
+ @status_machine = StateMachine::Machine.new(@klass, :status, :attribute => :state)
518
+ @status_machine.states << @state = StateMachine::State.new(@status_machine, :parked)
519
+
520
+ assert_equal '', $stderr.string
521
+ end
522
+
523
+ def test_should_not_output_warning_if_using_different_namespace
524
+ @status_machine = StateMachine::Machine.new(@klass, :status, :namespace => 'alarm')
525
+ @status_machine.states << @state = StateMachine::State.new(@status_machine, :parked)
526
+
527
+ assert_equal '', $stderr.string
528
+ end
529
+
530
+ def teardown
531
+ $stderr = @original_stderr
485
532
  end
486
533
  end
487
534
 
@@ -489,7 +536,7 @@ class StateWithNamespaceTest < Test::Unit::TestCase
489
536
  def setup
490
537
  @klass = Class.new
491
538
  @machine = StateMachine::Machine.new(@klass, :namespace => 'alarm')
492
- @state = StateMachine::State.new(@machine, :active)
539
+ @machine.states << @state = StateMachine::State.new(@machine, :active)
493
540
  @object = @klass.new
494
541
  end
495
542
 
@@ -509,7 +556,7 @@ end
509
556
  class StateAfterBeingCopiedTest < Test::Unit::TestCase
510
557
  def setup
511
558
  @machine = StateMachine::Machine.new(Class.new)
512
- @state = StateMachine::State.new(@machine, :parked)
559
+ @machine.states << @state = StateMachine::State.new(@machine, :parked)
513
560
  @copied_state = @state.dup
514
561
  end
515
562
 
@@ -523,7 +570,7 @@ class StateWithContextTest < Test::Unit::TestCase
523
570
  @klass = Class.new
524
571
  @machine = StateMachine::Machine.new(@klass)
525
572
  @ancestors = @klass.ancestors
526
- @state = StateMachine::State.new(@machine, :idling)
573
+ @machine.states << @state = StateMachine::State.new(@machine, :idling)
527
574
 
528
575
  speed_method = nil
529
576
  rpm_method = nil
@@ -568,7 +615,7 @@ class StateWithMultipleContextsTest < Test::Unit::TestCase
568
615
  @klass = Class.new
569
616
  @machine = StateMachine::Machine.new(@klass)
570
617
  @ancestors = @klass.ancestors
571
- @state = StateMachine::State.new(@machine, :idling)
618
+ @machine.states << @state = StateMachine::State.new(@machine, :idling)
572
619
 
573
620
  speed_method = nil
574
621
  @state.context do
@@ -621,7 +668,7 @@ class StateWithExistingContextMethodTest < Test::Unit::TestCase
621
668
  @original_speed_method = @klass.instance_method(:speed)
622
669
 
623
670
  @machine = StateMachine::Machine.new(@klass)
624
- @state = StateMachine::State.new(@machine, :idling)
671
+ @machine.states << @state = StateMachine::State.new(@machine, :idling)
625
672
  @state.context do
626
673
  def speed
627
674
  0
@@ -638,7 +685,7 @@ class StateWithRedefinedContextMethodTest < Test::Unit::TestCase
638
685
  def setup
639
686
  @klass = Class.new
640
687
  @machine = StateMachine::Machine.new(@klass)
641
- @state = StateMachine::State.new(@machine, 'on')
688
+ @machine.states << @state = StateMachine::State.new(@machine, 'on')
642
689
 
643
690
  old_speed_method = nil
644
691
  @state.context do
@@ -669,7 +716,7 @@ class StateWithInvalidMethodCallTest < Test::Unit::TestCase
669
716
  @klass = Class.new
670
717
  @machine = StateMachine::Machine.new(@klass)
671
718
  @ancestors = @klass.ancestors
672
- @state = StateMachine::State.new(@machine, :idling)
719
+ @machine.states << @state = StateMachine::State.new(@machine, :idling)
673
720
  @state.context do
674
721
  def speed
675
722
  0
@@ -689,7 +736,7 @@ class StateWithValidMethodCallTest < Test::Unit::TestCase
689
736
  @klass = Class.new
690
737
  @machine = StateMachine::Machine.new(@klass)
691
738
  @ancestors = @klass.ancestors
692
- @state = StateMachine::State.new(@machine, :idling)
739
+ @machine.states << @state = StateMachine::State.new(@machine, :idling)
693
740
  @state.context do
694
741
  def speed(arg = nil)
695
742
  block_given? ? [arg, yield] : arg
@@ -725,10 +772,10 @@ begin
725
772
  class StateDrawingTest < Test::Unit::TestCase
726
773
  def setup
727
774
  @machine = StateMachine::Machine.new(Class.new)
775
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :value => 1)
728
776
  @machine.event :ignite do
729
777
  transition :parked => :idling
730
778
  end
731
- @state = StateMachine::State.new(@machine, :parked, :value => 1)
732
779
 
733
780
  graph = GraphViz.new('G')
734
781
  @node = @state.draw(graph)
@@ -747,7 +794,7 @@ begin
747
794
  end
748
795
 
749
796
  def test_should_use_stringified_name_as_name
750
- assert_equal 'parked', @node.name
797
+ assert_equal 'parked', Gem::Version.new(Constants::RGV_VERSION) <= Gem::Version.new('0.9.11') ? @node.name : @node.id
751
798
  end
752
799
 
753
800
  def test_should_use_description_as_label
@@ -758,10 +805,10 @@ begin
758
805
  class StateDrawingInitialTest < Test::Unit::TestCase
759
806
  def setup
760
807
  @machine = StateMachine::Machine.new(Class.new)
808
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :initial => true)
761
809
  @machine.event :ignite do
762
810
  transition :parked => :idling
763
811
  end
764
- @state = StateMachine::State.new(@machine, :parked, :initial => true)
765
812
 
766
813
  @graph = GraphViz.new('G')
767
814
  @node = @state.draw(@graph)
@@ -780,14 +827,14 @@ begin
780
827
  class StateDrawingNilNameTest < Test::Unit::TestCase
781
828
  def setup
782
829
  @machine = StateMachine::Machine.new(Class.new)
783
- @state = StateMachine::State.new(@machine, nil)
830
+ @machine.states << @state = StateMachine::State.new(@machine, nil)
784
831
 
785
832
  graph = GraphViz.new('G')
786
833
  @node = @state.draw(graph)
787
834
  end
788
835
 
789
836
  def test_should_use_stringified_nil_as_name
790
- assert_equal 'nil', @node.name
837
+ assert_equal 'nil', Gem::Version.new(Constants::RGV_VERSION) <= Gem::Version.new('0.9.11') ? @node.name : @node.id
791
838
  end
792
839
 
793
840
  def test_should_use_description_as_label
@@ -798,14 +845,14 @@ begin
798
845
  class StateDrawingLambdaValueTest < Test::Unit::TestCase
799
846
  def setup
800
847
  @machine = StateMachine::Machine.new(Class.new)
801
- @state = StateMachine::State.new(@machine, :parked, :value => lambda {})
848
+ @machine.states << @state = StateMachine::State.new(@machine, :parked, :value => lambda {})
802
849
 
803
850
  graph = GraphViz.new('G')
804
851
  @node = @state.draw(graph)
805
852
  end
806
853
 
807
854
  def test_should_use_stringified_name_as_name
808
- assert_equal 'parked', @node.name
855
+ assert_equal 'parked', Gem::Version.new(Constants::RGV_VERSION) <= Gem::Version.new('0.9.11') ? @node.name : @node.id
809
856
  end
810
857
 
811
858
  def test_should_use_description_as_label
@@ -816,10 +863,10 @@ begin
816
863
  class StateDrawingNonFinalTest < Test::Unit::TestCase
817
864
  def setup
818
865
  @machine = StateMachine::Machine.new(Class.new)
866
+ @machine.states << @state = StateMachine::State.new(@machine, :parked)
819
867
  @machine.event :ignite do
820
868
  transition :parked => :idling
821
869
  end
822
- @state = StateMachine::State.new(@machine, :parked)
823
870
 
824
871
  graph = GraphViz.new('G')
825
872
  @node = @state.draw(graph)
@@ -833,7 +880,7 @@ begin
833
880
  class StateDrawingFinalTest < Test::Unit::TestCase
834
881
  def setup
835
882
  @machine = StateMachine::Machine.new(Class.new)
836
- @state = StateMachine::State.new(@machine, :parked)
883
+ @machine.states << @state = StateMachine::State.new(@machine, :parked)
837
884
 
838
885
  graph = GraphViz.new('G')
839
886
  @node = @state.draw(graph)
@@ -646,13 +646,11 @@ class TransitionCollectionWithDifferentActionsTest < Test::Unit::TestCase
646
646
  end
647
647
 
648
648
  @callbacks = []
649
- @state.after_transition(:include_failures => true) { @callbacks << :state_after }
650
- @state.around_transition(:include_failures => true) {|block| block.call; @callbacks << :state_around }
651
- @status.after_transition(:include_failures => true) { @callbacks << :status_after }
652
- @status.around_transition(:include_failures => true) {|block| block.call; @callbacks << :status_around }
649
+ @state.after_failure { @callbacks << :state_after }
650
+ @status.after_failure { @callbacks << :status_after }
653
651
 
654
652
  @transitions.perform
655
- assert_equal [:status_around, :status_after, :state_around, :state_after], @callbacks
653
+ assert_equal [:status_after, :state_after], @callbacks
656
654
  end
657
655
 
658
656
  def test_should_run_after_failure_callbacks_if_action_fails_for_second_transition
@@ -663,13 +661,11 @@ class TransitionCollectionWithDifferentActionsTest < Test::Unit::TestCase
663
661
  end
664
662
 
665
663
  @callbacks = []
666
- @state.after_transition(:include_failures => true) { @callbacks << :state_after }
667
- @state.around_transition(:include_failures => true) {|block| block.call; @callbacks << :state_around }
668
- @status.after_transition(:include_failures => true) { @callbacks << :status_after }
669
- @status.around_transition(:include_failures => true) {|block| block.call; @callbacks << :status_around }
664
+ @state.after_failure { @callbacks << :state_after }
665
+ @status.after_failure { @callbacks << :status_after }
670
666
 
671
667
  @transitions.perform
672
- assert_equal [:status_around, :status_after, :state_around, :state_after], @callbacks
668
+ assert_equal [:status_after, :state_after], @callbacks
673
669
  end
674
670
  end
675
671
 
@@ -769,6 +765,7 @@ class TransitionCollectionWithActionFailedTest < Test::Unit::TestCase
769
765
  @around_before_count = 0
770
766
  @after_count = 0
771
767
  @around_after_count = 0
768
+ @failure_count = 0
772
769
 
773
770
  @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
774
771
  @machine.state :idling
@@ -776,9 +773,8 @@ class TransitionCollectionWithActionFailedTest < Test::Unit::TestCase
776
773
 
777
774
  @machine.before_transition {@before_count += 1}
778
775
  @machine.after_transition {@after_count += 1}
779
- @machine.after_transition(:include_failures => true) {@after_count += 1}
780
776
  @machine.around_transition {|block| @around_before_count += 1; block.call; @around_after_count += 1}
781
- @machine.around_transition(:include_failures => true) {|block| @around_before_count += 1; block.call; @around_after_count += 1}
777
+ @machine.after_failure {@failure_count += 1}
782
778
 
783
779
  @object = @klass.new
784
780
 
@@ -801,15 +797,19 @@ class TransitionCollectionWithActionFailedTest < Test::Unit::TestCase
801
797
  end
802
798
 
803
799
  def test_should_run_around_callbacks_before_yield
804
- assert_equal 2, @around_before_count
800
+ assert_equal 1, @around_before_count
805
801
  end
806
802
 
807
- def test_should_only_run_after_callbacks_that_include_failures
808
- assert_equal 1, @after_count
803
+ def test_should_not_run_after_callbacks
804
+ assert_equal 0, @after_count
805
+ end
806
+
807
+ def test_should_not_run_around_callbacks
808
+ assert_equal 0, @around_after_count
809
809
  end
810
810
 
811
- def test_should_only_run_around_callbacks_after_yield_that_include_failures
812
- assert_equal 1, @around_after_count
811
+ def test_should_run_failure_callbacks
812
+ assert_equal 1, @failure_count
813
813
  end
814
814
  end
815
815
 
@@ -824,6 +824,7 @@ class TransitionCollectionWithActionErrorTest < Test::Unit::TestCase
824
824
  @around_before_count = 0
825
825
  @after_count = 0
826
826
  @around_after_count = 0
827
+ @failure_count = 0
827
828
 
828
829
  @machine = StateMachine::Machine.new(@klass, :initial => :parked, :action => :save)
829
830
  @machine.state :idling
@@ -831,9 +832,8 @@ class TransitionCollectionWithActionErrorTest < Test::Unit::TestCase
831
832
 
832
833
  @machine.before_transition {@before_count += 1}
833
834
  @machine.after_transition {@after_count += 1}
834
- @machine.after_transition(:include_failures => true) {@after_count += 1}
835
835
  @machine.around_transition {|block| @around_before_count += 1; block.call; @around_after_count += 1}
836
- @machine.around_transition(:include_failures => true) {|block| @around_before_count += 1; block.call; @around_after_count += 1}
836
+ @machine.after_failure {@failure_count += 1}
837
837
 
838
838
  @object = @klass.new
839
839
 
@@ -862,7 +862,7 @@ class TransitionCollectionWithActionErrorTest < Test::Unit::TestCase
862
862
  end
863
863
 
864
864
  def test_should_run_around_callbacks_before_yield
865
- assert_equal 2, @around_before_count
865
+ assert_equal 1, @around_before_count
866
866
  end
867
867
 
868
868
  def test_should_not_run_after_callbacks
@@ -872,6 +872,10 @@ class TransitionCollectionWithActionErrorTest < Test::Unit::TestCase
872
872
  def test_should_not_run_around_callbacks_after_yield
873
873
  assert_equal 0, @around_after_count
874
874
  end
875
+
876
+ def test_should_not_run_failure_callbacks
877
+ assert_equal 0, @failure_count
878
+ end
875
879
  end
876
880
 
877
881
  class TransitionCollectionWithCallbacksTest < Test::Unit::TestCase
@@ -891,15 +895,15 @@ class TransitionCollectionWithCallbacksTest < Test::Unit::TestCase
891
895
  @state.state :idling
892
896
  @state.event :ignite
893
897
  @state.before_transition {@before_callbacks << :state_before}
894
- @state.after_transition(:include_failures => true) {@after_callbacks << :state_after}
895
- @state.around_transition(:include_failures => true) {|block| @before_callbacks << :state_around; block.call; @after_callbacks << :state_around}
898
+ @state.after_transition {@after_callbacks << :state_after}
899
+ @state.around_transition {|block| @before_callbacks << :state_around; block.call; @after_callbacks << :state_around}
896
900
 
897
901
  @status = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save)
898
902
  @status.state :second_gear
899
903
  @status.event :shift_up
900
904
  @status.before_transition {@before_callbacks << :status_before}
901
- @status.after_transition(:include_failures => true) {@after_callbacks << :status_after}
902
- @status.around_transition(:include_failures => true) {|block| @before_callbacks << :status_around; block.call; @after_callbacks << :status_around}
905
+ @status.after_transition {@after_callbacks << :status_after}
906
+ @status.around_transition {|block| @before_callbacks << :status_around; block.call; @after_callbacks << :status_around}
903
907
 
904
908
  @object = @klass.new
905
909
  @transitions = StateMachine::TransitionCollection.new([
@@ -947,14 +951,14 @@ class TransitionCollectionWithCallbacksTest < Test::Unit::TestCase
947
951
  end
948
952
 
949
953
  def test_should_not_halt_if_after_callback_halted_for_first_transition
950
- @state.after_transition(:include_failures => true) {throw :halt}
954
+ @state.after_transition {throw :halt}
951
955
 
952
956
  assert_equal true, @transitions.perform
953
957
  assert_equal [:status_around, :status_after, :state_around, :state_after], @after_callbacks
954
958
  end
955
959
 
956
960
  def test_should_not_halt_if_around_callback_halted_for_second_transition
957
- @status.around_transition(:include_failures => true) {|block| block.call; throw :halt}
961
+ @status.around_transition {|block| block.call; throw :halt}
958
962
 
959
963
  assert_equal true, @transitions.perform
960
964
  assert_equal [:state_around, :state_after], @after_callbacks
@@ -1152,7 +1156,7 @@ class TransitionCollectionWithSkippedAfterCallbacksTest < Test::Unit::TestCase
1152
1156
  end
1153
1157
  end
1154
1158
 
1155
- class TransitionCollectionWithActionHelperBaseTest < Test::Unit::TestCase
1159
+ class TransitionCollectionWithActionHookBaseTest < Test::Unit::TestCase
1156
1160
  def setup
1157
1161
  @superclass = Class.new do
1158
1162
  def save
@@ -1185,7 +1189,7 @@ class TransitionCollectionWithActionHelperBaseTest < Test::Unit::TestCase
1185
1189
  end
1186
1190
  end
1187
1191
 
1188
- class TransitionCollectionWithActionHelperAndSkippedActionTest < TransitionCollectionWithActionHelperBaseTest
1192
+ class TransitionCollectionWithActionHookAndSkippedActionTest < TransitionCollectionWithActionHookBaseTest
1189
1193
  def setup
1190
1194
  super
1191
1195
  @result = StateMachine::TransitionCollection.new([@transition], :actions => false).perform
@@ -1200,7 +1204,7 @@ class TransitionCollectionWithActionHelperAndSkippedActionTest < TransitionColle
1200
1204
  end
1201
1205
  end
1202
1206
 
1203
- class TransitionCollectionWithActionHelperAndSkippedAfterCallbacksTest < TransitionCollectionWithActionHelperBaseTest
1207
+ class TransitionCollectionWithActionHookAndSkippedAfterCallbacksTest < TransitionCollectionWithActionHookBaseTest
1204
1208
  def setup
1205
1209
  super
1206
1210
  @result = StateMachine::TransitionCollection.new([@transition], :after => false).perform
@@ -1235,7 +1239,7 @@ class TransitionCollectionWithActionHelperAndSkippedAfterCallbacksTest < Transit
1235
1239
  end
1236
1240
  end
1237
1241
 
1238
- class TransitionCollectionWithActionHelperAndBlockTest < TransitionCollectionWithActionHelperBaseTest
1242
+ class TransitionCollectionWithActionHookAndBlockTest < TransitionCollectionWithActionHookBaseTest
1239
1243
  def setup
1240
1244
  super
1241
1245
  @result = StateMachine::TransitionCollection.new([@transition]).perform { true }
@@ -1250,7 +1254,7 @@ class TransitionCollectionWithActionHelperAndBlockTest < TransitionCollectionWit
1250
1254
  end
1251
1255
  end
1252
1256
 
1253
- class TransitionCollectionWithActionHelperInvalidTest < TransitionCollectionWithActionHelperBaseTest
1257
+ class TransitionCollectionWithActionHookInvalidTest < TransitionCollectionWithActionHookBaseTest
1254
1258
  def setup
1255
1259
  super
1256
1260
  @result = StateMachine::TransitionCollection.new([@transition, nil]).perform
@@ -1265,15 +1269,15 @@ class TransitionCollectionWithActionHelperInvalidTest < TransitionCollectionWith
1265
1269
  end
1266
1270
  end
1267
1271
 
1268
- class TransitionCollectionWithActionHelperWithNilActionTest < TransitionCollectionWithActionHelperBaseTest
1272
+ class TransitionCollectionWithActionHookWithNilActionTest < TransitionCollectionWithActionHookBaseTest
1269
1273
  def setup
1270
1274
  super
1271
1275
 
1272
- @machine = StateMachine::Machine.new(@klass, :status, :initial => :parked)
1273
- @machine.state :idling
1274
- @machine.event :ignite
1276
+ @machine = StateMachine::Machine.new(@klass, :status, :initial => :first_gear)
1277
+ @machine.state :second_gear
1278
+ @machine.event :shift_up
1275
1279
 
1276
- @result = StateMachine::TransitionCollection.new([@transition, StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)]).perform
1280
+ @result = StateMachine::TransitionCollection.new([@transition, StateMachine::Transition.new(@object, @machine, :shift_up, :first_gear, :second_gear)]).perform
1277
1281
  end
1278
1282
 
1279
1283
  def test_should_succeed
@@ -1305,7 +1309,7 @@ class TransitionCollectionWithActionHelperWithNilActionTest < TransitionCollecti
1305
1309
  end
1306
1310
  end
1307
1311
 
1308
- class TransitionCollectionWithActionHelperWithDifferentActionsTest < TransitionCollectionWithActionHelperBaseTest
1312
+ class TransitionCollectionWithActionHookWithDifferentActionsTest < TransitionCollectionWithActionHookBaseTest
1309
1313
  def setup
1310
1314
  super
1311
1315
 
@@ -1315,11 +1319,11 @@ class TransitionCollectionWithActionHelperWithDifferentActionsTest < TransitionC
1315
1319
  end
1316
1320
  end
1317
1321
 
1318
- @machine = StateMachine::Machine.new(@klass, :status, :initial => :parked, :action => :save_status)
1319
- @machine.state :idling
1320
- @machine.event :ignite
1322
+ @machine = StateMachine::Machine.new(@klass, :status, :initial => :first_gear, :action => :save_status)
1323
+ @machine.state :second_gear
1324
+ @machine.event :shift_up
1321
1325
 
1322
- @result = StateMachine::TransitionCollection.new([@transition, StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)]).perform
1326
+ @result = StateMachine::TransitionCollection.new([@transition, StateMachine::Transition.new(@object, @machine, :shift_up, :first_gear, :second_gear)]).perform
1323
1327
  end
1324
1328
 
1325
1329
  def test_should_succeed
@@ -1351,7 +1355,7 @@ class TransitionCollectionWithActionHelperWithDifferentActionsTest < TransitionC
1351
1355
  end
1352
1356
  end
1353
1357
 
1354
- class TransitionCollectionWithActionHelperTest < TransitionCollectionWithActionHelperBaseTest
1358
+ class TransitionCollectionWithActionHookTest < TransitionCollectionWithActionHookBaseTest
1355
1359
  def setup
1356
1360
  super
1357
1361
  @result = StateMachine::TransitionCollection.new([@transition]).perform
@@ -1394,7 +1398,7 @@ class TransitionCollectionWithActionHelperTest < TransitionCollectionWithActionH
1394
1398
  end
1395
1399
  end
1396
1400
 
1397
- class TransitionCollectionWithActionHelperMultipleTest < TransitionCollectionWithActionHelperBaseTest
1401
+ class TransitionCollectionWithActionHookMultipleTest < TransitionCollectionWithActionHookBaseTest
1398
1402
  def setup
1399
1403
  super
1400
1404
 
@@ -1469,7 +1473,7 @@ class TransitionCollectionWithActionHelperMultipleTest < TransitionCollectionWit
1469
1473
  end
1470
1474
  end
1471
1475
 
1472
- class TransitionCollectionWithActionHelperErrorTest < TransitionCollectionWithActionHelperBaseTest
1476
+ class TransitionCollectionWithActionHookErrorTest < TransitionCollectionWithActionHookBaseTest
1473
1477
  def setup
1474
1478
  super
1475
1479