state_machine 0.4.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +17 -0
- data/LICENSE +1 -1
- data/README.rdoc +54 -84
- data/Rakefile +1 -1
- data/examples/Car_state.png +0 -0
- data/examples/Vehicle_state.png +0 -0
- data/examples/auto_shop.rb +11 -0
- data/examples/car.rb +19 -0
- data/examples/traffic_light.rb +9 -0
- data/examples/vehicle.rb +35 -0
- data/lib/state_machine.rb +65 -52
- data/lib/state_machine/assertions.rb +1 -1
- data/lib/state_machine/callback.rb +13 -9
- data/lib/state_machine/eval_helpers.rb +4 -3
- data/lib/state_machine/event.rb +51 -33
- data/lib/state_machine/extensions.rb +2 -2
- data/lib/state_machine/guard.rb +47 -41
- data/lib/state_machine/integrations.rb +67 -0
- data/lib/state_machine/integrations/active_record.rb +62 -36
- data/lib/state_machine/integrations/active_record/observer.rb +41 -0
- data/lib/state_machine/integrations/data_mapper.rb +23 -37
- data/lib/state_machine/integrations/data_mapper/observer.rb +23 -9
- data/lib/state_machine/integrations/sequel.rb +23 -24
- data/lib/state_machine/machine.rb +380 -277
- data/lib/state_machine/node_collection.rb +142 -0
- data/lib/state_machine/state.rb +114 -69
- data/lib/state_machine/state_collection.rb +38 -0
- data/lib/state_machine/transition.rb +36 -17
- data/test/active_record.log +2940 -85664
- data/test/functional/state_machine_test.rb +49 -53
- data/test/sequel.log +747 -11990
- data/test/unit/assertions_test.rb +2 -1
- data/test/unit/callback_test.rb +14 -12
- data/test/unit/eval_helpers_test.rb +25 -6
- data/test/unit/event_test.rb +144 -124
- data/test/unit/guard_test.rb +118 -140
- data/test/unit/integrations/active_record_test.rb +102 -68
- data/test/unit/integrations/data_mapper_test.rb +48 -37
- data/test/unit/integrations/sequel_test.rb +34 -25
- data/test/unit/integrations_test.rb +42 -0
- data/test/unit/machine_test.rb +460 -531
- data/test/unit/node_collection_test.rb +208 -0
- data/test/unit/state_collection_test.rb +167 -0
- data/test/unit/state_machine_test.rb +1 -1
- data/test/unit/state_test.rb +223 -200
- data/test/unit/transition_test.rb +81 -46
- metadata +17 -3
- data/test/data_mapper.log +0 -30860
data/test/unit/guard_test.rb
CHANGED
@@ -2,15 +2,16 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
|
2
2
|
|
3
3
|
class GuardTest < Test::Unit::TestCase
|
4
4
|
def setup
|
5
|
-
@guard = StateMachine::Guard.new(:to =>
|
5
|
+
@guard = StateMachine::Guard.new(:to => :idling, :from => :parked)
|
6
6
|
end
|
7
7
|
|
8
8
|
def test_should_raise_exception_if_invalid_option_specified
|
9
|
-
assert_raise(ArgumentError) { StateMachine::Guard.new(:invalid => true) }
|
9
|
+
exception = assert_raise(ArgumentError) { StateMachine::Guard.new(:invalid => true) }
|
10
|
+
assert_equal 'Invalid key(s): invalid', exception.message
|
10
11
|
end
|
11
12
|
|
12
13
|
def test_should_have_requirements
|
13
|
-
expected = {:to =>
|
14
|
+
expected = {:to => [:idling], :from => [:parked]}
|
14
15
|
assert_equal expected, @guard.requirements
|
15
16
|
end
|
16
17
|
end
|
@@ -30,134 +31,134 @@ class GuardWithNoRequirementsTest < Test::Unit::TestCase
|
|
30
31
|
end
|
31
32
|
|
32
33
|
def test_should_match_non_empty_query
|
33
|
-
assert @guard.matches?(@object, :
|
34
|
+
assert @guard.matches?(@object, :to => :idling, :from => :parked, :on => :ignite)
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
37
|
-
class
|
38
|
+
class GuardWithFromRequirementTest < Test::Unit::TestCase
|
38
39
|
def setup
|
39
40
|
@object = Object.new
|
40
|
-
@guard = StateMachine::Guard.new(:
|
41
|
+
@guard = StateMachine::Guard.new(:from => :parked)
|
41
42
|
end
|
42
43
|
|
43
44
|
def test_should_match_if_not_specified
|
44
|
-
assert @guard.matches?(@object, :
|
45
|
+
assert @guard.matches?(@object, :to => :idling)
|
45
46
|
end
|
46
47
|
|
47
48
|
def test_should_match_if_included
|
48
|
-
assert @guard.matches?(@object, :
|
49
|
+
assert @guard.matches?(@object, :from => :parked)
|
49
50
|
end
|
50
51
|
|
51
52
|
def test_should_not_match_if_not_included
|
52
|
-
assert !@guard.matches?(@object, :
|
53
|
+
assert !@guard.matches?(@object, :from => :idling)
|
53
54
|
end
|
54
55
|
|
55
56
|
def test_should_not_match_if_nil
|
56
|
-
assert !@guard.matches?(@object, :
|
57
|
+
assert !@guard.matches?(@object, :from => nil)
|
57
58
|
end
|
58
59
|
|
59
|
-
def
|
60
|
-
assert @guard.matches?(@object, :
|
60
|
+
def test_should_ignore_to
|
61
|
+
assert @guard.matches?(@object, :from => :parked, :to => :idling)
|
61
62
|
end
|
62
63
|
|
63
64
|
def test_should_ignore_on
|
64
|
-
assert @guard.matches?(@object, :
|
65
|
+
assert @guard.matches?(@object, :from => :parked, :on => :ignite)
|
65
66
|
end
|
66
67
|
|
67
68
|
def test_should_be_included_in_known_states
|
68
|
-
assert_equal
|
69
|
+
assert_equal [:parked], @guard.known_states
|
69
70
|
end
|
70
71
|
end
|
71
72
|
|
72
|
-
class
|
73
|
+
class GuardWithMultipleFromRequirementsTest < Test::Unit::TestCase
|
73
74
|
def setup
|
74
75
|
@object = Object.new
|
75
|
-
@guard = StateMachine::Guard.new(:
|
76
|
+
@guard = StateMachine::Guard.new(:from => [:idling, :parked])
|
76
77
|
end
|
77
78
|
|
78
79
|
def test_should_match_if_included
|
79
|
-
assert @guard.matches?(@object, :
|
80
|
+
assert @guard.matches?(@object, :from => :idling)
|
80
81
|
end
|
81
82
|
|
82
83
|
def test_should_not_match_if_not_included
|
83
|
-
assert !@guard.matches?(@object, :
|
84
|
+
assert !@guard.matches?(@object, :from => :first_gear)
|
84
85
|
end
|
85
86
|
|
86
87
|
def test_should_be_included_in_known_states
|
87
|
-
assert_equal
|
88
|
+
assert_equal [:idling, :parked], @guard.known_states
|
88
89
|
end
|
89
90
|
end
|
90
91
|
|
91
|
-
class
|
92
|
+
class GuardWithToRequirementTest < Test::Unit::TestCase
|
92
93
|
def setup
|
93
94
|
@object = Object.new
|
94
|
-
@guard = StateMachine::Guard.new(:
|
95
|
+
@guard = StateMachine::Guard.new(:to => :idling)
|
95
96
|
end
|
96
97
|
|
97
98
|
def test_should_match_if_not_specified
|
98
|
-
assert @guard.matches?(@object, :
|
99
|
+
assert @guard.matches?(@object, :from => :parked)
|
99
100
|
end
|
100
101
|
|
101
102
|
def test_should_match_if_included
|
102
|
-
assert @guard.matches?(@object, :
|
103
|
+
assert @guard.matches?(@object, :to => :idling)
|
103
104
|
end
|
104
105
|
|
105
106
|
def test_should_not_match_if_not_included
|
106
|
-
assert !@guard.matches?(@object, :
|
107
|
+
assert !@guard.matches?(@object, :to => :parked)
|
107
108
|
end
|
108
109
|
|
109
110
|
def test_should_not_match_if_nil
|
110
|
-
assert !@guard.matches?(@object, :
|
111
|
+
assert !@guard.matches?(@object, :to => nil)
|
111
112
|
end
|
112
113
|
|
113
|
-
def
|
114
|
-
assert @guard.matches?(@object, :
|
114
|
+
def test_should_ignore_from
|
115
|
+
assert @guard.matches?(@object, :to => :idling, :from => :parked)
|
115
116
|
end
|
116
117
|
|
117
118
|
def test_should_ignore_on
|
118
|
-
assert @guard.matches?(@object, :
|
119
|
+
assert @guard.matches?(@object, :to => :idling, :on => :ignite)
|
119
120
|
end
|
120
121
|
|
121
122
|
def test_should_be_included_in_known_states
|
122
|
-
assert_equal
|
123
|
+
assert_equal [:idling], @guard.known_states
|
123
124
|
end
|
124
125
|
end
|
125
126
|
|
126
|
-
class
|
127
|
+
class GuardWithMultipleToRequirementsTest < Test::Unit::TestCase
|
127
128
|
def setup
|
128
129
|
@object = Object.new
|
129
|
-
@guard = StateMachine::Guard.new(:
|
130
|
+
@guard = StateMachine::Guard.new(:to => [:idling, :parked])
|
130
131
|
end
|
131
132
|
|
132
133
|
def test_should_match_if_included
|
133
|
-
assert @guard.matches?(@object, :
|
134
|
+
assert @guard.matches?(@object, :to => :idling)
|
134
135
|
end
|
135
136
|
|
136
137
|
def test_should_not_match_if_not_included
|
137
|
-
assert !@guard.matches?(@object, :
|
138
|
+
assert !@guard.matches?(@object, :to => :first_gear)
|
138
139
|
end
|
139
140
|
|
140
141
|
def test_should_be_included_in_known_states
|
141
|
-
assert_equal
|
142
|
+
assert_equal [:idling, :parked], @guard.known_states
|
142
143
|
end
|
143
144
|
end
|
144
145
|
|
145
146
|
class GuardWithOnRequirementTest < Test::Unit::TestCase
|
146
147
|
def setup
|
147
148
|
@object = Object.new
|
148
|
-
@guard = StateMachine::Guard.new(:on =>
|
149
|
+
@guard = StateMachine::Guard.new(:on => :ignite)
|
149
150
|
end
|
150
151
|
|
151
152
|
def test_should_match_if_not_specified
|
152
|
-
assert @guard.matches?(@object, :from =>
|
153
|
+
assert @guard.matches?(@object, :from => :parked)
|
153
154
|
end
|
154
155
|
|
155
156
|
def test_should_match_if_included
|
156
|
-
assert @guard.matches?(@object, :on =>
|
157
|
+
assert @guard.matches?(@object, :on => :ignite)
|
157
158
|
end
|
158
159
|
|
159
160
|
def test_should_not_match_if_not_included
|
160
|
-
assert !@guard.matches?(@object, :on =>
|
161
|
+
assert !@guard.matches?(@object, :on => :park)
|
161
162
|
end
|
162
163
|
|
163
164
|
def test_should_not_match_if_nil
|
@@ -165,11 +166,11 @@ class GuardWithOnRequirementTest < Test::Unit::TestCase
|
|
165
166
|
end
|
166
167
|
|
167
168
|
def test_should_ignore_to
|
168
|
-
assert @guard.matches?(@object, :on =>
|
169
|
+
assert @guard.matches?(@object, :on => :ignite, :to => :parked)
|
169
170
|
end
|
170
171
|
|
171
172
|
def test_should_ignore_from
|
172
|
-
assert @guard.matches?(@object, :on =>
|
173
|
+
assert @guard.matches?(@object, :on => :ignite, :from => :parked)
|
173
174
|
end
|
174
175
|
|
175
176
|
def test_should_not_be_included_in_known_states
|
@@ -180,130 +181,130 @@ end
|
|
180
181
|
class GuardWithMultipleOnRequirementsTest < Test::Unit::TestCase
|
181
182
|
def setup
|
182
183
|
@object = Object.new
|
183
|
-
@guard = StateMachine::Guard.new(:on =>
|
184
|
+
@guard = StateMachine::Guard.new(:on => [:ignite, :park])
|
184
185
|
end
|
185
186
|
|
186
187
|
def test_should_match_if_included
|
187
|
-
assert @guard.matches?(@object, :on =>
|
188
|
+
assert @guard.matches?(@object, :on => :ignite)
|
188
189
|
end
|
189
190
|
|
190
191
|
def test_should_not_match_if_not_included
|
191
|
-
assert !@guard.matches?(@object, :on =>
|
192
|
+
assert !@guard.matches?(@object, :on => :shift_up)
|
192
193
|
end
|
193
194
|
end
|
194
195
|
|
195
|
-
class
|
196
|
+
class GuardWithExceptFromRequirementTest < Test::Unit::TestCase
|
196
197
|
def setup
|
197
198
|
@object = Object.new
|
198
|
-
@guard = StateMachine::Guard.new(:
|
199
|
+
@guard = StateMachine::Guard.new(:except_from => :parked)
|
199
200
|
end
|
200
201
|
|
201
202
|
def test_should_match_if_not_included
|
202
|
-
assert @guard.matches?(@object, :
|
203
|
+
assert @guard.matches?(@object, :from => :idling)
|
203
204
|
end
|
204
205
|
|
205
206
|
def test_should_not_match_if_included
|
206
|
-
assert !@guard.matches?(@object, :
|
207
|
+
assert !@guard.matches?(@object, :from => :parked)
|
207
208
|
end
|
208
209
|
|
209
210
|
def test_should_match_if_nil
|
210
|
-
assert @guard.matches?(@object, :
|
211
|
+
assert @guard.matches?(@object, :from => nil)
|
211
212
|
end
|
212
213
|
|
213
|
-
def
|
214
|
-
assert @guard.matches?(@object, :
|
214
|
+
def test_should_ignore_to
|
215
|
+
assert @guard.matches?(@object, :from => :idling, :to => :parked)
|
215
216
|
end
|
216
217
|
|
217
218
|
def test_should_ignore_on
|
218
|
-
assert @guard.matches?(@object, :
|
219
|
+
assert @guard.matches?(@object, :from => :idling, :on => :ignite)
|
219
220
|
end
|
220
221
|
|
221
222
|
def test_should_be_included_in_known_states
|
222
|
-
assert_equal
|
223
|
+
assert_equal [:parked], @guard.known_states
|
223
224
|
end
|
224
225
|
end
|
225
226
|
|
226
|
-
class
|
227
|
+
class GuardWithMultipleExceptFromRequirementsTest < Test::Unit::TestCase
|
227
228
|
def setup
|
228
229
|
@object = Object.new
|
229
|
-
@guard = StateMachine::Guard.new(:
|
230
|
+
@guard = StateMachine::Guard.new(:except_from => [:idling, :parked])
|
230
231
|
end
|
231
232
|
|
232
233
|
def test_should_match_if_not_included
|
233
|
-
assert @guard.matches?(@object, :
|
234
|
+
assert @guard.matches?(@object, :from => :first_gear)
|
234
235
|
end
|
235
236
|
|
236
237
|
def test_should_not_match_if_included
|
237
|
-
assert !@guard.matches?(@object, :
|
238
|
+
assert !@guard.matches?(@object, :from => :idling)
|
238
239
|
end
|
239
240
|
|
240
241
|
def test_should_be_included_in_known_states
|
241
|
-
assert_equal
|
242
|
+
assert_equal [:idling, :parked], @guard.known_states
|
242
243
|
end
|
243
244
|
end
|
244
245
|
|
245
|
-
class
|
246
|
+
class GuardWithExceptToRequirementTest < Test::Unit::TestCase
|
246
247
|
def setup
|
247
248
|
@object = Object.new
|
248
|
-
@guard = StateMachine::Guard.new(:
|
249
|
+
@guard = StateMachine::Guard.new(:except_to => :idling)
|
249
250
|
end
|
250
251
|
|
251
252
|
def test_should_match_if_not_included
|
252
|
-
assert @guard.matches?(@object, :
|
253
|
+
assert @guard.matches?(@object, :to => :parked)
|
253
254
|
end
|
254
255
|
|
255
256
|
def test_should_not_match_if_included
|
256
|
-
assert !@guard.matches?(@object, :
|
257
|
+
assert !@guard.matches?(@object, :to => :idling)
|
257
258
|
end
|
258
259
|
|
259
260
|
def test_should_match_if_nil
|
260
|
-
assert @guard.matches?(@object, :
|
261
|
+
assert @guard.matches?(@object, :to => nil)
|
261
262
|
end
|
262
263
|
|
263
|
-
def
|
264
|
-
assert @guard.matches?(@object, :
|
264
|
+
def test_should_ignore_from
|
265
|
+
assert @guard.matches?(@object, :to => :parked, :from => :idling)
|
265
266
|
end
|
266
267
|
|
267
268
|
def test_should_ignore_on
|
268
|
-
assert @guard.matches?(@object, :
|
269
|
+
assert @guard.matches?(@object, :to => :parked, :on => :ignite)
|
269
270
|
end
|
270
271
|
|
271
272
|
def test_should_be_included_in_known_states
|
272
|
-
assert_equal
|
273
|
+
assert_equal [:idling], @guard.known_states
|
273
274
|
end
|
274
275
|
end
|
275
276
|
|
276
|
-
class
|
277
|
+
class GuardWithMultipleExceptToRequirementsTest < Test::Unit::TestCase
|
277
278
|
def setup
|
278
279
|
@object = Object.new
|
279
|
-
@guard = StateMachine::Guard.new(:
|
280
|
+
@guard = StateMachine::Guard.new(:except_to => [:idling, :parked])
|
280
281
|
end
|
281
282
|
|
282
283
|
def test_should_match_if_not_included
|
283
|
-
assert @guard.matches?(@object, :
|
284
|
+
assert @guard.matches?(@object, :to => :first_gear)
|
284
285
|
end
|
285
286
|
|
286
287
|
def test_should_not_match_if_included
|
287
|
-
assert !@guard.matches?(@object, :
|
288
|
+
assert !@guard.matches?(@object, :to => :idling)
|
288
289
|
end
|
289
290
|
|
290
291
|
def test_should_be_included_in_known_states
|
291
|
-
assert_equal
|
292
|
+
assert_equal [:idling, :parked], @guard.known_states
|
292
293
|
end
|
293
294
|
end
|
294
295
|
|
295
296
|
class GuardWithExceptOnRequirementTest < Test::Unit::TestCase
|
296
297
|
def setup
|
297
298
|
@object = Object.new
|
298
|
-
@guard = StateMachine::Guard.new(:except_on =>
|
299
|
+
@guard = StateMachine::Guard.new(:except_on => :ignite)
|
299
300
|
end
|
300
301
|
|
301
302
|
def test_should_match_if_not_included
|
302
|
-
assert @guard.matches?(@object, :on =>
|
303
|
+
assert @guard.matches?(@object, :on => :park)
|
303
304
|
end
|
304
305
|
|
305
306
|
def test_should_not_match_if_included
|
306
|
-
assert !@guard.matches?(@object, :on =>
|
307
|
+
assert !@guard.matches?(@object, :on => :ignite)
|
307
308
|
end
|
308
309
|
|
309
310
|
def test_should_match_if_nil
|
@@ -311,11 +312,11 @@ class GuardWithExceptOnRequirementTest < Test::Unit::TestCase
|
|
311
312
|
end
|
312
313
|
|
313
314
|
def test_should_ignore_to
|
314
|
-
assert @guard.matches?(@object, :on =>
|
315
|
+
assert @guard.matches?(@object, :on => :park, :to => :idling)
|
315
316
|
end
|
316
317
|
|
317
318
|
def test_should_ignore_from
|
318
|
-
assert @guard.matches?(@object, :on =>
|
319
|
+
assert @guard.matches?(@object, :on => :park, :from => :parked)
|
319
320
|
end
|
320
321
|
|
321
322
|
def test_should_not_be_included_in_known_states
|
@@ -326,55 +327,55 @@ end
|
|
326
327
|
class GuardWithMultipleExceptOnRequirementsTest < Test::Unit::TestCase
|
327
328
|
def setup
|
328
329
|
@object = Object.new
|
329
|
-
@guard = StateMachine::Guard.new(:except_on =>
|
330
|
+
@guard = StateMachine::Guard.new(:except_on => [:ignite, :park])
|
330
331
|
end
|
331
332
|
|
332
333
|
def test_should_match_if_not_included
|
333
|
-
assert @guard.matches?(@object, :on =>
|
334
|
+
assert @guard.matches?(@object, :on => :shift_up)
|
334
335
|
end
|
335
336
|
|
336
337
|
def test_should_not_match_if_included
|
337
|
-
assert !@guard.matches?(@object, :on =>
|
338
|
+
assert !@guard.matches?(@object, :on => :ignite)
|
338
339
|
end
|
339
340
|
end
|
340
341
|
|
341
|
-
class
|
342
|
+
class GuardWithConflictingFromRequirementsTest < Test::Unit::TestCase
|
342
343
|
def setup
|
343
344
|
@object = Object.new
|
344
|
-
@guard = StateMachine::Guard.new(:
|
345
|
+
@guard = StateMachine::Guard.new(:from => :parked, :except_from => :parked)
|
345
346
|
end
|
346
347
|
|
347
348
|
def test_should_ignore_except_requirement
|
348
|
-
assert @guard.matches?(@object, :
|
349
|
+
assert @guard.matches?(@object, :from => :parked)
|
349
350
|
end
|
350
351
|
end
|
351
352
|
|
352
|
-
class
|
353
|
+
class GuardWithConflictingToRequirementsTest < Test::Unit::TestCase
|
353
354
|
def setup
|
354
355
|
@object = Object.new
|
355
|
-
@guard = StateMachine::Guard.new(:
|
356
|
+
@guard = StateMachine::Guard.new(:to => :idling, :except_to => :idling)
|
356
357
|
end
|
357
358
|
|
358
359
|
def test_should_ignore_except_requirement
|
359
|
-
assert @guard.matches?(@object, :
|
360
|
+
assert @guard.matches?(@object, :to => :idling)
|
360
361
|
end
|
361
362
|
end
|
362
363
|
|
363
364
|
class GuardWithConflictingOnRequirementsTest < Test::Unit::TestCase
|
364
365
|
def setup
|
365
366
|
@object = Object.new
|
366
|
-
@guard = StateMachine::Guard.new(:on =>
|
367
|
+
@guard = StateMachine::Guard.new(:on => :ignite, :except_on => :ignite)
|
367
368
|
end
|
368
369
|
|
369
370
|
def test_should_ignore_except_requirement
|
370
|
-
assert @guard.matches?(@object, :on =>
|
371
|
+
assert @guard.matches?(@object, :on => :ignite)
|
371
372
|
end
|
372
373
|
end
|
373
374
|
|
374
375
|
class GuardWithDifferentRequirementsTest < Test::Unit::TestCase
|
375
376
|
def setup
|
376
377
|
@object = Object.new
|
377
|
-
@guard = StateMachine::Guard.new(:from =>
|
378
|
+
@guard = StateMachine::Guard.new(:from => :parked, :to => :idling, :on => :ignite)
|
378
379
|
end
|
379
380
|
|
380
381
|
def test_should_match_empty_query
|
@@ -382,28 +383,28 @@ class GuardWithDifferentRequirementsTest < Test::Unit::TestCase
|
|
382
383
|
end
|
383
384
|
|
384
385
|
def test_should_match_if_all_requirements_match
|
385
|
-
assert @guard.matches?(@object, :from =>
|
386
|
+
assert @guard.matches?(@object, :from => :parked, :to => :idling, :on => :ignite)
|
386
387
|
end
|
387
388
|
|
388
389
|
def test_should_not_match_if_from_not_included
|
389
|
-
assert !@guard.matches?(@object, :from =>
|
390
|
+
assert !@guard.matches?(@object, :from => :idling)
|
390
391
|
end
|
391
392
|
|
392
393
|
def test_should_not_match_if_to_not_included
|
393
|
-
assert !@guard.matches?(@object, :to =>
|
394
|
+
assert !@guard.matches?(@object, :to => :parked)
|
394
395
|
end
|
395
396
|
|
396
397
|
def test_should_not_match_if_on_not_included
|
397
|
-
assert !@guard.matches?(@object, :on =>
|
398
|
+
assert !@guard.matches?(@object, :on => :park)
|
398
399
|
end
|
399
400
|
|
400
401
|
def test_should_include_all_known_states
|
401
|
-
assert_equal
|
402
|
+
assert_equal [:parked, :idling], @guard.known_states
|
402
403
|
end
|
403
404
|
|
404
405
|
def test_should_not_duplicate_known_statse
|
405
|
-
guard = StateMachine::Guard.new(:except_from =>
|
406
|
-
assert_equal
|
406
|
+
guard = StateMachine::Guard.new(:except_from => :idling, :to => :idling, :on => :ignite)
|
407
|
+
assert_equal [:idling], guard.known_states
|
407
408
|
end
|
408
409
|
end
|
409
410
|
|
@@ -422,11 +423,11 @@ class GuardWithNilRequirementsTest < Test::Unit::TestCase
|
|
422
423
|
end
|
423
424
|
|
424
425
|
def test_should_not_match_if_from_not_included
|
425
|
-
assert !@guard.matches?(@object, :from =>
|
426
|
+
assert !@guard.matches?(@object, :from => :parked)
|
426
427
|
end
|
427
428
|
|
428
429
|
def test_should_not_match_if_to_not_included
|
429
|
-
assert !@guard.matches?(@object, :to =>
|
430
|
+
assert !@guard.matches?(@object, :to => :idling)
|
430
431
|
end
|
431
432
|
|
432
433
|
def test_should_include_all_known_states
|
@@ -490,13 +491,13 @@ begin
|
|
490
491
|
class GuardDrawingTest < Test::Unit::TestCase
|
491
492
|
def setup
|
492
493
|
@machine = StateMachine::Machine.new(Class.new)
|
493
|
-
states =
|
494
|
+
states = [:parked, :idling]
|
494
495
|
|
495
496
|
graph = GraphViz.new('G')
|
496
|
-
states.each {|state| graph.add_node(state)}
|
497
|
+
states.each {|state| graph.add_node(state.to_s)}
|
497
498
|
|
498
|
-
@guard = StateMachine::Guard.new(:from =>
|
499
|
-
@edges = @guard.draw(graph,
|
499
|
+
@guard = StateMachine::Guard.new(:from => :idling, :to => :parked)
|
500
|
+
@edges = @guard.draw(graph, :park, states)
|
500
501
|
end
|
501
502
|
|
502
503
|
def test_should_create_edges
|
@@ -519,19 +520,19 @@ begin
|
|
519
520
|
class GuardDrawingWithFromRequirementTest < Test::Unit::TestCase
|
520
521
|
def setup
|
521
522
|
@machine = StateMachine::Machine.new(Class.new)
|
522
|
-
states =
|
523
|
+
states = [:parked, :idling, :first_gear]
|
523
524
|
|
524
525
|
graph = GraphViz.new('G')
|
525
|
-
states.each {|state| graph.add_node(state)}
|
526
|
+
states.each {|state| graph.add_node(state.to_s)}
|
526
527
|
|
527
|
-
@guard = StateMachine::Guard.new(:from =>
|
528
|
-
@edges = @guard.draw(graph,
|
528
|
+
@guard = StateMachine::Guard.new(:from => [:idling, :first_gear], :to => :parked)
|
529
|
+
@edges = @guard.draw(graph, :park, states)
|
529
530
|
end
|
530
531
|
|
531
532
|
def test_should_generate_edges_for_each_valid_from_state
|
532
|
-
|
533
|
+
[:idling, :first_gear].each_with_index do |from_state, index|
|
533
534
|
edge = @edges[index]
|
534
|
-
assert_equal from_state, edge.instance_variable_get('@xNodeOne')
|
535
|
+
assert_equal from_state.to_s, edge.instance_variable_get('@xNodeOne')
|
535
536
|
assert_equal 'parked', edge.instance_variable_get('@xNodeTwo')
|
536
537
|
end
|
537
538
|
end
|
@@ -540,13 +541,13 @@ begin
|
|
540
541
|
class GuardDrawingWithExceptFromRequirementTest < Test::Unit::TestCase
|
541
542
|
def setup
|
542
543
|
@machine = StateMachine::Machine.new(Class.new)
|
543
|
-
states =
|
544
|
+
states = [:parked, :idling, :first_gear]
|
544
545
|
|
545
546
|
graph = GraphViz.new('G')
|
546
|
-
states.each {|state| graph.add_node(state)}
|
547
|
+
states.each {|state| graph.add_node(state.to_s)}
|
547
548
|
|
548
|
-
@guard = StateMachine::Guard.new(:except_from =>
|
549
|
-
@edges = @guard.draw(graph,
|
549
|
+
@guard = StateMachine::Guard.new(:except_from => :parked, :to => :parked)
|
550
|
+
@edges = @guard.draw(graph, :park, states)
|
550
551
|
end
|
551
552
|
|
552
553
|
def test_should_generate_edges_for_each_valid_from_state
|
@@ -561,13 +562,13 @@ begin
|
|
561
562
|
class GuardDrawingWithoutFromRequirementTest < Test::Unit::TestCase
|
562
563
|
def setup
|
563
564
|
@machine = StateMachine::Machine.new(Class.new)
|
564
|
-
states =
|
565
|
+
states = [:parked, :idling, :first_gear]
|
565
566
|
|
566
567
|
graph = GraphViz.new('G')
|
567
|
-
states.each {|state| graph.add_node(state)}
|
568
|
+
states.each {|state| graph.add_node(state.to_s)}
|
568
569
|
|
569
|
-
@guard = StateMachine::Guard.new(:to =>
|
570
|
-
@edges = @guard.draw(graph,
|
570
|
+
@guard = StateMachine::Guard.new(:to => :parked)
|
571
|
+
@edges = @guard.draw(graph, :park, states)
|
571
572
|
end
|
572
573
|
|
573
574
|
def test_should_generate_edges_for_each_valid_from_state
|
@@ -586,8 +587,8 @@ begin
|
|
586
587
|
graph = GraphViz.new('G')
|
587
588
|
graph.add_node('parked')
|
588
589
|
|
589
|
-
@guard = StateMachine::Guard.new(:from =>
|
590
|
-
@edges = @guard.draw(graph,
|
590
|
+
@guard = StateMachine::Guard.new(:from => :parked)
|
591
|
+
@edges = @guard.draw(graph, :park, [:parked])
|
591
592
|
end
|
592
593
|
|
593
594
|
def test_should_create_loopback_edge
|
@@ -595,29 +596,6 @@ begin
|
|
595
596
|
assert_equal 'parked', @edges.first.instance_variable_get('@xNodeTwo')
|
596
597
|
end
|
597
598
|
end
|
598
|
-
|
599
|
-
class GuardWithProcStatesTest < Test::Unit::TestCase
|
600
|
-
def setup
|
601
|
-
@machine = StateMachine::Machine.new(Class.new)
|
602
|
-
@from_state = lambda {}
|
603
|
-
@to_state = lambda {}
|
604
|
-
states = [@from_state, @to_state]
|
605
|
-
|
606
|
-
graph = GraphViz.new('G')
|
607
|
-
states.each {|state| graph.add_node("lambda#{state.object_id.abs}")}
|
608
|
-
|
609
|
-
@guard = StateMachine::Guard.new(:from => @from_state, :to => @to_state)
|
610
|
-
@edges = @guard.draw(graph, 'park', states)
|
611
|
-
end
|
612
|
-
|
613
|
-
def test_should_use_state_id_for_from_state
|
614
|
-
assert_equal "lambda#{@from_state.object_id.abs}", @edges.first.instance_variable_get('@xNodeOne')
|
615
|
-
end
|
616
|
-
|
617
|
-
def test_should_use_state_id_for_to_state
|
618
|
-
assert_equal "lambda#{@to_state.object_id.abs}", @edges.first.instance_variable_get('@xNodeTwo')
|
619
|
-
end
|
620
|
-
end
|
621
599
|
rescue LoadError
|
622
600
|
$stderr.puts 'Skipping GraphViz StateMachine::Guard tests. `gem install ruby-graphviz` and try again.'
|
623
601
|
end
|