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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +34 -0
- data/README.md +564 -569
- data/Rakefile +5 -1
- data/benchmarks/memory_profile.rb +11 -0
- data/benchmarks/memory_usage.rb +16 -9
- data/finite_machine.gemspec +10 -3
- data/lib/finite_machine.rb +34 -46
- data/lib/finite_machine/async_call.rb +5 -21
- data/lib/finite_machine/callable.rb +4 -4
- data/lib/finite_machine/catchable.rb +4 -2
- data/lib/finite_machine/choice_merger.rb +19 -19
- data/lib/finite_machine/const.rb +16 -0
- data/lib/finite_machine/definition.rb +2 -2
- data/lib/finite_machine/dsl.rb +66 -149
- data/lib/finite_machine/env.rb +4 -2
- data/lib/finite_machine/event_definition.rb +7 -15
- data/lib/finite_machine/{events_chain.rb → events_map.rb} +39 -51
- data/lib/finite_machine/hook_event.rb +60 -61
- data/lib/finite_machine/hooks.rb +44 -36
- data/lib/finite_machine/listener.rb +2 -2
- data/lib/finite_machine/logger.rb +5 -4
- data/lib/finite_machine/message_queue.rb +39 -30
- data/lib/finite_machine/observer.rb +55 -37
- data/lib/finite_machine/safety.rb +12 -10
- data/lib/finite_machine/state_definition.rb +3 -5
- data/lib/finite_machine/state_machine.rb +83 -64
- data/lib/finite_machine/state_parser.rb +51 -79
- data/lib/finite_machine/subscribers.rb +1 -1
- data/lib/finite_machine/threadable.rb +3 -1
- data/lib/finite_machine/transition.rb +30 -31
- data/lib/finite_machine/transition_builder.rb +23 -32
- data/lib/finite_machine/transition_event.rb +12 -11
- data/lib/finite_machine/two_phase_lock.rb +3 -1
- data/lib/finite_machine/undefined_transition.rb +5 -6
- data/lib/finite_machine/version.rb +2 -2
- data/spec/integration/system_spec.rb +36 -38
- data/spec/performance/benchmark_spec.rb +13 -21
- data/spec/unit/alias_target_spec.rb +22 -41
- data/spec/unit/async_callbacks_spec.rb +8 -13
- data/spec/unit/auto_methods_spec.rb +44 -0
- data/spec/unit/callable/call_spec.rb +1 -3
- data/spec/unit/callbacks_spec.rb +372 -463
- data/spec/unit/can_spec.rb +13 -23
- data/spec/unit/cancel_callbacks_spec.rb +46 -0
- data/spec/unit/choice_spec.rb +105 -141
- data/spec/unit/define_spec.rb +31 -31
- data/spec/unit/definition_spec.rb +24 -41
- data/spec/unit/event_names_spec.rb +6 -10
- data/spec/unit/events_map/add_spec.rb +23 -0
- data/spec/unit/events_map/choice_transition_spec.rb +25 -0
- data/spec/unit/events_map/clear_spec.rb +13 -0
- data/spec/unit/events_map/events_spec.rb +16 -0
- data/spec/unit/events_map/inspect_spec.rb +22 -0
- data/spec/unit/{events_chain → events_map}/match_transition_spec.rb +12 -14
- data/spec/unit/{events_chain → events_map}/move_to_spec.rb +14 -17
- data/spec/unit/events_map/states_for_spec.rb +17 -0
- data/spec/unit/events_spec.rb +91 -160
- data/spec/unit/handlers_spec.rb +34 -66
- data/spec/unit/hook_event/any_state_or_event_spec.rb +13 -0
- data/spec/unit/hook_event/build_spec.rb +1 -3
- data/spec/unit/hook_event/eql_spec.rb +1 -3
- data/spec/unit/hook_event/initialize_spec.rb +2 -4
- data/spec/unit/hook_event/notify_spec.rb +2 -4
- data/spec/unit/hooks/clear_spec.rb +1 -1
- data/spec/unit/hooks/{call_spec.rb → find_spec.rb} +4 -9
- data/spec/unit/hooks/inspect_spec.rb +16 -8
- data/spec/unit/hooks/register_spec.rb +4 -9
- data/spec/unit/if_unless_spec.rb +76 -115
- data/spec/unit/initial_spec.rb +50 -82
- data/spec/unit/inspect_spec.rb +14 -9
- data/spec/unit/is_spec.rb +12 -18
- data/spec/unit/log_transitions_spec.rb +4 -10
- data/spec/unit/logger_spec.rb +1 -3
- data/spec/unit/{event_queue_spec.rb → message_queue_spec.rb} +15 -8
- data/spec/unit/new_spec.rb +50 -0
- data/spec/unit/respond_to_spec.rb +2 -6
- data/spec/unit/state_parser/parse_spec.rb +9 -12
- data/spec/unit/states_spec.rb +12 -18
- data/spec/unit/subscribers_spec.rb +1 -3
- data/spec/unit/target_spec.rb +60 -93
- data/spec/unit/terminated_spec.rb +15 -25
- data/spec/unit/transition/check_conditions_spec.rb +16 -15
- data/spec/unit/transition/inspect_spec.rb +6 -6
- data/spec/unit/transition/matches_spec.rb +5 -7
- data/spec/unit/transition/states_spec.rb +5 -7
- data/spec/unit/transition/to_state_spec.rb +5 -13
- data/spec/unit/trigger_spec.rb +5 -9
- data/spec/unit/undefined_transition/eql_spec.rb +1 -3
- metadata +86 -49
- data/.gitignore +0 -18
- data/.rspec +0 -5
- data/.travis.yml +0 -27
- data/Gemfile +0 -16
- data/assets/finite_machine_logo.png +0 -0
- data/lib/finite_machine/async_proxy.rb +0 -55
- data/spec/unit/async_events_spec.rb +0 -107
- data/spec/unit/events_chain/add_spec.rb +0 -25
- data/spec/unit/events_chain/cancel_transitions_spec.rb +0 -22
- data/spec/unit/events_chain/choice_transition_spec.rb +0 -28
- data/spec/unit/events_chain/clear_spec.rb +0 -15
- data/spec/unit/events_chain/events_spec.rb +0 -18
- data/spec/unit/events_chain/inspect_spec.rb +0 -24
- data/spec/unit/events_chain/states_for_spec.rb +0 -17
- data/spec/unit/hook_event/infer_default_name_spec.rb +0 -13
- data/spec/unit/state_parser/inspect_spec.rb +0 -25
data/spec/unit/events_spec.rb
CHANGED
@@ -1,17 +1,12 @@
|
|
1
|
-
#
|
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.
|
5
|
+
fsm = FiniteMachine.new do
|
9
6
|
initial :green
|
10
7
|
|
11
|
-
|
12
|
-
|
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.
|
20
|
+
fsm = FiniteMachine.new do
|
37
21
|
initial :green
|
38
22
|
|
39
|
-
|
40
|
-
|
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.
|
35
|
+
fsm = FiniteMachine.new do
|
54
36
|
initial :green
|
55
37
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
80
|
-
fsm = FiniteMachine.
|
59
|
+
it "permits event from any state using :from" do
|
60
|
+
fsm = FiniteMachine.new do
|
81
61
|
initial :green
|
82
62
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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.
|
93
|
+
fsm = FiniteMachine.new do
|
116
94
|
initial :red
|
117
95
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
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.
|
114
|
+
fsm = FiniteMachine.new do
|
139
115
|
initial :green
|
140
116
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
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.
|
148
|
+
fsm = FiniteMachine.new do
|
175
149
|
initial :green
|
176
150
|
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
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.
|
166
|
+
fsm = FiniteMachine.new do
|
196
167
|
initial :green
|
197
168
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
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.
|
183
|
+
fsm = FiniteMachine.new do
|
216
184
|
initial :green
|
217
185
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
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.
|
202
|
+
fsm = FiniteMachine.new do
|
238
203
|
initial :green
|
239
204
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
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.
|
219
|
+
fsm = FiniteMachine.new do
|
258
220
|
initial :green
|
259
221
|
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
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.
|
238
|
+
fsm = FiniteMachine.new do
|
280
239
|
initial :green
|
281
240
|
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
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.
|
264
|
+
fsm = FiniteMachine.new do
|
308
265
|
initial :green
|
309
266
|
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
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.
|
291
|
+
fsm = FiniteMachine.new do
|
337
292
|
initial :green
|
338
293
|
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
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.
|
315
|
+
fsm = FiniteMachine.new do
|
363
316
|
initial :initial
|
364
317
|
|
365
|
-
|
366
|
-
|
367
|
-
|
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.
|
330
|
+
fsm = FiniteMachine.new do
|
380
331
|
initial :neutral
|
381
332
|
|
382
|
-
|
383
|
-
|
384
|
-
|
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
|
-
|
389
|
-
|
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.
|
351
|
+
phone = FiniteMachine.new do
|
405
352
|
initial :on_hook
|
406
353
|
|
407
|
-
|
408
|
-
|
409
|
-
|
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
|
-
|
414
|
-
|
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.
|
376
|
+
fsm = FiniteMachine.new do
|
444
377
|
initial :red
|
445
378
|
|
446
|
-
|
447
|
-
|
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)
|
data/spec/unit/handlers_spec.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
#
|
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.
|
20
|
+
fsm = FiniteMachine.new do
|
24
21
|
initial :green
|
25
22
|
|
26
|
-
|
27
|
-
|
28
|
-
event :stop, :yellow => :red
|
29
|
-
}
|
23
|
+
event :slow, :green => :yellow
|
24
|
+
event :stop, :yellow => :red
|
30
25
|
|
31
|
-
|
32
|
-
|
33
|
-
|
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.
|
41
|
+
fsm = FiniteMachine.new(logger) do
|
49
42
|
initial :green
|
50
43
|
|
51
|
-
|
44
|
+
event :slow, :green => :yellow
|
45
|
+
event :stop, :yellow => :red
|
52
46
|
|
53
|
-
|
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.
|
59
|
+
fsm = FiniteMachine.new(target: logger) do
|
73
60
|
initial :green
|
74
61
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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.
|
82
|
+
fsm = FiniteMachine.new do
|
103
83
|
initial :green
|
104
84
|
|
105
|
-
|
106
|
-
|
107
|
-
event :stop, :yellow => :red
|
108
|
-
}
|
85
|
+
event :slow, :green => :yellow
|
86
|
+
event :stop, :yellow => :red
|
109
87
|
|
110
|
-
|
111
|
-
|
112
|
-
|
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.
|
102
|
+
expect { FiniteMachine.new do
|
127
103
|
initial :green
|
128
104
|
|
129
|
-
|
130
|
-
event :slow, :green => :yellow
|
131
|
-
}
|
105
|
+
event :slow, :green => :yellow
|
132
106
|
|
133
|
-
|
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.
|
112
|
+
expect { FiniteMachine.new do
|
141
113
|
initial :green
|
142
114
|
|
143
|
-
|
144
|
-
event :slow, :green => :yellow
|
145
|
-
}
|
115
|
+
event :slow, :green => :yellow
|
146
116
|
|
147
|
-
|
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
|