hsume2-state_machine 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +413 -0
- data/LICENSE +20 -0
- data/README.rdoc +717 -0
- data/Rakefile +77 -0
- data/examples/AutoShop_state.png +0 -0
- data/examples/Car_state.png +0 -0
- data/examples/TrafficLight_state.png +0 -0
- data/examples/Vehicle_state.png +0 -0
- data/examples/auto_shop.rb +11 -0
- data/examples/car.rb +19 -0
- data/examples/merb-rest/controller.rb +51 -0
- data/examples/merb-rest/model.rb +28 -0
- data/examples/merb-rest/view_edit.html.erb +24 -0
- data/examples/merb-rest/view_index.html.erb +23 -0
- data/examples/merb-rest/view_new.html.erb +13 -0
- data/examples/merb-rest/view_show.html.erb +17 -0
- data/examples/rails-rest/controller.rb +43 -0
- data/examples/rails-rest/migration.rb +11 -0
- data/examples/rails-rest/model.rb +23 -0
- data/examples/rails-rest/view_edit.html.erb +25 -0
- data/examples/rails-rest/view_index.html.erb +23 -0
- data/examples/rails-rest/view_new.html.erb +14 -0
- data/examples/rails-rest/view_show.html.erb +17 -0
- data/examples/traffic_light.rb +7 -0
- data/examples/vehicle.rb +31 -0
- data/init.rb +1 -0
- data/lib/state_machine.rb +448 -0
- data/lib/state_machine/alternate_machine.rb +79 -0
- data/lib/state_machine/assertions.rb +36 -0
- data/lib/state_machine/branch.rb +224 -0
- data/lib/state_machine/callback.rb +236 -0
- data/lib/state_machine/condition_proxy.rb +94 -0
- data/lib/state_machine/error.rb +13 -0
- data/lib/state_machine/eval_helpers.rb +86 -0
- data/lib/state_machine/event.rb +304 -0
- data/lib/state_machine/event_collection.rb +139 -0
- data/lib/state_machine/extensions.rb +149 -0
- data/lib/state_machine/initializers.rb +4 -0
- data/lib/state_machine/initializers/merb.rb +1 -0
- data/lib/state_machine/initializers/rails.rb +25 -0
- data/lib/state_machine/integrations.rb +110 -0
- data/lib/state_machine/integrations/active_model.rb +502 -0
- data/lib/state_machine/integrations/active_model/locale.rb +11 -0
- data/lib/state_machine/integrations/active_model/observer.rb +45 -0
- data/lib/state_machine/integrations/active_model/versions.rb +31 -0
- data/lib/state_machine/integrations/active_record.rb +424 -0
- data/lib/state_machine/integrations/active_record/locale.rb +20 -0
- data/lib/state_machine/integrations/active_record/versions.rb +143 -0
- data/lib/state_machine/integrations/base.rb +91 -0
- data/lib/state_machine/integrations/data_mapper.rb +392 -0
- data/lib/state_machine/integrations/data_mapper/observer.rb +210 -0
- data/lib/state_machine/integrations/data_mapper/versions.rb +62 -0
- data/lib/state_machine/integrations/mongo_mapper.rb +272 -0
- data/lib/state_machine/integrations/mongo_mapper/locale.rb +4 -0
- data/lib/state_machine/integrations/mongo_mapper/versions.rb +110 -0
- data/lib/state_machine/integrations/mongoid.rb +357 -0
- data/lib/state_machine/integrations/mongoid/locale.rb +4 -0
- data/lib/state_machine/integrations/mongoid/versions.rb +18 -0
- data/lib/state_machine/integrations/sequel.rb +428 -0
- data/lib/state_machine/integrations/sequel/versions.rb +36 -0
- data/lib/state_machine/machine.rb +1873 -0
- data/lib/state_machine/machine_collection.rb +87 -0
- data/lib/state_machine/matcher.rb +123 -0
- data/lib/state_machine/matcher_helpers.rb +54 -0
- data/lib/state_machine/node_collection.rb +157 -0
- data/lib/state_machine/path.rb +120 -0
- data/lib/state_machine/path_collection.rb +90 -0
- data/lib/state_machine/state.rb +271 -0
- data/lib/state_machine/state_collection.rb +112 -0
- data/lib/state_machine/transition.rb +458 -0
- data/lib/state_machine/transition_collection.rb +244 -0
- data/lib/tasks/state_machine.rake +1 -0
- data/lib/tasks/state_machine.rb +27 -0
- data/test/files/en.yml +17 -0
- data/test/files/switch.rb +11 -0
- data/test/functional/alternate_state_machine_test.rb +122 -0
- data/test/functional/state_machine_test.rb +993 -0
- data/test/test_helper.rb +4 -0
- data/test/unit/assertions_test.rb +40 -0
- data/test/unit/branch_test.rb +890 -0
- data/test/unit/callback_test.rb +701 -0
- data/test/unit/condition_proxy_test.rb +328 -0
- data/test/unit/error_test.rb +43 -0
- data/test/unit/eval_helpers_test.rb +222 -0
- data/test/unit/event_collection_test.rb +358 -0
- data/test/unit/event_test.rb +985 -0
- data/test/unit/integrations/active_model_test.rb +1097 -0
- data/test/unit/integrations/active_record_test.rb +2021 -0
- data/test/unit/integrations/base_test.rb +99 -0
- data/test/unit/integrations/data_mapper_test.rb +1909 -0
- data/test/unit/integrations/mongo_mapper_test.rb +1611 -0
- data/test/unit/integrations/mongoid_test.rb +1591 -0
- data/test/unit/integrations/sequel_test.rb +1523 -0
- data/test/unit/integrations_test.rb +61 -0
- data/test/unit/invalid_event_test.rb +20 -0
- data/test/unit/invalid_parallel_transition_test.rb +18 -0
- data/test/unit/invalid_transition_test.rb +77 -0
- data/test/unit/machine_collection_test.rb +599 -0
- data/test/unit/machine_test.rb +3043 -0
- data/test/unit/matcher_helpers_test.rb +37 -0
- data/test/unit/matcher_test.rb +155 -0
- data/test/unit/node_collection_test.rb +217 -0
- data/test/unit/path_collection_test.rb +266 -0
- data/test/unit/path_test.rb +485 -0
- data/test/unit/state_collection_test.rb +310 -0
- data/test/unit/state_machine_test.rb +31 -0
- data/test/unit/state_test.rb +924 -0
- data/test/unit/transition_collection_test.rb +2102 -0
- data/test/unit/transition_test.rb +1541 -0
- metadata +207 -0
@@ -0,0 +1,701 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
class CallbackTest < Test::Unit::TestCase
|
4
|
+
def test_should_raise_exception_if_invalid_type_specified
|
5
|
+
exception = assert_raise(ArgumentError) { StateMachine::Callback.new(:invalid) {} }
|
6
|
+
assert_equal 'Type must be :before, :after, :around, or :failure', exception.message
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_should_not_raise_exception_if_using_before_type
|
10
|
+
assert_nothing_raised { StateMachine::Callback.new(:before) {} }
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_should_not_raise_exception_if_using_after_type
|
14
|
+
assert_nothing_raised { StateMachine::Callback.new(:after) {} }
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_should_not_raise_exception_if_using_around_type
|
18
|
+
assert_nothing_raised { StateMachine::Callback.new(:around) {} }
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_should_not_raise_exception_if_using_failure_type
|
22
|
+
assert_nothing_raised { StateMachine::Callback.new(:failure) {} }
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_should_raise_exception_if_no_methods_specified
|
26
|
+
exception = assert_raise(ArgumentError) { StateMachine::Callback.new(:before) }
|
27
|
+
assert_equal 'Method(s) for callback must be specified', exception.message
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_should_not_raise_exception_if_method_specified_in_do_option
|
31
|
+
assert_nothing_raised { StateMachine::Callback.new(:before, :do => :run) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_should_not_raise_exception_if_method_specified_as_argument
|
35
|
+
assert_nothing_raised { StateMachine::Callback.new(:before, :run) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_should_not_raise_exception_if_method_specified_as_block
|
39
|
+
assert_nothing_raised { StateMachine::Callback.new(:before, :run) {} }
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_should_not_raise_exception_if_implicit_option_specified
|
43
|
+
assert_nothing_raised { StateMachine::Callback.new(:before, :do => :run, :invalid => :valid) }
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_should_not_bind_to_objects
|
47
|
+
assert !StateMachine::Callback.bind_to_object
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_should_not_have_a_terminator
|
51
|
+
assert_nil StateMachine::Callback.terminator
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class CallbackByDefaultTest < Test::Unit::TestCase
|
56
|
+
def setup
|
57
|
+
@callback = StateMachine::Callback.new(:before) {}
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_should_have_type
|
61
|
+
assert_equal :before, @callback.type
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_should_not_have_a_terminator
|
65
|
+
assert_nil @callback.terminator
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_should_have_a_branch_with_all_matcher_requirements
|
69
|
+
assert_equal StateMachine::AllMatcher.instance, @callback.branch.event_requirement
|
70
|
+
assert_equal StateMachine::AllMatcher.instance, @callback.branch.state_requirements.first[:from]
|
71
|
+
assert_equal StateMachine::AllMatcher.instance, @callback.branch.state_requirements.first[:to]
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_should_not_have_any_known_states
|
75
|
+
assert_equal [], @callback.known_states
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class CallbackWithMethodArgumentTest < Test::Unit::TestCase
|
80
|
+
def setup
|
81
|
+
@callback = StateMachine::Callback.new(:before, lambda {|*args| @args = args})
|
82
|
+
|
83
|
+
@object = Object.new
|
84
|
+
@result = @callback.call(@object)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_should_be_successful
|
88
|
+
assert @result
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_should_call_with_empty_context
|
92
|
+
assert_equal [@object], @args
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class CallbackWithMultipleMethodArgumentsTest < Test::Unit::TestCase
|
97
|
+
def setup
|
98
|
+
@callback = StateMachine::Callback.new(:before, :run_1, :run_2)
|
99
|
+
|
100
|
+
class << @object = Object.new
|
101
|
+
attr_accessor :callbacks
|
102
|
+
|
103
|
+
def run_1
|
104
|
+
(@callbacks ||= []) << :run_1
|
105
|
+
end
|
106
|
+
|
107
|
+
def run_2
|
108
|
+
(@callbacks ||= []) << :run_2
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
@result = @callback.call(@object)
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_should_be_successful
|
116
|
+
assert @result
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_should_call_each_callback_in_order
|
120
|
+
assert_equal [:run_1, :run_2], @object.callbacks
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
class CallbackWithDoMethodTest < Test::Unit::TestCase
|
125
|
+
def setup
|
126
|
+
@callback = StateMachine::Callback.new(:before, :do => lambda {|*args| @args = args})
|
127
|
+
|
128
|
+
@object = Object.new
|
129
|
+
@result = @callback.call(@object)
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_should_be_successful
|
133
|
+
assert @result
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_should_call_with_empty_context
|
137
|
+
assert_equal [@object], @args
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
class CallbackWithMultipleDoMethodsTest < Test::Unit::TestCase
|
142
|
+
def setup
|
143
|
+
@callback = StateMachine::Callback.new(:before, :do => [:run_1, :run_2])
|
144
|
+
|
145
|
+
class << @object = Object.new
|
146
|
+
attr_accessor :callbacks
|
147
|
+
|
148
|
+
def run_1
|
149
|
+
(@callbacks ||= []) << :run_1
|
150
|
+
end
|
151
|
+
|
152
|
+
def run_2
|
153
|
+
(@callbacks ||= []) << :run_2
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
@result = @callback.call(@object)
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_should_be_successful
|
161
|
+
assert @result
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_should_call_each_callback_in_order
|
165
|
+
assert_equal [:run_1, :run_2], @object.callbacks
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
class CallbackWithBlockTest < Test::Unit::TestCase
|
170
|
+
def setup
|
171
|
+
@callback = StateMachine::Callback.new(:before) do |*args|
|
172
|
+
@args = args
|
173
|
+
end
|
174
|
+
|
175
|
+
@object = Object.new
|
176
|
+
@result = @callback.call(@object)
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_should_be_successful
|
180
|
+
assert @result
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_should_call_with_empty_context
|
184
|
+
assert_equal [@object], @args
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
class CallbackWithMixedMethodsTest < Test::Unit::TestCase
|
189
|
+
def setup
|
190
|
+
@callback = StateMachine::Callback.new(:before, :run_argument, :do => :run_do) do |object|
|
191
|
+
object.callbacks << :block
|
192
|
+
end
|
193
|
+
|
194
|
+
class << @object = Object.new
|
195
|
+
attr_accessor :callbacks
|
196
|
+
|
197
|
+
def run_argument
|
198
|
+
(@callbacks ||= []) << :argument
|
199
|
+
end
|
200
|
+
|
201
|
+
def run_do
|
202
|
+
(@callbacks ||= []) << :do
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
@result = @callback.call(@object)
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_should_be_successful
|
210
|
+
assert @result
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_should_call_each_callback_in_order
|
214
|
+
assert_equal [:argument, :do, :block], @object.callbacks
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
class CallbackWithExplicitRequirementsTest < Test::Unit::TestCase
|
219
|
+
def setup
|
220
|
+
@object = Object.new
|
221
|
+
@callback = StateMachine::Callback.new(:before, :from => :parked, :to => :idling, :on => :ignite, :do => lambda {})
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_should_call_with_empty_context
|
225
|
+
assert @callback.call(@object, {})
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_should_not_call_if_from_not_included
|
229
|
+
assert !@callback.call(@object, :from => :idling)
|
230
|
+
end
|
231
|
+
|
232
|
+
def test_should_not_call_if_to_not_included
|
233
|
+
assert !@callback.call(@object, :to => :parked)
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_should_not_call_if_on_not_included
|
237
|
+
assert !@callback.call(@object, :on => :park)
|
238
|
+
end
|
239
|
+
|
240
|
+
def test_should_call_if_all_requirements_met
|
241
|
+
assert @callback.call(@object, :from => :parked, :to => :idling, :on => :ignite)
|
242
|
+
end
|
243
|
+
|
244
|
+
def test_should_include_in_known_states
|
245
|
+
assert_equal [:parked, :idling], @callback.known_states
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
class CallbackWithImplicitRequirementsTest < Test::Unit::TestCase
|
250
|
+
def setup
|
251
|
+
@object = Object.new
|
252
|
+
@callback = StateMachine::Callback.new(:before, :parked => :idling, :on => :ignite, :do => lambda {})
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_should_call_with_empty_context
|
256
|
+
assert @callback.call(@object, {})
|
257
|
+
end
|
258
|
+
|
259
|
+
def test_should_not_call_if_from_not_included
|
260
|
+
assert !@callback.call(@object, :from => :idling)
|
261
|
+
end
|
262
|
+
|
263
|
+
def test_should_not_call_if_to_not_included
|
264
|
+
assert !@callback.call(@object, :to => :parked)
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_should_not_call_if_on_not_included
|
268
|
+
assert !@callback.call(@object, :on => :park)
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_should_call_if_all_requirements_met
|
272
|
+
assert @callback.call(@object, :from => :parked, :to => :idling, :on => :ignite)
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_should_include_in_known_states
|
276
|
+
assert_equal [:parked, :idling], @callback.known_states
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
class CallbackWithIfConditionTest < Test::Unit::TestCase
|
281
|
+
def setup
|
282
|
+
@object = Object.new
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_should_call_if_true
|
286
|
+
callback = StateMachine::Callback.new(:before, :if => lambda {true}, :do => lambda {})
|
287
|
+
assert callback.call(@object)
|
288
|
+
end
|
289
|
+
|
290
|
+
def test_should_not_call_if_false
|
291
|
+
callback = StateMachine::Callback.new(:before, :if => lambda {false}, :do => lambda {})
|
292
|
+
assert !callback.call(@object)
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
class CallbackWithUnlessConditionTest < Test::Unit::TestCase
|
297
|
+
def setup
|
298
|
+
@object = Object.new
|
299
|
+
end
|
300
|
+
|
301
|
+
def test_should_call_if_false
|
302
|
+
callback = StateMachine::Callback.new(:before, :unless => lambda {false}, :do => lambda {})
|
303
|
+
assert callback.call(@object)
|
304
|
+
end
|
305
|
+
|
306
|
+
def test_should_not_call_if_true
|
307
|
+
callback = StateMachine::Callback.new(:before, :unless => lambda {true}, :do => lambda {})
|
308
|
+
assert !callback.call(@object)
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
class CallbackWithoutTerminatorTest < Test::Unit::TestCase
|
313
|
+
def setup
|
314
|
+
@object = Object.new
|
315
|
+
end
|
316
|
+
|
317
|
+
def test_should_not_halt_if_result_is_false
|
318
|
+
callback = StateMachine::Callback.new(:before, :do => lambda {false}, :terminator => nil)
|
319
|
+
assert_nothing_thrown { callback.call(@object) }
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
class CallbackWithTerminatorTest < Test::Unit::TestCase
|
324
|
+
def setup
|
325
|
+
@object = Object.new
|
326
|
+
end
|
327
|
+
|
328
|
+
def test_should_not_halt_if_terminator_does_not_match
|
329
|
+
callback = StateMachine::Callback.new(:before, :do => lambda {false}, :terminator => lambda {|result| result == true})
|
330
|
+
assert_nothing_thrown { callback.call(@object) }
|
331
|
+
end
|
332
|
+
|
333
|
+
def test_should_halt_if_terminator_matches
|
334
|
+
callback = StateMachine::Callback.new(:before, :do => lambda {false}, :terminator => lambda {|result| result == false})
|
335
|
+
assert_throws(:halt) { callback.call(@object) }
|
336
|
+
end
|
337
|
+
|
338
|
+
def test_should_halt_if_terminator_matches_any_method
|
339
|
+
callback = StateMachine::Callback.new(:before, :do => [lambda {true}, lambda {false}], :terminator => lambda {|result| result == false})
|
340
|
+
assert_throws(:halt) { callback.call(@object) }
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
class CallbackWithoutArgumentsTest < Test::Unit::TestCase
|
345
|
+
def setup
|
346
|
+
@callback = StateMachine::Callback.new(:before, :do => lambda {|object| @arg = object})
|
347
|
+
|
348
|
+
@object = Object.new
|
349
|
+
@callback.call(@object, {}, 1, 2, 3)
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_should_call_method_with_object_as_argument
|
353
|
+
assert_equal @object, @arg
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
class CallbackWithArgumentsTest < Test::Unit::TestCase
|
358
|
+
def setup
|
359
|
+
@callback = StateMachine::Callback.new(:before, :do => lambda {|*args| @args = args})
|
360
|
+
|
361
|
+
@object = Object.new
|
362
|
+
@callback.call(@object, {}, 1, 2, 3)
|
363
|
+
end
|
364
|
+
|
365
|
+
def test_should_call_method_with_all_arguments
|
366
|
+
assert_equal [@object, 1, 2, 3], @args
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
class CallbackWithUnboundMethodTest < Test::Unit::TestCase
|
371
|
+
def setup
|
372
|
+
@callback = StateMachine::Callback.new(:before, :do => lambda {|*args| @context = args.unshift(self)})
|
373
|
+
|
374
|
+
@object = Object.new
|
375
|
+
@callback.call(@object, {}, 1, 2, 3)
|
376
|
+
end
|
377
|
+
|
378
|
+
def test_should_call_method_outside_the_context_of_the_object
|
379
|
+
assert_equal [self, @object, 1, 2, 3], @context
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
class CallbackWithBoundMethodTest < Test::Unit::TestCase
|
384
|
+
def setup
|
385
|
+
@object = Object.new
|
386
|
+
end
|
387
|
+
|
388
|
+
def test_should_call_method_within_the_context_of_the_object_for_block_methods
|
389
|
+
context = nil
|
390
|
+
callback = StateMachine::Callback.new(:before, :do => lambda {|*args| context = [self] + args}, :bind_to_object => true)
|
391
|
+
callback.call(@object, {}, 1, 2, 3)
|
392
|
+
|
393
|
+
assert_equal [@object, 1, 2, 3], context
|
394
|
+
end
|
395
|
+
|
396
|
+
def test_should_ignore_option_for_symbolic_methods
|
397
|
+
class << @object
|
398
|
+
attr_reader :context
|
399
|
+
|
400
|
+
def after_ignite(*args)
|
401
|
+
@context = args
|
402
|
+
end
|
403
|
+
end
|
404
|
+
|
405
|
+
callback = StateMachine::Callback.new(:before, :do => :after_ignite, :bind_to_object => true)
|
406
|
+
callback.call(@object)
|
407
|
+
|
408
|
+
assert_equal [], @object.context
|
409
|
+
end
|
410
|
+
|
411
|
+
def test_should_ignore_option_for_string_methods
|
412
|
+
callback = StateMachine::Callback.new(:before, :do => '[1, 2, 3]', :bind_to_object => true)
|
413
|
+
assert callback.call(@object)
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
417
|
+
class CallbackWithMultipleBoundMethodsTest < Test::Unit::TestCase
|
418
|
+
def setup
|
419
|
+
@object = Object.new
|
420
|
+
|
421
|
+
first_context = nil
|
422
|
+
second_context = nil
|
423
|
+
|
424
|
+
@callback = StateMachine::Callback.new(:before, :do => [lambda {first_context = self}, lambda {second_context = self}], :bind_to_object => true)
|
425
|
+
@callback.call(@object)
|
426
|
+
|
427
|
+
@first_context = first_context
|
428
|
+
@second_context = second_context
|
429
|
+
end
|
430
|
+
|
431
|
+
def test_should_call_each_method_within_the_context_of_the_object
|
432
|
+
assert_equal @object, @first_context
|
433
|
+
assert_equal @object, @second_context
|
434
|
+
end
|
435
|
+
end
|
436
|
+
|
437
|
+
class CallbackWithApplicationBoundObjectTest < Test::Unit::TestCase
|
438
|
+
def setup
|
439
|
+
@original_bind_to_object = StateMachine::Callback.bind_to_object
|
440
|
+
StateMachine::Callback.bind_to_object = true
|
441
|
+
|
442
|
+
context = nil
|
443
|
+
@callback = StateMachine::Callback.new(:before, :do => lambda {|*args| context = self})
|
444
|
+
|
445
|
+
@object = Object.new
|
446
|
+
@callback.call(@object)
|
447
|
+
@context = context
|
448
|
+
end
|
449
|
+
|
450
|
+
def test_should_call_method_within_the_context_of_the_object
|
451
|
+
assert_equal @object, @context
|
452
|
+
end
|
453
|
+
|
454
|
+
def teardown
|
455
|
+
StateMachine::Callback.bind_to_object = @original_bind_to_object
|
456
|
+
end
|
457
|
+
end
|
458
|
+
|
459
|
+
class CallbackWithBoundMethodAndArgumentsTest < Test::Unit::TestCase
|
460
|
+
def setup
|
461
|
+
@object = Object.new
|
462
|
+
end
|
463
|
+
|
464
|
+
def test_should_include_single_argument_if_specified
|
465
|
+
context = nil
|
466
|
+
callback = StateMachine::Callback.new(:before, :do => lambda {|arg1| context = [arg1]}, :bind_to_object => true)
|
467
|
+
callback.call(@object, {}, 1)
|
468
|
+
assert_equal [1], context
|
469
|
+
end
|
470
|
+
|
471
|
+
def test_should_include_multiple_arguments_if_specified
|
472
|
+
context = nil
|
473
|
+
callback = StateMachine::Callback.new(:before, :do => lambda {|arg1, arg2, arg3| context = [arg1, arg2, arg3]}, :bind_to_object => true)
|
474
|
+
callback.call(@object, {}, 1, 2, 3)
|
475
|
+
assert_equal [1, 2, 3], context
|
476
|
+
end
|
477
|
+
|
478
|
+
def test_should_include_arguments_if_splat_used
|
479
|
+
context = nil
|
480
|
+
callback = StateMachine::Callback.new(:before, :do => lambda {|*args| context = args}, :bind_to_object => true)
|
481
|
+
callback.call(@object, {}, 1, 2, 3)
|
482
|
+
assert_equal [1, 2, 3], context
|
483
|
+
end
|
484
|
+
end
|
485
|
+
|
486
|
+
class CallbackWithApplicationTerminatorTest < Test::Unit::TestCase
|
487
|
+
def setup
|
488
|
+
@original_terminator = StateMachine::Callback.bind_to_object
|
489
|
+
StateMachine::Callback.terminator = lambda {|result| result == false}
|
490
|
+
|
491
|
+
@object = Object.new
|
492
|
+
end
|
493
|
+
|
494
|
+
def test_should_not_halt_if_terminator_does_not_match
|
495
|
+
callback = StateMachine::Callback.new(:before, :do => lambda {true})
|
496
|
+
assert_nothing_thrown { callback.call(@object) }
|
497
|
+
end
|
498
|
+
|
499
|
+
def test_should_halt_if_terminator_matches
|
500
|
+
callback = StateMachine::Callback.new(:before, :do => lambda {false})
|
501
|
+
assert_throws(:halt) { callback.call(@object) }
|
502
|
+
end
|
503
|
+
|
504
|
+
def teardown
|
505
|
+
StateMachine::Callback.bind_to_object = @original_bind_to_object
|
506
|
+
end
|
507
|
+
end
|
508
|
+
|
509
|
+
class CallbackWithAroundTypeAndBlockTest < Test::Unit::TestCase
|
510
|
+
def setup
|
511
|
+
@object = Object.new
|
512
|
+
@callbacks = []
|
513
|
+
end
|
514
|
+
|
515
|
+
def test_should_evaluate_before_without_after
|
516
|
+
callback = StateMachine::Callback.new(:around, lambda {|*args| block = args.pop; @args = args; block.call})
|
517
|
+
assert callback.call(@object)
|
518
|
+
assert_equal [@object], @args
|
519
|
+
end
|
520
|
+
|
521
|
+
def test_should_evaluate_after_without_before
|
522
|
+
callback = StateMachine::Callback.new(:around, lambda {|*args| block = args.pop; block.call; @args = args})
|
523
|
+
assert callback.call(@object)
|
524
|
+
assert_equal [@object], @args
|
525
|
+
end
|
526
|
+
|
527
|
+
def test_should_halt_if_not_yielded
|
528
|
+
callback = StateMachine::Callback.new(:around, lambda {|block|})
|
529
|
+
assert_throws(:halt) { callback.call(@object) }
|
530
|
+
end
|
531
|
+
|
532
|
+
def test_should_call_block_after_before
|
533
|
+
callback = StateMachine::Callback.new(:around, lambda {|block| @callbacks << :before; block.call})
|
534
|
+
assert callback.call(@object) { @callbacks << :block }
|
535
|
+
assert_equal [:before, :block], @callbacks
|
536
|
+
end
|
537
|
+
|
538
|
+
def test_should_call_block_before_after
|
539
|
+
@callbacks = []
|
540
|
+
callback = StateMachine::Callback.new(:around, lambda {|block| block.call; @callbacks << :after})
|
541
|
+
assert callback.call(@object) { @callbacks << :block }
|
542
|
+
assert_equal [:block, :after], @callbacks
|
543
|
+
end
|
544
|
+
|
545
|
+
def test_should_halt_if_block_halts
|
546
|
+
callback = StateMachine::Callback.new(:around, lambda {|block| block.call; @callbacks << :after})
|
547
|
+
assert_throws(:halt) { callback.call(@object) { throw :halt } }
|
548
|
+
assert_equal [], @callbacks
|
549
|
+
end
|
550
|
+
end
|
551
|
+
|
552
|
+
class CallbackWithAroundTypeAndMultipleMethodsTest < Test::Unit::TestCase
|
553
|
+
def setup
|
554
|
+
@callback = StateMachine::Callback.new(:around, :run_1, :run_2)
|
555
|
+
|
556
|
+
class << @object = Object.new
|
557
|
+
attr_accessor :before_callbacks
|
558
|
+
attr_accessor :after_callbacks
|
559
|
+
|
560
|
+
def run_1
|
561
|
+
(@before_callbacks ||= []) << :run_1
|
562
|
+
yield
|
563
|
+
(@after_callbacks ||= []) << :run_1
|
564
|
+
end
|
565
|
+
|
566
|
+
def run_2
|
567
|
+
(@before_callbacks ||= []) << :run_2
|
568
|
+
yield
|
569
|
+
(@after_callbacks ||= []) << :run_2
|
570
|
+
end
|
571
|
+
end
|
572
|
+
end
|
573
|
+
|
574
|
+
def test_should_succeed
|
575
|
+
assert @callback.call(@object)
|
576
|
+
end
|
577
|
+
|
578
|
+
def test_should_evaluate_before_callbacks_in_order
|
579
|
+
@callback.call(@object)
|
580
|
+
assert_equal [:run_1, :run_2], @object.before_callbacks
|
581
|
+
end
|
582
|
+
|
583
|
+
def test_should_evaluate_after_callbacks_in_reverse_order
|
584
|
+
@callback.call(@object)
|
585
|
+
assert_equal [:run_2, :run_1], @object.after_callbacks
|
586
|
+
end
|
587
|
+
|
588
|
+
def test_should_call_block_after_before_callbacks
|
589
|
+
@callback.call(@object) { (@object.before_callbacks ||= []) << :block }
|
590
|
+
assert_equal [:run_1, :run_2, :block], @object.before_callbacks
|
591
|
+
end
|
592
|
+
|
593
|
+
def test_should_call_block_before_after_callbacks
|
594
|
+
@callback.call(@object) { (@object.after_callbacks ||= []) << :block }
|
595
|
+
assert_equal [:block, :run_2, :run_1], @object.after_callbacks
|
596
|
+
end
|
597
|
+
|
598
|
+
def test_should_halt_if_first_doesnt_yield
|
599
|
+
class << @object
|
600
|
+
def run_1
|
601
|
+
(@before_callbacks ||= []) << :run_1
|
602
|
+
end
|
603
|
+
end
|
604
|
+
|
605
|
+
catch(:halt) do
|
606
|
+
@callback.call(@object) { (@object.before_callbacks ||= []) << :block }
|
607
|
+
end
|
608
|
+
|
609
|
+
assert_equal [:run_1], @object.before_callbacks
|
610
|
+
assert_nil @object.after_callbacks
|
611
|
+
end
|
612
|
+
|
613
|
+
def test_should_halt_if_last_doesnt_yield
|
614
|
+
class << @object
|
615
|
+
def run_2
|
616
|
+
(@before_callbacks ||= []) << :run_2
|
617
|
+
end
|
618
|
+
end
|
619
|
+
|
620
|
+
catch(:halt) { @callback.call(@object) }
|
621
|
+
assert_equal [:run_1, :run_2], @object.before_callbacks
|
622
|
+
assert_nil @object.after_callbacks
|
623
|
+
end
|
624
|
+
|
625
|
+
def test_should_not_evaluate_further_methods_if_after_halts
|
626
|
+
class << @object
|
627
|
+
def run_2
|
628
|
+
(@before_callbacks ||= []) << :run_2
|
629
|
+
yield
|
630
|
+
(@after_callbacks ||= []) << :run_2
|
631
|
+
throw :halt
|
632
|
+
end
|
633
|
+
end
|
634
|
+
|
635
|
+
catch(:halt) { @callback.call(@object) }
|
636
|
+
assert_equal [:run_1, :run_2], @object.before_callbacks
|
637
|
+
assert_equal [:run_2], @object.after_callbacks
|
638
|
+
end
|
639
|
+
end
|
640
|
+
|
641
|
+
class CallbackWithAroundTypeAndArgumentsTest < Test::Unit::TestCase
|
642
|
+
def setup
|
643
|
+
@object = Object.new
|
644
|
+
end
|
645
|
+
|
646
|
+
def test_should_include_object_if_specified
|
647
|
+
callback = StateMachine::Callback.new(:around, lambda {|object, block| @args = [object]; block.call})
|
648
|
+
callback.call(@object)
|
649
|
+
assert_equal [@object], @args
|
650
|
+
end
|
651
|
+
|
652
|
+
def test_should_include_arguments_if_specified
|
653
|
+
callback = StateMachine::Callback.new(:around, lambda {|object, arg1, arg2, arg3, block| @args = [object, arg1, arg2, arg3]; block.call})
|
654
|
+
callback.call(@object, {}, 1, 2, 3)
|
655
|
+
assert_equal [@object, 1, 2, 3], @args
|
656
|
+
end
|
657
|
+
|
658
|
+
def test_should_include_arguments_if_splat_used
|
659
|
+
callback = StateMachine::Callback.new(:around, lambda {|*args| block = args.pop; @args = args; block.call})
|
660
|
+
callback.call(@object, {}, 1, 2, 3)
|
661
|
+
assert_equal [@object, 1, 2, 3], @args
|
662
|
+
end
|
663
|
+
end
|
664
|
+
|
665
|
+
class CallbackWithAroundTypeAndTerminatorTest < Test::Unit::TestCase
|
666
|
+
def setup
|
667
|
+
@object = Object.new
|
668
|
+
end
|
669
|
+
|
670
|
+
def test_should_not_halt_if_terminator_does_not_match
|
671
|
+
callback = StateMachine::Callback.new(:around, :do => lambda {|block| block.call(false); false}, :terminator => lambda {|result| result == true})
|
672
|
+
assert_nothing_thrown { callback.call(@object) }
|
673
|
+
end
|
674
|
+
|
675
|
+
def test_should_not_halt_if_terminator_matches
|
676
|
+
callback = StateMachine::Callback.new(:around, :do => lambda {|block| block.call(false); false}, :terminator => lambda {|result| result == false})
|
677
|
+
assert_nothing_thrown { callback.call(@object) }
|
678
|
+
end
|
679
|
+
end
|
680
|
+
|
681
|
+
class CallbackWithAroundTypeAndBoundMethodTest < Test::Unit::TestCase
|
682
|
+
def setup
|
683
|
+
@object = Object.new
|
684
|
+
end
|
685
|
+
|
686
|
+
def test_should_call_method_within_the_context_of_the_object
|
687
|
+
context = nil
|
688
|
+
callback = StateMachine::Callback.new(:around, :do => lambda {|block| context = self; block.call}, :bind_to_object => true)
|
689
|
+
callback.call(@object, {}, 1, 2, 3)
|
690
|
+
|
691
|
+
assert_equal @object, context
|
692
|
+
end
|
693
|
+
|
694
|
+
def test_should_include_arguments_if_specified
|
695
|
+
context = nil
|
696
|
+
callback = StateMachine::Callback.new(:around, :do => lambda {|*args| block = args.pop; context = args; block.call}, :bind_to_object => true)
|
697
|
+
callback.call(@object, {}, 1, 2, 3)
|
698
|
+
|
699
|
+
assert_equal [1, 2, 3], context
|
700
|
+
end
|
701
|
+
end
|