aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/state_machine/state_transition_test.rb
blob: 9a9e7f60c5f73e6fbd07e8ba5f0dac89418ca4b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))

class StateTransitionTest < ActiveModel::TestCase
  test 'should set from, to, and opts attr readers' do
    opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
    st = ActiveModel::StateMachine::StateTransition.new(opts)

    assert_equal opts[:from], st.from
    assert_equal opts[:to],   st.to
    assert_equal opts,        st.options
  end

  uses_mocha 'checking ActiveModel StateMachine transitions' do
    test 'should pass equality check if from and to are the same' do
      opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
      st = ActiveModel::StateMachine::StateTransition.new(opts)

      obj = stub
      obj.stubs(:from).returns(opts[:from])
      obj.stubs(:to).returns(opts[:to])

      assert_equal st, obj
    end

    test 'should fail equality check if from are not the same' do
      opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
      st = ActiveModel::StateMachine::StateTransition.new(opts)

      obj = stub
      obj.stubs(:from).returns('blah')
      obj.stubs(:to).returns(opts[:to])

      assert_not_equal st, obj
    end
  
    test 'should fail equality check if to are not the same' do
      opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
      st = ActiveModel::StateMachine::StateTransition.new(opts)

      obj = stub
      obj.stubs(:from).returns(opts[:from])
      obj.stubs(:to).returns('blah')

      assert_not_equal st, obj
    end
  end
end

class StateTransitionGuardCheckTest < ActiveModel::TestCase
  test 'should return true of there is no guard' do
    opts = {:from => 'foo', :to => 'bar'}
      st = ActiveModel::StateMachine::StateTransition.new(opts)

    assert st.perform(nil)
  end

  uses_mocha 'checking ActiveModel StateMachine transition guard checks' do
    test 'should call the method on the object if guard is a symbol' do
      opts = {:from => 'foo', :to => 'bar', :guard => :test_guard}
      st = ActiveModel::StateMachine::StateTransition.new(opts)
    
      obj = stub
      obj.expects(:test_guard)
      
      st.perform(obj)
    end
    
    test 'should call the method on the object if guard is a string' do
      opts = {:from => 'foo', :to => 'bar', :guard => 'test_guard'}
      st = ActiveModel::StateMachine::StateTransition.new(opts)
    
      obj = stub
      obj.expects(:test_guard)
      
      st.perform(obj)
    end
    
    test 'should call the proc passing the object if the guard is a proc' do
      opts = {:from => 'foo', :to => 'bar', :guard => Proc.new {|o| o.test_guard}}
      st = ActiveModel::StateMachine::StateTransition.new(opts)
    
      obj = stub
      obj.expects(:test_guard)
    
      st.perform(obj)
    end
  end
end