finite_machine 0.11.3 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +34 -0
  3. data/README.md +564 -569
  4. data/Rakefile +5 -1
  5. data/benchmarks/memory_profile.rb +11 -0
  6. data/benchmarks/memory_usage.rb +16 -9
  7. data/finite_machine.gemspec +10 -3
  8. data/lib/finite_machine.rb +34 -46
  9. data/lib/finite_machine/async_call.rb +5 -21
  10. data/lib/finite_machine/callable.rb +4 -4
  11. data/lib/finite_machine/catchable.rb +4 -2
  12. data/lib/finite_machine/choice_merger.rb +19 -19
  13. data/lib/finite_machine/const.rb +16 -0
  14. data/lib/finite_machine/definition.rb +2 -2
  15. data/lib/finite_machine/dsl.rb +66 -149
  16. data/lib/finite_machine/env.rb +4 -2
  17. data/lib/finite_machine/event_definition.rb +7 -15
  18. data/lib/finite_machine/{events_chain.rb → events_map.rb} +39 -51
  19. data/lib/finite_machine/hook_event.rb +60 -61
  20. data/lib/finite_machine/hooks.rb +44 -36
  21. data/lib/finite_machine/listener.rb +2 -2
  22. data/lib/finite_machine/logger.rb +5 -4
  23. data/lib/finite_machine/message_queue.rb +39 -30
  24. data/lib/finite_machine/observer.rb +55 -37
  25. data/lib/finite_machine/safety.rb +12 -10
  26. data/lib/finite_machine/state_definition.rb +3 -5
  27. data/lib/finite_machine/state_machine.rb +83 -64
  28. data/lib/finite_machine/state_parser.rb +51 -79
  29. data/lib/finite_machine/subscribers.rb +1 -1
  30. data/lib/finite_machine/threadable.rb +3 -1
  31. data/lib/finite_machine/transition.rb +30 -31
  32. data/lib/finite_machine/transition_builder.rb +23 -32
  33. data/lib/finite_machine/transition_event.rb +12 -11
  34. data/lib/finite_machine/two_phase_lock.rb +3 -1
  35. data/lib/finite_machine/undefined_transition.rb +5 -6
  36. data/lib/finite_machine/version.rb +2 -2
  37. data/spec/integration/system_spec.rb +36 -38
  38. data/spec/performance/benchmark_spec.rb +13 -21
  39. data/spec/unit/alias_target_spec.rb +22 -41
  40. data/spec/unit/async_callbacks_spec.rb +8 -13
  41. data/spec/unit/auto_methods_spec.rb +44 -0
  42. data/spec/unit/callable/call_spec.rb +1 -3
  43. data/spec/unit/callbacks_spec.rb +372 -463
  44. data/spec/unit/can_spec.rb +13 -23
  45. data/spec/unit/cancel_callbacks_spec.rb +46 -0
  46. data/spec/unit/choice_spec.rb +105 -141
  47. data/spec/unit/define_spec.rb +31 -31
  48. data/spec/unit/definition_spec.rb +24 -41
  49. data/spec/unit/event_names_spec.rb +6 -10
  50. data/spec/unit/events_map/add_spec.rb +23 -0
  51. data/spec/unit/events_map/choice_transition_spec.rb +25 -0
  52. data/spec/unit/events_map/clear_spec.rb +13 -0
  53. data/spec/unit/events_map/events_spec.rb +16 -0
  54. data/spec/unit/events_map/inspect_spec.rb +22 -0
  55. data/spec/unit/{events_chain → events_map}/match_transition_spec.rb +12 -14
  56. data/spec/unit/{events_chain → events_map}/move_to_spec.rb +14 -17
  57. data/spec/unit/events_map/states_for_spec.rb +17 -0
  58. data/spec/unit/events_spec.rb +91 -160
  59. data/spec/unit/handlers_spec.rb +34 -66
  60. data/spec/unit/hook_event/any_state_or_event_spec.rb +13 -0
  61. data/spec/unit/hook_event/build_spec.rb +1 -3
  62. data/spec/unit/hook_event/eql_spec.rb +1 -3
  63. data/spec/unit/hook_event/initialize_spec.rb +2 -4
  64. data/spec/unit/hook_event/notify_spec.rb +2 -4
  65. data/spec/unit/hooks/clear_spec.rb +1 -1
  66. data/spec/unit/hooks/{call_spec.rb → find_spec.rb} +4 -9
  67. data/spec/unit/hooks/inspect_spec.rb +16 -8
  68. data/spec/unit/hooks/register_spec.rb +4 -9
  69. data/spec/unit/if_unless_spec.rb +76 -115
  70. data/spec/unit/initial_spec.rb +50 -82
  71. data/spec/unit/inspect_spec.rb +14 -9
  72. data/spec/unit/is_spec.rb +12 -18
  73. data/spec/unit/log_transitions_spec.rb +4 -10
  74. data/spec/unit/logger_spec.rb +1 -3
  75. data/spec/unit/{event_queue_spec.rb → message_queue_spec.rb} +15 -8
  76. data/spec/unit/new_spec.rb +50 -0
  77. data/spec/unit/respond_to_spec.rb +2 -6
  78. data/spec/unit/state_parser/parse_spec.rb +9 -12
  79. data/spec/unit/states_spec.rb +12 -18
  80. data/spec/unit/subscribers_spec.rb +1 -3
  81. data/spec/unit/target_spec.rb +60 -93
  82. data/spec/unit/terminated_spec.rb +15 -25
  83. data/spec/unit/transition/check_conditions_spec.rb +16 -15
  84. data/spec/unit/transition/inspect_spec.rb +6 -6
  85. data/spec/unit/transition/matches_spec.rb +5 -7
  86. data/spec/unit/transition/states_spec.rb +5 -7
  87. data/spec/unit/transition/to_state_spec.rb +5 -13
  88. data/spec/unit/trigger_spec.rb +5 -9
  89. data/spec/unit/undefined_transition/eql_spec.rb +1 -3
  90. metadata +86 -49
  91. data/.gitignore +0 -18
  92. data/.rspec +0 -5
  93. data/.travis.yml +0 -27
  94. data/Gemfile +0 -16
  95. data/assets/finite_machine_logo.png +0 -0
  96. data/lib/finite_machine/async_proxy.rb +0 -55
  97. data/spec/unit/async_events_spec.rb +0 -107
  98. data/spec/unit/events_chain/add_spec.rb +0 -25
  99. data/spec/unit/events_chain/cancel_transitions_spec.rb +0 -22
  100. data/spec/unit/events_chain/choice_transition_spec.rb +0 -28
  101. data/spec/unit/events_chain/clear_spec.rb +0 -15
  102. data/spec/unit/events_chain/events_spec.rb +0 -18
  103. data/spec/unit/events_chain/inspect_spec.rb +0 -24
  104. data/spec/unit/events_chain/states_for_spec.rb +0 -17
  105. data/spec/unit/hook_event/infer_default_name_spec.rb +0 -13
  106. data/spec/unit/state_parser/inspect_spec.rb +0 -25
@@ -1,17 +1,12 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
1
+ # frozen_string_literal: true
4
2
 
5
3
  RSpec.describe FiniteMachine, 'events' do
6
-
7
4
  it "allows for hash rocket syntax to describe transition" do
8
- fsm = FiniteMachine.define do
5
+ fsm = FiniteMachine.new do
9
6
  initial :green
10
7
 
11
- events {
12
- event :slow, :green => :yellow
13
- event :stop, :yellow => :red
14
- }
8
+ event :slow, :green => :yellow
9
+ event :stop, :yellow => :red
15
10
  end
16
11
 
17
12
  expect(fsm.current).to eql(:green)
@@ -21,25 +16,12 @@ RSpec.describe FiniteMachine, 'events' do
21
16
  expect(fsm.current).to eql(:red)
22
17
  end
23
18
 
24
- it "allows to add event without events scope" do
25
- fsm = FiniteMachine.define do
26
- initial :green
27
-
28
- event :slow, :green => :yellow
29
- event :stop, :yellow => :red
30
- end
31
-
32
- expect(fsm.current).to eql(:green)
33
- end
34
-
35
19
  it "allows for (:from | :to) key pairs to describe transition" do
36
- fsm = FiniteMachine.define do
20
+ fsm = FiniteMachine.new do
37
21
  initial :green
38
22
 
39
- events {
40
- event :slow, from: :green, to: :yellow
41
- event :stop, from: :yellow, to: :red
42
- }
23
+ event :slow, from: :green, to: :yellow
24
+ event :stop, from: :yellow, to: :red
43
25
  end
44
26
 
45
27
  expect(fsm.current).to eql(:green)
@@ -50,16 +32,14 @@ RSpec.describe FiniteMachine, 'events' do
50
32
  end
51
33
 
52
34
  it "permits no-op event without 'to' transition" do
53
- fsm = FiniteMachine.define do
35
+ fsm = FiniteMachine.new do
54
36
  initial :green
55
37
 
56
- events {
57
- event :noop, from: :green
58
- event :slow, from: :green, to: :yellow
59
- event :stop, from: :yellow, to: :red
60
- event :ready, from: :red, to: :yellow
61
- event :go, from: :yellow, to: :green
62
- }
38
+ event :noop, from: :green
39
+ event :slow, from: :green, to: :yellow
40
+ event :stop, from: :yellow, to: :red
41
+ event :ready, from: :red, to: :yellow
42
+ event :go, from: :yellow, to: :green
63
43
  end
64
44
 
65
45
  expect(fsm.current).to eql(:green)
@@ -76,17 +56,15 @@ RSpec.describe FiniteMachine, 'events' do
76
56
  expect(fsm.cannot?(:slow)).to be true
77
57
  end
78
58
 
79
- it "permits event from any state with :any 'from'" do
80
- fsm = FiniteMachine.define do
59
+ it "permits event from any state using :from" do
60
+ fsm = FiniteMachine.new do
81
61
  initial :green
82
62
 
83
- events {
84
- event :slow, from: :green, to: :yellow
85
- event :stop, from: :yellow, to: :red
86
- event :ready, from: :red, to: :yellow
87
- event :go, from: :yellow, to: :green
88
- event :run, from: :any, to: :green
89
- }
63
+ event :slow, from: :green, to: :yellow
64
+ event :stop, from: :yellow, to: :red
65
+ event :ready, from: :red, to: :yellow
66
+ event :go, from: :yellow, to: :green
67
+ event :run, from: any_state, to: :green
90
68
  end
91
69
 
92
70
  expect(fsm.current).to eql(:green)
@@ -112,15 +90,13 @@ RSpec.describe FiniteMachine, 'events' do
112
90
  end
113
91
 
114
92
  it "permits event from any state for hash syntax" do
115
- fsm = FiniteMachine.define do
93
+ fsm = FiniteMachine.new do
116
94
  initial :red
117
95
 
118
- events {
119
- event :start, :red => :yellow
120
- event :run, :yellow => :green
121
- event :stop, :green => :red
122
- event :go, :any => :green
123
- }
96
+ event :start, :red => :yellow
97
+ event :run, :yellow => :green
98
+ event :stop, :green => :red
99
+ event :go, any_state => :green
124
100
  end
125
101
 
126
102
  expect(fsm.current).to eql(:red)
@@ -135,16 +111,14 @@ RSpec.describe FiniteMachine, 'events' do
135
111
  end
136
112
 
137
113
  it "permits event from any state without 'from'" do
138
- fsm = FiniteMachine.define do
114
+ fsm = FiniteMachine.new do
139
115
  initial :green
140
116
 
141
- events {
142
- event :slow, from: :green, to: :yellow
143
- event :stop, from: :yellow, to: :red
144
- event :ready, from: :red, to: :yellow
145
- event :go, from: :yellow, to: :green
146
- event :run, to: :green
147
- }
117
+ event :slow, from: :green, to: :yellow
118
+ event :stop, from: :yellow, to: :red
119
+ event :ready, from: :red, to: :yellow
120
+ event :go, from: :yellow, to: :green
121
+ event :run, to: :green
148
122
  end
149
123
 
150
124
  expect(fsm.current).to eql(:green)
@@ -171,16 +145,13 @@ RSpec.describe FiniteMachine, 'events' do
171
145
 
172
146
  it "doesn't raise error on invalid transition for non-dangerous version" do
173
147
  called = []
174
- fsm = FiniteMachine.define do
148
+ fsm = FiniteMachine.new do
175
149
  initial :green
176
150
 
177
- events {
178
- event :stop, from: :yellow, to: :red
179
- }
180
- callbacks {
181
- on_before :stop do |event| called << 'on_before_stop' end
182
- on_after :stop do |event| called << 'on_before_stop' end
183
- }
151
+ event :stop, from: :yellow, to: :red
152
+
153
+ on_before :stop do |event| called << 'on_before_stop' end
154
+ on_after :stop do |event| called << 'on_before_stop' end
184
155
  end
185
156
 
186
157
  expect(fsm.current).to eq(:green)
@@ -192,16 +163,13 @@ RSpec.describe FiniteMachine, 'events' do
192
163
  context 'for non-dangerous version' do
193
164
  it "doesn't raise error on invalid transition and fires callbacks" do
194
165
  called = []
195
- fsm = FiniteMachine.define do
166
+ fsm = FiniteMachine.new do
196
167
  initial :green
197
168
 
198
- events {
199
- event :stop, from: :yellow, to: :red
200
- }
201
- callbacks {
202
- on_before :stop do |event| called << 'on_before_stop' end
203
- on_after :stop do |event| called << 'on_before_stop' end
204
- }
169
+ event :stop, from: :yellow, to: :red
170
+
171
+ on_before :stop do |event| called << 'on_before_stop' end
172
+ on_after :stop do |event| called << 'on_before_stop' end
205
173
  end
206
174
 
207
175
  expect(fsm.current).to eq(:green)
@@ -212,17 +180,14 @@ RSpec.describe FiniteMachine, 'events' do
212
180
 
213
181
  it "raises error on invalid transition for dangerous version" do
214
182
  called = []
215
- fsm = FiniteMachine.define do
183
+ fsm = FiniteMachine.new do
216
184
  initial :green
217
185
 
218
- events {
219
- event :slow, from: :green, to: :yellow
220
- event :stop, from: :yellow, to: :red, silent: true
221
- }
222
- callbacks {
223
- on_before :stop do |event| called << 'on_before_stop' end
224
- on_after :stop do |event| called << 'on_before_stop' end
225
- }
186
+ event :slow, from: :green, to: :yellow
187
+ event :stop, from: :yellow, to: :red, silent: true
188
+
189
+ on_before :stop do |event| called << 'on_before_stop' end
190
+ on_after :stop do |event| called << 'on_before_stop' end
226
191
  end
227
192
 
228
193
  expect(fsm.current).to eql(:green)
@@ -234,16 +199,13 @@ RSpec.describe FiniteMachine, 'events' do
234
199
  context 'for dangerous version' do
235
200
  it "raises error on invalid transition without callbacks" do
236
201
  called = []
237
- fsm = FiniteMachine.define do
202
+ fsm = FiniteMachine.new do
238
203
  initial :green
239
204
 
240
- events {
241
- event :start, :red => :yellow, silent: true
242
- }
243
- callbacks {
244
- on_before :start do |event| called << 'on_before_start' end
245
- on_after :start do |event| called << 'on_after_start' end
246
- }
205
+ event :start, :red => :yellow, silent: true
206
+
207
+ on_before :start do |event| called << 'on_before_start' end
208
+ on_after :start do |event| called << 'on_after_start' end
247
209
  end
248
210
 
249
211
  expect(fsm.current).to eq(:green)
@@ -254,16 +216,13 @@ RSpec.describe FiniteMachine, 'events' do
254
216
 
255
217
  it "raises error on invalid transition with callbacks fired" do
256
218
  called = []
257
- fsm = FiniteMachine.define do
219
+ fsm = FiniteMachine.new do
258
220
  initial :green
259
221
 
260
- events {
261
- event :start, :red => :yellow
262
- }
263
- callbacks {
264
- on_before :start do |event| called << 'on_before_start' end
265
- on_after :start do |event| called << 'on_after_start' end
266
- }
222
+ event :start, :red => :yellow
223
+
224
+ on_before :start do |event| called << 'on_before_start' end
225
+ on_after :start do |event| called << 'on_after_start' end
267
226
  end
268
227
 
269
228
  expect(fsm.current).to eq(:green)
@@ -276,15 +235,13 @@ RSpec.describe FiniteMachine, 'events' do
276
235
 
277
236
  context 'when multiple from states' do
278
237
  it "allows for array from key" do
279
- fsm = FiniteMachine.define do
238
+ fsm = FiniteMachine.new do
280
239
  initial :green
281
240
 
282
- events {
283
- event :slow, :green => :yellow
284
- event :stop, [:green, :yellow] => :red
285
- event :ready, :red => :yellow
286
- event :go, [:yellow, :red] => :green
287
- }
241
+ event :slow, :green => :yellow
242
+ event :stop, [:green, :yellow] => :red
243
+ event :ready, :red => :yellow
244
+ event :go, [:yellow, :red] => :green
288
245
  end
289
246
 
290
247
  expect(fsm.current).to eql(:green)
@@ -304,15 +261,13 @@ RSpec.describe FiniteMachine, 'events' do
304
261
  end
305
262
 
306
263
  it "allows for hash of states" do
307
- fsm = FiniteMachine.define do
264
+ fsm = FiniteMachine.new do
308
265
  initial :green
309
266
 
310
- events {
311
- event :slow, :green => :yellow
312
- event :stop, :green => :red, :yellow => :red
313
- event :ready, :red => :yellow
314
- event :go, :yellow => :green, :red => :green
315
- }
267
+ event :slow, :green => :yellow
268
+ event :stop, :green => :red, :yellow => :red
269
+ event :ready, :red => :yellow
270
+ event :go, :yellow => :green, :red => :green
316
271
  end
317
272
 
318
273
  expect(fsm.current).to eql(:green)
@@ -333,15 +288,13 @@ RSpec.describe FiniteMachine, 'events' do
333
288
  end
334
289
 
335
290
  it "groups events with the same name" do
336
- fsm = FiniteMachine.define do
291
+ fsm = FiniteMachine.new do
337
292
  initial :green
338
293
 
339
- events {
340
- event :stop, :green => :yellow
341
- event :stop, :yellow => :red
342
- event :stop, :red => :pink
343
- event :cycle, [:yellow, :red, :pink] => :green
344
- }
294
+ event :stop, :green => :yellow
295
+ event :stop, :yellow => :red
296
+ event :stop, :red => :pink
297
+ event :cycle, [:yellow, :red, :pink] => :green
345
298
  end
346
299
 
347
300
  expect(fsm.current).to eql(:green)
@@ -359,14 +312,12 @@ RSpec.describe FiniteMachine, 'events' do
359
312
  end
360
313
 
361
314
  it "groups transitions under one event name" do
362
- fsm = FiniteMachine.define do
315
+ fsm = FiniteMachine.new do
363
316
  initial :initial
364
317
 
365
- events {
366
- event :bump, :initial => :low,
367
- :low => :medium,
368
- :medium => :high
369
- }
318
+ event :bump, :initial => :low,
319
+ :low => :medium,
320
+ :medium => :high
370
321
  end
371
322
 
372
323
  expect(fsm.current).to eq(:initial)
@@ -376,19 +327,15 @@ RSpec.describe FiniteMachine, 'events' do
376
327
  end
377
328
 
378
329
  it "returns values for events" do
379
- fsm = FiniteMachine.define do
330
+ fsm = FiniteMachine.new do
380
331
  initial :neutral
381
332
 
382
- events {
383
- event :start, :neutral => :engine_on
384
- event :drive, :engine_on => :running, if: -> { return false }
385
- event :stop, :any => :neutral
386
- }
333
+ event :start, :neutral => :engine_on
334
+ event :drive, :engine_on => :running, if: -> { return false }
335
+ event :stop, any_state => :neutral
387
336
 
388
- callbacks {
389
- on_before(:drive) { FiniteMachine::CANCELLED }
390
- on_after(:stop) { }
391
- }
337
+ on_before(:drive) { cancel_event }
338
+ on_after(:stop) { }
392
339
  end
393
340
 
394
341
  expect(fsm.current).to eql(:neutral)
@@ -401,19 +348,15 @@ RSpec.describe FiniteMachine, 'events' do
401
348
  it "allows for self transition events" do
402
349
  digits = []
403
350
  callbacks = []
404
- phone = FiniteMachine.define do
351
+ phone = FiniteMachine.new do
405
352
  initial :on_hook
406
353
 
407
- events {
408
- event :digit, :on_hook => :dialing
409
- event :digit, :dialing => :dialing
410
- event :off_hook, :dialing => :alerting
411
- }
354
+ event :digit, :on_hook => :dialing
355
+ event :digit, :dialing => :dialing
356
+ event :off_hook, :dialing => :alerting
412
357
 
413
- callbacks {
414
- on_before_digit { |event, digit| digits << digit}
415
- on_before_off_hook { |event| callbacks << "dialing #{digits.join}" }
416
- }
358
+ on_before_digit { |event, digit| digits << digit}
359
+ on_before_off_hook { |event| callbacks << "dialing #{digits.join}" }
417
360
  end
418
361
 
419
362
  expect(phone.current).to eq(:on_hook)
@@ -429,24 +372,12 @@ RSpec.describe FiniteMachine, 'events' do
429
372
  expect(callbacks).to match_array(["dialing 911"])
430
373
  end
431
374
 
432
- it "detects dangerous event names" do
433
- expect {
434
- FiniteMachine.define do
435
- events {
436
- event :trigger, :a => :b
437
- }
438
- end
439
- }.to raise_error(FiniteMachine::AlreadyDefinedError)
440
- end
441
-
442
375
  it "executes event block" do
443
- fsm = FiniteMachine.define do
376
+ fsm = FiniteMachine.new do
444
377
  initial :red
445
378
 
446
- events {
447
- event :start, :red => :green
448
- event :stop, :green => :red
449
- }
379
+ event :start, :red => :green
380
+ event :stop, :green => :red
450
381
  end
451
382
 
452
383
  expect(fsm.current).to eq(:red)
@@ -1,9 +1,6 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
1
+ # frozen_string_literal: true
4
2
 
5
3
  RSpec.describe FiniteMachine, 'handlers' do
6
-
7
4
  before(:each) {
8
5
  stub_const("DummyLogger", Class.new do
9
6
  attr_reader :result
@@ -20,19 +17,15 @@ RSpec.describe FiniteMachine, 'handlers' do
20
17
 
21
18
  it "allows to customise error handling" do
22
19
  called = []
23
- fsm = FiniteMachine.define do
20
+ fsm = FiniteMachine.new do
24
21
  initial :green
25
22
 
26
- events {
27
- event :slow, :green => :yellow
28
- event :stop, :yellow => :red
29
- }
23
+ event :slow, :green => :yellow
24
+ event :stop, :yellow => :red
30
25
 
31
- handlers {
32
- handle FiniteMachine::InvalidStateError do |exception|
33
- called << 'invalidstate'
34
- end
35
- }
26
+ handle FiniteMachine::InvalidStateError do |exception|
27
+ called << 'invalidstate'
28
+ end
36
29
  end
37
30
 
38
31
  expect(fsm.current).to eql(:green)
@@ -45,19 +38,13 @@ RSpec.describe FiniteMachine, 'handlers' do
45
38
 
46
39
  it 'allows for :with to be symbol' do
47
40
  logger = DummyLogger.new
48
- fsm = FiniteMachine.define do
41
+ fsm = FiniteMachine.new(logger) do
49
42
  initial :green
50
43
 
51
- target logger
44
+ event :slow, :green => :yellow
45
+ event :stop, :yellow => :red
52
46
 
53
- events {
54
- event :slow, :green => :yellow
55
- event :stop, :yellow => :red
56
- }
57
-
58
- handlers {
59
- handle FiniteMachine::InvalidStateError, with: :log_error
60
- }
47
+ handle FiniteMachine::InvalidStateError, with: :log_error
61
48
  end
62
49
 
63
50
  expect(fsm.current).to eql(:green)
@@ -69,26 +56,19 @@ RSpec.describe FiniteMachine, 'handlers' do
69
56
  it 'allows for error type as string' do
70
57
  logger = DummyLogger.new
71
58
  called = []
72
- fsm = FiniteMachine.define do
59
+ fsm = FiniteMachine.new(target: logger) do
73
60
  initial :green
74
61
 
75
- target logger
76
-
77
- events {
78
- event :slow, :green => :yellow
79
- event :stop, :yellow => :red
80
- }
81
-
82
- callbacks {
83
- on_enter_yellow do |event|
84
- raise_error
85
- end
86
- }
87
- handlers {
88
- handle 'InvalidStateError' do |exception|
89
- called << 'invalid_state_error'
90
- end
91
- }
62
+ event :slow, :green => :yellow
63
+ event :stop, :yellow => :red
64
+
65
+ on_enter_yellow do |event|
66
+ raise_error
67
+ end
68
+
69
+ handle 'InvalidStateError' do |exception|
70
+ called << 'invalid_state_error'
71
+ end
92
72
  end
93
73
 
94
74
  expect(fsm.current).to eql(:green)
@@ -99,19 +79,15 @@ RSpec.describe FiniteMachine, 'handlers' do
99
79
 
100
80
  it 'allows for empty block handler' do
101
81
  called = []
102
- fsm = FiniteMachine.define do
82
+ fsm = FiniteMachine.new do
103
83
  initial :green
104
84
 
105
- events {
106
- event :slow, :green => :yellow
107
- event :stop, :yellow => :red
108
- }
85
+ event :slow, :green => :yellow
86
+ event :stop, :yellow => :red
109
87
 
110
- handlers {
111
- handle FiniteMachine::InvalidStateError do
112
- called << 'invalidstate'
113
- end
114
- }
88
+ handle FiniteMachine::InvalidStateError do
89
+ called << 'invalidstate'
90
+ end
115
91
  end
116
92
 
117
93
  expect(fsm.current).to eql(:green)
@@ -123,30 +99,22 @@ RSpec.describe FiniteMachine, 'handlers' do
123
99
  end
124
100
 
125
101
  it 'requires error handler' do
126
- expect { FiniteMachine.define do
102
+ expect { FiniteMachine.new do
127
103
  initial :green
128
104
 
129
- events {
130
- event :slow, :green => :yellow
131
- }
105
+ event :slow, :green => :yellow
132
106
 
133
- handlers {
134
- handle 'UnknownErrorType'
135
- }
107
+ handle 'UnknownErrorType'
136
108
  end }.to raise_error(ArgumentError, /error handler/)
137
109
  end
138
110
 
139
111
  it 'checks handler class to be Exception' do
140
- expect { FiniteMachine.define do
112
+ expect { FiniteMachine.new do
141
113
  initial :green
142
114
 
143
- events {
144
- event :slow, :green => :yellow
145
- }
115
+ event :slow, :green => :yellow
146
116
 
147
- handlers {
148
- handle Object do end
149
- }
117
+ handle Object do end
150
118
  end }.to raise_error(ArgumentError, /Object isn't an Exception/)
151
119
  end
152
120
  end