state_machine 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +5 -0
- data/README.rdoc +3 -0
- data/Rakefile +1 -1
- data/lib/state_machine/callback.rb +4 -2
- data/lib/state_machine/integrations/data_mapper.rb +14 -1
- data/test/active_record.log +7826 -0
- data/test/data_mapper.log +2735 -0
- data/test/sequel.log +1204 -0
- data/test/unit/callback_test.rb +16 -0
- data/test/unit/integrations/active_record_test.rb +25 -0
- data/test/unit/integrations/data_mapper_test.rb +145 -116
- data/test/unit/integrations/sequel_test.rb +25 -0
- metadata +1 -1
data/test/unit/callback_test.rb
CHANGED
@@ -186,4 +186,20 @@ class CallbackWithBoundObjectTest < Test::Unit::TestCase
|
|
186
186
|
def test_should_call_method_within_the_context_of_the_object
|
187
187
|
assert_equal [@object, 1, 2, 3], @callback.call(@object, {}, 1, 2, 3)
|
188
188
|
end
|
189
|
+
|
190
|
+
def test_should_ignore_option_for_symbolic_callbacks
|
191
|
+
class << @object
|
192
|
+
def after_turn_on(*args)
|
193
|
+
args
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
@callback = StateMachine::Callback.new(:do => :after_turn_on, :bind_to_object => true)
|
198
|
+
assert_equal [], @callback.call(@object)
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_should_ignore_option_for_string_callbacks
|
202
|
+
@callback = StateMachine::Callback.new(:do => '[1, 2, 3]', :bind_to_object => true)
|
203
|
+
assert_equal [1, 2, 3], @callback.call(@object)
|
204
|
+
end
|
189
205
|
end
|
@@ -343,6 +343,31 @@ begin
|
|
343
343
|
|
344
344
|
assert_equal %w(error off), @machine.states.keys.sort
|
345
345
|
end
|
346
|
+
|
347
|
+
def test_should_allow_symbolic_callbacks
|
348
|
+
callback_args = nil
|
349
|
+
|
350
|
+
klass = class << @record; self; end
|
351
|
+
klass.send(:define_method, :after_turn_on) do |*args|
|
352
|
+
callback_args = args
|
353
|
+
end
|
354
|
+
|
355
|
+
@machine.before_transition(:after_turn_on)
|
356
|
+
|
357
|
+
@transition.perform
|
358
|
+
assert_equal [@transition], callback_args
|
359
|
+
end
|
360
|
+
|
361
|
+
def test_should_allow_string_callbacks
|
362
|
+
class << @record
|
363
|
+
attr_reader :callback_result
|
364
|
+
end
|
365
|
+
|
366
|
+
@machine.before_transition('@callback_result = [1, 2, 3]')
|
367
|
+
@transition.perform
|
368
|
+
|
369
|
+
assert_equal [1, 2, 3], @record.callback_result
|
370
|
+
end
|
346
371
|
end
|
347
372
|
|
348
373
|
class MachineWithFailedBeforeCallbacksTest < ActiveRecord::TestCase
|
@@ -4,8 +4,6 @@ begin
|
|
4
4
|
# Load library
|
5
5
|
require 'rubygems'
|
6
6
|
require 'dm-core'
|
7
|
-
require 'dm-observer'
|
8
|
-
require 'dm-aggregates'
|
9
7
|
|
10
8
|
# Establish database connection
|
11
9
|
DataMapper.setup(:default, 'sqlite3::memory:')
|
@@ -123,7 +121,7 @@ begin
|
|
123
121
|
false
|
124
122
|
end
|
125
123
|
|
126
|
-
assert_equal 0, @resource.
|
124
|
+
assert_equal 0, @resource.all.size
|
127
125
|
end
|
128
126
|
|
129
127
|
def test_should_not_rollback_transaction_if_true
|
@@ -132,7 +130,7 @@ begin
|
|
132
130
|
true
|
133
131
|
end
|
134
132
|
|
135
|
-
assert_equal 1, @resource.
|
133
|
+
assert_equal 1, @resource.all.size
|
136
134
|
end
|
137
135
|
|
138
136
|
def test_should_not_override_the_column_reader
|
@@ -253,155 +251,186 @@ begin
|
|
253
251
|
@transition.perform
|
254
252
|
assert_equal @record, context
|
255
253
|
end
|
256
|
-
end
|
257
|
-
|
258
|
-
class MachineWithObserversTest < BaseTestCase
|
259
|
-
def setup
|
260
|
-
@resource = new_resource
|
261
|
-
@machine = StateMachine::Machine.new(@resource)
|
262
|
-
@record = @resource.new(:state => 'off')
|
263
|
-
@transition = StateMachine::Transition.new(@record, @machine, 'turn_on', 'off', 'on')
|
264
|
-
end
|
265
254
|
|
266
|
-
def
|
267
|
-
|
255
|
+
def test_should_allow_symbolic_callbacks
|
256
|
+
callback_args = nil
|
268
257
|
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
end
|
258
|
+
klass = class << @record; self; end
|
259
|
+
klass.send(:define_method, :after_turn_on) do |*args|
|
260
|
+
callback_args = args
|
273
261
|
end
|
274
262
|
|
263
|
+
@machine.before_transition(:after_turn_on)
|
264
|
+
|
275
265
|
@transition.perform
|
276
|
-
|
266
|
+
assert_equal [@transition], callback_args
|
277
267
|
end
|
278
268
|
|
279
|
-
def
|
280
|
-
|
281
|
-
|
282
|
-
observer = new_observer(@resource) do
|
283
|
-
before_transition :from => 'on' do
|
284
|
-
called = true
|
285
|
-
end
|
269
|
+
def test_should_allow_string_callbacks
|
270
|
+
class << @record
|
271
|
+
attr_reader :callback_result
|
286
272
|
end
|
287
273
|
|
274
|
+
@machine.before_transition('@callback_result = [1, 2, 3]')
|
288
275
|
@transition.perform
|
289
|
-
assert !called
|
290
|
-
end
|
291
|
-
|
292
|
-
def test_should_allow_targeting_specific_machine
|
293
|
-
@second_machine = StateMachine::Machine.new(@resource, :status)
|
294
276
|
|
295
|
-
|
296
|
-
|
277
|
+
assert_equal [1, 2, 3], @record.callback_result
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
begin
|
282
|
+
require 'dm-observer'
|
283
|
+
|
284
|
+
class MachineWithObserversTest < BaseTestCase
|
285
|
+
def setup
|
286
|
+
@resource = new_resource
|
287
|
+
@machine = StateMachine::Machine.new(@resource)
|
288
|
+
@record = @resource.new(:state => 'off')
|
289
|
+
@transition = StateMachine::Transition.new(@record, @machine, 'turn_on', 'off', 'on')
|
290
|
+
end
|
297
291
|
|
298
|
-
|
299
|
-
|
300
|
-
called_state = true
|
301
|
-
end
|
292
|
+
def test_should_call_before_transition_callback_if_requirements_match
|
293
|
+
called = false
|
302
294
|
|
303
|
-
|
304
|
-
|
295
|
+
observer = new_observer(@resource) do
|
296
|
+
before_transition :from => 'off' do
|
297
|
+
called = true
|
298
|
+
end
|
305
299
|
end
|
300
|
+
|
301
|
+
@transition.perform
|
302
|
+
assert called
|
306
303
|
end
|
307
304
|
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
callback_args = nil
|
316
|
-
|
317
|
-
observer = new_observer(@resource) do
|
318
|
-
before_transition do |*args|
|
319
|
-
callback_args = args
|
305
|
+
def test_should_not_call_before_transition_callback_if_requirements_do_not_match
|
306
|
+
called = false
|
307
|
+
|
308
|
+
observer = new_observer(@resource) do
|
309
|
+
before_transition :from => 'on' do
|
310
|
+
called = true
|
311
|
+
end
|
320
312
|
end
|
313
|
+
|
314
|
+
@transition.perform
|
315
|
+
assert !called
|
321
316
|
end
|
322
317
|
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
318
|
+
def test_should_allow_targeting_specific_machine
|
319
|
+
@second_machine = StateMachine::Machine.new(@resource, :status)
|
320
|
+
|
321
|
+
called_state = false
|
322
|
+
called_status = false
|
323
|
+
|
324
|
+
observer = new_observer(@resource) do
|
325
|
+
before_transition :state, :from => 'off' do
|
326
|
+
called_state = true
|
327
|
+
end
|
328
|
+
|
329
|
+
before_transition :status, :from => 'off' do
|
330
|
+
called_status = true
|
331
|
+
end
|
333
332
|
end
|
333
|
+
|
334
|
+
@transition.perform
|
335
|
+
|
336
|
+
assert called_state
|
337
|
+
assert !called_status
|
334
338
|
end
|
335
339
|
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
observer = new_observer(@resource) do
|
344
|
-
after_transition :from => 'on' do
|
345
|
-
called = true
|
340
|
+
def test_should_pass_transition_to_before_callbacks
|
341
|
+
callback_args = nil
|
342
|
+
|
343
|
+
observer = new_observer(@resource) do
|
344
|
+
before_transition do |*args|
|
345
|
+
callback_args = args
|
346
|
+
end
|
346
347
|
end
|
348
|
+
|
349
|
+
@transition.perform
|
350
|
+
assert_equal [@transition], callback_args
|
347
351
|
end
|
348
352
|
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
observer = new_observer(@resource) do
|
357
|
-
after_transition do |*args|
|
358
|
-
callback_args = args
|
353
|
+
def test_should_call_after_transition_callback_if_requirements_match
|
354
|
+
called = false
|
355
|
+
|
356
|
+
observer = new_observer(@resource) do
|
357
|
+
after_transition :from => 'off' do
|
358
|
+
called = true
|
359
|
+
end
|
359
360
|
end
|
361
|
+
|
362
|
+
@transition.perform
|
363
|
+
assert called
|
360
364
|
end
|
361
365
|
|
362
|
-
|
363
|
-
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
class MachineWithMixedCallbacksTest < BaseTestCase
|
368
|
-
def setup
|
369
|
-
@resource = new_resource
|
370
|
-
@machine = StateMachine::Machine.new(@resource)
|
371
|
-
@record = @resource.new(:state => 'off')
|
372
|
-
@transition = StateMachine::Transition.new(@record, @machine, 'turn_on', 'off', 'on')
|
373
|
-
|
374
|
-
@notifications = notifications = []
|
375
|
-
|
376
|
-
# Create callbacks
|
377
|
-
@machine.before_transition(lambda {notifications << :callback_before_transition})
|
378
|
-
@machine.after_transition(lambda {notifications << :callback_after_transition})
|
379
|
-
|
380
|
-
observer = new_observer(@resource) do
|
381
|
-
before_transition do
|
382
|
-
notifications << :observer_before_transition
|
383
|
-
end
|
366
|
+
def test_should_not_call_after_transition_callback_if_requirements_do_not_match
|
367
|
+
called = false
|
384
368
|
|
385
|
-
|
386
|
-
|
369
|
+
observer = new_observer(@resource) do
|
370
|
+
after_transition :from => 'on' do
|
371
|
+
called = true
|
372
|
+
end
|
387
373
|
end
|
374
|
+
|
375
|
+
@transition.perform
|
376
|
+
assert !called
|
388
377
|
end
|
389
378
|
|
390
|
-
|
379
|
+
def test_should_pass_transition_and_result_to_before_callbacks
|
380
|
+
callback_args = nil
|
381
|
+
|
382
|
+
observer = new_observer(@resource) do
|
383
|
+
after_transition do |*args|
|
384
|
+
callback_args = args
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
@transition.perform
|
389
|
+
assert_equal [@transition, true], callback_args
|
390
|
+
end
|
391
391
|
end
|
392
392
|
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
:
|
398
|
-
|
399
|
-
|
393
|
+
class MachineWithMixedCallbacksTest < BaseTestCase
|
394
|
+
def setup
|
395
|
+
@resource = new_resource
|
396
|
+
@machine = StateMachine::Machine.new(@resource)
|
397
|
+
@record = @resource.new(:state => 'off')
|
398
|
+
@transition = StateMachine::Transition.new(@record, @machine, 'turn_on', 'off', 'on')
|
399
|
+
|
400
|
+
@notifications = notifications = []
|
401
|
+
|
402
|
+
# Create callbacks
|
403
|
+
@machine.before_transition(lambda {notifications << :callback_before_transition})
|
404
|
+
@machine.after_transition(lambda {notifications << :callback_after_transition})
|
405
|
+
|
406
|
+
observer = new_observer(@resource) do
|
407
|
+
before_transition do
|
408
|
+
notifications << :observer_before_transition
|
409
|
+
end
|
410
|
+
|
411
|
+
after_transition do
|
412
|
+
notifications << :observer_after_transition
|
413
|
+
end
|
414
|
+
end
|
415
|
+
|
416
|
+
@transition.perform
|
417
|
+
end
|
400
418
|
|
401
|
-
|
419
|
+
def test_should_invoke_callbacks_in_specific_order
|
420
|
+
expected = [
|
421
|
+
:callback_before_transition,
|
422
|
+
:observer_before_transition,
|
423
|
+
:callback_after_transition,
|
424
|
+
:observer_after_transition
|
425
|
+
]
|
426
|
+
|
427
|
+
assert_equal expected, @notifications
|
428
|
+
end
|
402
429
|
end
|
430
|
+
rescue LoadError
|
431
|
+
$stderr.puts 'Skipping DataMapper Observer tests. `gem install dm-observer` and try again.'
|
403
432
|
end
|
404
433
|
end
|
405
434
|
rescue LoadError
|
406
|
-
$stderr.puts 'Skipping DataMapper tests. `gem install dm-core
|
435
|
+
$stderr.puts 'Skipping DataMapper Core tests. `gem install dm-core` and try again.'
|
407
436
|
end
|
@@ -237,6 +237,31 @@ begin
|
|
237
237
|
@transition.perform
|
238
238
|
assert_equal @record, context
|
239
239
|
end
|
240
|
+
|
241
|
+
def test_should_allow_symbolic_callbacks
|
242
|
+
callback_args = nil
|
243
|
+
|
244
|
+
klass = class << @record; self; end
|
245
|
+
klass.send(:define_method, :after_turn_on) do |*args|
|
246
|
+
callback_args = args
|
247
|
+
end
|
248
|
+
|
249
|
+
@machine.before_transition(:after_turn_on)
|
250
|
+
|
251
|
+
@transition.perform
|
252
|
+
assert_equal [@transition], callback_args
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_should_allow_string_callbacks
|
256
|
+
class << @record
|
257
|
+
attr_reader :callback_result
|
258
|
+
end
|
259
|
+
|
260
|
+
@machine.before_transition('@callback_result = [1, 2, 3]')
|
261
|
+
@transition.perform
|
262
|
+
|
263
|
+
assert_equal [1, 2, 3], @record.callback_result
|
264
|
+
end
|
240
265
|
end
|
241
266
|
end
|
242
267
|
rescue LoadError
|