Sha256: d4a26d6cec126cde1c021a1261106f17700acc0a106b25ac84d746669bca7ff7

Contents?: true

Size: 1.87 KB

Versions: 2

Compression:

Stored size: 1.87 KB

Contents

require 'helper'

class TestStateTransitionGuardCheck < Test::Unit::TestCase
  args = [:foo, 'bar']

  test 'should return true of there is no guard' do
    opts = { from: 'foo', to: 'bar' }
    st = Transitions::StateTransition.new(opts)

    assert st.executable?(nil, *args)
  end

  test 'should call the method on the object if guard is a symbol' do
    opts = { from: 'foo', to: 'bar', guard: :test_guard }
    st = Transitions::StateTransition.new(opts)

    obj = stub
    obj.expects(:test_guard).with(*args)

    st.executable?(obj, *args)
  end

  test 'should call the method on the object if guard is a string' do
    opts = { from: 'foo', to: 'bar', guard: 'test_guard' }
    st = Transitions::StateTransition.new(opts)

    obj = stub
    obj.expects(:test_guard).with(*args)

    st.executable?(obj, *args)
  end

  test 'should call the proc passing the object if the guard is a proc' do
    opts = { from: 'foo', to: 'bar', guard: proc { |o, *args| o.test_guard(*args) } }
    st = Transitions::StateTransition.new(opts)

    obj = stub
    obj.expects(:test_guard).with(*args)

    st.executable?(obj, *args)
  end

  test 'should call the callable passing the object if the guard responds to #call' do
    callable = Object.new
    callable.define_singleton_method(:call) { |obj, *args| obj.test_guard(*args) }

    opts = { from: 'foo', to: 'bar', guard: callable }
    st = Transitions::StateTransition.new(opts)

    obj = stub
    obj.expects(:test_guard).with(*args)

    st.executable?(obj, *args)
  end

  test 'should call the method on the object if guard is a symbol' do
    opts = { from: 'foo', to: 'bar', guard: [:test_guard, :test_another_guard] }
    st = Transitions::StateTransition.new(opts)

    obj = stub
    obj.expects(:test_guard).with(*args).returns(true)
    obj.expects(:test_another_guard).with(*args).returns(true)

    assert st.executable?(obj, *args)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
transitions-1.0.0 test/state_transition/test_state_transition_guard_check.rb
transitions-0.2.1 test/state_transition/test_state_transition_guard_check.rb