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
|
module Mocha # :nodoc:
# A state machine that is used to constrain the order of invocations.
# An invocation can be constrained to occur when a state is, or is_not, active.
class StateMachine
class State # :nodoc:
def initialize(state_machine, state)
@state_machine, @state = state_machine, state
end
def activate
@state_machine.current_state = @state
end
def active?
@state_machine.current_state == @state
end
def mocha_inspect
"#{@state_machine.name} is #{@state.mocha_inspect}"
end
end
class StatePredicate # :nodoc:
def initialize(state_machine, state)
@state_machine, @state = state_machine, state
end
def active?
@state_machine.current_state != @state
end
def mocha_inspect
"#{@state_machine.name} is not #{@state.mocha_inspect}"
end
end
attr_reader :name # :nodoc:
attr_accessor :current_state # :nodoc:
def initialize(name) # :nodoc:
@name = name
@current_state = nil
end
# :call-seq: starts_as(initial_state) -> state_machine
#
# Put the +state_machine+ into the +initial_state+.
def starts_as(initial_state)
become(initial_state)
self
end
# :call-seq: become(next_state)
#
# Put the +state_machine+ into the +next_state+.
def become(next_state)
@current_state = next_state
end
# :call-seq: is(state)
#
# Determines whether the +state_machine+ is in the specified +state+.
def is(state)
State.new(self, state)
end
# :call-seq: is_not(state)
#
# Determines whether the +state_machine+ is not in the specified +state+.
def is_not(state)
StatePredicate.new(self, state)
end
def mocha_inspect # :nodoc:
if @current_state
"#{@name} is #{@current_state.mocha_inspect}"
else
"#{@name} has no current state"
end
end
end
end
|