aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/state_machine
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-03-20 10:32:24 +0000
committerPratik Naik <pratiknaik@gmail.com>2009-03-20 10:32:24 +0000
commit5b1a1bf5bfc520248285b036672146122dd2a815 (patch)
tree31d82a9155db6c9e553888da6375bd89fdb122cf /activemodel/test/state_machine
parent93e2d378df5f599e3e48e74932f37e1c679f633e (diff)
downloadrails-5b1a1bf5bfc520248285b036672146122dd2a815.tar.gz
rails-5b1a1bf5bfc520248285b036672146122dd2a815.tar.bz2
rails-5b1a1bf5bfc520248285b036672146122dd2a815.zip
Make Active Model test suite similar to Active Record
Diffstat (limited to 'activemodel/test/state_machine')
-rw-r--r--activemodel/test/state_machine/event_test.rb49
-rw-r--r--activemodel/test/state_machine/machine_test.rb43
-rw-r--r--activemodel/test/state_machine/state_test.rb72
-rw-r--r--activemodel/test/state_machine/state_transition_test.rb84
4 files changed, 0 insertions, 248 deletions
diff --git a/activemodel/test/state_machine/event_test.rb b/activemodel/test/state_machine/event_test.rb
deleted file mode 100644
index 64dc8c4875..0000000000
--- a/activemodel/test/state_machine/event_test.rb
+++ /dev/null
@@ -1,49 +0,0 @@
-require 'test_helper'
-
-class EventTest < ActiveModel::TestCase
- def setup
- @state_name = :close_order
- @success = :success_callback
- end
-
- def new_event
- @event = ActiveModel::StateMachine::Event.new(nil, @state_name, {:success => @success}) do
- transitions :to => :closed, :from => [:open, :received]
- end
- end
-
- test 'should set the name' do
- assert_equal @state_name, new_event.name
- end
-
- test 'should set the success option' do
- assert_equal @success, new_event.success
- end
-
- test 'should create StateTransitions' do
- ActiveModel::StateMachine::StateTransition.expects(:new).with(:to => :closed, :from => :open)
- ActiveModel::StateMachine::StateTransition.expects(:new).with(:to => :closed, :from => :received)
- new_event
- end
-end
-
-class EventBeingFiredTest < ActiveModel::TestCase
- test 'should raise an AASM::InvalidTransition error if the transitions are empty' do
- event = ActiveModel::StateMachine::Event.new(nil, :event)
-
- assert_raise ActiveModel::StateMachine::InvalidTransition do
- event.fire(nil)
- end
- end
-
- test 'should return the state of the first matching transition it finds' do
- event = ActiveModel::StateMachine::Event.new(nil, :event) do
- transitions :to => :closed, :from => [:open, :received]
- end
-
- obj = stub
- obj.stubs(:current_state).returns(:open)
-
- assert_equal :closed, event.fire(obj)
- end
-end
diff --git a/activemodel/test/state_machine/machine_test.rb b/activemodel/test/state_machine/machine_test.rb
deleted file mode 100644
index d23c223160..0000000000
--- a/activemodel/test/state_machine/machine_test.rb
+++ /dev/null
@@ -1,43 +0,0 @@
-require 'test_helper'
-
-class MachineTestSubject
- include ActiveModel::StateMachine
-
- state_machine do
- state :open
- state :closed
- end
-
- state_machine :initial => :foo do
- event :shutdown do
- transitions :from => :open, :to => :closed
- end
-
- event :timeout do
- transitions :from => :open, :to => :closed
- end
- 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
-
- test "finds events for given state" do
- events = MachineTestSubject.state_machine.events_for(:open)
- assert events.include?(:shutdown)
- assert events.include?(:timeout)
- end
-end
diff --git a/activemodel/test/state_machine/state_test.rb b/activemodel/test/state_machine/state_test.rb
deleted file mode 100644
index fbf9ce7b0a..0000000000
--- a/activemodel/test/state_machine/state_test.rb
+++ /dev/null
@@ -1,72 +0,0 @@
-require 'test_helper'
-
-class StateTestSubject
- include ActiveModel::StateMachine
-
- state_machine do
- end
-end
-
-class StateTest < ActiveModel::TestCase
- def setup
- @state_name = :astate
- @machine = StateTestSubject.state_machine
- @options = { :crazy_custom_key => 'key', :machine => @machine }
- end
-
- def new_state(options={})
- ActiveModel::StateMachine::State.new(@state_name, @options.merge(options))
- end
-
- test 'sets the name' do
- assert_equal :astate, new_state.name
- end
-
- test 'sets the display_name from name' do
- assert_equal "Astate", new_state.display_name
- end
-
- test 'sets the display_name from options' do
- assert_equal "A State", new_state(:display => "A State").display_name
- end
-
- test 'sets the options and expose them as options' do
- @options.delete(:machine)
- assert_equal @options, new_state.options
- end
-
- test 'equals a symbol of the same name' do
- assert_equal new_state, :astate
- end
-
- test 'equals a State of the same name' do
- assert_equal new_state, new_state
- end
-
- test 'should send a message to the record for an action if the action is present as a symbol' do
- state = new_state(:entering => :foo)
-
- record = stub
- record.expects(:foo)
-
- state.call_action(:entering, record)
- end
-
- test 'should send a message to the record for an action if the action is present as a string' do
- state = new_state(:entering => 'foo')
-
- record = stub
- record.expects(:foo)
-
- state.call_action(:entering, record)
- end
-
- test 'should call a proc, passing in the record for an action if the action is present' do
- state = new_state(:entering => Proc.new {|r| r.foobar})
-
- record = stub
- record.expects(:foobar)
-
- state.call_action(:entering, record)
- end
-end
diff --git a/activemodel/test/state_machine/state_transition_test.rb b/activemodel/test/state_machine/state_transition_test.rb
deleted file mode 100644
index b59ff5a6a7..0000000000
--- a/activemodel/test/state_machine/state_transition_test.rb
+++ /dev/null
@@ -1,84 +0,0 @@
-require '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
-
- 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
-
-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
-
- 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