aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test
diff options
context:
space:
mode:
authorrick <technoweenie@gmail.com>2008-06-28 09:19:44 -0700
committerrick <technoweenie@gmail.com>2008-06-28 09:19:44 -0700
commit74cb05698684f237a7eb91afadec0020d8910c70 (patch)
treeff961d9f9c2119a6608db90bc41d713a191a9b9f /activemodel/test
parentb9528ad3c5379896b00772cb44faf1db0fd882d7 (diff)
downloadrails-74cb05698684f237a7eb91afadec0020d8910c70.tar.gz
rails-74cb05698684f237a7eb91afadec0020d8910c70.tar.bz2
rails-74cb05698684f237a7eb91afadec0020d8910c70.zip
add basic events and transitions. still more tests to convert
Diffstat (limited to 'activemodel/test')
-rw-r--r--activemodel/test/state_machine/event_test.rb51
-rw-r--r--activemodel/test/state_machine/machine_test.rb13
-rw-r--r--activemodel/test/state_machine/state_test.rb5
-rw-r--r--activemodel/test/state_machine/state_transition_test.rb88
-rw-r--r--activemodel/test/state_machine_test.rb166
5 files changed, 233 insertions, 90 deletions
diff --git a/activemodel/test/state_machine/event_test.rb b/activemodel/test/state_machine/event_test.rb
new file mode 100644
index 0000000000..01f3464cf2
--- /dev/null
+++ b/activemodel/test/state_machine/event_test.rb
@@ -0,0 +1,51 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
+
+class EventTest < ActiveModel::TestCase
+ def setup
+ @name = :close_order
+ @success = :success_callback
+ end
+
+ def new_event
+ @event = ActiveModel::StateMachine::Event.new(@name, {:success => @success}) do
+ transitions :to => :closed, :from => [:open, :received]
+ end
+ end
+
+ test 'should set the name' do
+ assert_equal @name, new_event.name
+ end
+
+ test 'should set the success option' do
+ assert new_event.success?
+ end
+
+ uses_mocha 'StateTransition creation' do
+ 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
+end
+
+class EventBeingFiredTest < ActiveModel::TestCase
+ test 'should raise an AASM::InvalidTransition error if the transitions are empty' do
+ event = ActiveModel::StateMachine::Event.new(:event)
+
+ assert_raises 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(: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
index 34a4b384ce..64dea42b1f 100644
--- a/activemodel/test/state_machine/machine_test.rb
+++ b/activemodel/test/state_machine/machine_test.rb
@@ -4,9 +4,18 @@ 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
@@ -25,4 +34,8 @@ class StateMachineMachineTest < ActiveModel::TestCase
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
+ assert_equal [:shutdown, :timeout], MachineTestSubject.state_machine.events_for(:open)
+ end
end \ No newline at end of file
diff --git a/activemodel/test/state_machine/state_test.rb b/activemodel/test/state_machine/state_test.rb
index 444435d271..22d0d9eb93 100644
--- a/activemodel/test/state_machine/state_test.rb
+++ b/activemodel/test/state_machine/state_test.rb
@@ -10,12 +10,12 @@ end
class StateTest < ActiveModel::TestCase
def setup
@name = :astate
- @options = { :crazy_custom_key => 'key' }
@machine = StateTestSubject.state_machine
+ @options = { :crazy_custom_key => 'key', :machine => @machine }
end
def new_state(options={})
- ActiveModel::StateMachine::State.new(options.delete(:machine) || @machine, @name, @options.merge(options))
+ ActiveModel::StateMachine::State.new(@name, @options.merge(options))
end
test 'sets the name' do
@@ -31,6 +31,7 @@ class StateTest < ActiveModel::TestCase
end
test 'sets the options and expose them as options' do
+ @options.delete(:machine)
assert_equal @options, new_state.options
end
diff --git a/activemodel/test/state_machine/state_transition_test.rb b/activemodel/test/state_machine/state_transition_test.rb
new file mode 100644
index 0000000000..9a9e7f60c5
--- /dev/null
+++ b/activemodel/test/state_machine/state_transition_test.rb
@@ -0,0 +1,88 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', '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
+
+ uses_mocha 'checking ActiveModel StateMachine transitions' do
+ 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
+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
+
+ uses_mocha 'checking ActiveModel StateMachine transition guard checks' do
+ 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
+end
diff --git a/activemodel/test/state_machine_test.rb b/activemodel/test/state_machine_test.rb
index e906744c77..963ce84248 100644
--- a/activemodel/test/state_machine_test.rb
+++ b/activemodel/test/state_machine_test.rb
@@ -7,22 +7,22 @@ class StateMachineSubject
state :open, :exit => :exit
state :closed, :enter => :enter
- #event :close, :success => :success_callback do
- # transitions :to => :closed, :from => [:open]
- #end
- #
- #event :null do
- # transitions :to => :closed, :from => [:open], :guard => :always_false
- #end
+ event :close, :success => :success_callback do
+ transitions :to => :closed, :from => [:open]
+ end
+
+ event :null do
+ transitions :to => :closed, :from => [:open], :guard => :always_false
+ end
end
state_machine :bar do
state :read
state :ended
- #event :foo do
- # transitions :to => :ended, :from => [:read]
- #end
+ event :foo do
+ transitions :to => :ended, :from => [:read]
+ end
end
def always_false
@@ -80,9 +80,13 @@ class StateMachineInstanceLevelTest < ActiveModel::TestCase
assert @foo.respond_to?(:open?)
end
- #test 'should define an event! inance method' do
- # assert @foo.respond_to?(:close!)
- #end
+ test 'defines an event! instance method' do
+ assert @foo.respond_to?(:close!)
+ end
+
+ test 'defines an event instance method' do
+ assert @foo.respond_to?(:close)
+ end
end
class StateMachineInitialStatesTest < ActiveModel::TestCase
@@ -90,19 +94,19 @@ class StateMachineInitialStatesTest < ActiveModel::TestCase
@foo = StateMachineSubject.new
end
- test 'should set the initial state' do
+ test 'sets the initial state' do
assert_equal :open, @foo.current_state
end
- #test '#open? should be initially true' do
- # @foo.open?.should be_true
- #end
- #
- #test '#closed? should be initially false' do
- # @foo.closed?.should be_false
- #end
+ test '#open? should be initially true' do
+ assert @foo.open?
+ end
+
+ test '#closed? should be initially false' do
+ assert !@foo.closed?
+ end
- test 'should use the first state defined if no initial state is given' do
+ test 'uses the first state defined if no initial state is given' do
assert_equal :read, @foo.current_state(:bar)
end
end
@@ -141,34 +145,36 @@ end
# foo.close!
# end
#end
-#
-#describe AASM, '- event firing without persistence' do
-# it 'should fire the Event' do
-# foo = Foo.new
-#
-# Foo.aasm_events[:close].should_receive(:fire).with(foo)
-# foo.close
-# end
-#
-# it 'should update the current state' do
-# foo = Foo.new
-# foo.close
-#
-# foo.aasm_current_state.should == :closed
-# end
-#
-# it 'should attempt to persist if aasm_write_state is defined' do
-# foo = Foo.new
-#
-# def foo.aasm_write_state
-# end
-#
-# foo.should_receive(:aasm_write_state_without_persistence)
-#
-# foo.close
-# end
-#end
-#
+
+class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase
+ test 'updates the current state' do
+ subj = StateMachineSubject.new
+ assert_equal :open, subj.current_state
+ subj.close
+ assert_equal :closed, subj.current_state
+ end
+
+ uses_mocha 'StateMachineEventFiringWithoutPersistence' do
+ test 'fires the Event' do
+ subj = StateMachineSubject.new
+
+ StateMachineSubject.state_machine.events[:close].expects(:fire).with(subj)
+ subj.close
+ end
+
+ test 'should attempt to persist if aasm_write_state is defined' do
+ subj = StateMachineSubject.new
+
+ def subj.aasm_write_state
+ end
+
+ subj.expects(:aasm_write_state_without_persistence)
+
+ subj.close
+ end
+ end
+end
+
#describe AASM, '- persistence' do
# it 'should read the state if it has not been set and aasm_read_state is defined' do
# foo = Foo.new
@@ -180,22 +186,7 @@ end
# foo.aasm_current_state
# end
#end
-#
-#describe AASM, '- getting events for a state' do
-# it '#aasm_events_for_current_state should use current state' do
-# foo = Foo.new
-# foo.should_receive(:aasm_current_state)
-# foo.aasm_events_for_current_state
-# end
-#
-# it '#aasm_events_for_current_state should use aasm_events_for_state' do
-# foo = Foo.new
-# foo.stub!(:aasm_current_state).and_return(:foo)
-# foo.should_receive(:aasm_events_for_state).with(:foo)
-# foo.aasm_events_for_current_state
-# end
-#end
-#
+
#describe AASM, '- event callbacks' do
# it 'should call aasm_event_fired if defined and successful for bang fire' do
# foo = Foo.new
@@ -237,32 +228,31 @@ end
# foo.null
# end
#end
-#
-#describe AASM, '- state actions' do
-# it "should call enter when entering state" do
-# foo = Foo.new
-# foo.should_receive(:enter)
-#
-# foo.close
-# end
-#
-# it "should call exit when exiting state" do
-# foo = Foo.new
-# foo.should_receive(:exit)
-#
-# foo.close
-# end
-#end
-#
-#
+
+uses_mocha 'StateMachineStateActionsTest' do
+ class StateMachineStateActionsTest < ActiveModel::TestCase
+ test "calls enter when entering state" do
+ subj = StateMachineSubject.new
+ subj.expects(:enter)
+ subj.close
+ end
+
+ test "calls exit when exiting state" do
+ subj = StateMachineSubject.new
+ subj.expects(:exit)
+ subj.close
+ end
+ end
+end
+
class StateMachineInheritanceTest < ActiveModel::TestCase
- test "should have the same states as it's parent" do
+ test "has the same states as its parent" do
assert_equal StateMachineSubject.state_machine.states, StateMachineSubjectSubclass.state_machine.states
end
- #test "should have the same events as it's parent" do
- # StateMachineSubjectSubclass.aasm_events.should == Bar.aasm_events
- #end
+ test "has the same events as its parent" do
+ assert_equal StateMachineSubject.state_machine.events, StateMachineSubjectSubclass.state_machine.events
+ end
end
#
#