state_machine 0.9.4 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -138,12 +138,12 @@ end
138
138
 
139
139
  class NodeCollectionWithNodesTest < Test::Unit::TestCase
140
140
  def setup
141
- machine = StateMachine::Machine.new(Class.new)
142
- @collection = StateMachine::NodeCollection.new(machine)
141
+ @machine = StateMachine::Machine.new(Class.new)
142
+ @collection = StateMachine::NodeCollection.new(@machine)
143
143
 
144
144
  @klass = Struct.new(:name, :machine)
145
- @parked = @klass.new(:parked, machine)
146
- @idling = @klass.new(:idling, machine)
145
+ @parked = @klass.new(:parked, @machine)
146
+ @idling = @klass.new(:idling, @machine)
147
147
 
148
148
  @collection << @parked
149
149
  @collection << @idling
@@ -156,6 +156,16 @@ class NodeCollectionWithNodesTest < Test::Unit::TestCase
156
156
  assert_equal [@parked, @idling], order
157
157
  end
158
158
 
159
+ def test_should_be_able_to_concatenate_multiple_nodes
160
+ @first_gear = @klass.new(:first_gear, @machine)
161
+ @second_gear = @klass.new(:second_gear, @machine)
162
+ @collection.concat([@first_gear, @second_gear])
163
+
164
+ order = []
165
+ @collection.each {|object| order << object}
166
+ assert_equal [@parked, @idling, @first_gear, @second_gear], order
167
+ end
168
+
159
169
  def test_should_be_able_to_access_by_index
160
170
  assert_equal @parked, @collection.at(0)
161
171
  assert_equal @idling, @collection.at(1)
@@ -0,0 +1,266 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class PathCollectionByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @klass = Class.new
6
+ @machine = StateMachine::Machine.new(@klass)
7
+ @machine.state :parked
8
+
9
+ @object = @klass.new
10
+ @object.state = 'parked'
11
+
12
+ @paths = StateMachine::PathCollection.new(@object, @machine)
13
+ end
14
+
15
+ def test_should_have_an_object
16
+ assert_equal @object, @paths.object
17
+ end
18
+
19
+ def test_should_have_a_machine
20
+ assert_equal @machine, @paths.machine
21
+ end
22
+
23
+ def test_should_have_a_from_name
24
+ assert_equal :parked, @paths.from_name
25
+ end
26
+
27
+ def test_should_not_have_a_to_name
28
+ assert_nil @paths.to_name
29
+ end
30
+
31
+ def test_should_have_no_from_states
32
+ assert_equal [], @paths.from_states
33
+ end
34
+
35
+ def test_should_have_no_to_states
36
+ assert_equal [], @paths.to_states
37
+ end
38
+
39
+ def test_should_have_no_events
40
+ assert_equal [], @paths.events
41
+ end
42
+
43
+ def test_should_have_no_paths
44
+ assert @paths.empty?
45
+ end
46
+ end
47
+
48
+ class PathCollectionTest < Test::Unit::TestCase
49
+ def setup
50
+ @klass = Class.new
51
+ @machine = StateMachine::Machine.new(@klass)
52
+ @object = @klass.new
53
+ end
54
+
55
+ def test_should_raise_exception_if_invalid_option_specified
56
+ exception = assert_raise(ArgumentError) {StateMachine::PathCollection.new(@object, @machine, :invalid => true)}
57
+ assert_equal 'Invalid key(s): invalid', exception.message
58
+ end
59
+
60
+ def test_should_raise_exception_if_invalid_from_state_specified
61
+ exception = assert_raise(IndexError) {StateMachine::PathCollection.new(@object, @machine, :from => :invalid)}
62
+ assert_equal ':invalid is an invalid name', exception.message
63
+ end
64
+
65
+ def test_should_raise_exception_if_invalid_to_state_specified
66
+ exception = assert_raise(IndexError) {StateMachine::PathCollection.new(@object, @machine, :to => :invalid)}
67
+ assert_equal ':invalid is an invalid name', exception.message
68
+ end
69
+ end
70
+
71
+ class PathCollectionWithPathsTest < Test::Unit::TestCase
72
+ def setup
73
+ @klass = Class.new
74
+ @machine = StateMachine::Machine.new(@klass)
75
+ @machine.state :parked, :idling, :first_gear
76
+ @machine.event :ignite do
77
+ transition :parked => :idling
78
+ end
79
+ @machine.event :shift_up do
80
+ transition :idling => :first_gear
81
+ end
82
+
83
+ @object = @klass.new
84
+ @object.state = 'parked'
85
+
86
+ @paths = StateMachine::PathCollection.new(@object, @machine)
87
+ end
88
+
89
+ def test_should_enumerate_paths
90
+ assert_equal [[
91
+ StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
92
+ StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)
93
+ ]], @paths
94
+ end
95
+
96
+ def test_should_have_a_from_name
97
+ assert_equal :parked, @paths.from_name
98
+ end
99
+
100
+ def test_should_not_have_a_to_name
101
+ assert_nil @paths.to_name
102
+ end
103
+
104
+ def test_should_have_from_states
105
+ assert_equal [:parked, :idling], @paths.from_states
106
+ end
107
+
108
+ def test_should_have_to_states
109
+ assert_equal [:idling, :first_gear], @paths.to_states
110
+ end
111
+
112
+ def test_should_have_no_events
113
+ assert_equal [:ignite, :shift_up], @paths.events
114
+ end
115
+ end
116
+
117
+ class PathWithGuardedPathsTest < Test::Unit::TestCase
118
+ def setup
119
+ @klass = Class.new
120
+ @machine = StateMachine::Machine.new(@klass)
121
+ @machine.state :parked, :idling, :first_gear
122
+ @machine.event :ignite do
123
+ transition :parked => :idling, :if => lambda {false}
124
+ end
125
+
126
+ @object = @klass.new
127
+ @object.state = 'parked'
128
+ end
129
+
130
+ def test_should_not_enumerate_paths_if_guard_enabled
131
+ assert_equal [], StateMachine::PathCollection.new(@object, @machine)
132
+ end
133
+
134
+ def test_should_enumerate_paths_if_guard_disabled
135
+ paths = StateMachine::PathCollection.new(@object, @machine, :guard => false)
136
+ assert_equal [[
137
+ StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
138
+ ]], paths
139
+ end
140
+ end
141
+
142
+ class PathCollectionWithDuplicateNodesTest < Test::Unit::TestCase
143
+ def setup
144
+ @klass = Class.new
145
+ @machine = StateMachine::Machine.new(@klass)
146
+ @machine.state :parked, :idling
147
+ @machine.event :shift_up do
148
+ transition :parked => :idling, :idling => :first_gear
149
+ end
150
+ @machine.event :park do
151
+ transition :first_gear => :idling
152
+ end
153
+ @object = @klass.new
154
+ @object.state = 'parked'
155
+
156
+ @paths = StateMachine::PathCollection.new(@object, @machine)
157
+ end
158
+
159
+ def test_should_not_include_duplicates_in_from_states
160
+ assert_equal [:parked, :idling, :first_gear], @paths.from_states
161
+ end
162
+
163
+ def test_should_not_include_duplicates_in_to_states
164
+ assert_equal [:idling, :first_gear], @paths.to_states
165
+ end
166
+
167
+ def test_should_not_include_duplicates_in_events
168
+ assert_equal [:shift_up, :park], @paths.events
169
+ end
170
+ end
171
+
172
+ class PathCollectionWithFromStateTest < Test::Unit::TestCase
173
+ def setup
174
+ @klass = Class.new
175
+ @machine = StateMachine::Machine.new(@klass)
176
+ @machine.state :parked, :idling, :first_gear
177
+ @machine.event :park do
178
+ transition :idling => :parked
179
+ end
180
+
181
+ @object = @klass.new
182
+ @object.state = 'parked'
183
+
184
+ @paths = StateMachine::PathCollection.new(@object, @machine, :from => :idling)
185
+ end
186
+
187
+ def test_should_generate_paths_from_custom_from_state
188
+ assert_equal [[
189
+ StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)
190
+ ]], @paths
191
+ end
192
+
193
+ def test_should_have_a_from_name
194
+ assert_equal :idling, @paths.from_name
195
+ end
196
+ end
197
+
198
+ class PathCollectionWithToStateTest < Test::Unit::TestCase
199
+ def setup
200
+ @klass = Class.new
201
+ @machine = StateMachine::Machine.new(@klass)
202
+ @machine.state :parked, :idling
203
+ @machine.event :ignite do
204
+ transition :parked => :idling
205
+ end
206
+ @machine.event :shift_up do
207
+ transition :parked => :idling, :idling => :first_gear
208
+ end
209
+ @machine.event :shift_down do
210
+ transition :first_gear => :idling
211
+ end
212
+ @object = @klass.new
213
+ @object.state = 'parked'
214
+
215
+ @paths = StateMachine::PathCollection.new(@object, @machine, :to => :idling)
216
+ end
217
+
218
+ def test_should_stop_paths_once_target_state_reached
219
+ assert_equal [
220
+ [StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)],
221
+ [StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :idling)]
222
+ ], @paths
223
+ end
224
+ end
225
+
226
+ class PathCollectionWithDeepPathsTest < Test::Unit::TestCase
227
+ def setup
228
+ @klass = Class.new
229
+ @machine = StateMachine::Machine.new(@klass)
230
+ @machine.state :parked, :idling
231
+ @machine.event :ignite do
232
+ transition :parked => :idling
233
+ end
234
+ @machine.event :shift_up do
235
+ transition :parked => :idling, :idling => :first_gear
236
+ end
237
+ @machine.event :shift_down do
238
+ transition :first_gear => :idling
239
+ end
240
+ @object = @klass.new
241
+ @object.state = 'parked'
242
+
243
+ @paths = StateMachine::PathCollection.new(@object, @machine, :to => :idling, :deep => true)
244
+ end
245
+
246
+ def test_should_allow_target_to_be_reached_more_than_once_per_path
247
+ assert_equal [
248
+ [
249
+ StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
250
+ ],
251
+ [
252
+ StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
253
+ StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear),
254
+ StateMachine::Transition.new(@object, @machine, :shift_down, :first_gear, :idling)
255
+ ],
256
+ [
257
+ StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :idling)
258
+ ],
259
+ [
260
+ StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :idling),
261
+ StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear),
262
+ StateMachine::Transition.new(@object, @machine, :shift_down, :first_gear, :idling)
263
+ ]
264
+ ], @paths
265
+ end
266
+ end
@@ -0,0 +1,485 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class PathByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @klass = Class.new
6
+ @machine = StateMachine::Machine.new(@klass)
7
+ @object = @klass.new
8
+
9
+ @path = StateMachine::Path.new(@object, @machine)
10
+ end
11
+
12
+ def test_should_have_an_object
13
+ assert_equal @object, @path.object
14
+ end
15
+
16
+ def test_should_have_a_machine
17
+ assert_equal @machine, @path.machine
18
+ end
19
+
20
+ def test_should_not_have_walked_anywhere
21
+ assert_equal [], @path
22
+ end
23
+
24
+ def test_should_not_have_a_from_name
25
+ assert_nil @path.from_name
26
+ end
27
+
28
+ def test_should_have_no_from_states
29
+ assert_equal [], @path.from_states
30
+ end
31
+
32
+ def test_should_not_have_a_to_name
33
+ assert_nil @path.to_name
34
+ end
35
+
36
+ def test_should_have_no_to_states
37
+ assert_equal [], @path.to_states
38
+ end
39
+
40
+ def test_should_have_no_events
41
+ assert_equal [], @path.events
42
+ end
43
+
44
+ def test_should_not_be_able_to_walk_anywhere
45
+ walked = false
46
+ @path.walk { walked = true }
47
+ assert_equal false, walked
48
+ end
49
+
50
+ def test_should_not_be_complete
51
+ assert_equal false, @path.complete?
52
+ end
53
+ end
54
+
55
+ class PathTest < Test::Unit::TestCase
56
+ def setup
57
+ @klass = Class.new
58
+ @machine = StateMachine::Machine.new(@klass)
59
+ @object = @klass.new
60
+ end
61
+
62
+ def test_should_raise_exception_if_invalid_option_specified
63
+ exception = assert_raise(ArgumentError) {StateMachine::Path.new(@object, @machine, :invalid => true)}
64
+ assert_equal 'Invalid key(s): invalid', exception.message
65
+ end
66
+ end
67
+
68
+ class PathWithoutTransitionsTest < Test::Unit::TestCase
69
+ def setup
70
+ @klass = Class.new
71
+ @machine = StateMachine::Machine.new(@klass)
72
+ @machine.state :parked, :idling
73
+ @machine.event :ignite
74
+
75
+ @object = @klass.new
76
+
77
+ @path = StateMachine::Path.new(@object, @machine)
78
+ @path.concat([
79
+ @ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
80
+ ])
81
+ end
82
+
83
+ def test_should_not_be_able_to_walk_anywhere
84
+ walked = false
85
+ @path.walk { walked = true }
86
+ assert_equal false, walked
87
+ end
88
+ end
89
+
90
+ class PathWithTransitionsTest < Test::Unit::TestCase
91
+ def setup
92
+ @klass = Class.new
93
+ @machine = StateMachine::Machine.new(@klass)
94
+ @machine.state :parked, :idling, :first_gear
95
+ @machine.event :ignite, :shift_up
96
+
97
+ @object = @klass.new
98
+ @object.state = 'parked'
99
+
100
+ @path = StateMachine::Path.new(@object, @machine)
101
+ @path.concat([
102
+ @ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
103
+ @shift_up_transition = StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)
104
+ ])
105
+ end
106
+
107
+ def test_should_enumerate_transitions
108
+ assert_equal [@ignite_transition, @shift_up_transition], @path
109
+ end
110
+
111
+ def test_should_have_a_from_name
112
+ assert_equal :parked, @path.from_name
113
+ end
114
+
115
+ def test_should_have_from_states
116
+ assert_equal [:parked, :idling], @path.from_states
117
+ end
118
+
119
+ def test_should_have_a_to_name
120
+ assert_equal :first_gear, @path.to_name
121
+ end
122
+
123
+ def test_should_have_to_states
124
+ assert_equal [:idling, :first_gear], @path.to_states
125
+ end
126
+
127
+ def test_should_have_events
128
+ assert_equal [:ignite, :shift_up], @path.events
129
+ end
130
+
131
+ def test_should_not_be_able_to_walk_anywhere
132
+ walked = false
133
+ @path.walk { walked = true }
134
+ assert_equal false, walked
135
+ end
136
+
137
+ def test_should_be_complete
138
+ assert_equal true, @path.complete?
139
+ end
140
+ end
141
+
142
+ class PathWithDuplicatesTest < Test::Unit::TestCase
143
+ def setup
144
+ @klass = Class.new
145
+ @machine = StateMachine::Machine.new(@klass)
146
+ @machine.state :parked, :idling
147
+ @machine.event :park, :ignite
148
+
149
+ @object = @klass.new
150
+ @object.state = 'parked'
151
+
152
+ @path = StateMachine::Path.new(@object, @machine)
153
+ @path.concat([
154
+ @ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
155
+ @park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked),
156
+ @ignite_again_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
157
+ ])
158
+ end
159
+
160
+ def test_should_not_include_duplicates_in_from_states
161
+ assert_equal [:parked, :idling], @path.from_states
162
+ end
163
+
164
+ def test_should_not_include_duplicates_in_to_states
165
+ assert_equal [:idling, :parked], @path.to_states
166
+ end
167
+
168
+ def test_should_not_include_duplicates_in_events
169
+ assert_equal [:ignite, :park], @path.events
170
+ end
171
+ end
172
+
173
+ class PathWithAvailableTransitionsTest < Test::Unit::TestCase
174
+ def setup
175
+ @klass = Class.new
176
+ @machine = StateMachine::Machine.new(@klass)
177
+ @machine.state :parked, :idling, :first_gear
178
+ @machine.event :ignite
179
+ @machine.event :shift_up do
180
+ transition :idling => :first_gear
181
+ end
182
+ @machine.event :park do
183
+ transition :idling => :parked
184
+ end
185
+
186
+ @object = @klass.new
187
+ @object.state = 'parked'
188
+
189
+ @path = StateMachine::Path.new(@object, @machine)
190
+ @path.concat([
191
+ @ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
192
+ ])
193
+ end
194
+
195
+ def test_should_not_be_complete
196
+ assert !@path.complete?
197
+ end
198
+
199
+ def test_should_walk_each_available_transition
200
+ paths = []
201
+ @path.walk {|path| paths << path}
202
+
203
+ assert_equal [
204
+ [@ignite_transition, StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)],
205
+ [@ignite_transition, StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)]
206
+ ], paths
207
+ end
208
+
209
+ def test_should_yield_path_instances_when_walking
210
+ @path.walk do |path|
211
+ assert_instance_of StateMachine::Path, path
212
+ end
213
+ end
214
+
215
+ def test_should_not_modify_current_path_after_walking
216
+ @path.walk {}
217
+ assert_equal [@ignite_transition], @path
218
+ end
219
+
220
+ def test_should_not_modify_object_after_walking
221
+ @path.walk {}
222
+ assert_equal 'parked', @object.state
223
+ end
224
+ end
225
+
226
+ class PathWithGuardedTransitionsTest < Test::Unit::TestCase
227
+ def setup
228
+ @klass = Class.new
229
+ @machine = StateMachine::Machine.new(@klass)
230
+ @machine.state :parked, :idling
231
+ @machine.event :ignite
232
+ @machine.event :shift_up do
233
+ transition :idling => :first_gear, :if => lambda {false}
234
+ end
235
+
236
+ @object = @klass.new
237
+ @object.state = 'parked'
238
+ end
239
+
240
+ def test_should_not_walk_transitions_if_guard_enabled
241
+ path = StateMachine::Path.new(@object, @machine)
242
+ path.concat([
243
+ ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
244
+ ])
245
+
246
+ paths = []
247
+ path.walk {|next_path| paths << next_path}
248
+
249
+ assert_equal [], paths
250
+ end
251
+
252
+ def test_should_not_walk_transitions_if_guard_disabled
253
+ path = StateMachine::Path.new(@object, @machine, :guard => false)
254
+ path.concat([
255
+ ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
256
+ ])
257
+
258
+ paths = []
259
+ path.walk {|next_path| paths << next_path}
260
+
261
+ assert_equal [
262
+ [ignite_transition, StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)]
263
+ ], paths
264
+ end
265
+ end
266
+
267
+ class PathWithEncounteredTransitionsTest < Test::Unit::TestCase
268
+ def setup
269
+ @klass = Class.new
270
+ @machine = StateMachine::Machine.new(@klass)
271
+ @machine.state :parked, :idling, :first_gear
272
+ @machine.event :ignite do
273
+ transition :parked => :idling
274
+ end
275
+ @machine.event :park do
276
+ transition :idling => :parked
277
+ end
278
+
279
+ @object = @klass.new
280
+ @object.state = 'parked'
281
+
282
+ @path = StateMachine::Path.new(@object, @machine)
283
+ @path.concat([
284
+ @ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
285
+ @park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)
286
+ ])
287
+ end
288
+
289
+ def test_should_be_complete
290
+ assert_equal true, @path.complete?
291
+ end
292
+
293
+ def test_should_not_be_able_to_walk
294
+ walked = false
295
+ @path.walk { walked = true }
296
+ assert_equal false, walked
297
+ end
298
+ end
299
+
300
+ class PathWithUnreachedTargetTest < Test::Unit::TestCase
301
+ def setup
302
+ @klass = Class.new
303
+ @machine = StateMachine::Machine.new(@klass)
304
+ @machine.state :parked, :idling
305
+ @machine.event :ignite do
306
+ transition :parked => :idling
307
+ end
308
+
309
+ @object = @klass.new
310
+ @object.state = 'parked'
311
+
312
+ @path = StateMachine::Path.new(@object, @machine, :target => :parked)
313
+ @path.concat([
314
+ @ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
315
+ ])
316
+ end
317
+
318
+ def test_should_not_be_complete
319
+ assert_equal false, @path.complete?
320
+ end
321
+
322
+ def test_should_not_be_able_to_walk
323
+ walked = false
324
+ @path.walk { walked = true }
325
+ assert_equal false, walked
326
+ end
327
+ end
328
+
329
+ class PathWithReachedTargetTest < Test::Unit::TestCase
330
+ def setup
331
+ @klass = Class.new
332
+ @machine = StateMachine::Machine.new(@klass)
333
+ @machine.state :parked, :idling
334
+ @machine.event :ignite do
335
+ transition :parked => :idling
336
+ end
337
+ @machine.event :park do
338
+ transition :idling => :parked
339
+ end
340
+
341
+ @object = @klass.new
342
+ @object.state = 'parked'
343
+
344
+ @path = StateMachine::Path.new(@object, @machine, :target => :parked)
345
+ @path.concat([
346
+ @ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
347
+ @park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)
348
+ ])
349
+ end
350
+
351
+ def test_should_be_complete
352
+ assert_equal true, @path.complete?
353
+ end
354
+
355
+ def test_should_not_be_able_to_walk
356
+ walked = false
357
+ @path.walk { walked = true }
358
+ assert_equal false, walked
359
+ end
360
+ end
361
+
362
+ class PathWithAvailableTransitionsAfterReachingTargetTest < Test::Unit::TestCase
363
+ def setup
364
+ @klass = Class.new
365
+ @machine = StateMachine::Machine.new(@klass)
366
+ @machine.state :parked, :idling
367
+ @machine.event :ignite do
368
+ transition :parked => :idling
369
+ end
370
+ @machine.event :shift_up do
371
+ transition :parked => :first_gear
372
+ end
373
+ @machine.event :park do
374
+ transition [:idling, :first_gear] => :parked
375
+ end
376
+
377
+ @object = @klass.new
378
+ @object.state = 'parked'
379
+
380
+ @path = StateMachine::Path.new(@object, @machine, :target => :parked)
381
+ @path.concat([
382
+ @ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
383
+ @park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)
384
+ ])
385
+ end
386
+
387
+ def test_should_be_complete
388
+ assert_equal true, @path.complete?
389
+ end
390
+
391
+ def test_should_be_able_to_walk
392
+ paths = []
393
+ @path.walk {|path| paths << path}
394
+ assert_equal [
395
+ [@ignite_transition, @park_transition, StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear)]
396
+ ], paths
397
+ end
398
+ end
399
+
400
+ class PathWithDeepTargetTest < Test::Unit::TestCase
401
+ def setup
402
+ @klass = Class.new
403
+ @machine = StateMachine::Machine.new(@klass)
404
+ @machine.state :parked, :idling
405
+ @machine.event :ignite do
406
+ transition :parked => :idling
407
+ end
408
+ @machine.event :shift_up do
409
+ transition :parked => :first_gear
410
+ end
411
+ @machine.event :park do
412
+ transition [:idling, :first_gear] => :parked
413
+ end
414
+
415
+ @object = @klass.new
416
+ @object.state = 'parked'
417
+
418
+ @path = StateMachine::Path.new(@object, @machine, :target => :parked)
419
+ @path.concat([
420
+ @ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
421
+ @park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked),
422
+ @shift_up_transition = StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear)
423
+ ])
424
+ end
425
+
426
+ def test_should_not_be_complete
427
+ assert_equal false, @path.complete?
428
+ end
429
+
430
+ def test_should_be_able_to_walk
431
+ paths = []
432
+ @path.walk {|path| paths << path}
433
+ assert_equal [
434
+ [@ignite_transition, @park_transition, @shift_up_transition, StateMachine::Transition.new(@object, @machine, :park, :first_gear, :parked)]
435
+ ], paths
436
+ end
437
+ end
438
+
439
+ class PathWithDeepTargetReachedTest < Test::Unit::TestCase
440
+ def setup
441
+ @klass = Class.new
442
+ @machine = StateMachine::Machine.new(@klass)
443
+ @machine.state :parked, :idling
444
+ @machine.event :ignite do
445
+ transition :parked => :idling
446
+ end
447
+ @machine.event :shift_up do
448
+ transition :parked => :first_gear
449
+ end
450
+ @machine.event :park do
451
+ transition [:idling, :first_gear] => :parked
452
+ end
453
+
454
+ @object = @klass.new
455
+ @object.state = 'parked'
456
+
457
+ @path = StateMachine::Path.new(@object, @machine, :target => :parked)
458
+ @path.concat([
459
+ @ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
460
+ @park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked),
461
+ @shift_up_transition = StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear),
462
+ @park_transition_2 = StateMachine::Transition.new(@object, @machine, :park, :first_gear, :parked)
463
+ ])
464
+ end
465
+
466
+ def test_should_be_complete
467
+ assert_equal true, @path.complete?
468
+ end
469
+
470
+ def test_should_not_be_able_to_walk
471
+ walked = false
472
+ @path.walk { walked = true }
473
+ assert_equal false, walked
474
+ end
475
+
476
+ def test_should_not_be_able_to_walk_with_available_transitions
477
+ @machine.event :park do
478
+ transition :parked => same
479
+ end
480
+
481
+ walked = false
482
+ @path.walk { walked = true }
483
+ assert_equal false, walked
484
+ end
485
+ end