aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/state_machine
diff options
context:
space:
mode:
authorrick <technoweenie@gmail.com>2008-06-28 00:55:02 -0700
committerrick <technoweenie@gmail.com>2008-06-28 00:55:02 -0700
commitb9528ad3c5379896b00772cb44faf1db0fd882d7 (patch)
tree422ae423e519f744d51698d45a6fb931172b9cb2 /activemodel/test/state_machine
parentb7c6ceff9a31cc478c4bc89d57980900a775fbed (diff)
downloadrails-b9528ad3c5379896b00772cb44faf1db0fd882d7.tar.gz
rails-b9528ad3c5379896b00772cb44faf1db0fd882d7.tar.bz2
rails-b9528ad3c5379896b00772cb44faf1db0fd882d7.zip
initial statemachine machine and state classes
Diffstat (limited to 'activemodel/test/state_machine')
-rw-r--r--activemodel/test/state_machine/machine_test.rb28
-rw-r--r--activemodel/test/state_machine/state_test.rb73
2 files changed, 101 insertions, 0 deletions
diff --git a/activemodel/test/state_machine/machine_test.rb b/activemodel/test/state_machine/machine_test.rb
new file mode 100644
index 0000000000..34a4b384ce
--- /dev/null
+++ b/activemodel/test/state_machine/machine_test.rb
@@ -0,0 +1,28 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
+
+class MachineTestSubject
+ include ActiveModel::StateMachine
+
+ state_machine do
+ end
+
+ state_machine :initial => :foo do
+ 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
+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
new file mode 100644
index 0000000000..444435d271
--- /dev/null
+++ b/activemodel/test/state_machine/state_test.rb
@@ -0,0 +1,73 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
+
+class StateTestSubject
+ include ActiveModel::StateMachine
+
+ state_machine do
+ end
+end
+
+class StateTest < ActiveModel::TestCase
+ def setup
+ @name = :astate
+ @options = { :crazy_custom_key => 'key' }
+ @machine = StateTestSubject.state_machine
+ end
+
+ def new_state(options={})
+ ActiveModel::StateMachine::State.new(options.delete(:machine) || @machine, @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
+ 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
+
+ uses_mocha 'state actions' do
+ 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
+end \ No newline at end of file