state_machine 0.1.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 +101 -0
- data/MIT-LICENSE +20 -0
- data/README +97 -0
- data/Rakefile +79 -0
- data/init.rb +1 -0
- data/lib/state_machine.rb +92 -0
- data/lib/state_machine/event.rb +127 -0
- data/lib/state_machine/machine.rb +141 -0
- data/lib/state_machine/transition.rb +75 -0
- data/test/app_root/app/models/auto_shop.rb +34 -0
- data/test/app_root/app/models/car.rb +19 -0
- data/test/app_root/app/models/highway.rb +3 -0
- data/test/app_root/app/models/motorcycle.rb +3 -0
- data/test/app_root/app/models/switch.rb +12 -0
- data/test/app_root/app/models/toggle_switch.rb +2 -0
- data/test/app_root/app/models/vehicle.rb +71 -0
- data/test/app_root/db/migrate/001_create_switches.rb +12 -0
- data/test/app_root/db/migrate/002_create_auto_shops.rb +13 -0
- data/test/app_root/db/migrate/003_create_highways.rb +11 -0
- data/test/app_root/db/migrate/004_create_vehicles.rb +16 -0
- data/test/factory.rb +61 -0
- data/test/functional/state_machine_test.rb +443 -0
- data/test/test_helper.rb +13 -0
- data/test/unit/event_test.rb +234 -0
- data/test/unit/machine_test.rb +152 -0
- data/test/unit/state_machine_test.rb +102 -0
- data/test/unit/transition_test.rb +298 -0
- metadata +91 -0
@@ -0,0 +1,443 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
class VehicleTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@vehicle = create_vehicle
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_should_not_allow_access_to_subclass_events
|
9
|
+
assert !@vehicle.respond_to?(:reverse!)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class VehicleParkedTest < Test::Unit::TestCase
|
14
|
+
def setup
|
15
|
+
@vehicle = create_vehicle
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_be_in_parked_state
|
19
|
+
assert_equal 'parked', @vehicle.state
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_should_not_have_the_seatbelt_on
|
23
|
+
assert !@vehicle.seatbelt_on
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_should_not_allow_park
|
27
|
+
assert !@vehicle.park!
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_should_allow_ignite
|
31
|
+
assert @vehicle.ignite!
|
32
|
+
assert_equal 'idling', @vehicle.state
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_should_not_allow_idle
|
36
|
+
assert !@vehicle.idle!
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_should_not_allow_shift_up
|
40
|
+
assert !@vehicle.shift_up!
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_should_not_allow_shift_down
|
44
|
+
assert !@vehicle.shift_down!
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_should_not_allow_crash
|
48
|
+
assert !@vehicle.crash!
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_should_not_allow_repair
|
52
|
+
assert !@vehicle.repair!
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class VehicleIdlingTest < Test::Unit::TestCase
|
57
|
+
def setup
|
58
|
+
@vehicle = create_vehicle
|
59
|
+
@vehicle.ignite!
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_should_be_in_idling_state
|
63
|
+
assert_equal 'idling', @vehicle.state
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_should_have_seatbelt_on
|
67
|
+
assert @vehicle.seatbelt_on
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_should_allow_park
|
71
|
+
assert @vehicle.park!
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_should_not_allow_idle
|
75
|
+
assert !@vehicle.idle!
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_should_allow_shift_up
|
79
|
+
assert @vehicle.shift_up!
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_should_not_allow_shift_down
|
83
|
+
assert !@vehicle.shift_down!
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_should_not_allow_crash
|
87
|
+
assert !@vehicle.crash!
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_should_not_allow_repair
|
91
|
+
assert !@vehicle.repair!
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
class VehicleFirstGearTest < Test::Unit::TestCase
|
96
|
+
def setup
|
97
|
+
@vehicle = create_vehicle
|
98
|
+
@vehicle.ignite!
|
99
|
+
@vehicle.shift_up!
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_should_be_in_first_gear_state
|
103
|
+
assert_equal 'first_gear', @vehicle.state
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_should_allow_park
|
107
|
+
assert @vehicle.park!
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_should_allow_idle
|
111
|
+
assert @vehicle.idle!
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_should_allow_shift_up
|
115
|
+
assert @vehicle.shift_up!
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_should_not_allow_shift_down
|
119
|
+
assert !@vehicle.shift_down!
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_should_allow_crash
|
123
|
+
assert @vehicle.crash!
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_should_not_allow_repair
|
127
|
+
assert !@vehicle.repair!
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
class VehicleSecondGearTest < Test::Unit::TestCase
|
132
|
+
def setup
|
133
|
+
@vehicle = create_vehicle
|
134
|
+
@vehicle.ignite!
|
135
|
+
2.times {@vehicle.shift_up!}
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_should_be_in_second_gear_state
|
139
|
+
assert_equal 'second_gear', @vehicle.state
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_should_not_allow_park
|
143
|
+
assert !@vehicle.park!
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_should_not_allow_idle
|
147
|
+
assert !@vehicle.idle!
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_should_allow_shift_up
|
151
|
+
assert @vehicle.shift_up!
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_should_allow_shift_down
|
155
|
+
assert @vehicle.shift_down!
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_should_allow_crash
|
159
|
+
assert @vehicle.crash!
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_should_not_allow_repair
|
163
|
+
assert !@vehicle.repair!
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
class VehicleThirdGearTest < Test::Unit::TestCase
|
168
|
+
def setup
|
169
|
+
@vehicle = create_vehicle
|
170
|
+
@vehicle.ignite!
|
171
|
+
3.times {@vehicle.shift_up!}
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_should_be_in_third_gear_state
|
175
|
+
assert_equal 'third_gear', @vehicle.state
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_should_not_allow_park
|
179
|
+
assert !@vehicle.park!
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_should_not_allow_idle
|
183
|
+
assert !@vehicle.idle!
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_should_not_allow_shift_up
|
187
|
+
assert !@vehicle.shift_up!
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_should_allow_shift_down
|
191
|
+
assert @vehicle.shift_down!
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_should_allow_crash
|
195
|
+
assert @vehicle.crash!
|
196
|
+
end
|
197
|
+
|
198
|
+
def test_should_not_allow_repair
|
199
|
+
assert !@vehicle.repair!
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
class VehicleStalledTest < Test::Unit::TestCase
|
204
|
+
def setup
|
205
|
+
@vehicle = create_vehicle
|
206
|
+
@vehicle.ignite!
|
207
|
+
@vehicle.shift_up!
|
208
|
+
@vehicle.crash!
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_should_be_in_stalled_state
|
212
|
+
assert_equal 'stalled', @vehicle.state
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_should_be_towed
|
216
|
+
assert @vehicle.auto_shop.busy?
|
217
|
+
assert_equal 1, @vehicle.auto_shop.num_customers
|
218
|
+
end
|
219
|
+
|
220
|
+
def test_should_have_an_increased_insurance_premium
|
221
|
+
assert_equal 150, @vehicle.insurance_premium
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_should_not_allow_park
|
225
|
+
assert !@vehicle.park!
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_should_allow_ignite
|
229
|
+
assert @vehicle.ignite!
|
230
|
+
end
|
231
|
+
|
232
|
+
def test_should_not_change_state_when_ignited
|
233
|
+
assert_equal 'stalled', @vehicle.state
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_should_not_allow_idle
|
237
|
+
assert !@vehicle.idle!
|
238
|
+
end
|
239
|
+
|
240
|
+
def test_should_now_allow_shift_up
|
241
|
+
assert !@vehicle.shift_up!
|
242
|
+
end
|
243
|
+
|
244
|
+
def test_should_not_allow_shift_down
|
245
|
+
assert !@vehicle.shift_down!
|
246
|
+
end
|
247
|
+
|
248
|
+
def test_should_not_allow_crash
|
249
|
+
assert !@vehicle.crash!
|
250
|
+
end
|
251
|
+
|
252
|
+
def test_should_allow_repair_if_auto_shop_is_busy
|
253
|
+
assert @vehicle.repair!
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_should_not_allow_repair_if_auto_shop_is_available
|
257
|
+
@vehicle.auto_shop.fix_vehicle!
|
258
|
+
assert !@vehicle.repair!
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
class VehicleRepairedTest < Test::Unit::TestCase
|
263
|
+
def setup
|
264
|
+
@vehicle = create_vehicle
|
265
|
+
@vehicle.ignite!
|
266
|
+
@vehicle.shift_up!
|
267
|
+
@vehicle.crash!
|
268
|
+
@vehicle.repair!
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_should_be_in_parked_state
|
272
|
+
assert_equal 'parked', @vehicle.state
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_should_not_have_a_busy_auto_shop
|
276
|
+
assert @vehicle.auto_shop.available?
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
class MotorcycleTest < Test::Unit::TestCase
|
281
|
+
def setup
|
282
|
+
@motorcycle = create_motorcycle
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_should_be_in_idling_state
|
286
|
+
assert_equal 'idling', @motorcycle.state
|
287
|
+
end
|
288
|
+
|
289
|
+
def test_should_allow_park
|
290
|
+
assert @motorcycle.park!
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_should_not_allow_ignite
|
294
|
+
assert !@motorcycle.ignite!
|
295
|
+
end
|
296
|
+
|
297
|
+
def test_should_allow_shift_up
|
298
|
+
assert @motorcycle.shift_up!
|
299
|
+
end
|
300
|
+
|
301
|
+
def test_should_not_allow_shift_down
|
302
|
+
assert !@motorcycle.shift_down!
|
303
|
+
end
|
304
|
+
|
305
|
+
def test_should_not_allow_crash
|
306
|
+
assert !@motorcycle.crash!
|
307
|
+
end
|
308
|
+
|
309
|
+
def test_should_not_allow_repair
|
310
|
+
assert !@motorcycle.repair!
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
314
|
+
class CarTest < Test::Unit::TestCase
|
315
|
+
def setup
|
316
|
+
@car = create_car
|
317
|
+
end
|
318
|
+
|
319
|
+
def test_should_be_in_parked_state
|
320
|
+
assert_equal 'parked', @car.state
|
321
|
+
end
|
322
|
+
|
323
|
+
def test_should_not_have_the_seatbelt_on
|
324
|
+
assert !@car.seatbelt_on
|
325
|
+
end
|
326
|
+
|
327
|
+
def test_should_not_allow_park
|
328
|
+
assert !@car.park!
|
329
|
+
end
|
330
|
+
|
331
|
+
def test_should_allow_ignite
|
332
|
+
assert @car.ignite!
|
333
|
+
assert_equal 'idling', @car.state
|
334
|
+
end
|
335
|
+
|
336
|
+
def test_should_not_allow_idle
|
337
|
+
assert !@car.idle!
|
338
|
+
end
|
339
|
+
|
340
|
+
def test_should_not_allow_shift_up
|
341
|
+
assert !@car.shift_up!
|
342
|
+
end
|
343
|
+
|
344
|
+
def test_should_not_allow_shift_down
|
345
|
+
assert !@car.shift_down!
|
346
|
+
end
|
347
|
+
|
348
|
+
def test_should_not_allow_crash
|
349
|
+
assert !@car.crash!
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_should_not_allow_repair
|
353
|
+
assert !@car.repair!
|
354
|
+
end
|
355
|
+
|
356
|
+
def test_should_allow_reverse
|
357
|
+
assert @car.reverse!
|
358
|
+
end
|
359
|
+
end
|
360
|
+
|
361
|
+
class CarBackingUpTest < Test::Unit::TestCase
|
362
|
+
def setup
|
363
|
+
@car = create_car
|
364
|
+
@car.reverse!
|
365
|
+
end
|
366
|
+
|
367
|
+
def test_should_be_in_backing_up_state
|
368
|
+
assert_equal 'backing_up', @car.state
|
369
|
+
end
|
370
|
+
|
371
|
+
def test_should_allow_park
|
372
|
+
assert @car.park!
|
373
|
+
end
|
374
|
+
|
375
|
+
def test_should_not_allow_ignite
|
376
|
+
assert !@car.ignite!
|
377
|
+
end
|
378
|
+
|
379
|
+
def test_should_allow_idle
|
380
|
+
assert @car.idle!
|
381
|
+
end
|
382
|
+
|
383
|
+
def test_should_allow_shift_up
|
384
|
+
assert @car.shift_up!
|
385
|
+
end
|
386
|
+
|
387
|
+
def test_should_not_allow_shift_down
|
388
|
+
assert !@car.shift_down!
|
389
|
+
end
|
390
|
+
|
391
|
+
def test_should_not_allow_crash
|
392
|
+
assert !@car.crash!
|
393
|
+
end
|
394
|
+
|
395
|
+
def test_should_not_allow_repair
|
396
|
+
assert !@car.repair!
|
397
|
+
end
|
398
|
+
|
399
|
+
def test_should_not_allow_reverse
|
400
|
+
assert !@car.reverse!
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
class AutoShopAvailableTest < Test::Unit::TestCase
|
405
|
+
def setup
|
406
|
+
@auto_shop = create_auto_shop
|
407
|
+
end
|
408
|
+
|
409
|
+
def test_should_be_in_available_state
|
410
|
+
assert_equal 'available', @auto_shop.state
|
411
|
+
end
|
412
|
+
|
413
|
+
def test_should_allow_tow_vehicle
|
414
|
+
assert @auto_shop.tow_vehicle!
|
415
|
+
end
|
416
|
+
|
417
|
+
def test_should_not_allow_fix_vehicle
|
418
|
+
assert !@auto_shop.fix_vehicle!
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
class AutoShopBusyTest < Test::Unit::TestCase
|
423
|
+
def setup
|
424
|
+
@auto_shop = create_auto_shop
|
425
|
+
@auto_shop.tow_vehicle!
|
426
|
+
end
|
427
|
+
|
428
|
+
def test_should_be_in_busy_state
|
429
|
+
assert_equal 'busy', @auto_shop.state
|
430
|
+
end
|
431
|
+
|
432
|
+
def test_should_have_incremented_number_of_customers
|
433
|
+
assert_equal 1, @auto_shop.num_customers
|
434
|
+
end
|
435
|
+
|
436
|
+
def test_should_not_allow_tow_vehicle
|
437
|
+
assert !@auto_shop.tow_vehicle!
|
438
|
+
end
|
439
|
+
|
440
|
+
def test_should_allow_fix_vehicle
|
441
|
+
assert @auto_shop.fix_vehicle!
|
442
|
+
end
|
443
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Load the plugin testing framework
|
2
|
+
$:.unshift("#{File.dirname(__FILE__)}/../../plugin_test_helper/lib")
|
3
|
+
require 'rubygems'
|
4
|
+
require 'plugin_test_helper'
|
5
|
+
|
6
|
+
# Run the migrations
|
7
|
+
ActiveRecord::Migrator.migrate("#{RAILS_ROOT}/db/migrate")
|
8
|
+
|
9
|
+
# Mixin the factory helper
|
10
|
+
require File.expand_path("#{File.dirname(__FILE__)}/factory")
|
11
|
+
class Test::Unit::TestCase #:nodoc:
|
12
|
+
include Factory
|
13
|
+
end
|