class DataMapperTest::MachineWithObserversTest

Public Instance Methods

setup() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1492
def setup
  @resource = new_resource
  @machine = StateMachine::Machine.new(@resource)
  @machine.state :parked, :idling
  @machine.event :ignite
  @record = @resource.new(:state => 'parked')
  @transition = StateMachine::Transition.new(@record, @machine, :ignite, :parked, :idling)
end
test_should_allow_targeting_multiple_specific_machines() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1709
def test_should_allow_targeting_multiple_specific_machines
  @second_machine = StateMachine::Machine.new(@resource, :status, :namespace => 'alarm')
  @second_machine.state :parked, :idling
  @second_machine.event :ignite
  @resource.auto_migrate!
  
  called_attribute = nil
  
  attributes = []
  observer = new_observer(@resource) do
    before_transition :state, :status, :from => :parked do |transition|
      called_attribute = transition.attribute
    end
  end
  
  @transition.perform
  assert_equal :state, called_attribute
  
  StateMachine::Transition.new(@record, @second_machine, :ignite, :parked, :idling).perform
  assert_equal :status, called_attribute
end
test_should_allow_targeting_specific_machine() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1686
def test_should_allow_targeting_specific_machine
  @second_machine = StateMachine::Machine.new(@resource, :status, :namespace => 'alarm')
  @resource.auto_migrate!
  
  called_state = false
  called_status = false
  
  observer = new_observer(@resource) do
    before_transition :state, :from => :parked do
      called_state = true
    end
    
    before_transition :status, :from => :parked do
      called_status = true
    end
  end
  
  @transition.perform
  
  assert called_state
  assert !called_status
end
test_should_call_after_transition_callback_if_requirements_match() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1550
def test_should_call_after_transition_callback_if_requirements_match
  called = false
  
  observer = new_observer(@resource) do
    after_transition :from => :parked do
      called = true
    end
  end
  
  @transition.perform
  assert called
end
test_should_call_around_transition_callback_if_requirements_match() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1589
def test_should_call_around_transition_callback_if_requirements_match
  called = false
  
  observer = new_observer(@resource) do
    around_transition :from => :parked do |block|
      called = true
      block.call
    end
  end
  
  @transition.perform
  assert called
end
test_should_call_before_transition_callback_if_requirements_match() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1511
def test_should_call_before_transition_callback_if_requirements_match
  called = false
  
  observer = new_observer(@resource) do
    before_transition :from => :parked do
      called = true
    end
  end
  
  @transition.perform
  assert called
end
test_should_call_failure_callback_if_requirements_match() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1632
def test_should_call_failure_callback_if_requirements_match
  @resource.before(:create) { throw :halt } 
  
  called = false
  
  observer = new_observer(@resource) do
    after_transition_failure :on => :ignite do
      called = true
    end
  end
  
  @transition.perform
  assert called
end
test_should_not_call_after_transition_callback_if_requirements_do_not_match() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1563
def test_should_not_call_after_transition_callback_if_requirements_do_not_match
  called = false
  
  observer = new_observer(@resource) do
    after_transition :from => :idling do
      called = true
    end
  end
  
  @transition.perform
  assert !called
end
test_should_not_call_before_transition_callback_if_requirements_do_not_match() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1524
def test_should_not_call_before_transition_callback_if_requirements_do_not_match
  called = false
  
  observer = new_observer(@resource) do
    before_transition :from => :idling do
      called = true
    end
  end
  
  @transition.perform
  assert !called
end
test_should_not_call_failure_callback_if_requirements_do_not_match() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1647
def test_should_not_call_failure_callback_if_requirements_do_not_match
  @resource.before(:create) { throw :halt } 
  
  called = false
  
  observer = new_observer(@resource) do
    after_transition_failure :on => :park do
      called = true
    end
  end
  
  @transition.perform
  assert !called
end
test_should_pass_transition_to_after_callbacks() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1576
def test_should_pass_transition_to_after_callbacks
  callback_args = nil
  
  observer = new_observer(@resource) do
    after_transition do |*args|
      callback_args = args
    end
  end
  
  @transition.perform
  assert_equal [@transition], callback_args
end
test_should_pass_transition_to_around_callbacks() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1617
def test_should_pass_transition_to_around_callbacks
  callback_args = nil
  
  observer = new_observer(@resource) do
    around_transition do |*args|
      block = args.pop
      callback_args = args
      block.call
    end
  end
  
  @transition.perform
  assert_equal [@transition], callback_args
end
test_should_pass_transition_to_before_callbacks() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1537
def test_should_pass_transition_to_before_callbacks
  callback_args = nil
  
  observer = new_observer(@resource) do
    before_transition do |*args|
      callback_args = args
    end
  end
  
  @transition.perform
  assert_equal [@transition], callback_args
end
test_should_pass_transition_to_failure_callbacks() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1662
def test_should_pass_transition_to_failure_callbacks
  @resource.before(:create) { throw :halt } 
  
  callback_args = nil
  
  observer = new_observer(@resource) do
    after_transition_failure do |*args|
      callback_args = args
    end
  end
  
  @transition.perform
  assert_equal [@transition], callback_args
end
test_should_provide_matcher_helpers() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1501
def test_should_provide_matcher_helpers
  matchers = []
  
  new_observer(@resource) do
    matchers = [all, any, same]
  end
  
  assert_equal [StateMachine::AllMatcher.instance, StateMachine::AllMatcher.instance, StateMachine::LoopbackMatcher.instance], matchers
end
test_should_raise_exception_if_targeting_invalid_machine() click to toggle source
# File test/unit/integrations/data_mapper_test.rb, line 1677
def test_should_raise_exception_if_targeting_invalid_machine
  assert_raise(RUBY_VERSION < '1.9' ? IndexError : KeyError) do
    new_observer(@resource) do
      before_transition :invalid, :from => :parked do
      end
    end
  end
end