1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

first tests for state machines with multiple state attributes

This commit is contained in:
Thorsten Böttger 2015-05-18 21:27:56 +12:00
parent cf7ac74664
commit f043bbb04d
4 changed files with 96 additions and 3 deletions

View file

@ -77,7 +77,7 @@ module AASM
def event(name, options={}, &block)
@state_machine.add_event(name, options, &block)
if @klass.instance_methods.include?("may_#{name}?")
if @klass.instance_methods.include?("may_#{name}?".to_sym)
warn "The event name #{name} is already used!"
end

View file

@ -4,10 +4,10 @@ module AASM
# may be overwritten by persistence mixins
def aasm_read_state(name=:default)
# all the following lines behave like @current_state ||= aasm.enter_initial_state
# all the following lines behave like @current_state ||= aasm(name).enter_initial_state
current = aasm(name).instance_variable_get("@current_state_#{name}")
return current if current
aasm(name).instance_variable_set("@current_state_#{name}", aasm.enter_initial_state)
aasm(name).instance_variable_set("@current_state_#{name}", aasm(name).enter_initial_state)
end
# may be overwritten by persistence mixins

View file

@ -0,0 +1,30 @@
class SimpleMultipleExample
include AASM
aasm(:move) do
state :standing, :initial => true
state :walking
state :running
event :walk do
transitions :from => :standing, :to => :walking
end
event :run do
transitions :from => [:standing, :walking], :to => :running
end
event :hold do
transitions :from => [:walking, :running], :to => :standing
end
end
aasm(:work) do
state :sleeping, :initial => true
state :processing
event :start do
transitions :from => :sleeping, :to => :processing
end
event :stop do
transitions :from => :processing, :to => :sleeping
end
end
end

View file

@ -0,0 +1,63 @@
require 'spec_helper'
describe 'state machine' do
let(:simple) { SimpleMultipleExample.new }
it 'starts with an initial state' do
expect(simple.aasm(:move).current_state).to eq(:standing)
expect(simple).to respond_to(:standing?)
expect(simple).to be_standing
expect(simple.aasm(:work).current_state).to eq(:sleeping)
expect(simple).to respond_to(:sleeping?)
expect(simple).to be_sleeping
end
it 'allows transitions to other states' do
expect(simple).to respond_to(:walk)
expect(simple).to respond_to(:walk!)
simple.walk!
expect(simple).to respond_to(:walking?)
expect(simple).to be_walking
expect(simple).to respond_to(:run)
expect(simple).to respond_to(:run!)
simple.run
expect(simple).to respond_to(:running?)
expect(simple).to be_running
expect(simple).to respond_to(:start)
expect(simple).to respond_to(:start!)
simple.start
expect(simple).to respond_to(:processing?)
expect(simple).to be_processing
end
it 'denies transitions to other states' do
expect {simple.hold}.to raise_error(AASM::InvalidTransition)
expect {simple.hold!}.to raise_error(AASM::InvalidTransition)
simple.walk
expect {simple.walk}.to raise_error(AASM::InvalidTransition)
expect {simple.walk!}.to raise_error(AASM::InvalidTransition)
simple.run
expect {simple.walk}.to raise_error(AASM::InvalidTransition)
expect {simple.walk!}.to raise_error(AASM::InvalidTransition)
expect {simple.stop}.to raise_error(AASM::InvalidTransition)
expect {simple.stop!}.to raise_error(AASM::InvalidTransition)
simple.start
expect {simple.start}.to raise_error(AASM::InvalidTransition)
expect {simple.start!}.to raise_error(AASM::InvalidTransition)
simple.stop
end
it 'defines constants for each state name' do
expect(SimpleMultipleExample::STATE_STANDING).to eq(:standing)
expect(SimpleMultipleExample::STATE_WALKING).to eq(:walking)
expect(SimpleMultipleExample::STATE_RUNNING).to eq(:running)
expect(SimpleMultipleExample::STATE_SLEEPING).to eq(:sleeping)
expect(SimpleMultipleExample::STATE_PROCESSING).to eq(:processing)
expect(SimpleMultipleExample::STATE_RUNNING).to eq(:running)
end
end