diff options
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/state_machine.rb | 14 | ||||
-rw-r--r-- | activemodel/lib/active_model/state_machine/event.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/state_machine/machine.rb | 8 | ||||
-rw-r--r-- | activemodel/lib/active_model/state_machine/state.rb | 7 | ||||
-rw-r--r-- | activemodel/test/observing_test.rb | 38 | ||||
-rw-r--r-- | activemodel/test/state_machine/event_test.rb | 18 | ||||
-rw-r--r-- | activemodel/test/state_machine/machine_test.rb | 4 | ||||
-rw-r--r-- | activemodel/test/state_machine/state_test.rb | 46 | ||||
-rw-r--r-- | activemodel/test/state_machine/state_transition_test.rb | 106 | ||||
-rw-r--r-- | activemodel/test/state_machine_test.rb | 174 | ||||
-rw-r--r-- | activemodel/test/test_helper.rb | 36 |
11 files changed, 209 insertions, 244 deletions
diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb index 96df6539ae..bce90fd743 100644 --- a/activemodel/lib/active_model/state_machine.rb +++ b/activemodel/lib/active_model/state_machine.rb @@ -1,14 +1,10 @@ -Dir[File.dirname(__FILE__) + "/state_machine/*.rb"].sort.each do |path| - filename = File.basename(path) - require "active_model/state_machine/#{filename}" -end - module ActiveModel module StateMachine class InvalidTransition < Exception end def self.included(base) + require 'active_model/state_machine/machine' base.extend ClassMethods end @@ -35,6 +31,12 @@ module ActiveModel state_machines[name] ||= Machine.new(self, name) block ? state_machines[name].update(options, &block) : state_machines[name] end + + def define_state_query_method(state_name) + name = "#{state_name}?" + undef_method(name) if method_defined?(name) + class_eval "def #{name}; current_state.to_s == %(#{state_name}) end" + end end def current_state(name = nil, new_state = nil, persist = false) @@ -63,4 +65,4 @@ module ActiveModel end end end -end
\ No newline at end of file +end diff --git a/activemodel/lib/active_model/state_machine/event.rb b/activemodel/lib/active_model/state_machine/event.rb index 8acde7fd47..3eb656b6d6 100644 --- a/activemodel/lib/active_model/state_machine/event.rb +++ b/activemodel/lib/active_model/state_machine/event.rb @@ -1,3 +1,5 @@ +require 'active_model/state_machine/state_transition' + module ActiveModel module StateMachine class Event diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb index 170505c0b2..a5ede021b1 100644 --- a/activemodel/lib/active_model/state_machine/machine.rb +++ b/activemodel/lib/active_model/state_machine/machine.rb @@ -1,7 +1,11 @@ +require 'active_model/state_machine/state' +require 'active_model/state_machine/event' + module ActiveModel module StateMachine class Machine - attr_accessor :initial_state, :states, :events, :state_index + attr_writer :initial_state + attr_accessor :states, :events, :state_index attr_reader :klass, :name def initialize(klass, name, options = {}, &block) @@ -71,4 +75,4 @@ module ActiveModel end end end -end
\ No newline at end of file +end diff --git a/activemodel/lib/active_model/state_machine/state.rb b/activemodel/lib/active_model/state_machine/state.rb index 68eb2aa34a..76916b1d86 100644 --- a/activemodel/lib/active_model/state_machine/state.rb +++ b/activemodel/lib/active_model/state_machine/state.rb @@ -5,11 +5,8 @@ module ActiveModel def initialize(name, options = {}) @name = name - machine = options.delete(:machine) - if machine - machine.klass.send(:define_method, "#{name}?") do - current_state.to_s == name.to_s - end + if machine = options.delete(:machine) + machine.klass.define_state_query_method(name) end update(options) end diff --git a/activemodel/test/observing_test.rb b/activemodel/test/observing_test.rb index 6e124de52f..dc41c9f881 100644 --- a/activemodel/test/observing_test.rb +++ b/activemodel/test/observing_test.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) +require 'test_helper' class ObservedModel < ActiveModel::Base class Observer @@ -40,24 +40,22 @@ class ObservingTest < ActiveModel::TestCase assert ObservedModel.observers.include?(:bar), ":bar not in #{ObservedModel.observers.inspect}" end - uses_mocha "observer instantiation" do - test "instantiates observer names passed as strings" do - ObservedModel.observers << 'foo_observer' - FooObserver.expects(:instance) - ObservedModel.instantiate_observers - end - - test "instantiates observer names passed as symbols" do - ObservedModel.observers << :foo_observer - FooObserver.expects(:instance) - ObservedModel.instantiate_observers - end - - test "instantiates observer classes" do - ObservedModel.observers << ObservedModel::Observer - ObservedModel::Observer.expects(:instance) - ObservedModel.instantiate_observers - end + test "instantiates observer names passed as strings" do + ObservedModel.observers << 'foo_observer' + FooObserver.expects(:instance) + ObservedModel.instantiate_observers + end + + test "instantiates observer names passed as symbols" do + ObservedModel.observers << :foo_observer + FooObserver.expects(:instance) + ObservedModel.instantiate_observers + end + + test "instantiates observer classes" do + ObservedModel.observers << ObservedModel::Observer + ObservedModel::Observer.expects(:instance) + ObservedModel.instantiate_observers end test "passes observers to subclasses" do @@ -120,4 +118,4 @@ class ObserverTest < ActiveModel::TestCase Foo.send(:changed) Foo.send(:notify_observers, :whatever, foo) end -end
\ No newline at end of file +end diff --git a/activemodel/test/state_machine/event_test.rb b/activemodel/test/state_machine/event_test.rb index 40b630da7c..8fb7e82ec2 100644 --- a/activemodel/test/state_machine/event_test.rb +++ b/activemodel/test/state_machine/event_test.rb @@ -1,31 +1,29 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) +require 'test_helper' class EventTest < ActiveModel::TestCase def setup - @name = :close_order + @state_name = :close_order @success = :success_callback end def new_event - @event = ActiveModel::StateMachine::Event.new(nil, @name, {:success => @success}) do + @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 @name, new_event.name + assert_equal @state_name, new_event.name end test 'should set the success option' do assert_equal @success, 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 + 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 diff --git a/activemodel/test/state_machine/machine_test.rb b/activemodel/test/state_machine/machine_test.rb index 2cdfcd9554..d23c223160 100644 --- a/activemodel/test/state_machine/machine_test.rb +++ b/activemodel/test/state_machine/machine_test.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) +require 'test_helper' class MachineTestSubject include ActiveModel::StateMachine @@ -40,4 +40,4 @@ class StateMachineMachineTest < ActiveModel::TestCase assert events.include?(:shutdown) assert events.include?(:timeout) end -end
\ No newline at end of file +end diff --git a/activemodel/test/state_machine/state_test.rb b/activemodel/test/state_machine/state_test.rb index 22d0d9eb93..fbf9ce7b0a 100644 --- a/activemodel/test/state_machine/state_test.rb +++ b/activemodel/test/state_machine/state_test.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) +require 'test_helper' class StateTestSubject include ActiveModel::StateMachine @@ -9,13 +9,13 @@ end class StateTest < ActiveModel::TestCase def setup - @name = :astate + @state_name = :astate @machine = StateTestSubject.state_machine @options = { :crazy_custom_key => 'key', :machine => @machine } end def new_state(options={}) - ActiveModel::StateMachine::State.new(@name, @options.merge(options)) + ActiveModel::StateMachine::State.new(@state_name, @options.merge(options)) end test 'sets the name' do @@ -43,32 +43,30 @@ class StateTest < ActiveModel::TestCase 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) + 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) + record = stub + record.expects(:foo) - state.call_action(:entering, record) - end + 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') + 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) + record = stub + record.expects(:foo) - state.call_action(:entering, record) - end + 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}) + 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 + record = stub + record.expects(:foobar) + + state.call_action(:entering, record) end -end
\ No newline at end of file +end diff --git a/activemodel/test/state_machine/state_transition_test.rb b/activemodel/test/state_machine/state_transition_test.rb index 9a9e7f60c5..b59ff5a6a7 100644 --- a/activemodel/test/state_machine/state_transition_test.rb +++ b/activemodel/test/state_machine/state_transition_test.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) +require 'test_helper' class StateTransitionTest < ActiveModel::TestCase test 'should set from, to, and opts attr readers' do @@ -10,39 +10,37 @@ class StateTransitionTest < ActiveModel::TestCase 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) + 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]) + obj = stub + obj.stubs(:from).returns(opts[:from]) + obj.stubs(:to).returns(opts[:to]) - assert_equal st, obj - end + 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) + 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]) + 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) + 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') + obj = stub + obj.stubs(:from).returns(opts[:from]) + obj.stubs(:to).returns('blah') - assert_not_equal st, obj - end + assert_not_equal st, obj end end @@ -54,35 +52,33 @@ class StateTransitionGuardCheckTest < ActiveModel::TestCase 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) + 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) - 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 + 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 diff --git a/activemodel/test/state_machine_test.rb b/activemodel/test/state_machine_test.rb index b2f0fc4ec0..312d8728ba 100644 --- a/activemodel/test/state_machine_test.rb +++ b/activemodel/test/state_machine_test.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper')) +require 'test_helper' class StateMachineSubject include ActiveModel::StateMachine @@ -122,24 +122,22 @@ class StateMachineEventFiringWithPersistenceTest < ActiveModel::TestCase assert_equal :closed, @subj.current_state end - uses_mocha "StateMachineEventFiringWithPersistenceTest with callbacks" do - test 'fires the Event' do - @subj.class.state_machine.events[:close].expects(:fire).with(@subj) - @subj.close! - end - - test 'calls the success callback if one was provided' do - @subj.expects(:success_callback) - @subj.close! - end + test 'fires the Event' do + @subj.class.state_machine.events[:close].expects(:fire).with(@subj) + @subj.close! + end - test 'attempts to persist if write_state is defined' do - def @subj.write_state - end + test 'calls the success callback if one was provided' do + @subj.expects(:success_callback) + @subj.close! + end - @subj.expects(:write_state) - @subj.close! + test 'attempts to persist if write_state is defined' do + def @subj.write_state end + + @subj.expects(:write_state) + @subj.close! end end @@ -151,98 +149,90 @@ class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase assert_equal :closed, subj.current_state end - uses_mocha 'StateMachineEventFiringWithoutPersistence' do - test 'fires the Event' do - subj = StateMachineSubject.new + test 'fires the Event' do + subj = StateMachineSubject.new - StateMachineSubject.state_machine.events[:close].expects(:fire).with(subj) - subj.close - end + StateMachineSubject.state_machine.events[:close].expects(:fire).with(subj) + subj.close + end - test 'attempts to persist if write_state is defined' do - subj = StateMachineSubject.new + test 'attempts to persist if write_state is defined' do + subj = StateMachineSubject.new - def subj.write_state - end + def subj.write_state + end - subj.expects(:write_state_without_persistence) + subj.expects(:write_state_without_persistence) - subj.close - end + subj.close end end -uses_mocha 'StateMachinePersistenceTest' do - class StateMachinePersistenceTest < ActiveModel::TestCase - test 'reads the state if it has not been set and read_state is defined' do - subj = StateMachineSubject.new - def subj.read_state - end +class StateMachinePersistenceTest < ActiveModel::TestCase + test 'reads the state if it has not been set and read_state is defined' do + subj = StateMachineSubject.new + def subj.read_state + end - subj.expects(:read_state).with(StateMachineSubject.state_machine) + subj.expects(:read_state).with(StateMachineSubject.state_machine) - subj.current_state - end + subj.current_state end end -uses_mocha 'StateMachineEventCallbacksTest' do - class StateMachineEventCallbacksTest < ActiveModel::TestCase - test 'should call aasm_event_fired if defined and successful for bang fire' do - subj = StateMachineSubject.new - def subj.aasm_event_fired(from, to) - end - - subj.expects(:event_fired) - - subj.close! +class StateMachineEventCallbacksTest < ActiveModel::TestCase + test 'should call aasm_event_fired if defined and successful for bang fire' do + subj = StateMachineSubject.new + def subj.aasm_event_fired(from, to) end - test 'should call aasm_event_fired if defined and successful for non-bang fire' do - subj = StateMachineSubject.new - def subj.aasm_event_fired(from, to) - end + subj.expects(:event_fired) - subj.expects(:event_fired) + subj.close! + end - subj.close + test 'should call aasm_event_fired if defined and successful for non-bang fire' do + subj = StateMachineSubject.new + def subj.aasm_event_fired(from, to) end - test 'should call aasm_event_failed if defined and transition failed for bang fire' do - subj = StateMachineSubject.new - def subj.event_failed(event) - end + subj.expects(:event_fired) - subj.expects(:event_failed) + subj.close + end - subj.null! + test 'should call aasm_event_failed if defined and transition failed for bang fire' do + subj = StateMachineSubject.new + def subj.event_failed(event) end - test 'should call aasm_event_failed if defined and transition failed for non-bang fire' do - subj = StateMachineSubject.new - def subj.aasm_event_failed(event) - end + subj.expects(:event_failed) - subj.expects(:event_failed) + subj.null! + end - subj.null + test 'should call aasm_event_failed if defined and transition failed for non-bang fire' do + subj = StateMachineSubject.new + def subj.aasm_event_failed(event) end + + subj.expects(:event_failed) + + subj.null 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 +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 + test "calls exit when exiting state" do + subj = StateMachineSubject.new + subj.expects(:exit) + subj.close end end @@ -306,19 +296,17 @@ class StateMachineWithComplexTransitionsTest < ActiveModel::TestCase assert_equal :working, @subj.current_state(:chetan_patil) end - uses_mocha "StateMachineWithComplexTransitionsTest on_transition tests" do - test 'calls on_transition method with args' do - @subj.wakeup! :showering - - @subj.expects(:wear_clothes).with('blue', 'jeans') - @subj.dress! :working, 'blue', 'jeans' - end - - test 'calls on_transition proc' do - @subj.wakeup! :showering - - @subj.expects(:wear_clothes).with('purple', 'slacks') - @subj.dress!(:dating, 'purple', 'slacks') - end + test 'calls on_transition method with args' do + @subj.wakeup! :showering + + @subj.expects(:wear_clothes).with('blue', 'jeans') + @subj.dress! :working, 'blue', 'jeans' + end + + test 'calls on_transition proc' do + @subj.wakeup! :showering + + @subj.expects(:wear_clothes).with('purple', 'slacks') + @subj.dress!(:dating, 'purple', 'slacks') end -end
\ No newline at end of file +end diff --git a/activemodel/test/test_helper.rb b/activemodel/test/test_helper.rb index ccf93280ec..5b5678e42d 100644 --- a/activemodel/test/test_helper.rb +++ b/activemodel/test/test_helper.rb @@ -1,39 +1,21 @@ -$:.unshift "#{File.dirname(__FILE__)}/../lib" -$:.unshift File.dirname(__FILE__) - +require 'rubygems' require 'test/unit' + +gem 'mocha', '>= 0.9.3' +require 'mocha' + require 'active_model' require 'active_model/state_machine' -require 'active_support/callbacks' # needed by ActiveModel::TestCase -require 'active_support/test_case' -def uses_gem(gem_name, test_name, version = '> 0') - require 'rubygems' - gem gem_name.to_s, version - require gem_name.to_s - yield -rescue LoadError - $stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again." -end +$:.unshift File.dirname(__FILE__) + "/../../activesupport/lib" +require 'active_support' +require 'active_support/test_case' -# Wrap tests that use Mocha and skip if unavailable. -unless defined? uses_mocha - def uses_mocha(test_name, &block) - uses_gem('mocha', test_name, '>= 0.5.5', &block) - end +class ActiveModel::TestCase < ActiveSupport::TestCase end begin - require 'rubygems' require 'ruby-debug' Debugger.start rescue LoadError end - -ActiveSupport::TestCase.send :include, ActiveSupport::Testing::Default - -module ActiveModel - class TestCase < ActiveSupport::TestCase - include ActiveSupport::Testing::Default - end -end
\ No newline at end of file |