class StateContextTransitionTest

Public Instance Methods

setup() click to toggle source
# File test/unit/state_context_test.rb, line 31
def setup
  @klass = Class.new
  @machine = StateMachine::Machine.new(@klass, :initial => :parked)
  @state = @machine.state :parked
  
  @state_context = StateMachine::StateContext.new(@state)
end
test_should_allow_if_condition() click to toggle source
# File test/unit/state_context_test.rb, line 86
def test_should_allow_if_condition
  assert_nothing_raised {@state_context.transition(:to => :idling, :on => :park, :if => :seatbelt_on?)}
end
test_should_allow_multiple_events() click to toggle source
# File test/unit/state_context_test.rb, line 106
def test_should_allow_multiple_events
  @state_context.transition(:to => :idling, :on => [:ignite, :shift_up])
  
  assert_equal [:ignite, :shift_up], @machine.events.map {|event| event.name}
end
test_should_allow_unless_condition() click to toggle source
# File test/unit/state_context_test.rb, line 90
def test_should_allow_unless_condition
  assert_nothing_raised {@state_context.transition(:to => :idling, :on => :park, :unless => :seatbelt_off?)}
end
test_should_automatically_set_from_option() click to toggle source
# File test/unit/state_context_test.rb, line 74
def test_should_automatically_set_from_option
  branch = @state_context.transition(:to => :idling, :on => :ignite)
  assert_instance_of StateMachine::Branch, branch
  
  state_requirements = branch.state_requirements
  assert_equal 1, state_requirements.length
  
  from_requirement = state_requirements[0][:from]
  assert_instance_of StateMachine::WhitelistMatcher, from_requirement
  assert_equal [:parked], from_requirement.values
end
test_should_include_all_transition_events_in_machine_events() click to toggle source
# File test/unit/state_context_test.rb, line 100
def test_should_include_all_transition_events_in_machine_events
  @state_context.transition(:to => :idling, :on => :ignite)
  
  assert_equal [:ignite], @machine.events.map {|event| event.name}
end
test_should_include_all_transition_states_in_machine_states() click to toggle source
# File test/unit/state_context_test.rb, line 94
def test_should_include_all_transition_states_in_machine_states
  @state_context.transition(:to => :idling, :on => :ignite)
  
  assert_equal [:parked, :idling], @machine.states.map {|state| state.name}
end
test_should_not_allow_except_from() click to toggle source
# File test/unit/state_context_test.rb, line 49
def test_should_not_allow_except_from
  exception = assert_raise(ArgumentError) { @state_context.transition(:except_from => :idling) }
  assert_equal 'Invalid key(s): except_from', exception.message
end
test_should_not_allow_except_on() click to toggle source
# File test/unit/state_context_test.rb, line 59
def test_should_not_allow_except_on
  exception = assert_raise(ArgumentError) { @state_context.transition(:except_on => :park) }
  assert_equal 'Invalid key(s): except_on', exception.message
end
test_should_not_allow_except_to() click to toggle source
# File test/unit/state_context_test.rb, line 39
def test_should_not_allow_except_to
  exception = assert_raise(ArgumentError) { @state_context.transition(:except_to => :idling) }
  assert_equal 'Invalid key(s): except_to', exception.message
end
test_should_not_allow_from() click to toggle source
# File test/unit/state_context_test.rb, line 44
def test_should_not_allow_from
  exception = assert_raise(ArgumentError) { @state_context.transition(:from => :idling) }
  assert_equal 'Invalid key(s): from', exception.message
end
test_should_not_allow_implicit_transitions() click to toggle source
# File test/unit/state_context_test.rb, line 54
def test_should_not_allow_implicit_transitions
  exception = assert_raise(ArgumentError) { @state_context.transition(:parked => :idling) }
  assert_equal 'Invalid key(s): parked', exception.message
end
test_should_require_on_event() click to toggle source
# File test/unit/state_context_test.rb, line 64
def test_should_require_on_event
  exception = assert_raise(ArgumentError) { @state_context.transition(:to => :idling) }
  assert_equal 'Must specify :to state and :on event', exception.message
end
test_should_require_to_state() click to toggle source
# File test/unit/state_context_test.rb, line 69
def test_should_require_to_state
  exception = assert_raise(ArgumentError) { @state_context.transition(:on => :ignite) }
  assert_equal 'Must specify :to state and :on event', exception.message
end