enum_state_machine 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rvmrc +1 -0
  4. data/Appraisals +28 -0
  5. data/CHANGELOG.md +0 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE +23 -0
  8. data/README.md +4 -0
  9. data/Rakefile +43 -0
  10. data/enum_state_machine.gemspec +25 -0
  11. data/gemfiles/active_model_4.0.4.gemfile +9 -0
  12. data/gemfiles/active_model_4.0.4.gemfile.lock +51 -0
  13. data/gemfiles/active_record_4.0.4.gemfile +11 -0
  14. data/gemfiles/active_record_4.0.4.gemfile.lock +61 -0
  15. data/gemfiles/default.gemfile +7 -0
  16. data/gemfiles/default.gemfile.lock +27 -0
  17. data/gemfiles/graphviz_1.0.9.gemfile +7 -0
  18. data/gemfiles/graphviz_1.0.9.gemfile.lock +30 -0
  19. data/lib/enum_state_machine/assertions.rb +36 -0
  20. data/lib/enum_state_machine/branch.rb +225 -0
  21. data/lib/enum_state_machine/callback.rb +232 -0
  22. data/lib/enum_state_machine/core.rb +12 -0
  23. data/lib/enum_state_machine/core_ext/class/state_machine.rb +5 -0
  24. data/lib/enum_state_machine/core_ext.rb +2 -0
  25. data/lib/enum_state_machine/error.rb +13 -0
  26. data/lib/enum_state_machine/eval_helpers.rb +87 -0
  27. data/lib/enum_state_machine/event.rb +257 -0
  28. data/lib/enum_state_machine/event_collection.rb +141 -0
  29. data/lib/enum_state_machine/extensions.rb +149 -0
  30. data/lib/enum_state_machine/graph.rb +92 -0
  31. data/lib/enum_state_machine/helper_module.rb +17 -0
  32. data/lib/enum_state_machine/initializers/rails.rb +22 -0
  33. data/lib/enum_state_machine/initializers.rb +4 -0
  34. data/lib/enum_state_machine/integrations/active_model/locale.rb +11 -0
  35. data/lib/enum_state_machine/integrations/active_model/observer.rb +33 -0
  36. data/lib/enum_state_machine/integrations/active_model/observer_update.rb +42 -0
  37. data/lib/enum_state_machine/integrations/active_model/versions.rb +31 -0
  38. data/lib/enum_state_machine/integrations/active_model.rb +585 -0
  39. data/lib/enum_state_machine/integrations/active_record/locale.rb +20 -0
  40. data/lib/enum_state_machine/integrations/active_record/versions.rb +123 -0
  41. data/lib/enum_state_machine/integrations/active_record.rb +548 -0
  42. data/lib/enum_state_machine/integrations/base.rb +100 -0
  43. data/lib/enum_state_machine/integrations.rb +97 -0
  44. data/lib/enum_state_machine/machine.rb +2292 -0
  45. data/lib/enum_state_machine/machine_collection.rb +86 -0
  46. data/lib/enum_state_machine/macro_methods.rb +518 -0
  47. data/lib/enum_state_machine/matcher.rb +123 -0
  48. data/lib/enum_state_machine/matcher_helpers.rb +54 -0
  49. data/lib/enum_state_machine/node_collection.rb +222 -0
  50. data/lib/enum_state_machine/path.rb +120 -0
  51. data/lib/enum_state_machine/path_collection.rb +90 -0
  52. data/lib/enum_state_machine/state.rb +297 -0
  53. data/lib/enum_state_machine/state_collection.rb +112 -0
  54. data/lib/enum_state_machine/state_context.rb +138 -0
  55. data/lib/enum_state_machine/state_enum.rb +23 -0
  56. data/lib/enum_state_machine/transition.rb +470 -0
  57. data/lib/enum_state_machine/transition_collection.rb +245 -0
  58. data/lib/enum_state_machine/version.rb +3 -0
  59. data/lib/enum_state_machine/yard/handlers/base.rb +32 -0
  60. data/lib/enum_state_machine/yard/handlers/event.rb +25 -0
  61. data/lib/enum_state_machine/yard/handlers/machine.rb +344 -0
  62. data/lib/enum_state_machine/yard/handlers/state.rb +25 -0
  63. data/lib/enum_state_machine/yard/handlers/transition.rb +47 -0
  64. data/lib/enum_state_machine/yard/handlers.rb +12 -0
  65. data/lib/enum_state_machine/yard/templates/default/class/html/setup.rb +30 -0
  66. data/lib/enum_state_machine/yard/templates/default/class/html/state_machines.erb +12 -0
  67. data/lib/enum_state_machine/yard/templates.rb +3 -0
  68. data/lib/enum_state_machine/yard.rb +8 -0
  69. data/lib/enum_state_machine.rb +9 -0
  70. data/lib/tasks/enum_state_machine.rake +1 -0
  71. data/lib/tasks/enum_state_machine.rb +24 -0
  72. data/lib/yard-enum_state_machine.rb +2 -0
  73. data/test/files/en.yml +9 -0
  74. data/test/files/switch.rb +15 -0
  75. data/test/functional/state_machine_test.rb +1066 -0
  76. data/test/test_helper.rb +7 -0
  77. data/test/unit/assertions_test.rb +40 -0
  78. data/test/unit/branch_test.rb +969 -0
  79. data/test/unit/callback_test.rb +704 -0
  80. data/test/unit/error_test.rb +43 -0
  81. data/test/unit/eval_helpers_test.rb +270 -0
  82. data/test/unit/event_collection_test.rb +398 -0
  83. data/test/unit/event_test.rb +1196 -0
  84. data/test/unit/graph_test.rb +98 -0
  85. data/test/unit/helper_module_test.rb +17 -0
  86. data/test/unit/integrations/active_model_test.rb +1245 -0
  87. data/test/unit/integrations/active_record_test.rb +2551 -0
  88. data/test/unit/integrations/base_test.rb +104 -0
  89. data/test/unit/integrations_test.rb +71 -0
  90. data/test/unit/invalid_event_test.rb +20 -0
  91. data/test/unit/invalid_parallel_transition_test.rb +18 -0
  92. data/test/unit/invalid_transition_test.rb +115 -0
  93. data/test/unit/machine_collection_test.rb +603 -0
  94. data/test/unit/machine_test.rb +3395 -0
  95. data/test/unit/matcher_helpers_test.rb +37 -0
  96. data/test/unit/matcher_test.rb +155 -0
  97. data/test/unit/node_collection_test.rb +362 -0
  98. data/test/unit/path_collection_test.rb +266 -0
  99. data/test/unit/path_test.rb +485 -0
  100. data/test/unit/state_collection_test.rb +352 -0
  101. data/test/unit/state_context_test.rb +441 -0
  102. data/test/unit/state_enum_test.rb +50 -0
  103. data/test/unit/state_machine_test.rb +31 -0
  104. data/test/unit/state_test.rb +1101 -0
  105. data/test/unit/transition_collection_test.rb +2168 -0
  106. data/test/unit/transition_test.rb +1558 -0
  107. metadata +249 -0
@@ -0,0 +1,969 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class BranchTest < Test::Unit::TestCase
4
+ def setup
5
+ @branch = EnumStateMachine::Branch.new(:from => :parked, :to => :idling)
6
+ end
7
+
8
+ def test_should_not_raise_exception_if_implicit_option_specified
9
+ assert_nothing_raised { EnumStateMachine::Branch.new(:invalid => :valid) }
10
+ end
11
+
12
+ def test_should_not_have_an_if_condition
13
+ assert_nil @branch.if_condition
14
+ end
15
+
16
+ def test_should_not_have_an_unless_condition
17
+ assert_nil @branch.unless_condition
18
+ end
19
+
20
+ def test_should_have_a_state_requirement
21
+ assert_equal 1, @branch.state_requirements.length
22
+ end
23
+
24
+ def test_should_raise_an_exception_if_invalid_match_option_specified
25
+ exception = assert_raise(ArgumentError) { @branch.match(Object.new, :invalid => true) }
26
+ assert_equal 'Invalid key(s): invalid', exception.message
27
+ end
28
+ end
29
+
30
+ class BranchWithNoRequirementsTest < Test::Unit::TestCase
31
+ def setup
32
+ @object = Object.new
33
+ @branch = EnumStateMachine::Branch.new
34
+ end
35
+
36
+ def test_should_use_all_matcher_for_event_requirement
37
+ assert_equal EnumStateMachine::AllMatcher.instance, @branch.event_requirement
38
+ end
39
+
40
+ def test_should_use_all_matcher_for_from_state_requirement
41
+ assert_equal EnumStateMachine::AllMatcher.instance, @branch.state_requirements.first[:from]
42
+ end
43
+
44
+ def test_should_use_all_matcher_for_to_state_requirement
45
+ assert_equal EnumStateMachine::AllMatcher.instance, @branch.state_requirements.first[:to]
46
+ end
47
+
48
+ def test_should_match_empty_query
49
+ assert @branch.matches?(@object, {})
50
+ end
51
+
52
+ def test_should_match_non_empty_query
53
+ assert @branch.matches?(@object, :to => :idling, :from => :parked, :on => :ignite)
54
+ end
55
+
56
+ def test_should_include_all_requirements_in_match
57
+ match = @branch.match(@object, {})
58
+
59
+ assert_equal @branch.state_requirements.first[:from], match[:from]
60
+ assert_equal @branch.state_requirements.first[:to], match[:to]
61
+ assert_equal @branch.event_requirement, match[:on]
62
+ end
63
+ end
64
+
65
+ class BranchWithFromRequirementTest < Test::Unit::TestCase
66
+ def setup
67
+ @object = Object.new
68
+ @branch = EnumStateMachine::Branch.new(:from => :parked)
69
+ end
70
+
71
+ def test_should_use_a_whitelist_matcher
72
+ assert_instance_of EnumStateMachine::WhitelistMatcher, @branch.state_requirements.first[:from]
73
+ end
74
+
75
+ def test_should_match_if_not_specified
76
+ assert @branch.matches?(@object, :to => :idling)
77
+ end
78
+
79
+ def test_should_match_if_included
80
+ assert @branch.matches?(@object, :from => :parked)
81
+ end
82
+
83
+ def test_should_not_match_if_not_included
84
+ assert !@branch.matches?(@object, :from => :idling)
85
+ end
86
+
87
+ def test_should_not_match_if_nil
88
+ assert !@branch.matches?(@object, :from => nil)
89
+ end
90
+
91
+ def test_should_ignore_to
92
+ assert @branch.matches?(@object, :from => :parked, :to => :idling)
93
+ end
94
+
95
+ def test_should_ignore_on
96
+ assert @branch.matches?(@object, :from => :parked, :on => :ignite)
97
+ end
98
+
99
+ def test_should_be_included_in_known_states
100
+ assert_equal [:parked], @branch.known_states
101
+ end
102
+
103
+ def test_should_include_requirement_in_match
104
+ match = @branch.match(@object, :from => :parked)
105
+ assert_equal @branch.state_requirements.first[:from], match[:from]
106
+ end
107
+ end
108
+
109
+ class BranchWithMultipleFromRequirementsTest < Test::Unit::TestCase
110
+ def setup
111
+ @object = Object.new
112
+ @branch = EnumStateMachine::Branch.new(:from => [:idling, :parked])
113
+ end
114
+
115
+ def test_should_match_if_included
116
+ assert @branch.matches?(@object, :from => :idling)
117
+ end
118
+
119
+ def test_should_not_match_if_not_included
120
+ assert !@branch.matches?(@object, :from => :first_gear)
121
+ end
122
+
123
+ def test_should_be_included_in_known_states
124
+ assert_equal [:idling, :parked], @branch.known_states
125
+ end
126
+ end
127
+
128
+ class BranchWithFromMatcherRequirementTest < Test::Unit::TestCase
129
+ def setup
130
+ @object = Object.new
131
+ @branch = EnumStateMachine::Branch.new(:from => EnumStateMachine::BlacklistMatcher.new([:idling, :parked]))
132
+ end
133
+
134
+ def test_should_match_if_included
135
+ assert @branch.matches?(@object, :from => :first_gear)
136
+ end
137
+
138
+ def test_should_not_match_if_not_included
139
+ assert !@branch.matches?(@object, :from => :idling)
140
+ end
141
+
142
+ def test_include_values_in_known_states
143
+ assert_equal [:idling, :parked], @branch.known_states
144
+ end
145
+ end
146
+
147
+ class BranchWithToRequirementTest < Test::Unit::TestCase
148
+ def setup
149
+ @object = Object.new
150
+ @branch = EnumStateMachine::Branch.new(:to => :idling)
151
+ end
152
+
153
+ def test_should_use_a_whitelist_matcher
154
+ assert_instance_of EnumStateMachine::WhitelistMatcher, @branch.state_requirements.first[:to]
155
+ end
156
+
157
+ def test_should_match_if_not_specified
158
+ assert @branch.matches?(@object, :from => :parked)
159
+ end
160
+
161
+ def test_should_match_if_included
162
+ assert @branch.matches?(@object, :to => :idling)
163
+ end
164
+
165
+ def test_should_not_match_if_not_included
166
+ assert !@branch.matches?(@object, :to => :parked)
167
+ end
168
+
169
+ def test_should_not_match_if_nil
170
+ assert !@branch.matches?(@object, :to => nil)
171
+ end
172
+
173
+ def test_should_ignore_from
174
+ assert @branch.matches?(@object, :to => :idling, :from => :parked)
175
+ end
176
+
177
+ def test_should_ignore_on
178
+ assert @branch.matches?(@object, :to => :idling, :on => :ignite)
179
+ end
180
+
181
+ def test_should_be_included_in_known_states
182
+ assert_equal [:idling], @branch.known_states
183
+ end
184
+
185
+ def test_should_include_requirement_in_match
186
+ match = @branch.match(@object, :to => :idling)
187
+ assert_equal @branch.state_requirements.first[:to], match[:to]
188
+ end
189
+ end
190
+
191
+ class BranchWithMultipleToRequirementsTest < Test::Unit::TestCase
192
+ def setup
193
+ @object = Object.new
194
+ @branch = EnumStateMachine::Branch.new(:to => [:idling, :parked])
195
+ end
196
+
197
+ def test_should_match_if_included
198
+ assert @branch.matches?(@object, :to => :idling)
199
+ end
200
+
201
+ def test_should_not_match_if_not_included
202
+ assert !@branch.matches?(@object, :to => :first_gear)
203
+ end
204
+
205
+ def test_should_be_included_in_known_states
206
+ assert_equal [:idling, :parked], @branch.known_states
207
+ end
208
+ end
209
+
210
+ class BranchWithToMatcherRequirementTest < Test::Unit::TestCase
211
+ def setup
212
+ @object = Object.new
213
+ @branch = EnumStateMachine::Branch.new(:to => EnumStateMachine::BlacklistMatcher.new([:idling, :parked]))
214
+ end
215
+
216
+ def test_should_match_if_included
217
+ assert @branch.matches?(@object, :to => :first_gear)
218
+ end
219
+
220
+ def test_should_not_match_if_not_included
221
+ assert !@branch.matches?(@object, :to => :idling)
222
+ end
223
+
224
+ def test_include_values_in_known_states
225
+ assert_equal [:idling, :parked], @branch.known_states
226
+ end
227
+ end
228
+
229
+ class BranchWithOnRequirementTest < Test::Unit::TestCase
230
+ def setup
231
+ @object = Object.new
232
+ @branch = EnumStateMachine::Branch.new(:on => :ignite)
233
+ end
234
+
235
+ def test_should_use_a_whitelist_matcher
236
+ assert_instance_of EnumStateMachine::WhitelistMatcher, @branch.event_requirement
237
+ end
238
+
239
+ def test_should_match_if_not_specified
240
+ assert @branch.matches?(@object, :from => :parked)
241
+ end
242
+
243
+ def test_should_match_if_included
244
+ assert @branch.matches?(@object, :on => :ignite)
245
+ end
246
+
247
+ def test_should_not_match_if_not_included
248
+ assert !@branch.matches?(@object, :on => :park)
249
+ end
250
+
251
+ def test_should_not_match_if_nil
252
+ assert !@branch.matches?(@object, :on => nil)
253
+ end
254
+
255
+ def test_should_ignore_to
256
+ assert @branch.matches?(@object, :on => :ignite, :to => :parked)
257
+ end
258
+
259
+ def test_should_ignore_from
260
+ assert @branch.matches?(@object, :on => :ignite, :from => :parked)
261
+ end
262
+
263
+ def test_should_not_be_included_in_known_states
264
+ assert_equal [], @branch.known_states
265
+ end
266
+
267
+ def test_should_include_requirement_in_match
268
+ match = @branch.match(@object, :on => :ignite)
269
+ assert_equal @branch.event_requirement, match[:on]
270
+ end
271
+ end
272
+
273
+ class BranchWithMultipleOnRequirementsTest < Test::Unit::TestCase
274
+ def setup
275
+ @object = Object.new
276
+ @branch = EnumStateMachine::Branch.new(:on => [:ignite, :park])
277
+ end
278
+
279
+ def test_should_match_if_included
280
+ assert @branch.matches?(@object, :on => :ignite)
281
+ end
282
+
283
+ def test_should_not_match_if_not_included
284
+ assert !@branch.matches?(@object, :on => :shift_up)
285
+ end
286
+ end
287
+
288
+ class BranchWithOnMatcherRequirementTest < Test::Unit::TestCase
289
+ def setup
290
+ @object = Object.new
291
+ @branch = EnumStateMachine::Branch.new(:on => EnumStateMachine::BlacklistMatcher.new([:ignite, :park]))
292
+ end
293
+
294
+ def test_should_match_if_included
295
+ assert @branch.matches?(@object, :on => :shift_up)
296
+ end
297
+
298
+ def test_should_not_match_if_not_included
299
+ assert !@branch.matches?(@object, :on => :ignite)
300
+ end
301
+ end
302
+
303
+ class BranchWithExceptFromRequirementTest < Test::Unit::TestCase
304
+ def setup
305
+ @object = Object.new
306
+ @branch = EnumStateMachine::Branch.new(:except_from => :parked)
307
+ end
308
+
309
+ def test_should_use_a_blacklist_matcher
310
+ assert_instance_of EnumStateMachine::BlacklistMatcher, @branch.state_requirements.first[:from]
311
+ end
312
+
313
+ def test_should_match_if_not_included
314
+ assert @branch.matches?(@object, :from => :idling)
315
+ end
316
+
317
+ def test_should_not_match_if_included
318
+ assert !@branch.matches?(@object, :from => :parked)
319
+ end
320
+
321
+ def test_should_match_if_nil
322
+ assert @branch.matches?(@object, :from => nil)
323
+ end
324
+
325
+ def test_should_ignore_to
326
+ assert @branch.matches?(@object, :from => :idling, :to => :parked)
327
+ end
328
+
329
+ def test_should_ignore_on
330
+ assert @branch.matches?(@object, :from => :idling, :on => :ignite)
331
+ end
332
+
333
+ def test_should_be_included_in_known_states
334
+ assert_equal [:parked], @branch.known_states
335
+ end
336
+ end
337
+
338
+ class BranchWithMultipleExceptFromRequirementsTest < Test::Unit::TestCase
339
+ def setup
340
+ @object = Object.new
341
+ @branch = EnumStateMachine::Branch.new(:except_from => [:idling, :parked])
342
+ end
343
+
344
+ def test_should_match_if_not_included
345
+ assert @branch.matches?(@object, :from => :first_gear)
346
+ end
347
+
348
+ def test_should_not_match_if_included
349
+ assert !@branch.matches?(@object, :from => :idling)
350
+ end
351
+
352
+ def test_should_be_included_in_known_states
353
+ assert_equal [:idling, :parked], @branch.known_states
354
+ end
355
+ end
356
+
357
+ class BranchWithExceptFromMatcherRequirementTest < Test::Unit::TestCase
358
+ def test_should_raise_an_exception
359
+ exception = assert_raise(ArgumentError) { EnumStateMachine::Branch.new(:except_from => EnumStateMachine::AllMatcher.instance) }
360
+ assert_equal ':except_from option cannot use matchers; use :from instead', exception.message
361
+ end
362
+ end
363
+
364
+ class BranchWithExceptToRequirementTest < Test::Unit::TestCase
365
+ def setup
366
+ @object = Object.new
367
+ @branch = EnumStateMachine::Branch.new(:except_to => :idling)
368
+ end
369
+
370
+ def test_should_use_a_blacklist_matcher
371
+ assert_instance_of EnumStateMachine::BlacklistMatcher, @branch.state_requirements.first[:to]
372
+ end
373
+
374
+ def test_should_match_if_not_included
375
+ assert @branch.matches?(@object, :to => :parked)
376
+ end
377
+
378
+ def test_should_not_match_if_included
379
+ assert !@branch.matches?(@object, :to => :idling)
380
+ end
381
+
382
+ def test_should_match_if_nil
383
+ assert @branch.matches?(@object, :to => nil)
384
+ end
385
+
386
+ def test_should_ignore_from
387
+ assert @branch.matches?(@object, :to => :parked, :from => :idling)
388
+ end
389
+
390
+ def test_should_ignore_on
391
+ assert @branch.matches?(@object, :to => :parked, :on => :ignite)
392
+ end
393
+
394
+ def test_should_be_included_in_known_states
395
+ assert_equal [:idling], @branch.known_states
396
+ end
397
+ end
398
+
399
+ class BranchWithMultipleExceptToRequirementsTest < Test::Unit::TestCase
400
+ def setup
401
+ @object = Object.new
402
+ @branch = EnumStateMachine::Branch.new(:except_to => [:idling, :parked])
403
+ end
404
+
405
+ def test_should_match_if_not_included
406
+ assert @branch.matches?(@object, :to => :first_gear)
407
+ end
408
+
409
+ def test_should_not_match_if_included
410
+ assert !@branch.matches?(@object, :to => :idling)
411
+ end
412
+
413
+ def test_should_be_included_in_known_states
414
+ assert_equal [:idling, :parked], @branch.known_states
415
+ end
416
+ end
417
+
418
+ class BranchWithExceptToMatcherRequirementTest < Test::Unit::TestCase
419
+ def test_should_raise_an_exception
420
+ exception = assert_raise(ArgumentError) { EnumStateMachine::Branch.new(:except_to => EnumStateMachine::AllMatcher.instance) }
421
+ assert_equal ':except_to option cannot use matchers; use :to instead', exception.message
422
+ end
423
+ end
424
+
425
+ class BranchWithExceptOnRequirementTest < Test::Unit::TestCase
426
+ def setup
427
+ @object = Object.new
428
+ @branch = EnumStateMachine::Branch.new(:except_on => :ignite)
429
+ end
430
+
431
+ def test_should_use_a_blacklist_matcher
432
+ assert_instance_of EnumStateMachine::BlacklistMatcher, @branch.event_requirement
433
+ end
434
+
435
+ def test_should_match_if_not_included
436
+ assert @branch.matches?(@object, :on => :park)
437
+ end
438
+
439
+ def test_should_not_match_if_included
440
+ assert !@branch.matches?(@object, :on => :ignite)
441
+ end
442
+
443
+ def test_should_match_if_nil
444
+ assert @branch.matches?(@object, :on => nil)
445
+ end
446
+
447
+ def test_should_ignore_to
448
+ assert @branch.matches?(@object, :on => :park, :to => :idling)
449
+ end
450
+
451
+ def test_should_ignore_from
452
+ assert @branch.matches?(@object, :on => :park, :from => :parked)
453
+ end
454
+
455
+ def test_should_not_be_included_in_known_states
456
+ assert_equal [], @branch.known_states
457
+ end
458
+ end
459
+
460
+ class BranchWithExceptOnMatcherRequirementTest < Test::Unit::TestCase
461
+ def test_should_raise_an_exception
462
+ exception = assert_raise(ArgumentError) { EnumStateMachine::Branch.new(:except_on => EnumStateMachine::AllMatcher.instance) }
463
+ assert_equal ':except_on option cannot use matchers; use :on instead', exception.message
464
+ end
465
+ end
466
+
467
+ class BranchWithMultipleExceptOnRequirementsTest < Test::Unit::TestCase
468
+ def setup
469
+ @object = Object.new
470
+ @branch = EnumStateMachine::Branch.new(:except_on => [:ignite, :park])
471
+ end
472
+
473
+ def test_should_match_if_not_included
474
+ assert @branch.matches?(@object, :on => :shift_up)
475
+ end
476
+
477
+ def test_should_not_match_if_included
478
+ assert !@branch.matches?(@object, :on => :ignite)
479
+ end
480
+ end
481
+
482
+ class BranchWithConflictingFromRequirementsTest < Test::Unit::TestCase
483
+ def test_should_raise_an_exception
484
+ exception = assert_raise(ArgumentError) { EnumStateMachine::Branch.new(:from => :parked, :except_from => :parked) }
485
+ assert_equal 'Conflicting keys: from, except_from', exception.message
486
+ end
487
+ end
488
+
489
+ class BranchWithConflictingToRequirementsTest < Test::Unit::TestCase
490
+ def test_should_raise_an_exception
491
+ exception = assert_raise(ArgumentError) { EnumStateMachine::Branch.new(:to => :idling, :except_to => :idling) }
492
+ assert_equal 'Conflicting keys: to, except_to', exception.message
493
+ end
494
+ end
495
+
496
+ class BranchWithConflictingOnRequirementsTest < Test::Unit::TestCase
497
+ def test_should_raise_an_exception
498
+ exception = assert_raise(ArgumentError) { EnumStateMachine::Branch.new(:on => :ignite, :except_on => :ignite) }
499
+ assert_equal 'Conflicting keys: on, except_on', exception.message
500
+ end
501
+ end
502
+
503
+ class BranchWithDifferentRequirementsTest < Test::Unit::TestCase
504
+ def setup
505
+ @object = Object.new
506
+ @branch = EnumStateMachine::Branch.new(:from => :parked, :to => :idling, :on => :ignite)
507
+ end
508
+
509
+ def test_should_match_empty_query
510
+ assert @branch.matches?(@object)
511
+ end
512
+
513
+ def test_should_match_if_all_requirements_match
514
+ assert @branch.matches?(@object, :from => :parked, :to => :idling, :on => :ignite)
515
+ end
516
+
517
+ def test_should_not_match_if_from_not_included
518
+ assert !@branch.matches?(@object, :from => :idling)
519
+ end
520
+
521
+ def test_should_not_match_if_to_not_included
522
+ assert !@branch.matches?(@object, :to => :parked)
523
+ end
524
+
525
+ def test_should_not_match_if_on_not_included
526
+ assert !@branch.matches?(@object, :on => :park)
527
+ end
528
+
529
+ def test_should_be_nil_if_unmatched
530
+ assert_nil @branch.match(@object, :from => :parked, :to => :idling, :on => :park)
531
+ end
532
+
533
+ def test_should_include_all_known_states
534
+ assert_equal [:parked, :idling], @branch.known_states
535
+ end
536
+
537
+ def test_should_not_duplicate_known_statse
538
+ branch = EnumStateMachine::Branch.new(:except_from => :idling, :to => :idling, :on => :ignite)
539
+ assert_equal [:idling], branch.known_states
540
+ end
541
+ end
542
+
543
+ class BranchWithNilRequirementsTest < Test::Unit::TestCase
544
+ def setup
545
+ @object = Object.new
546
+ @branch = EnumStateMachine::Branch.new(:from => nil, :to => nil)
547
+ end
548
+
549
+ def test_should_match_empty_query
550
+ assert @branch.matches?(@object)
551
+ end
552
+
553
+ def test_should_match_if_all_requirements_match
554
+ assert @branch.matches?(@object, :from => nil, :to => nil)
555
+ end
556
+
557
+ def test_should_not_match_if_from_not_included
558
+ assert !@branch.matches?(@object, :from => :parked)
559
+ end
560
+
561
+ def test_should_not_match_if_to_not_included
562
+ assert !@branch.matches?(@object, :to => :idling)
563
+ end
564
+
565
+ def test_should_include_all_known_states
566
+ assert_equal [nil], @branch.known_states
567
+ end
568
+ end
569
+
570
+ class BranchWithImplicitRequirementTest < Test::Unit::TestCase
571
+ def setup
572
+ @branch = EnumStateMachine::Branch.new(:parked => :idling, :on => :ignite)
573
+ end
574
+
575
+ def test_should_create_an_event_requirement
576
+ assert_instance_of EnumStateMachine::WhitelistMatcher, @branch.event_requirement
577
+ assert_equal [:ignite], @branch.event_requirement.values
578
+ end
579
+
580
+ def test_should_use_a_whitelist_from_matcher
581
+ assert_instance_of EnumStateMachine::WhitelistMatcher, @branch.state_requirements.first[:from]
582
+ end
583
+
584
+ def test_should_use_a_whitelist_to_matcher
585
+ assert_instance_of EnumStateMachine::WhitelistMatcher, @branch.state_requirements.first[:to]
586
+ end
587
+ end
588
+
589
+ class BranchWithMultipleImplicitRequirementsTest < Test::Unit::TestCase
590
+ def setup
591
+ @object = Object.new
592
+ @branch = EnumStateMachine::Branch.new(:parked => :idling, :idling => :first_gear, :on => :ignite)
593
+ end
594
+
595
+ def test_should_create_multiple_state_requirements
596
+ assert_equal 2, @branch.state_requirements.length
597
+ end
598
+
599
+ def test_should_not_match_event_as_state_requirement
600
+ assert !@branch.matches?(@object, :from => :on, :to => :ignite)
601
+ end
602
+
603
+ def test_should_match_if_from_included_in_any
604
+ assert @branch.matches?(@object, :from => :parked)
605
+ assert @branch.matches?(@object, :from => :idling)
606
+ end
607
+
608
+ def test_should_not_match_if_from_not_included_in_any
609
+ assert !@branch.matches?(@object, :from => :first_gear)
610
+ end
611
+
612
+ def test_should_match_if_to_included_in_any
613
+ assert @branch.matches?(@object, :to => :idling)
614
+ assert @branch.matches?(@object, :to => :first_gear)
615
+ end
616
+
617
+ def test_should_not_match_if_to_not_included_in_any
618
+ assert !@branch.matches?(@object, :to => :parked)
619
+ end
620
+
621
+ def test_should_match_if_all_options_match
622
+ assert @branch.matches?(@object, :from => :parked, :to => :idling, :on => :ignite)
623
+ assert @branch.matches?(@object, :from => :idling, :to => :first_gear, :on => :ignite)
624
+ end
625
+
626
+ def test_should_not_match_if_any_options_do_not_match
627
+ assert !@branch.matches?(@object, :from => :parked, :to => :idling, :on => :park)
628
+ assert !@branch.matches?(@object, :from => :parked, :to => :first_gear, :on => :park)
629
+ end
630
+
631
+ def test_should_include_all_known_states
632
+ assert_equal [:first_gear, :idling, :parked], @branch.known_states.sort_by {|state| state.to_s}
633
+ end
634
+
635
+ def test_should_not_duplicate_known_statse
636
+ branch = EnumStateMachine::Branch.new(:parked => :idling, :first_gear => :idling)
637
+ assert_equal [:first_gear, :idling, :parked], branch.known_states.sort_by {|state| state.to_s}
638
+ end
639
+ end
640
+
641
+ class BranchWithImplicitFromRequirementMatcherTest < Test::Unit::TestCase
642
+ def setup
643
+ @matcher = EnumStateMachine::BlacklistMatcher.new(:parked)
644
+ @branch = EnumStateMachine::Branch.new(@matcher => :idling)
645
+ end
646
+
647
+ def test_should_not_convert_from_to_whitelist_matcher
648
+ assert_equal @matcher, @branch.state_requirements.first[:from]
649
+ end
650
+
651
+ def test_should_convert_to_to_whitelist_matcher
652
+ assert_instance_of EnumStateMachine::WhitelistMatcher, @branch.state_requirements.first[:to]
653
+ end
654
+ end
655
+
656
+ class BranchWithImplicitToRequirementMatcherTest < Test::Unit::TestCase
657
+ def setup
658
+ @matcher = EnumStateMachine::BlacklistMatcher.new(:idling)
659
+ @branch = EnumStateMachine::Branch.new(:parked => @matcher)
660
+ end
661
+
662
+ def test_should_convert_from_to_whitelist_matcher
663
+ assert_instance_of EnumStateMachine::WhitelistMatcher, @branch.state_requirements.first[:from]
664
+ end
665
+
666
+ def test_should_not_convert_to_to_whitelist_matcher
667
+ assert_equal @matcher, @branch.state_requirements.first[:to]
668
+ end
669
+ end
670
+
671
+ class BranchWithImplicitAndExplicitRequirementsTest < Test::Unit::TestCase
672
+ def setup
673
+ @branch = EnumStateMachine::Branch.new(:parked => :idling, :from => :parked)
674
+ end
675
+
676
+ def test_should_create_multiple_requirements
677
+ assert_equal 2, @branch.state_requirements.length
678
+ end
679
+
680
+ def test_should_create_implicit_requirements_for_implicit_options
681
+ assert(@branch.state_requirements.any? do |state_requirement|
682
+ state_requirement[:from].values == [:parked] && state_requirement[:to].values == [:idling]
683
+ end)
684
+ end
685
+
686
+ def test_should_create_implicit_requirements_for_explicit_options
687
+ assert(@branch.state_requirements.any? do |state_requirement|
688
+ state_requirement[:from].values == [:from] && state_requirement[:to].values == [:parked]
689
+ end)
690
+ end
691
+ end
692
+
693
+ class BranchWithIfConditionalTest < Test::Unit::TestCase
694
+ def setup
695
+ @object = Object.new
696
+ end
697
+
698
+ def test_should_have_an_if_condition
699
+ branch = EnumStateMachine::Branch.new(:if => lambda {true})
700
+ assert_not_nil branch.if_condition
701
+ end
702
+
703
+ def test_should_match_if_true
704
+ branch = EnumStateMachine::Branch.new(:if => lambda {true})
705
+ assert branch.matches?(@object)
706
+ end
707
+
708
+ def test_should_not_match_if_false
709
+ branch = EnumStateMachine::Branch.new(:if => lambda {false})
710
+ assert !branch.matches?(@object)
711
+ end
712
+
713
+ def test_should_be_nil_if_unmatched
714
+ branch = EnumStateMachine::Branch.new(:if => lambda {false})
715
+ assert_nil branch.match(@object)
716
+ end
717
+ end
718
+
719
+ class BranchWithMultipleIfConditionalsTest < Test::Unit::TestCase
720
+ def setup
721
+ @object = Object.new
722
+ end
723
+
724
+ def test_should_match_if_all_are_true
725
+ branch = EnumStateMachine::Branch.new(:if => [lambda {true}, lambda {true}])
726
+ assert branch.match(@object)
727
+ end
728
+
729
+ def test_should_not_match_if_any_are_false
730
+ branch = EnumStateMachine::Branch.new(:if => [lambda {true}, lambda {false}])
731
+ assert !branch.match(@object)
732
+
733
+ branch = EnumStateMachine::Branch.new(:if => [lambda {false}, lambda {true}])
734
+ assert !branch.match(@object)
735
+ end
736
+ end
737
+
738
+ class BranchWithUnlessConditionalTest < Test::Unit::TestCase
739
+ def setup
740
+ @object = Object.new
741
+ end
742
+
743
+ def test_should_have_an_unless_condition
744
+ branch = EnumStateMachine::Branch.new(:unless => lambda {true})
745
+ assert_not_nil branch.unless_condition
746
+ end
747
+
748
+ def test_should_match_if_false
749
+ branch = EnumStateMachine::Branch.new(:unless => lambda {false})
750
+ assert branch.matches?(@object)
751
+ end
752
+
753
+ def test_should_not_match_if_true
754
+ branch = EnumStateMachine::Branch.new(:unless => lambda {true})
755
+ assert !branch.matches?(@object)
756
+ end
757
+
758
+ def test_should_be_nil_if_unmatched
759
+ branch = EnumStateMachine::Branch.new(:unless => lambda {true})
760
+ assert_nil branch.match(@object)
761
+ end
762
+ end
763
+
764
+ class BranchWithMultipleUnlessConditionalsTest < Test::Unit::TestCase
765
+ def setup
766
+ @object = Object.new
767
+ end
768
+
769
+ def test_should_match_if_all_are_false
770
+ branch = EnumStateMachine::Branch.new(:unless => [lambda {false}, lambda {false}])
771
+ assert branch.match(@object)
772
+ end
773
+
774
+ def test_should_not_match_if_any_are_true
775
+ branch = EnumStateMachine::Branch.new(:unless => [lambda {true}, lambda {false}])
776
+ assert !branch.match(@object)
777
+
778
+ branch = EnumStateMachine::Branch.new(:unless => [lambda {false}, lambda {true}])
779
+ assert !branch.match(@object)
780
+ end
781
+ end
782
+
783
+ class BranchWithConflictingConditionalsTest < Test::Unit::TestCase
784
+ def setup
785
+ @object = Object.new
786
+ end
787
+
788
+ def test_should_match_if_if_is_true_and_unless_is_false
789
+ branch = EnumStateMachine::Branch.new(:if => lambda {true}, :unless => lambda {false})
790
+ assert branch.match(@object)
791
+ end
792
+
793
+ def test_should_not_match_if_if_is_false_and_unless_is_true
794
+ branch = EnumStateMachine::Branch.new(:if => lambda {false}, :unless => lambda {true})
795
+ assert !branch.match(@object)
796
+ end
797
+
798
+ def test_should_not_match_if_if_is_false_and_unless_is_false
799
+ branch = EnumStateMachine::Branch.new(:if => lambda {false}, :unless => lambda {false})
800
+ assert !branch.match(@object)
801
+ end
802
+
803
+ def test_should_not_match_if_if_is_true_and_unless_is_true
804
+ branch = EnumStateMachine::Branch.new(:if => lambda {true}, :unless => lambda {true})
805
+ assert !branch.match(@object)
806
+ end
807
+ end
808
+
809
+ class BranchWithoutGuardsTest < Test::Unit::TestCase
810
+ def setup
811
+ @object = Object.new
812
+ end
813
+
814
+ def test_should_match_if_if_is_false
815
+ branch = EnumStateMachine::Branch.new(:if => lambda {false})
816
+ assert branch.matches?(@object, :guard => false)
817
+ end
818
+
819
+ def test_should_match_if_if_is_true
820
+ branch = EnumStateMachine::Branch.new(:if => lambda {true})
821
+ assert branch.matches?(@object, :guard => false)
822
+ end
823
+
824
+ def test_should_match_if_unless_is_false
825
+ branch = EnumStateMachine::Branch.new(:unless => lambda {false})
826
+ assert branch.matches?(@object, :guard => false)
827
+ end
828
+
829
+ def test_should_match_if_unless_is_true
830
+ branch = EnumStateMachine::Branch.new(:unless => lambda {true})
831
+ assert branch.matches?(@object, :guard => false)
832
+ end
833
+ end
834
+
835
+ begin
836
+ # Load library
837
+ require 'graphviz'
838
+
839
+ class BranchDrawingTest < Test::Unit::TestCase
840
+ def setup
841
+ @machine = EnumStateMachine::Machine.new(Class.new)
842
+ states = [:parked, :idling]
843
+
844
+ @graph = EnumStateMachine::Graph.new('test')
845
+ states.each {|state| @graph.add_nodes(state.to_s)}
846
+
847
+ @branch = EnumStateMachine::Branch.new(:from => :idling, :to => :parked)
848
+ @branch.draw(@graph, :park, states)
849
+ @edge = @graph.get_edge_at_index(0)
850
+ end
851
+
852
+ def test_should_create_edges
853
+ assert_equal 1, @graph.edge_count
854
+ end
855
+
856
+ def test_should_use_from_state_from_start_node
857
+ assert_equal 'idling', @edge.node_one(false)
858
+ end
859
+
860
+ def test_should_use_to_state_for_end_node
861
+ assert_equal 'parked', @edge.node_two(false)
862
+ end
863
+
864
+ def test_should_use_event_name_as_label
865
+ assert_equal 'park', @edge['label'].to_s.gsub('"', '')
866
+ end
867
+ end
868
+
869
+ class BranchDrawingWithFromRequirementTest < Test::Unit::TestCase
870
+ def setup
871
+ @machine = EnumStateMachine::Machine.new(Class.new)
872
+ states = [:parked, :idling, :first_gear]
873
+
874
+ @graph = EnumStateMachine::Graph.new('test')
875
+ states.each {|state| @graph.add_nodes(state.to_s)}
876
+
877
+ @branch = EnumStateMachine::Branch.new(:from => [:idling, :first_gear], :to => :parked)
878
+ @branch.draw(@graph, :park, states)
879
+ end
880
+
881
+ def test_should_generate_edges_for_each_valid_from_state
882
+ [:idling, :first_gear].each_with_index do |from_state, index|
883
+ edge = @graph.get_edge_at_index(index)
884
+ assert_equal from_state.to_s, edge.node_one(false)
885
+ assert_equal 'parked', edge.node_two(false)
886
+ end
887
+ end
888
+ end
889
+
890
+ class BranchDrawingWithExceptFromRequirementTest < Test::Unit::TestCase
891
+ def setup
892
+ @machine = EnumStateMachine::Machine.new(Class.new)
893
+ states = [:parked, :idling, :first_gear]
894
+
895
+ @graph = EnumStateMachine::Graph.new('test')
896
+ states.each {|state| @graph.add_nodes(state.to_s)}
897
+
898
+ @branch = EnumStateMachine::Branch.new(:except_from => :parked, :to => :parked)
899
+ @branch.draw(@graph, :park, states)
900
+ end
901
+
902
+ def test_should_generate_edges_for_each_valid_from_state
903
+ %w(idling first_gear).each_with_index do |from_state, index|
904
+ edge = @graph.get_edge_at_index(index)
905
+ assert_equal from_state, edge.node_one(false)
906
+ assert_equal 'parked', edge.node_two(false)
907
+ end
908
+ end
909
+ end
910
+
911
+ class BranchDrawingWithoutFromRequirementTest < Test::Unit::TestCase
912
+ def setup
913
+ @machine = EnumStateMachine::Machine.new(Class.new)
914
+ states = [:parked, :idling, :first_gear]
915
+
916
+ @graph = EnumStateMachine::Graph.new('test')
917
+ states.each {|state| @graph.add_nodes(state.to_s)}
918
+
919
+ @branch = EnumStateMachine::Branch.new(:to => :parked)
920
+ @branch.draw(@graph, :park, states)
921
+ end
922
+
923
+ def test_should_generate_edges_for_each_valid_from_state
924
+ %w(parked idling first_gear).each_with_index do |from_state, index|
925
+ edge = @graph.get_edge_at_index(index)
926
+ assert_equal from_state, edge.node_one(false)
927
+ assert_equal 'parked', edge.node_two(false)
928
+ end
929
+ end
930
+ end
931
+
932
+ class BranchDrawingWithoutToRequirementTest < Test::Unit::TestCase
933
+ def setup
934
+ @machine = EnumStateMachine::Machine.new(Class.new)
935
+
936
+ graph = EnumStateMachine::Graph.new('test')
937
+ graph.add_nodes('parked')
938
+
939
+ @branch = EnumStateMachine::Branch.new(:from => :parked)
940
+ @branch.draw(graph, :park, [:parked])
941
+ @edge = graph.get_edge_at_index(0)
942
+ end
943
+
944
+ def test_should_create_loopback_edge
945
+ assert_equal 'parked', @edge.node_one(false)
946
+ assert_equal 'parked', @edge.node_two(false)
947
+ end
948
+ end
949
+
950
+ class BranchDrawingWithNilStateTest < Test::Unit::TestCase
951
+ def setup
952
+ @machine = EnumStateMachine::Machine.new(Class.new)
953
+
954
+ graph = EnumStateMachine::Graph.new('test')
955
+ graph.add_nodes('parked')
956
+
957
+ @branch = EnumStateMachine::Branch.new(:from => :idling, :to => nil)
958
+ @branch.draw(graph, :park, [nil, :idling])
959
+ @edge = graph.get_edge_at_index(0)
960
+ end
961
+
962
+ def test_should_generate_edges_for_each_valid_from_state
963
+ assert_equal 'idling', @edge.node_one(false)
964
+ assert_equal 'nil', @edge.node_two(false)
965
+ end
966
+ end
967
+ rescue LoadError
968
+ $stderr.puts 'Skipping GraphViz EnumStateMachine::Branch tests. `gem install ruby-graphviz` >= v0.9.17 and try again.'
969
+ end unless ENV['TRAVIS']