class TransitionWithFailureCallbacksTest

Public Instance Methods

setup() click to toggle source
# File test/unit/transition_test.rb, line 939
def setup
  @klass = Class.new
  
  @machine = StateMachine::Machine.new(@klass)
  @machine.state :parked, :idling
  @machine.event :ignite
  
  @object = @klass.new
  @object.state = 'parked'
  @transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
end
test_should_be_able_to_run_again_after_resetting() click to toggle source
# File test/unit/transition_test.rb, line 1011
def test_should_be_able_to_run_again_after_resetting
  @count = 0
  @machine.after_failure {@count += 1}
  @transition.run_callbacks {{:success => false}}
  @transition.reset
  @transition.run_callbacks {{:success => false}}
  assert_equal 2, @count
end
test_should_catch_halts() click to toggle source
# File test/unit/transition_test.rb, line 982
def test_should_catch_halts
  @machine.after_failure {throw :halt}
  
  result = nil
  assert_nothing_thrown { result = @transition.run_callbacks {{:success => false}} }
  assert_equal true, result
end
test_should_not_be_able_to_run_twice() click to toggle source
# File test/unit/transition_test.rb, line 995
def test_should_not_be_able_to_run_twice
  @count = 0
  @machine.after_failure {@count += 1}
  @transition.run_callbacks {{:success => false}}
  @transition.run_callbacks {{:success => false}}
  assert_equal 1, @count
end
test_should_not_be_able_to_run_twice_if_halted() click to toggle source
# File test/unit/transition_test.rb, line 1003
def test_should_not_be_able_to_run_twice_if_halted
  @count = 0
  @machine.after_failure {@count += 1; throw :halt}
  @transition.run_callbacks {{:success => false}}
  @transition.run_callbacks {{:success => false}}
  assert_equal 1, @count
end
test_should_not_catch_exceptions() click to toggle source
# File test/unit/transition_test.rb, line 990
def test_should_not_catch_exceptions
  @machine.after_failure {raise ArgumentError}
  assert_raise(ArgumentError) { @transition.run_callbacks {{:success => false}} }
end
test_should_not_run_if_successful() click to toggle source
# File test/unit/transition_test.rb, line 969
def test_should_not_run_if_successful
  @machine.after_failure {|object| @run = true}
  @transition.run_callbacks {{:success => true}}
  assert !@run
end
test_should_only_run_those_that_match_transition_context() click to toggle source
# File test/unit/transition_test.rb, line 951
def test_should_only_run_those_that_match_transition_context
  @count = 0
  callback = lambda {@count += 1}
  
  @machine.after_failure :do => callback
  @machine.after_failure :on => :park, :do => callback
  @machine.after_failure :on => :ignite, :do => callback
  @transition.run_callbacks {{:success => false}}
  
  assert_equal 2, @count
end
test_should_pass_transition_as_argument() click to toggle source
# File test/unit/transition_test.rb, line 975
def test_should_pass_transition_as_argument
  @machine.after_failure {|*args| @args = args}
  
  @transition.run_callbacks {{:success => false}}
  assert_equal [@object, @transition], @args
end
test_should_run_if_not_successful() click to toggle source
# File test/unit/transition_test.rb, line 963
def test_should_run_if_not_successful
  @machine.after_failure {|object| @run = true}
  @transition.run_callbacks {{:success => false}}
  assert @run
end