state_machine 0.7.6 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +17 -0
- data/Rakefile +1 -1
- data/lib/state_machine.rb +11 -8
- data/lib/state_machine/callback.rb +1 -1
- data/lib/state_machine/event.rb +9 -8
- data/lib/state_machine/event_collection.rb +21 -10
- data/lib/state_machine/extensions.rb +2 -11
- data/lib/state_machine/guard.rb +12 -1
- data/lib/state_machine/integrations/active_record.rb +37 -1
- data/lib/state_machine/integrations/data_mapper.rb +17 -2
- data/lib/state_machine/integrations/sequel.rb +32 -1
- data/lib/state_machine/machine.rb +56 -16
- data/lib/state_machine/machine_collection.rb +7 -5
- data/lib/state_machine/state.rb +1 -1
- data/lib/state_machine/transition.rb +41 -14
- data/test/unit/assertions_test.rb +3 -3
- data/test/unit/eval_helpers_test.rb +13 -22
- data/test/unit/event_collection_test.rb +24 -0
- data/test/unit/event_test.rb +94 -0
- data/test/unit/guard_test.rb +46 -0
- data/test/unit/integrations/active_record_test.rb +247 -18
- data/test/unit/integrations/data_mapper_test.rb +143 -1
- data/test/unit/integrations/sequel_test.rb +279 -10
- data/test/unit/machine_collection_test.rb +42 -19
- data/test/unit/machine_test.rb +55 -0
- data/test/unit/state_test.rb +1 -1
- data/test/unit/transition_test.rb +106 -7
- metadata +2 -2
data/test/unit/state_test.rb
CHANGED
@@ -628,7 +628,7 @@ class StateWithInvalidMethodCallTest < Test::Unit::TestCase
|
|
628
628
|
|
629
629
|
def test_should_raise_an_exception
|
630
630
|
exception = assert_raise(NoMethodError) { @state.call(@object, :invalid) }
|
631
|
-
assert_equal "undefined method 'invalid' for #{@object}
|
631
|
+
assert_equal "undefined method 'invalid' for #{@object} with idling state", exception.message
|
632
632
|
end
|
633
633
|
end
|
634
634
|
|
@@ -224,6 +224,27 @@ class TransitionWithCustomMachineAttributeTest < Test::Unit::TestCase
|
|
224
224
|
end
|
225
225
|
end
|
226
226
|
|
227
|
+
class TransitionWithoutReadingStateTest < Test::Unit::TestCase
|
228
|
+
def setup
|
229
|
+
@klass = Class.new
|
230
|
+
@machine = StateMachine::Machine.new(@klass)
|
231
|
+
@machine.state :parked, :idling
|
232
|
+
@machine.event :ignite
|
233
|
+
|
234
|
+
@object = @klass.new
|
235
|
+
@object.state = 'idling'
|
236
|
+
@transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling, false)
|
237
|
+
end
|
238
|
+
|
239
|
+
def test_should_not_read_from_value_from_object
|
240
|
+
assert_equal 'parked', @transition.from
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_should_have_to_value
|
244
|
+
assert_equal 'idling', @transition.to
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
227
248
|
class TransitionWithActionTest < Test::Unit::TestCase
|
228
249
|
def setup
|
229
250
|
@klass = Class.new do
|
@@ -276,6 +297,19 @@ class TransitionAfterBeingPersistedTest < Test::Unit::TestCase
|
|
276
297
|
assert_equal 'idling', @transition.to
|
277
298
|
end
|
278
299
|
|
300
|
+
def test_should_not_be_able_to_persist_twice
|
301
|
+
@object.state = 'parked'
|
302
|
+
@transition.persist
|
303
|
+
assert_equal 'parked', @object.state
|
304
|
+
end
|
305
|
+
|
306
|
+
def test_should_be_able_to_persist_again_after_resetting
|
307
|
+
@object.state = 'parked'
|
308
|
+
@transition.reset
|
309
|
+
@transition.persist
|
310
|
+
assert_equal 'idling', @object.state
|
311
|
+
end
|
312
|
+
|
279
313
|
def test_should_revert_to_from_state_on_rollback
|
280
314
|
@transition.rollback
|
281
315
|
assert_equal 'parked', @object.state
|
@@ -388,6 +422,23 @@ class TransitionWithCallbacksTest < Test::Unit::TestCase
|
|
388
422
|
assert_equal 'parked', @state
|
389
423
|
end
|
390
424
|
|
425
|
+
def test_should_not_be_able_to_run_before_callbacks_twice
|
426
|
+
@count = 0
|
427
|
+
@machine.before_transition(lambda {@count += 1})
|
428
|
+
@transition.before
|
429
|
+
@transition.before
|
430
|
+
assert_equal 1, @count
|
431
|
+
end
|
432
|
+
|
433
|
+
def test_should_be_able_to_run_before_callbacks_again_after_resetting
|
434
|
+
@count = 0
|
435
|
+
@machine.before_transition(lambda {@count += 1})
|
436
|
+
@transition.before
|
437
|
+
@transition.reset
|
438
|
+
@transition.before
|
439
|
+
assert_equal 2, @count
|
440
|
+
end
|
441
|
+
|
391
442
|
def test_should_run_after_callbacks_on_after
|
392
443
|
@machine.after_transition(lambda {|object| @run = true})
|
393
444
|
result = @transition.after(true)
|
@@ -426,6 +477,12 @@ class TransitionWithCallbacksTest < Test::Unit::TestCase
|
|
426
477
|
assert_equal 1, @count
|
427
478
|
end
|
428
479
|
|
480
|
+
def test_should_not_run_after_callbacks_if_not_successful
|
481
|
+
@machine.after_transition(lambda {|object| @run = true})
|
482
|
+
@transition.after(nil, false)
|
483
|
+
assert !@run
|
484
|
+
end
|
485
|
+
|
429
486
|
def test_should_pass_transition_to_after_callbacks
|
430
487
|
@machine.after_transition(lambda {|*args| @args = args})
|
431
488
|
|
@@ -452,6 +509,23 @@ class TransitionWithCallbacksTest < Test::Unit::TestCase
|
|
452
509
|
|
453
510
|
assert_equal 'idling', @state
|
454
511
|
end
|
512
|
+
|
513
|
+
def test_should_not_be_able_to_run_after_callbacks_twice
|
514
|
+
@count = 0
|
515
|
+
@machine.after_transition(lambda {@count += 1})
|
516
|
+
@transition.after
|
517
|
+
@transition.after
|
518
|
+
assert_equal 1, @count
|
519
|
+
end
|
520
|
+
|
521
|
+
def test_should_be_able_to_run_after_callbacks_again_after_resetting
|
522
|
+
@count = 0
|
523
|
+
@machine.after_transition(lambda {@count += 1})
|
524
|
+
@transition.after
|
525
|
+
@transition.reset
|
526
|
+
@transition.after
|
527
|
+
assert_equal 2, @count
|
528
|
+
end
|
455
529
|
end
|
456
530
|
|
457
531
|
class TransitionAfterBeingPerformedTest < Test::Unit::TestCase
|
@@ -547,6 +621,7 @@ class TransitionWithoutRunningActionTest < Test::Unit::TestCase
|
|
547
621
|
@machine = StateMachine::Machine.new(@klass, :action => :save)
|
548
622
|
@machine.state :parked, :idling
|
549
623
|
@machine.event :ignite
|
624
|
+
@machine.after_transition(lambda {|object| @run_after = true})
|
550
625
|
|
551
626
|
@object = @klass.new
|
552
627
|
@object.state = 'parked'
|
@@ -573,6 +648,10 @@ class TransitionWithoutRunningActionTest < Test::Unit::TestCase
|
|
573
648
|
def test_should_not_run_the_action
|
574
649
|
assert !@object.saved
|
575
650
|
end
|
651
|
+
|
652
|
+
def test_should_run_after_callbacks
|
653
|
+
assert @run_after
|
654
|
+
end
|
576
655
|
end
|
577
656
|
|
578
657
|
class TransitionWithTransactionsTest < Test::Unit::TestCase
|
@@ -669,7 +748,7 @@ class TransitionHaltedDuringBeforeCallbacksTest < Test::Unit::TestCase
|
|
669
748
|
end
|
670
749
|
|
671
750
|
def test_should_not_be_successful
|
672
|
-
|
751
|
+
assert_equal false, @result
|
673
752
|
end
|
674
753
|
|
675
754
|
def test_should_not_change_current_state
|
@@ -726,7 +805,7 @@ class TransitionHaltedAfterCallbackTest < Test::Unit::TestCase
|
|
726
805
|
end
|
727
806
|
|
728
807
|
def test_should_be_successful
|
729
|
-
|
808
|
+
assert_equal true, @result
|
730
809
|
end
|
731
810
|
|
732
811
|
def test_should_change_current_state
|
@@ -770,31 +849,45 @@ class TransitionWithActionFailedTest < Test::Unit::TestCase
|
|
770
849
|
|
771
850
|
@machine.before_transition lambda {@before_count += 1}
|
772
851
|
@machine.after_transition lambda {@after_count += 1}
|
852
|
+
@machine.after_transition lambda {@after_count += 1}, :include_failures => true
|
773
853
|
|
774
854
|
@object = @klass.new
|
775
855
|
@transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
|
776
|
-
@result = @transition.perform
|
777
856
|
end
|
778
857
|
|
779
858
|
def test_should_not_be_successful
|
780
|
-
|
859
|
+
assert_equal false, @transition.perform
|
781
860
|
end
|
782
861
|
|
783
862
|
def test_should_not_change_current_state
|
863
|
+
@transition.perform
|
784
864
|
assert_nil @object.state
|
785
865
|
end
|
786
866
|
|
787
867
|
def test_should_run_before_callbacks
|
868
|
+
@transition.perform
|
788
869
|
assert_equal 1, @before_count
|
789
870
|
end
|
790
871
|
|
791
|
-
def
|
872
|
+
def test_should_only_run_after_callbacks_that_include_failures
|
873
|
+
@transition.perform
|
792
874
|
assert_equal 1, @after_count
|
793
875
|
end
|
794
876
|
|
795
877
|
def test_should_cancel_the_transaction
|
878
|
+
@transition.perform
|
796
879
|
assert @klass.cancelled_transaction
|
797
880
|
end
|
881
|
+
|
882
|
+
def test_should_interpret_nil_as_failure
|
883
|
+
@klass.class_eval do
|
884
|
+
def save
|
885
|
+
nil
|
886
|
+
end
|
887
|
+
end
|
888
|
+
|
889
|
+
assert_equal false, @transition.perform
|
890
|
+
end
|
798
891
|
end
|
799
892
|
|
800
893
|
class TransitionWithActionErrorTest < Test::Unit::TestCase
|
@@ -1094,13 +1187,19 @@ class TransitionsWithPerformBlockTest < Test::Unit::TestCase
|
|
1094
1187
|
end
|
1095
1188
|
|
1096
1189
|
def test_should_be_perform_if_result_is_not_false
|
1097
|
-
|
1190
|
+
assert_equal true, StateMachine::Transition.perform([@state_transition, @status_transition]) { true }
|
1098
1191
|
assert_equal 'idling', @object.state
|
1099
1192
|
assert_equal 'second_gear', @object.status
|
1100
1193
|
end
|
1101
1194
|
|
1102
1195
|
def test_should_not_perform_if_result_is_false
|
1103
|
-
|
1196
|
+
assert_equal false, StateMachine::Transition.perform([@state_transition, @status_transition]) { false }
|
1197
|
+
assert_equal 'parked', @object.state
|
1198
|
+
assert_equal 'first_gear', @object.status
|
1199
|
+
end
|
1200
|
+
|
1201
|
+
def test_should_not_perform_if_result_is_nil
|
1202
|
+
assert_equal false, StateMachine::Transition.perform([@state_transition, @status_transition]) { nil }
|
1104
1203
|
assert_equal 'parked', @object.state
|
1105
1204
|
assert_equal 'first_gear', @object.status
|
1106
1205
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: state_machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Pfeifer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-08-15 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|