state_machine 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -69,6 +69,38 @@ class MachineCollectionStateInitializationTest < Test::Unit::TestCase
69
69
  assert_nil @object.state
70
70
  assert_equal 'active', @object.alarm_state
71
71
  end
72
+
73
+ def test_should_not_set_states_if_ignored
74
+ @machines.initialize_states(@object, :ignore => [:state, :alarm_state])
75
+
76
+ assert_nil @object.state
77
+ assert_nil @object.alarm_state
78
+ end
79
+
80
+ def test_should_set_states_if_not_ignored_and_nil
81
+ @machines.initialize_states(@object, :ignore => [])
82
+
83
+ assert_equal 'parked', @object.state
84
+ assert_equal 'active', @object.alarm_state
85
+ end
86
+
87
+ def test_should_set_states_if_not_ignored_and_empty
88
+ @object.state = ''
89
+ @object.alarm_state = ''
90
+ @machines.initialize_states(@object, :ignore => [])
91
+
92
+ assert_equal 'parked', @object.state
93
+ assert_equal 'active', @object.alarm_state
94
+ end
95
+
96
+ def test_should_set_states_if_not_ignored_and_not_empty
97
+ @object.state = 'idling'
98
+ @object.alarm_state = 'inactive'
99
+ @machines.initialize_states(@object, :ignore => [])
100
+
101
+ assert_equal 'parked', @object.state
102
+ assert_equal 'active', @object.alarm_state
103
+ end
72
104
  end
73
105
 
74
106
  class MachineCollectionFireExplicitTest < Test::Unit::TestCase
@@ -293,6 +325,10 @@ class MachineCollectionFireImplicitWithoutEventTest < MachineCollectionFireImpli
293
325
  def test_should_not_change_event_attribute
294
326
  assert_nil @object.state_event
295
327
  end
328
+
329
+ def test_should_not_have_event_transition
330
+ assert_nil @object.send(:state_event_transition)
331
+ end
296
332
  end
297
333
 
298
334
  class MachineCollectionFireImplicitWithBlankEventTest < MachineCollectionFireImplicitTest
@@ -318,6 +354,10 @@ class MachineCollectionFireImplicitWithBlankEventTest < MachineCollectionFireImp
318
354
  def test_should_not_change_event_attribute
319
355
  assert_nil @object.state_event
320
356
  end
357
+
358
+ def test_should_not_have_event_transition
359
+ assert_nil @object.send(:state_event_transition)
360
+ end
321
361
  end
322
362
 
323
363
  class MachineCollectionFireImplicitWithInvalidEventTest < MachineCollectionFireImplicitTest
@@ -343,6 +383,10 @@ class MachineCollectionFireImplicitWithInvalidEventTest < MachineCollectionFireI
343
383
  def test_should_not_reset_event_attribute
344
384
  assert_equal :invalid, @object.state_event
345
385
  end
386
+
387
+ def test_should_not_have_event_transition
388
+ assert_nil @object.send(:state_event_transition)
389
+ end
346
390
  end
347
391
 
348
392
  class MachineCollectionFireImplicitWithoutTransitionTest < MachineCollectionFireImplicitTest
@@ -369,6 +413,10 @@ class MachineCollectionFireImplicitWithoutTransitionTest < MachineCollectionFire
369
413
  def test_should_not_reset_event_attribute
370
414
  assert_equal :ignite, @object.state_event
371
415
  end
416
+
417
+ def test_should_not_have_event_transition
418
+ assert_nil @object.send(:state_event_transition)
419
+ end
372
420
  end
373
421
 
374
422
  class MachineCollectionFireImplicitWithTransitionTest < MachineCollectionFireImplicitTest
@@ -404,6 +452,10 @@ class MachineCollectionFireImplicitWithTransitionTest < MachineCollectionFireImp
404
452
  assert_nil @object.state_event
405
453
  end
406
454
 
455
+ def test_should_not_have_event_transition
456
+ assert_nil @object.send(:state_event_transition)
457
+ end
458
+
407
459
  def test_should_not_be_successful_if_fired_again
408
460
  @object.state_event = 'ignite'
409
461
  assert !@machines.fire_event_attributes(@object, :save) { true }
@@ -455,6 +507,10 @@ class MachineCollectionFireImplicitWithActionFailureTest < MachineCollectionFire
455
507
  def test_should_not_reset_event_attribute
456
508
  assert_equal :ignite, @object.state_event
457
509
  end
510
+
511
+ def test_should_not_have_event_transition
512
+ assert_nil @object.send(:state_event_transition)
513
+ end
458
514
  end
459
515
 
460
516
  class MachineCollectionFireImplicitWithActionErrorTest < MachineCollectionFireImplicitTest
@@ -472,34 +528,27 @@ class MachineCollectionFireImplicitWithActionErrorTest < MachineCollectionFireIm
472
528
  def test_should_not_reset_event_attribute
473
529
  assert_equal :ignite, @object.state_event
474
530
  end
531
+
532
+ def test_should_not_have_event_transition
533
+ assert_nil @object.send(:state_event_transition)
534
+ end
475
535
  end
476
536
 
477
537
  class MachineCollectionFireImplicitPartialTest < MachineCollectionFireImplicitTest
478
538
  def setup
479
539
  super
480
540
 
481
- @ran_before_callback = false
482
- @ran_after_callback = false
483
- @machine.before_transition { @ran_before_callback = true }
484
- @machine.after_transition { @ran_after_callback = true }
485
-
486
541
  @state_event = nil
542
+ @state_event_transition = nil
487
543
 
488
544
  @object.state_event = 'ignite'
489
545
  @result = @machines.fire_event_attributes(@object, :save, false) do
490
546
  @state_event = @object.state_event
547
+ @state_event_transition = @object.send(:state_event_transition)
491
548
  true
492
549
  end
493
550
  end
494
551
 
495
- def test_should_run_before_callbacks
496
- assert @ran_before_callback
497
- end
498
-
499
- def test_should_not_run_after_callbacks
500
- assert !@ran_after_callback
501
- end
502
-
503
552
  def test_should_be_successful
504
553
  assert @result
505
554
  end
@@ -508,20 +557,33 @@ class MachineCollectionFireImplicitPartialTest < MachineCollectionFireImplicitTe
508
557
  assert_nil @state_event
509
558
  end
510
559
 
560
+ def test_should_not_have_event_transition_while_running_action
561
+ assert_nil @state_event_transition
562
+ end
563
+
511
564
  def test_should_transition_state
512
565
  assert_equal 'idling', @object.state
513
566
  end
514
567
 
515
- def test_should_not_reset_event_attribute
516
- assert_equal :ignite, @object.state_event
568
+ def test_should_reset_event_attribute
569
+ assert_nil @object.state_event
517
570
  end
518
571
 
519
- def test_should_reset_event_attributes_after_next_fire_on_success
572
+ def test_should_have_event_transition
573
+ assert_not_nil @object.send(:state_event_transition)
574
+ end
575
+
576
+ def test_should_reset_event_after_next_fire_on_success
520
577
  assert @machines.fire_event_attributes(@object, :save) { true }
521
578
  assert_equal 'idling', @object.state
522
579
  assert_nil @object.state_event
523
580
  end
524
581
 
582
+ def test_should_reset_event_transition_after_next_fire_on_success
583
+ assert @machines.fire_event_attributes(@object, :save) { true }
584
+ assert_nil @object.send(:state_event_transition)
585
+ end
586
+
525
587
  def test_should_guard_transition_after_next_fire_on_success
526
588
  @machines.fire_event_attributes(@object, :save) { true }
527
589
 
@@ -534,6 +596,7 @@ class MachineCollectionFireImplicitPartialTest < MachineCollectionFireImplicitTe
534
596
  assert !@machines.fire_event_attributes(@object, :save) { false }
535
597
  assert_equal 'parked', @object.state
536
598
  assert_equal :ignite, @object.state_event
599
+ assert_nil @object.send(:state_event_transition)
537
600
 
538
601
  @object.state = 'idling'
539
602
  assert !@machines.fire_event_attributes(@object, :save) { false }
@@ -550,6 +613,7 @@ class MachineCollectionFireImplicitPartialTest < MachineCollectionFireImplicitTe
550
613
  assert_raise(ArgumentError) { @machines.fire_event_attributes(@object, :save) { raise ArgumentError } }
551
614
  assert_equal 'parked', @object.state
552
615
  assert_equal :ignite, @object.state_event
616
+ assert_nil @object.send(:state_event_transition)
553
617
  end
554
618
 
555
619
  def test_should_guard_transition_after_next_fire_on_error
@@ -563,6 +627,62 @@ class MachineCollectionFireImplicitPartialTest < MachineCollectionFireImplicitTe
563
627
  end
564
628
  end
565
629
 
630
+ class MachineCollectionFireImplicitPartialWithCallbacksTest < MachineCollectionFireImplicitTest
631
+ def setup
632
+ super
633
+
634
+ @object.state_event = 'ignite'
635
+ end
636
+
637
+ def test_should_run_before_callbacks
638
+ ran_callback = false
639
+ @machine.before_transition { ran_callback = true }
640
+ @machines.fire_event_attributes(@object, :save, false) { true }
641
+
642
+ assert ran_callback
643
+ end
644
+
645
+ def test_should_not_have_event_during_before_callbacks
646
+ state_event = nil
647
+ @machine.before_transition {|object, transition| state_event = object.state_event }
648
+ @machines.fire_event_attributes(@object, :save, false) { true }
649
+
650
+ assert_nil state_event
651
+ end
652
+
653
+ def test_should_not_have_event_transition_during_before_callbacks
654
+ state_event_transition = nil
655
+ @machine.before_transition {|object, transition| state_event_transition = object.send(:state_event_transition) }
656
+ @machines.fire_event_attributes(@object, :save, false) { true }
657
+
658
+ assert_nil state_event_transition
659
+ end
660
+
661
+ def test_should_not_run_after_callbacks
662
+ ran_callback = false
663
+ @machine.after_transition { ran_callback = true }
664
+ @machines.fire_event_attributes(@object, :save, false) { true }
665
+
666
+ assert !ran_callback
667
+ end
668
+
669
+ def test_should_not_have_event_during_after_callbacks
670
+ state_event = nil
671
+ @machine.after_transition {|object, transition| state_event = object.state_event }
672
+ @machines.fire_event_attributes(@object, :save, false) { true }
673
+
674
+ assert_nil state_event
675
+ end
676
+
677
+ def test_should_not_have_event_transition_during_after_callbacks
678
+ state_event_transition = nil
679
+ @machine.after_transition {|object, transition| state_event_transition = object.send(:state_event_transition) }
680
+ @machines.fire_event_attributes(@object, :save, false) { true }
681
+
682
+ assert_nil state_event_transition
683
+ end
684
+ end
685
+
566
686
  class MachineCollectionFireImplicitNestedPartialTest < MachineCollectionFireImplicitTest
567
687
  def setup
568
688
  super
@@ -591,6 +711,10 @@ class MachineCollectionFireImplicitNestedPartialTest < MachineCollectionFireImpl
591
711
  def test_should_reset_event_attribute
592
712
  assert_nil @object.state_event
593
713
  end
714
+
715
+ def test_should_reset_event_transition_attribute
716
+ assert_nil @object.send(:state_event_transition)
717
+ end
594
718
  end
595
719
 
596
720
  class MachineCollectionFireImplicitWithDifferentActionsTest < MachineCollectionFireImplicitTest
@@ -614,10 +738,14 @@ class MachineCollectionFireImplicitWithDifferentActionsTest < MachineCollectionF
614
738
  assert_equal 'idling', @object.state
615
739
  end
616
740
 
617
- def test_should_reset_event_attributes_for_action
741
+ def test_should_reset_event_attribute_for_action
618
742
  assert_nil @object.state_event
619
743
  end
620
744
 
745
+ def test_should_reset_event_transition_attribute_for_action
746
+ assert_nil @object.send(:state_event_transition)
747
+ end
748
+
621
749
  def test_should_not_transition_states_for_other_actions
622
750
  assert_equal 'active', @object.alarm_state
623
751
  end
@@ -723,11 +851,88 @@ class MachineCollectionFireImplicitWithCustomMachineNameTest < MachineCollection
723
851
  assert @machines.fire_event_attributes(@object, :save) { true }
724
852
  assert_equal 'idling', @object.state
725
853
  assert_nil @object.state_event
854
+ assert_nil @object.send(:state_event_transition)
726
855
  end
727
856
 
728
857
  def test_should_be_successful_on_partial_fire
729
858
  @machines.fire_event_attributes(@object, :save, false) { true }
730
859
  assert_equal 'idling', @object.state
731
- assert_equal :ignite, @object.state_event
860
+ assert_nil @object.state_event
861
+ assert_not_nil @object.send(:state_event_transition)
862
+ end
863
+ end
864
+
865
+ class MachineFireImplicitWithMarshallingTest < MachineCollectionFireImplicitTest
866
+ def setup
867
+ super
868
+ self.class.const_set('Example', @klass)
869
+
870
+ @object.state_event = 'ignite'
871
+ end
872
+
873
+ def test_should_marshal_during_before_callbacks
874
+ @machine.before_transition {|object, transition| Marshal.dump(object)}
875
+ assert_nothing_raised { @machines.fire_event_attributes(@object, :save) { true } }
876
+ end
877
+
878
+ def test_should_marshal_during_action
879
+ assert_nothing_raised do
880
+ @machines.fire_event_attributes(@object, :save) do
881
+ Marshal.dump(@object)
882
+ true
883
+ end
884
+ end
885
+ end
886
+
887
+ def test_should_marshal_during_after_callbacks
888
+ @machine.after_transition {|object, transition| Marshal.dump(object)}
889
+ assert_nothing_raised { @machines.fire_event_attributes(@object, :save) { true } }
890
+ end
891
+
892
+ def teardown
893
+ self.class.send(:remove_const, 'Example')
894
+ end
895
+ end
896
+
897
+ class MachineFireImplicitPartialWithMarshallingTest < MachineCollectionFireImplicitTest
898
+ def setup
899
+ super
900
+ self.class.const_set('Example', @klass)
901
+
902
+ @object.state_event = 'ignite'
903
+ end
904
+
905
+ def test_should_marshal_during_before_callbacks
906
+ @machine.before_transition {|object, transition| Marshal.dump(object)}
907
+ assert_nothing_raised do
908
+ @machines.fire_event_attributes(@object, :save, false) { true }
909
+ @machines.fire_event_attributes(@object, :save) { true }
910
+ end
911
+ end
912
+
913
+ def test_should_marshal_during_action
914
+ assert_nothing_raised do
915
+ @machines.fire_event_attributes(@object, :save, false) do
916
+ Marshal.dump(@object)
917
+ true
918
+ end
919
+
920
+ @machines.fire_event_attributes(@object, :save) do
921
+ Marshal.dump(@object)
922
+ true
923
+ end
924
+ end
925
+ end
926
+
927
+ def test_should_marshal_during_after_callbacks
928
+ @machine.after_transition {|object, transition| Marshal.dump(object)}
929
+ assert_nothing_raised do
930
+ @machines.fire_event_attributes(@object, :save, false) { true }
931
+ @machines.fire_event_attributes(@object, :save) { true }
932
+ end
933
+ end
934
+
935
+ def teardown
936
+ self.class.send(:remove_const, 'Example')
732
937
  end
733
938
  end
@@ -1853,34 +1853,37 @@ begin
1853
1853
  end
1854
1854
 
1855
1855
  def test_should_save_file_with_class_name_by_default
1856
- graph = @machine.draw(:output => false)
1857
- assert_equal './Vehicle_state.png', graph.instance_variable_get('@filename')
1856
+ graph = @machine.draw
1857
+ assert File.exists?('./Vehicle_state.png')
1858
1858
  end
1859
1859
 
1860
1860
  def test_should_allow_base_name_to_be_customized
1861
- graph = @machine.draw(:name => 'machine', :output => false)
1862
- assert_equal './machine.png', graph.instance_variable_get('@filename')
1861
+ graph = @machine.draw(:name => 'machine')
1862
+ assert File.exists?('./machine.png')
1863
1863
  end
1864
1864
 
1865
1865
  def test_should_allow_format_to_be_customized
1866
- graph = @machine.draw(:format => 'jpg', :output => false)
1867
- assert_equal './Vehicle_state.jpg', graph.instance_variable_get('@filename')
1868
- assert_equal 'jpg', graph.instance_variable_get('@format')
1866
+ graph = @machine.draw(:format => 'jpg')
1867
+ assert File.exists?('./Vehicle_state.jpg')
1869
1868
  end
1870
1869
 
1871
1870
  def test_should_allow_path_to_be_customized
1872
- graph = @machine.draw(:path => "#{File.dirname(__FILE__)}/", :output => false)
1873
- assert_equal "#{File.dirname(__FILE__)}/Vehicle_state.png", graph.instance_variable_get('@filename')
1871
+ graph = @machine.draw(:path => "#{File.dirname(__FILE__)}/")
1872
+ assert File.exists?("#{File.dirname(__FILE__)}/Vehicle_state.png")
1874
1873
  end
1875
1874
 
1876
1875
  def test_should_allow_orientation_to_be_landscape
1877
- graph = @machine.draw(:orientation => 'landscape', :output => false)
1878
- assert_equal 'LR', graph['rankdir']
1876
+ graph = @machine.draw(:orientation => 'landscape')
1877
+ assert_equal 'LR', graph['rankdir'].to_s.gsub('"', '')
1879
1878
  end
1880
1879
 
1881
1880
  def test_should_allow_orientation_to_be_portrait
1882
- graph = @machine.draw(:orientation => 'portrait', :output => false)
1883
- assert_equal 'TB', graph['rankdir']
1881
+ graph = @machine.draw(:orientation => 'portrait')
1882
+ assert_equal 'TB', graph['rankdir'].to_s.gsub('"', '')
1883
+ end
1884
+
1885
+ def teardown
1886
+ FileUtils.rm Dir["{.,#{File.dirname(__FILE__)}}/*.{png,jpg}"]
1884
1887
  end
1885
1888
  end
1886
1889
 
@@ -626,9 +626,8 @@ class StateWithInvalidMethodCallTest < Test::Unit::TestCase
626
626
  @object = @klass.new
627
627
  end
628
628
 
629
- def test_should_raise_an_exception
630
- exception = assert_raise(NoMethodError) { @state.call(@object, :invalid) }
631
- assert_equal "undefined method 'invalid' for #{@object} with idling state", exception.message
629
+ def test_should_call_method_missing_arg
630
+ assert_equal 1, @state.call(@object, :invalid, lambda {1})
632
631
  end
633
632
  end
634
633
 
@@ -648,11 +647,11 @@ class StateWithValidMethodCallTest < Test::Unit::TestCase
648
647
  end
649
648
 
650
649
  def test_should_not_raise_an_exception
651
- assert_nothing_raised { @state.call(@object, :speed) }
650
+ assert_nothing_raised { @state.call(@object, :speed, lambda {raise}) }
652
651
  end
653
652
 
654
653
  def test_should_pass_arguments_through
655
- assert_equal 1, @state.call(@object, :speed, 1)
654
+ assert_equal 1, @state.call(@object, :speed, lambda {}, 1)
656
655
  end
657
656
 
658
657
  def test_should_pass_blocks_through
@@ -660,7 +659,7 @@ class StateWithValidMethodCallTest < Test::Unit::TestCase
660
659
  end
661
660
 
662
661
  def test_should_pass_both_arguments_and_blocks_through
663
- assert_equal [1, 2], @state.call(@object, :speed, 1) {2}
662
+ assert_equal [1, 2], @state.call(@object, :speed, lambda {}, 1) {2}
664
663
  end
665
664
  end
666
665
 
@@ -682,15 +681,15 @@ begin
682
681
  end
683
682
 
684
683
  def test_should_use_ellipse_shape
685
- assert_equal 'ellipse', @node['shape']
684
+ assert_equal 'ellipse', @node['shape'].to_s.gsub('"', '')
686
685
  end
687
686
 
688
687
  def test_should_set_width_to_one
689
- assert_equal '1', @node['width']
688
+ assert_equal '1', @node['width'].to_s.gsub('"', '')
690
689
  end
691
690
 
692
691
  def test_should_set_height_to_one
693
- assert_equal '1', @node['height']
692
+ assert_equal '1', @node['height'].to_s.gsub('"', '')
694
693
  end
695
694
 
696
695
  def test_should_use_stringified_name_as_name
@@ -698,7 +697,7 @@ begin
698
697
  end
699
698
 
700
699
  def test_should_use_description_as_label
701
- assert_equal 'parked (1)', @node['label']
700
+ assert_equal 'parked (1)', @node['label'].to_s.gsub('"', '')
702
701
  end
703
702
  end
704
703
 
@@ -715,7 +714,7 @@ begin
715
714
  end
716
715
 
717
716
  def test_should_use_ellipse_as_shape
718
- assert_equal 'ellipse', @node['shape']
717
+ assert_equal 'ellipse', @node['shape'].to_s.gsub('"', '')
719
718
  end
720
719
 
721
720
  def test_should_draw_edge_between_point_and_state
@@ -738,7 +737,7 @@ begin
738
737
  end
739
738
 
740
739
  def test_should_use_description_as_label
741
- assert_equal 'nil', @node['label']
740
+ assert_equal 'nil', @node['label'].to_s.gsub('"', '')
742
741
  end
743
742
  end
744
743
 
@@ -756,7 +755,7 @@ begin
756
755
  end
757
756
 
758
757
  def test_should_use_description_as_label
759
- assert_equal 'parked (*)', @node['label']
758
+ assert_equal 'parked (*)', @node['label'].to_s.gsub('"', '')
760
759
  end
761
760
  end
762
761
 
@@ -773,7 +772,7 @@ begin
773
772
  end
774
773
 
775
774
  def test_should_use_ellipse_as_shape
776
- assert_equal 'ellipse', @node['shape']
775
+ assert_equal 'ellipse', @node['shape'].to_s.gsub('"', '')
777
776
  end
778
777
  end
779
778
 
@@ -787,7 +786,7 @@ begin
787
786
  end
788
787
 
789
788
  def test_should_use_doublecircle_as_shape
790
- assert_equal 'doublecircle', @node['shape']
789
+ assert_equal 'doublecircle', @node['shape'].to_s.gsub('"', '')
791
790
  end
792
791
  end
793
792
  rescue LoadError