state_machine 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/CHANGELOG.rdoc +17 -0
  2. data/LICENSE +1 -1
  3. data/README.rdoc +54 -84
  4. data/Rakefile +1 -1
  5. data/examples/Car_state.png +0 -0
  6. data/examples/Vehicle_state.png +0 -0
  7. data/examples/auto_shop.rb +11 -0
  8. data/examples/car.rb +19 -0
  9. data/examples/traffic_light.rb +9 -0
  10. data/examples/vehicle.rb +35 -0
  11. data/lib/state_machine.rb +65 -52
  12. data/lib/state_machine/assertions.rb +1 -1
  13. data/lib/state_machine/callback.rb +13 -9
  14. data/lib/state_machine/eval_helpers.rb +4 -3
  15. data/lib/state_machine/event.rb +51 -33
  16. data/lib/state_machine/extensions.rb +2 -2
  17. data/lib/state_machine/guard.rb +47 -41
  18. data/lib/state_machine/integrations.rb +67 -0
  19. data/lib/state_machine/integrations/active_record.rb +62 -36
  20. data/lib/state_machine/integrations/active_record/observer.rb +41 -0
  21. data/lib/state_machine/integrations/data_mapper.rb +23 -37
  22. data/lib/state_machine/integrations/data_mapper/observer.rb +23 -9
  23. data/lib/state_machine/integrations/sequel.rb +23 -24
  24. data/lib/state_machine/machine.rb +380 -277
  25. data/lib/state_machine/node_collection.rb +142 -0
  26. data/lib/state_machine/state.rb +114 -69
  27. data/lib/state_machine/state_collection.rb +38 -0
  28. data/lib/state_machine/transition.rb +36 -17
  29. data/test/active_record.log +2940 -85664
  30. data/test/functional/state_machine_test.rb +49 -53
  31. data/test/sequel.log +747 -11990
  32. data/test/unit/assertions_test.rb +2 -1
  33. data/test/unit/callback_test.rb +14 -12
  34. data/test/unit/eval_helpers_test.rb +25 -6
  35. data/test/unit/event_test.rb +144 -124
  36. data/test/unit/guard_test.rb +118 -140
  37. data/test/unit/integrations/active_record_test.rb +102 -68
  38. data/test/unit/integrations/data_mapper_test.rb +48 -37
  39. data/test/unit/integrations/sequel_test.rb +34 -25
  40. data/test/unit/integrations_test.rb +42 -0
  41. data/test/unit/machine_test.rb +460 -531
  42. data/test/unit/node_collection_test.rb +208 -0
  43. data/test/unit/state_collection_test.rb +167 -0
  44. data/test/unit/state_machine_test.rb +1 -1
  45. data/test/unit/state_test.rb +223 -200
  46. data/test/unit/transition_test.rb +81 -46
  47. metadata +17 -3
  48. data/test/data_mapper.log +0 -30860
@@ -8,6 +8,7 @@ class AssertionsTest < Test::Unit::TestCase
8
8
  end
9
9
 
10
10
  def test_should_raise_exception_if_key_is_invalid
11
- assert_raise(ArgumentError) { assert_valid_keys({:name => 'foo', :value => 'bar', :invalid => true}, :name, :value, :force) }
11
+ exception = assert_raise(ArgumentError) { assert_valid_keys({:name => 'foo', :value => 'bar', :invalid => true}, :name, :value, :force) }
12
+ assert_match 'Invalid key(s): invalid', exception.message
12
13
  end
13
14
  end
@@ -2,7 +2,8 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class CallbackTest < Test::Unit::TestCase
4
4
  def test_should_raise_exception_if_do_option_not_specified
5
- assert_raise(ArgumentError) { StateMachine::Callback.new }
5
+ exception = assert_raise(ArgumentError) { StateMachine::Callback.new }
6
+ assert_match ':do callback must be specified', exception.message
6
7
  end
7
8
 
8
9
  def test_should_not_raise_exception_if_do_option_specified
@@ -10,7 +11,8 @@ class CallbackTest < Test::Unit::TestCase
10
11
  end
11
12
 
12
13
  def test_should_raise_exception_if_invalid_option_specified
13
- assert_raise(ArgumentError) { StateMachine::Callback.new(:do => :run, :invalid => true) }
14
+ exception = assert_raise(ArgumentError) { StateMachine::Callback.new(:do => :run, :invalid => true) }
15
+ assert_match 'Invalid key(s): invalid', exception.message
14
16
  end
15
17
 
16
18
  def test_should_not_bind_to_objects
@@ -57,7 +59,7 @@ end
57
59
  class CallbackWithRequirementsTest < Test::Unit::TestCase
58
60
  def setup
59
61
  @object = Object.new
60
- @callback = StateMachine::Callback.new(:from => 'off', :to => 'on', :on => 'turn_on', :do => lambda {true})
62
+ @callback = StateMachine::Callback.new(:from => :parked, :to => :idling, :on => :ignite, :do => lambda {true})
61
63
  end
62
64
 
63
65
  def test_should_call_with_empty_context
@@ -65,27 +67,27 @@ class CallbackWithRequirementsTest < Test::Unit::TestCase
65
67
  end
66
68
 
67
69
  def test_should_not_call_if_from_not_included
68
- assert !@callback.call(@object, :from => 'on')
70
+ assert !@callback.call(@object, :from => :idling)
69
71
  end
70
72
 
71
73
  def test_should_not_call_if_to_not_included
72
- assert !@callback.call(@object, :to => 'off')
74
+ assert !@callback.call(@object, :to => :parked)
73
75
  end
74
76
 
75
77
  def test_should_not_call_if_on_not_included
76
- assert !@callback.call(@object, :on => 'turn_off')
78
+ assert !@callback.call(@object, :on => :park)
77
79
  end
78
80
 
79
81
  def test_should_call_if_all_requirements_met
80
- assert @callback.call(@object, :from => 'off', :to => 'on', :on => 'turn_on')
82
+ assert @callback.call(@object, :from => :parked, :to => :idling, :on => :ignite)
81
83
  end
82
84
 
83
85
  def test_should_include_in_known_states
84
- assert_equal %w(off on), @callback.known_states.sort
86
+ assert_equal [:parked, :idling], @callback.known_states
85
87
  end
86
88
  end
87
89
 
88
- class CallbackWithIfConditionalTest < Test::Unit::TestCase
90
+ class CallbackWithIfConditionTest < Test::Unit::TestCase
89
91
  def setup
90
92
  @object = Object.new
91
93
  end
@@ -101,7 +103,7 @@ class CallbackWithIfConditionalTest < Test::Unit::TestCase
101
103
  end
102
104
  end
103
105
 
104
- class CallbackWithUnlessConditionalTest < Test::Unit::TestCase
106
+ class CallbackWithUnlessConditionTest < Test::Unit::TestCase
105
107
  def setup
106
108
  @object = Object.new
107
109
  end
@@ -189,12 +191,12 @@ class CallbackWithBoundObjectTest < Test::Unit::TestCase
189
191
 
190
192
  def test_should_ignore_option_for_symbolic_callbacks
191
193
  class << @object
192
- def after_turn_on(*args)
194
+ def after_ignite(*args)
193
195
  args
194
196
  end
195
197
  end
196
198
 
197
- @callback = StateMachine::Callback.new(:do => :after_turn_on, :bind_to_object => true)
199
+ @callback = StateMachine::Callback.new(:do => :after_ignite, :bind_to_object => true)
198
200
  assert_equal [], @callback.call(@object)
199
201
  end
200
202
 
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
- class EvalHelperTest < Test::Unit::TestCase
3
+ class EvalHelpersTest < Test::Unit::TestCase
4
4
  include StateMachine::EvalHelpers
5
5
 
6
6
  def setup
@@ -8,7 +8,8 @@ class EvalHelperTest < Test::Unit::TestCase
8
8
  end
9
9
 
10
10
  def test_should_raise_exception_if_method_is_not_symbol_string_or_proc
11
- assert_raise(ArgumentError) { evaluate_method(@object, 1) }
11
+ exception = assert_raise(ArgumentError) { evaluate_method(@object, 1) }
12
+ assert_match /Methods must/, exception.message
12
13
  end
13
14
  end
14
15
 
@@ -28,7 +29,7 @@ class EvalHelpersSymbolTest < Test::Unit::TestCase
28
29
  end
29
30
  end
30
31
 
31
- class EvalHelperSymbolWithArgumentsTest < Test::Unit::TestCase
32
+ class EvalHelpersSymbolWithArgumentsTest < Test::Unit::TestCase
32
33
  include StateMachine::EvalHelpers
33
34
 
34
35
  def setup
@@ -44,7 +45,7 @@ class EvalHelperSymbolWithArgumentsTest < Test::Unit::TestCase
44
45
  end
45
46
  end
46
47
 
47
- class EvalHelperStringTest < Test::Unit::TestCase
48
+ class EvalHelpersStringTest < Test::Unit::TestCase
48
49
  include StateMachine::EvalHelpers
49
50
 
50
51
  def setup
@@ -65,7 +66,7 @@ class EvalHelperStringTest < Test::Unit::TestCase
65
66
  end
66
67
  end
67
68
 
68
- class EvalHelperProcTest < Test::Unit::TestCase
69
+ class EvalHelpersProcTest < Test::Unit::TestCase
69
70
  include StateMachine::EvalHelpers
70
71
 
71
72
  def setup
@@ -78,7 +79,25 @@ class EvalHelperProcTest < Test::Unit::TestCase
78
79
  end
79
80
  end
80
81
 
81
- class EvalHelperProcWithArgumentsTest < Test::Unit::TestCase
82
+ class EvalHelpersProcWithoutArgumentsTest < Test::Unit::TestCase
83
+ include StateMachine::EvalHelpers
84
+
85
+ def setup
86
+ @object = Object.new
87
+ @proc = lambda {|*args| args}
88
+ class << @proc
89
+ def arity
90
+ 0
91
+ end
92
+ end
93
+ end
94
+
95
+ def test_should_call_proc_with_no_arguments
96
+ assert_equal [], evaluate_method(@object, @proc, 1, 2, 3)
97
+ end
98
+ end
99
+
100
+ class EvalHelpersProcWithArgumentsTest < Test::Unit::TestCase
82
101
  include StateMachine::EvalHelpers
83
102
 
84
103
  def setup
@@ -1,21 +1,10 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
- class EventTest < Test::Unit::TestCase
4
- def setup
5
- @machine = StateMachine::Machine.new(Class.new)
6
- @event = StateMachine::Event.new(@machine, 'turn_on')
7
- end
8
-
9
- def test_should_raise_exception_if_invalid_option_specified
10
- assert_raise(ArgumentError) {StateMachine::Event.new(@machine, 'turn_on', :invalid => true)}
11
- end
12
- end
13
-
14
3
  class EventByDefaultTest < Test::Unit::TestCase
15
4
  def setup
16
5
  @klass = Class.new
17
6
  @machine = StateMachine::Machine.new(@klass)
18
- @event = StateMachine::Event.new(@machine, 'turn_on')
7
+ @event = StateMachine::Event.new(@machine, :ignite)
19
8
 
20
9
  @object = @klass.new
21
10
  end
@@ -25,7 +14,7 @@ class EventByDefaultTest < Test::Unit::TestCase
25
14
  end
26
15
 
27
16
  def test_should_have_a_name
28
- assert_equal 'turn_on', @event.name
17
+ assert_equal :ignite, @event.name
29
18
  end
30
19
 
31
20
  def test_should_not_have_any_guards
@@ -44,71 +33,113 @@ class EventByDefaultTest < Test::Unit::TestCase
44
33
  assert_nil @event.next_transition(@object)
45
34
  end
46
35
 
47
- def test_should_define_an_event_predicate_on_the_owner_class
48
- assert @object.respond_to?(:can_turn_on?)
36
+ def test_should_define_a_predicate
37
+ assert @object.respond_to?(:can_ignite?)
49
38
  end
50
39
 
51
- def test_should_define_an_event_transition_accessor_on_the_owner_class
52
- assert @object.respond_to?(:next_turn_on_transition)
40
+ def test_should_define_a_transition_accessor
41
+ assert @object.respond_to?(:next_ignite_transition)
53
42
  end
54
43
 
55
- def test_should_define_an_event_action_on_the_owner_class
56
- assert @object.respond_to?(:turn_on)
44
+ def test_should_define_an_action
45
+ assert @object.respond_to?(:ignite)
57
46
  end
58
47
 
59
- def test_should_define_an_event_bang_action_on_the_owner_class
60
- assert @object.respond_to?(:turn_on!)
48
+ def test_should_define_a_bang_action
49
+ assert @object.respond_to?(:ignite!)
61
50
  end
62
51
  end
63
52
 
64
- class EventTransitionsTest < Test::Unit::TestCase
53
+ class EventTest < Test::Unit::TestCase
65
54
  def setup
66
55
  @machine = StateMachine::Machine.new(Class.new)
67
- @event = StateMachine::Event.new(@machine, 'turn_on')
56
+ @event = StateMachine::Event.new(@machine, :ignite)
57
+ @event.transition :to => :idling, :from => :parked
68
58
  end
69
59
 
70
- def test_should_raise_exception_if_invalid_option_specified
71
- assert_raise(ArgumentError) {@event.transition(:invalid => true)}
60
+ def test_should_allow_changing_machine
61
+ new_machine = StateMachine::Machine.new(Class.new)
62
+ @event.machine = new_machine
63
+ assert_equal new_machine, @event.machine
72
64
  end
73
65
 
74
- def test_should_not_raise_exception_if_to_option_not_specified
75
- assert_nothing_raised {@event.transition(:from => 'off')}
66
+ def test_should_use_pretty_inspect
67
+ assert_match /#<StateMachine::Event name=:ignite transitions=\[\{.+\}\]>/, @event.inspect
68
+ end
69
+ end
70
+
71
+ class EventWithNamespaceTest < Test::Unit::TestCase
72
+ def setup
73
+ @klass = Class.new
74
+ @machine = StateMachine::Machine.new(@klass, :namespace => 'car')
75
+ @event = StateMachine::Event.new(@machine, :ignite)
76
+ @object = @klass.new
76
77
  end
77
78
 
78
- def test_should_not_raise_exception_if_from_option_not_specified
79
- assert_nothing_raised {@event.transition(:to => 'on')}
79
+ def test_should_namespace_predicate
80
+ assert @object.respond_to?(:can_ignite_car?)
81
+ end
82
+
83
+ def test_should_namespace_transition_accessor
84
+ assert @object.respond_to?(:next_ignite_car_transition)
85
+ end
86
+
87
+ def test_should_namespace_action
88
+ assert @object.respond_to?(:ignite_car)
89
+ end
90
+
91
+ def test_should_namespace_bang_action
92
+ assert @object.respond_to?(:ignite_car!)
93
+ end
94
+ end
95
+
96
+ class EventTransitionsTest < Test::Unit::TestCase
97
+ def setup
98
+ @machine = StateMachine::Machine.new(Class.new)
99
+ @event = StateMachine::Event.new(@machine, :ignite)
100
+ end
101
+
102
+ def test_should_raise_exception_if_invalid_option_specified
103
+ assert_raise(ArgumentError) {@event.transition(:invalid => true)}
80
104
  end
81
105
 
82
106
  def test_should_not_allow_on_option
83
- assert_raise(ArgumentError) {@event.transition(:on => 'turn_on')}
107
+ exception = assert_raise(ArgumentError) {@event.transition(:on => :ignite)}
108
+ assert_equal 'Invalid key(s): on', exception.message
84
109
  end
85
110
 
86
111
  def test_should_not_allow_except_to_option
87
- assert_raise(ArgumentError) {@event.transition(:except_to => 'off')}
112
+ exception = assert_raise(ArgumentError) {@event.transition(:except_to => :parked)}
113
+ assert_equal 'Invalid key(s): except_to', exception.message
88
114
  end
89
115
 
90
116
  def test_should_not_allow_except_on_option
91
- assert_raise(ArgumentError) {@event.transition(:except_on => 'turn_on')}
117
+ exception = assert_raise(ArgumentError) {@event.transition(:except_on => :ignite)}
118
+ assert_equal 'Invalid key(s): except_on', exception.message
119
+ end
120
+
121
+ def test_should_allow_transitioning_without_a_to_state
122
+ assert_nothing_raised {@event.transition(:from => :parked)}
92
123
  end
93
124
 
94
125
  def test_should_allow_transitioning_without_a_from_state
95
- assert @event.transition(:to => 'on')
126
+ assert_nothing_raised {@event.transition(:to => :idling)}
96
127
  end
97
128
 
98
- def test_should_allow_transitioning_without_a_to_state
99
- assert @event.transition(:from => 'off')
129
+ def test_should_allow_except_from_option
130
+ assert_nothing_raised {@event.transition(:except_from => :idling)}
100
131
  end
101
132
 
102
133
  def test_should_allow_transitioning_from_a_single_state
103
- assert @event.transition(:to => 'on', :from => 'off')
134
+ assert @event.transition(:to => :idling, :from => :parked)
104
135
  end
105
136
 
106
137
  def test_should_allow_transitioning_from_multiple_states
107
- assert @event.transition(:to => 'on', :from => %w(off on))
138
+ assert @event.transition(:to => :idling, :from => [:parked, :idling])
108
139
  end
109
140
 
110
141
  def test_should_have_transitions
111
- guard = @event.transition(:to => 'on')
142
+ guard = @event.transition(:to => :idling)
112
143
  assert_equal [guard], @event.guards
113
144
  end
114
145
  end
@@ -116,8 +147,7 @@ end
116
147
  class EventAfterBeingCopiedTest < Test::Unit::TestCase
117
148
  def setup
118
149
  @machine = StateMachine::Machine.new(Class.new)
119
- @event = StateMachine::Event.new(@machine, 'turn_on')
120
- @event.known_states # Call so that it's cached
150
+ @event = StateMachine::Event.new(@machine, :ignite)
121
151
  @copied_event = @event.dup
122
152
  end
123
153
 
@@ -134,7 +164,7 @@ class EventWithoutTransitionsTest < Test::Unit::TestCase
134
164
  def setup
135
165
  @klass = Class.new
136
166
  @machine = StateMachine::Machine.new(@klass)
137
- @event = StateMachine::Event.new(@machine, 'turn_on')
167
+ @event = StateMachine::Event.new(@machine, :ignite)
138
168
  @object = @klass.new
139
169
  end
140
170
 
@@ -150,6 +180,11 @@ class EventWithoutTransitionsTest < Test::Unit::TestCase
150
180
  assert !@event.fire(@object)
151
181
  end
152
182
 
183
+ def test_should_raise_exception_on_fire!
184
+ exception = assert_raise(StateMachine::InvalidTransition) { @event.fire!(@object) }
185
+ assert_equal 'Cannot transition state via :ignite from nil', exception.message
186
+ end
187
+
153
188
  def test_should_not_change_the_current_state
154
189
  @event.fire(@object)
155
190
  assert_nil @object.state
@@ -160,20 +195,20 @@ class EventWithTransitionsTest < Test::Unit::TestCase
160
195
  def setup
161
196
  @klass = Class.new
162
197
  @machine = StateMachine::Machine.new(@klass)
163
- @event = StateMachine::Event.new(@machine, 'turn_on')
164
- @event.transition(:to => 'on', :from => 'off')
165
- @event.transition(:to => 'on', :except_from => 'maybe')
198
+ @event = StateMachine::Event.new(@machine, :ignite)
199
+ @event.transition(:to => :idling, :from => :parked)
200
+ @event.transition(:to => :idling, :except_from => :first_gear)
166
201
  end
167
202
 
168
203
  def test_should_include_all_transition_states_in_known_states
169
- assert_equal %w(maybe off on), @event.known_states.sort
204
+ assert_equal [:parked, :idling, :first_gear], @event.known_states
170
205
  end
171
206
 
172
207
  def test_should_include_new_transition_states_after_calling_known_states
173
208
  @event.known_states
174
- @event.transition(:to => 'error')
209
+ @event.transition(:to => :idling, :from => :stalled)
175
210
 
176
- assert_equal %w(error maybe off on), @event.known_states.sort
211
+ assert_equal [:parked, :idling, :first_gear, :stalled], @event.known_states
177
212
  end
178
213
  end
179
214
 
@@ -181,11 +216,13 @@ class EventWithoutMatchingTransitionsTest < Test::Unit::TestCase
181
216
  def setup
182
217
  @klass = Class.new
183
218
  @machine = StateMachine::Machine.new(@klass)
184
- @event = StateMachine::Event.new(@machine, 'turn_on')
185
- @event.transition(:to => 'on', :from => 'off')
219
+ @machine.state :parked, :idling
220
+
221
+ @event = StateMachine::Event.new(@machine, :ignite)
222
+ @event.transition(:to => :idling, :from => :parked)
186
223
 
187
224
  @object = @klass.new
188
- @object.state = 'on'
225
+ @object.state = 'idling'
189
226
  end
190
227
 
191
228
  def test_should_not_be_able_to_fire
@@ -200,9 +237,14 @@ class EventWithoutMatchingTransitionsTest < Test::Unit::TestCase
200
237
  assert !@event.fire(@object)
201
238
  end
202
239
 
240
+ def test_should_raise_exception_on_fire!
241
+ exception = assert_raise(StateMachine::InvalidTransition) { @event.fire!(@object) }
242
+ assert_equal 'Cannot transition state via :ignite from :idling', exception.message
243
+ end
244
+
203
245
  def test_should_not_change_the_current_state
204
246
  @event.fire(@object)
205
- assert_equal 'on', @object.state
247
+ assert_equal 'idling', @object.state
206
248
  end
207
249
  end
208
250
 
@@ -210,11 +252,13 @@ class EventWithMatchingDisabledTransitionsTest < Test::Unit::TestCase
210
252
  def setup
211
253
  @klass = Class.new
212
254
  @machine = StateMachine::Machine.new(@klass)
213
- @event = StateMachine::Event.new(@machine, 'turn_on')
214
- @event.transition(:to => 'on', :from => 'off', :if => lambda {false})
255
+ @machine.state :parked, :idling
256
+
257
+ @event = StateMachine::Event.new(@machine, :ignite)
258
+ @event.transition(:to => :idling, :from => :parked, :if => lambda {false})
215
259
 
216
260
  @object = @klass.new
217
- @object.state = 'off'
261
+ @object.state = 'parked'
218
262
  end
219
263
 
220
264
  def test_should_not_be_able_to_fire
@@ -231,7 +275,7 @@ class EventWithMatchingDisabledTransitionsTest < Test::Unit::TestCase
231
275
 
232
276
  def test_should_not_change_the_current_state
233
277
  @event.fire(@object)
234
- assert_equal 'off', @object.state
278
+ assert_equal 'parked', @object.state
235
279
  end
236
280
  end
237
281
 
@@ -239,11 +283,13 @@ class EventWithMatchingEnabledTransitionsTest < Test::Unit::TestCase
239
283
  def setup
240
284
  @klass = Class.new
241
285
  @machine = StateMachine::Machine.new(@klass)
242
- @event = StateMachine::Event.new(@machine, 'turn_on')
243
- @event.transition(:to => 'on', :from => 'off')
286
+ @machine.state :parked, :idling
287
+
288
+ @event = StateMachine::Event.new(@machine, :ignite)
289
+ @event.transition(:to => :idling, :from => :parked)
244
290
 
245
291
  @object = @klass.new
246
- @object.state = 'off'
292
+ @object.state = 'parked'
247
293
  end
248
294
 
249
295
  def test_should_be_able_to_fire
@@ -253,9 +299,9 @@ class EventWithMatchingEnabledTransitionsTest < Test::Unit::TestCase
253
299
  def test_should_have_a_next_transition
254
300
  transition = @event.next_transition(@object)
255
301
  assert_not_nil transition
256
- assert_equal 'off', transition.from
257
- assert_equal 'on', transition.to
258
- assert_equal 'turn_on', transition.event
302
+ assert_equal 'parked', transition.from
303
+ assert_equal 'idling', transition.to
304
+ assert_equal :ignite, transition.event
259
305
  end
260
306
 
261
307
  def test_should_fire
@@ -264,7 +310,7 @@ class EventWithMatchingEnabledTransitionsTest < Test::Unit::TestCase
264
310
 
265
311
  def test_should_change_the_current_state
266
312
  @event.fire(@object)
267
- assert_equal 'on', @object.state
313
+ assert_equal 'idling', @object.state
268
314
  end
269
315
  end
270
316
 
@@ -272,11 +318,13 @@ class EventWithTransitionWithoutToStateTest < Test::Unit::TestCase
272
318
  def setup
273
319
  @klass = Class.new
274
320
  @machine = StateMachine::Machine.new(@klass)
275
- @event = StateMachine::Event.new(@machine, 'turn_off')
276
- @event.transition(:from => 'off')
321
+ @machine.state :parked
322
+
323
+ @event = StateMachine::Event.new(@machine, :park)
324
+ @event.transition(:from => :parked)
277
325
 
278
326
  @object = @klass.new
279
- @object.state = 'off'
327
+ @object.state = 'parked'
280
328
  end
281
329
 
282
330
  def test_should_be_able_to_fire
@@ -286,9 +334,9 @@ class EventWithTransitionWithoutToStateTest < Test::Unit::TestCase
286
334
  def test_should_have_a_next_transition
287
335
  transition = @event.next_transition(@object)
288
336
  assert_not_nil transition
289
- assert_equal 'off', transition.from
290
- assert_equal 'off', transition.to
291
- assert_equal 'turn_off', transition.event
337
+ assert_equal 'parked', transition.from
338
+ assert_equal 'parked', transition.to
339
+ assert_equal :park, transition.event
292
340
  end
293
341
 
294
342
  def test_should_fire
@@ -297,7 +345,7 @@ class EventWithTransitionWithoutToStateTest < Test::Unit::TestCase
297
345
 
298
346
  def test_should_not_change_the_current_state
299
347
  @event.fire(@object)
300
- assert_equal 'off', @object.state
348
+ assert_equal 'parked', @object.state
301
349
  end
302
350
  end
303
351
 
@@ -305,11 +353,14 @@ class EventWithTransitionWithNilToStateTest < Test::Unit::TestCase
305
353
  def setup
306
354
  @klass = Class.new
307
355
  @machine = StateMachine::Machine.new(@klass)
308
- @event = StateMachine::Event.new(@machine, 'turn_off')
309
- @event.transition(:from => 'off', :to => nil)
356
+ @machine.state :parked, :value => nil
357
+ @machine.state :idling
358
+
359
+ @event = StateMachine::Event.new(@machine, :park)
360
+ @event.transition(:from => :idling, :to => :parked)
310
361
 
311
362
  @object = @klass.new
312
- @object.state = 'off'
363
+ @object.state = 'idling'
313
364
  end
314
365
 
315
366
  def test_should_be_able_to_fire
@@ -319,9 +370,9 @@ class EventWithTransitionWithNilToStateTest < Test::Unit::TestCase
319
370
  def test_should_have_a_next_transition
320
371
  transition = @event.next_transition(@object)
321
372
  assert_not_nil transition
322
- assert_equal 'off', transition.from
373
+ assert_equal 'idling', transition.from
323
374
  assert_equal nil, transition.to
324
- assert_equal 'turn_off', transition.event
375
+ assert_equal :park, transition.event
325
376
  end
326
377
 
327
378
  def test_should_fire
@@ -334,49 +385,18 @@ class EventWithTransitionWithNilToStateTest < Test::Unit::TestCase
334
385
  end
335
386
  end
336
387
 
337
- class EventWithTransitionWithDynamicToStateTest < Test::Unit::TestCase
338
- def setup
339
- @klass = Class.new
340
- @machine = StateMachine::Machine.new(@klass)
341
- @event = StateMachine::Event.new(@machine, 'turn_on')
342
- @event.transition(:to => lambda {'on'}, :from => 'off')
343
-
344
- @object = @klass.new
345
- @object.state = 'off'
346
- end
347
-
348
- def test_should_be_able_to_fire
349
- assert @event.can_fire?(@object)
350
- end
351
-
352
- def test_should_have_a_next_transition
353
- transition = @event.next_transition(@object)
354
- assert_not_nil transition
355
- assert_equal 'off', transition.from
356
- assert_equal 'on', transition.to
357
- assert_equal 'turn_on', transition.event
358
- end
359
-
360
- def test_should_fire
361
- assert @event.fire(@object)
362
- end
363
-
364
- def test_should_change_the_current_state
365
- @event.fire(@object)
366
- assert_equal 'on', @object.state
367
- end
368
- end
369
-
370
388
  class EventWithMultipleTransitionsTest < Test::Unit::TestCase
371
389
  def setup
372
390
  @klass = Class.new
373
391
  @machine = StateMachine::Machine.new(@klass)
374
- @event = StateMachine::Event.new(@machine, 'turn_on')
375
- @event.transition(:to => 'on', :from => 'on')
376
- @event.transition(:to => 'on', :from => 'off') # This one should get used
392
+ @machine.state :parked, :idling
393
+
394
+ @event = StateMachine::Event.new(@machine, :ignite)
395
+ @event.transition(:to => :idling, :from => :idling)
396
+ @event.transition(:to => :idling, :from => :parked) # This one should get used
377
397
 
378
398
  @object = @klass.new
379
- @object.state = 'off'
399
+ @object.state = 'parked'
380
400
  end
381
401
 
382
402
  def test_should_be_able_to_fire
@@ -386,9 +406,9 @@ class EventWithMultipleTransitionsTest < Test::Unit::TestCase
386
406
  def test_should_have_a_next_transition
387
407
  transition = @event.next_transition(@object)
388
408
  assert_not_nil transition
389
- assert_equal 'off', transition.from
390
- assert_equal 'on', transition.to
391
- assert_equal 'turn_on', transition.event
409
+ assert_equal 'parked', transition.from
410
+ assert_equal 'idling', transition.to
411
+ assert_equal :ignite, transition.event
392
412
  end
393
413
 
394
414
  def test_should_fire
@@ -397,7 +417,7 @@ class EventWithMultipleTransitionsTest < Test::Unit::TestCase
397
417
 
398
418
  def test_should_change_the_current_state
399
419
  @event.fire(@object)
400
- assert_equal 'on', @object.state
420
+ assert_equal 'idling', @object.state
401
421
  end
402
422
  end
403
423
 
@@ -408,18 +428,18 @@ begin
408
428
 
409
429
  class EventDrawingTest < Test::Unit::TestCase
410
430
  def setup
411
- states = %w(parked idling first_gear)
431
+ states = [:parked, :idling, :first_gear]
412
432
 
413
- @machine = StateMachine::Machine.new(Class.new, :initial => 'parked')
414
- @machine.other_states(states)
433
+ @machine = StateMachine::Machine.new(Class.new, :initial => :parked)
434
+ @machine.other_states(*states)
415
435
 
416
436
  graph = GraphViz.new('G')
417
- states.each {|state| graph.add_node(state)}
437
+ states.each {|state| graph.add_node(state.to_s)}
418
438
 
419
- @event = StateMachine::Event.new(@machine , 'park')
420
- @event.transition :from => 'idling', :to => 'parked'
421
- @event.transition :from => 'first_gear', :to => 'parked'
422
- @event.transition :except_from => 'parked', :to => 'parked'
439
+ @event = StateMachine::Event.new(@machine , :park)
440
+ @event.transition :from => :idling, :to => :parked
441
+ @event.transition :from => :first_gear, :to => :parked
442
+ @event.transition :except_from => :parked, :to => :parked
423
443
 
424
444
  @edges = @event.draw(graph)
425
445
  end
@@ -433,5 +453,5 @@ begin
433
453
  end
434
454
  end
435
455
  rescue LoadError
436
- $stderr.puts 'Skipping GraphViz StateMachine::Guard tests. `gem install ruby-graphviz` and try again.'
456
+ $stderr.puts 'Skipping GraphViz StateMachine::Event tests. `gem install ruby-graphviz` and try again.'
437
457
  end