File: state_machine_test.rb

package info (click to toggle)
ruby-mocha 0.11.3-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,300 kB
  • sloc: ruby: 9,935; makefile: 2
file content (98 lines) | stat: -rw-r--r-- 3,131 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
require File.expand_path('../../test_helper', __FILE__)

require 'mocha/state_machine'

class StateMachineTest < Test::Unit::TestCase

  include Mocha

  def test_should_initially_be_in_no_state
    state_machine = StateMachine.new('name')
    any_state.each do |state|
      assert !state_machine.is(state).active?
      assert state_machine.is_not(state).active?
    end
  end

  def test_should_be_able_to_enter_a_state
    state_machine = StateMachine.new('name')
    state = 'A'
    other_states = any_state.reject { |s| s == state }

    state_machine.is(state).activate

    assert state_machine.is(state).active?
    assert !state_machine.is_not(state).active?
    other_states.each do |s|
      assert !state_machine.is(s).active?
      assert state_machine.is_not(s).active?
    end
  end

  def test_should_be_able_to_change_state
    state_machine = StateMachine.new('name')
    state = 'B'
    other_states = any_state.reject { |s| s == state }

    state_machine.is('A').activate
    state_machine.is(state).activate

    assert state_machine.is(state).active?
    assert !state_machine.is_not(state).active?
    other_states.each do |s|
      assert !state_machine.is(s).active?
      assert state_machine.is_not(s).active?
    end
  end

  def test_should_be_put_into_an_initial_state
    state_machine = StateMachine.new('name')
    initial_state = 'A'
    other_states = any_state.reject { |s| s == initial_state }

    state_machine.starts_as(initial_state)

    assert state_machine.is(initial_state).active?
    assert !state_machine.is_not(initial_state).active?
    other_states.each do |state|
      assert !state_machine.is(state).active?
      assert state_machine.is_not(state).active?
    end
  end

  def test_should_be_put_into_a_new_state
    next_state = 'B'

    other_states = any_state.reject { |s| s == next_state }
    state_machine = StateMachine.new('name').starts_as('A')

    state_machine.become(next_state)

    assert state_machine.is(next_state).active?
    assert !state_machine.is_not(next_state).active?
    other_states.each do |state|
      assert !state_machine.is(state).active?
      assert state_machine.is_not(state).active?
    end
  end

  def test_should_describe_itself_as_name_and_current_state
    state_machine = StateMachine.new('state_machine_name')
    assert_equal 'state_machine_name has no current state', state_machine.mocha_inspect
    inspectable_state = Class.new { define_method(:mocha_inspect) { "'inspectable_state'" } }.new
    state_machine.is(inspectable_state).activate
    assert_equal "state_machine_name is 'inspectable_state'", state_machine.mocha_inspect
  end

  def test_should_have_self_describing_states
    state_machine = StateMachine.new('state_machine_name')
    inspectable_state = Class.new { define_method(:mocha_inspect) { "'inspectable_state'" } }.new
    assert_equal "state_machine_name is 'inspectable_state'", state_machine.is(inspectable_state).mocha_inspect
    assert_equal "state_machine_name is not 'inspectable_state'", state_machine.is_not(inspectable_state).mocha_inspect
  end

  def any_state
    %w(A B C D)
  end

end