public
Description: An extendable, cloneable, dynamic UML 2 StateMachine for Ruby
Homepage: http://kurtstephens.com
Clone URL: git://github.com/kstephens/red_steak.git
Better tests for superstate exit/enter behavior.
kstephens (author)
Tue Dec 30 22:57:36 -0800 2008
commit  59bdbe3cf423936f5ffb6cf905422ceb9d4d585c
tree    19bf8998d2e36c4adb7351a44e17addef6cc3d6e
parent  e650828cfd442fe46480d0f4d9cc1fffbe37d1bc
...
56
57
58
59
 
60
61
62
...
56
57
58
 
59
60
61
62
0
@@ -56,7 +56,7 @@ module RedSteak
0
       # If some options are already set, merge them.
0
       if @options
0
         return @options if opts.empty?
0
-        @options.merge(_dup_opts opts)
0
+        @options.merge(_dup_opts(opts))
0
       else
0
         @options = _dup_opts opts
0
       end
...
118
119
120
121
122
123
124
125
 
 
 
 
 
 
 
 
126
127
128
...
118
119
120
 
 
 
 
 
121
122
123
124
125
126
127
128
129
130
131
0
@@ -118,11 +118,14 @@ module RedSteak
0
 
0
     # Returns an array of all ancestor states.
0
     def ancestors
0
-      x = [ self ]
0
-      if ss = superstate
0
-        x.push(*ss.ancestors)
0
-      end
0
-      x
0
+      @ancestors ||=
0
+        begin
0
+          x = [ self ]
0
+          if ss = superstate
0
+            x.push(*ss.ancestors)
0
+          end
0
+          x
0
+        end
0
     end
0
 
0
 
...
19
20
21
 
 
 
 
22
23
24
25
26
27
28
29
30
31
32
33
 
 
 
 
 
 
 
34
35
36
...
38
39
40
41
 
42
43
44
45
 
46
47
48
49
 
50
51
52
53
54
...
56
57
58
59
 
60
61
62
...
64
65
66
67
 
 
68
69
70
...
72
73
74
75
 
 
76
77
78
...
80
81
82
83
 
 
84
85
86
...
296
297
298
299
300
301
302
303
 
 
 
 
 
304
305
306
...
308
309
310
311
 
312
313
314
...
317
318
319
320
321
322
 
 
 
323
324
325
326
 
 
 
327
328
329
...
504
505
506
507
508
 
 
509
510
511
...
596
597
598
599
600
 
 
601
 
 
602
 
 
 
 
603
 
 
 
 
604
 
 
 
 
605
 
 
 
 
606
 
 
607
608
609
...
19
20
21
22
23
24
25
26
27
28
29
 
 
 
 
 
 
30
31
32
33
34
35
36
37
38
39
40
41
...
43
44
45
 
46
47
48
49
50
51
52
 
 
 
53
54
 
55
56
57
...
59
60
61
 
62
63
64
65
...
67
68
69
 
70
71
72
73
74
...
76
77
78
 
79
80
81
82
83
...
85
86
87
 
88
89
90
91
92
...
302
303
304
 
 
 
 
 
305
306
307
308
309
310
311
312
...
314
315
316
 
317
318
319
320
...
323
324
325
 
 
 
326
327
328
329
 
 
 
330
331
332
333
334
335
...
510
511
512
 
 
513
514
515
516
517
...
602
603
604
 
 
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
0
@@ -19,18 +19,23 @@ describe RedSteak do
0
     # For debugging.
0
     attr_accessor :logger
0
 
0
+    def initialize
0
+      clear!
0
+    end
0
+
0
     def clear!
0
       @_machine = 
0
         @_args =
0
         @_transition =
0
-        @_guard =
0
-        @_effect =
0
-        @_state =
0
-        @_enter =
0
-        @_exit =
0
-        @_doActivity =
0
         @_a_to_b =
0
         nil
0
+
0
+      @_guard = [ ]
0
+      @_effect = [ ]
0
+      @_state = [ ]
0
+      @_enter = [ ]
0
+      @_exit = [ ] 
0
+      @_doActivity = [ ]
0
     end
0
 
0
 
0
@@ -38,17 +43,15 @@ describe RedSteak do
0
     def guard(machine, trans, *args)
0
       @_machine = machine
0
       @_transition = trans
0
-      @_guard = @_args = args
0
+      @_guard << (@_args = args)
0
       _log
0
       nil # Ok
0
     end
0
 
0
+    # Special Guard.
0
     def a_to_b?(machine, trans, *args)
0
-      @_machine = machine
0
-      @_transition = trans
0
-      @_guard = @_args = args
0
+      guard(machine, trans, *args)
0
       @_a_to_b = args
0
-      _log
0
       true
0
     end
0
 
0
@@ -56,7 +59,7 @@ describe RedSteak do
0
     def effect(machine, trans, *args)
0
       @_machine = machine
0
       @_transition = trans
0
-      @_effect = @_args = args
0
+      @_effect << (@_args = args)
0
       _log
0
     end
0
 
0
@@ -64,7 +67,8 @@ describe RedSteak do
0
     def enter(machine, state, *args)
0
       @_machine = machine
0
       @_state = state
0
-      @_enter = @_args = args
0
+      @_args = args
0
+      @_enter << [ state.to_s, *args ]
0
       _log
0
     end
0
 
0
@@ -72,7 +76,8 @@ describe RedSteak do
0
     def exit(machine, state, *args)
0
       @_machine = machine
0
       @_state = state
0
-      @_exit = @_args = args
0
+      @_args = args
0
+      @_exit << [ state.to_s, *args ]
0
       _log
0
     end
0
 
0
@@ -80,7 +85,8 @@ describe RedSteak do
0
     def doActivity(machine, state, *args)
0
       @_machine = machine
0
       @_state = state
0
-      @_doActivity = @_args = args
0
+      @_args = args
0
+      @_doActivity << [ state.to_s, *args ]
0
       _log
0
     end
0
 
0
@@ -296,11 +302,11 @@ describe RedSteak do
0
     c._machine.should == m
0
     c._state.should == m.stateMachine.states[:a]
0
     c._transition.should == nil
0
-    c._guard.should == nil
0
-    c._effect.should == nil
0
-    # c._enter.should == [ ]
0
-    c._exit.should == nil
0
-    c._doActivity.should == [ ]
0
+    c._guard.should == [ ]
0
+    c._effect.should == [ ]
0
+    c._enter.should == [ [ "a" ] ]
0
+    c._exit.should == [ ]
0
+    c._doActivity.should == [ [ "a" ] ]
0
     m.history.size.should == 1
0
 
0
     #################################
0
@@ -308,7 +314,7 @@ describe RedSteak do
0
     #
0
 
0
     c.clear!
0
-    m.transition! "a_to_b", :arg
0
+    m.transition! "a_to_b", :arg1
0
     m.at_start?.should == false
0
     m.at_end?.should == false
0
 
0
@@ -317,13 +323,13 @@ describe RedSteak do
0
 
0
     c._machine.should == m
0
     c._transition.name.should == :a_to_b
0
-    c._guard.should == [ :arg ]
0
-    c._a_to_b.should == [ :arg ]
0
-    c._effect.should == [ :arg ]
0
+    c._guard.should == [ [ :arg1 ] ]
0
+    c._a_to_b.should == [ :arg1 ]
0
+    c._effect.should == [ [ :arg1 ] ]
0
     c._state.name.should == :b
0
-    c._enter.should == [ :arg ]
0
-    c._exit.should == [ :arg ]
0
-    c._doActivity.should == [ :arg ]
0
+    c._enter.should == [ [ "b", :arg1 ] ]
0
+    c._exit.should == [ [ "a", :arg1 ] ]
0
+    c._doActivity.should == [ [ "b", :arg1 ] ]
0
     m.history.size.should == 2
0
 
0
     #################################
0
@@ -504,8 +510,8 @@ describe RedSteak do
0
     m.state.name.should == :a
0
     c._machine.should == m
0
     c._state.name.should == :a
0
-    c._enter.should == [ :foo, :bar ]
0
-    c._exit.should == nil
0
+    c._enter.should == [ [ "a", :foo, :bar ] ]
0
+    c._exit.should == [ ]
0
  
0
     render_graph m, :show_history => true
0
 
0
@@ -596,14 +602,34 @@ describe RedSteak do
0
     sm.state[:c].state.map{|s| s.to_s}.should == [ ]
0
     sm.state[:c].source.map{|s| s.to_s}.should == ["a::a", "a::b", "a::c", "b", "b::a"]
0
 
0
-    m = sm.machine
0
-    m.history = [ ]
0
+    m = machine_with_context(sm)
0
+    c = m.context
0
     m.logger = $stderr
0
+
0
+    c.clear!
0
     m.start!
0
+    c._exit.should == [ ]
0
+    c._enter.should == [ [ "a" ], [ "a::a" ] ]
0
+
0
+    c.clear!
0
     m.transition_to! "b"
0
+    c._exit.should == [["a::a"], ["a"]]
0
+    c._enter.should == [ [ "b" ], [ "b::a" ] ]
0
+
0
+    c.clear!
0
     m.transition_to! "b::b"
0
+    c._exit.should == [["b::a"]]
0
+    c._enter.should == [["b::b"]]
0
+
0
+    c.clear!
0
     m.transition_to! "a::b"
0
+    c._exit.should == [["b::b"], ["b"]]
0
+    c._enter.should == [["a"], ["a::b"]] 
0
+
0
+    c.clear!
0
     m.transition_to! "c"
0
+    c._exit.should == [["a::b"], ["a"]]
0
+    c._enter.should == [["c"]]
0
 
0
     render_graph m, :show_history => true
0
 

Comments