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.
@@ -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.count
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.count
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 test_should_call_before_transition_callback_if_requirements_match
267
- called = false
255
+ def test_should_allow_symbolic_callbacks
256
+ callback_args = nil
268
257
 
269
- observer = new_observer(@resource) do
270
- before_transition :from => 'off' do
271
- called = true
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
- assert called
266
+ assert_equal [@transition], callback_args
277
267
  end
278
268
 
279
- def test_should_not_call_before_transition_callback_if_requirements_do_not_match
280
- called = false
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
- called_state = false
296
- called_status = false
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
- observer = new_observer(@resource) do
299
- before_transition :state, :from => 'off' do
300
- called_state = true
301
- end
292
+ def test_should_call_before_transition_callback_if_requirements_match
293
+ called = false
302
294
 
303
- before_transition :status, :from => 'off' do
304
- called_status = true
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
- @transition.perform
309
-
310
- assert called_state
311
- assert !called_status
312
- end
313
-
314
- def test_should_pass_transition_to_before_callbacks
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
- @transition.perform
324
- assert_equal [@transition], callback_args
325
- end
326
-
327
- def test_should_call_after_transition_callback_if_requirements_match
328
- called = false
329
-
330
- observer = new_observer(@resource) do
331
- after_transition :from => 'off' do
332
- called = true
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
- @transition.perform
337
- assert called
338
- end
339
-
340
- def test_should_not_call_after_transition_callback_if_requirements_do_not_match
341
- called = false
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
- @transition.perform
350
- assert !called
351
- end
352
-
353
- def test_should_pass_transition_and_result_to_before_callbacks
354
- callback_args = nil
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
- @transition.perform
363
- assert_equal [@transition, true], callback_args
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
- after_transition do
386
- notifications << :observer_after_transition
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
- @transition.perform
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
- def test_should_invoke_callbacks_in_specific_order
394
- expected = [
395
- :callback_before_transition,
396
- :observer_before_transition,
397
- :callback_after_transition,
398
- :observer_after_transition
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
- assert_equal expected, @notifications
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 dm-observer dm-aggregates` and try again.'
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Pfeifer