2008-06-28 03:55:02 -04:00
|
|
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
|
|
|
|
|
|
|
|
class MachineTestSubject
|
|
|
|
include ActiveModel::StateMachine
|
|
|
|
|
|
|
|
state_machine do
|
2008-06-28 12:19:44 -04:00
|
|
|
state :open
|
|
|
|
state :closed
|
2008-06-28 03:55:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
state_machine :initial => :foo do
|
2008-06-28 12:19:44 -04:00
|
|
|
event :shutdown do
|
|
|
|
transitions :from => :open, :to => :closed
|
|
|
|
end
|
|
|
|
|
|
|
|
event :timeout do
|
|
|
|
transitions :from => :open, :to => :closed
|
|
|
|
end
|
2008-06-28 03:55:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
state_machine :extra, :initial => :bar do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class StateMachineMachineTest < ActiveModel::TestCase
|
|
|
|
test "allows reuse of existing machines" do
|
|
|
|
assert_equal 2, MachineTestSubject.state_machines.size
|
|
|
|
end
|
|
|
|
|
|
|
|
test "sets #initial_state from :initial option" do
|
|
|
|
assert_equal :bar, MachineTestSubject.state_machine(:extra).initial_state
|
|
|
|
end
|
|
|
|
|
|
|
|
test "accesses non-default state machine" do
|
|
|
|
assert_kind_of ActiveModel::StateMachine::Machine, MachineTestSubject.state_machine(:extra)
|
|
|
|
end
|
2008-06-28 12:19:44 -04:00
|
|
|
|
|
|
|
test "finds events for given state" do
|
|
|
|
assert_equal [:shutdown, :timeout], MachineTestSubject.state_machine.events_for(:open)
|
|
|
|
end
|
2008-06-28 03:55:02 -04:00
|
|
|
end
|