2012-12-02 03:45:11 -05:00
|
|
|
require 'spec_helper'
|
2008-01-07 14:11:38 -05:00
|
|
|
|
2011-11-26 14:49:46 -05:00
|
|
|
describe 'transitions' do
|
|
|
|
|
|
|
|
it 'should raise an exception when whiny' do
|
|
|
|
process = ProcessWithNewDsl.new
|
|
|
|
lambda { process.stop! }.should raise_error(AASM::InvalidTransition)
|
|
|
|
process.should be_sleeping
|
|
|
|
end
|
|
|
|
|
2012-04-02 05:44:53 -04:00
|
|
|
it 'should not raise an exception when not whiny' do
|
2011-11-26 14:49:46 -05:00
|
|
|
silencer = Silencer.new
|
|
|
|
silencer.smile!.should be_false
|
|
|
|
silencer.should be_silent
|
|
|
|
end
|
|
|
|
|
2012-04-02 05:44:53 -04:00
|
|
|
it 'should not raise an exception when superclass not whiny' do
|
|
|
|
sub = SubClassing.new
|
|
|
|
sub.smile!.should be_false
|
|
|
|
sub.should be_silent
|
|
|
|
end
|
2012-10-19 02:18:03 -04:00
|
|
|
|
|
|
|
it 'should not raise an exception when from is nil even if whiny' do
|
|
|
|
silencer = Silencer.new
|
|
|
|
silencer.smile_any!.should be_true
|
|
|
|
silencer.should be_smiling
|
|
|
|
end
|
2012-12-02 03:45:11 -05:00
|
|
|
|
2013-09-03 07:13:53 -04:00
|
|
|
it 'should call the block when success' do
|
|
|
|
silencer = Silencer.new
|
|
|
|
success = false
|
|
|
|
lambda {
|
|
|
|
silencer.smile_any! do
|
|
|
|
success = true
|
|
|
|
end
|
|
|
|
}.should change { success }.to(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not call the block when failure' do
|
|
|
|
silencer = Silencer.new
|
|
|
|
success = false
|
|
|
|
lambda {
|
|
|
|
silencer.smile! do
|
|
|
|
success = true
|
|
|
|
end
|
|
|
|
}.should_not change { success }.to(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'blocks' do
|
2011-11-26 14:49:46 -05:00
|
|
|
end
|
|
|
|
|
2013-02-21 22:02:40 -05:00
|
|
|
describe AASM::Transition do
|
2008-01-07 14:11:38 -05:00
|
|
|
it 'should set from, to, and opts attr readers' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2008-01-07 14:11:38 -05:00
|
|
|
|
|
|
|
st.from.should == opts[:from]
|
|
|
|
st.to.should == opts[:to]
|
|
|
|
st.opts.should == opts
|
|
|
|
end
|
2008-02-21 10:16:08 -05:00
|
|
|
|
|
|
|
it 'should pass equality check if from and to are the same' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
|
|
|
obj.stub(:from).and_return(opts[:from])
|
|
|
|
obj.stub(:to).and_return(opts[:to])
|
2008-02-21 10:16:08 -05:00
|
|
|
|
|
|
|
st.should == obj
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should fail equality check if from are not the same' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
|
|
|
obj.stub(:from).and_return('blah')
|
|
|
|
obj.stub(:to).and_return(opts[:to])
|
2008-02-21 10:16:08 -05:00
|
|
|
|
|
|
|
st.should_not == obj
|
|
|
|
end
|
2009-04-09 01:25:16 -04:00
|
|
|
|
2008-02-21 10:16:08 -05:00
|
|
|
it 'should fail equality check if to are not the same' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
|
|
|
obj.stub(:from).and_return(opts[:from])
|
|
|
|
obj.stub(:to).and_return('blah')
|
2008-02-21 10:16:08 -05:00
|
|
|
|
|
|
|
st.should_not == obj
|
|
|
|
end
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
|
|
|
|
2013-02-21 22:02:40 -05:00
|
|
|
describe AASM::Transition, '- when performing guard checks' do
|
2008-02-21 10:46:06 -05:00
|
|
|
it 'should return true of there is no guard' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar'}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2008-02-21 10:46:06 -05:00
|
|
|
|
|
|
|
st.perform(nil).should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call the method on the object if guard is a symbol' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => :test}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2008-02-21 10:46:06 -05:00
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2008-02-21 10:46:06 -05:00
|
|
|
obj.should_receive(:test)
|
2009-04-09 01:25:16 -04:00
|
|
|
|
2008-02-21 10:46:06 -05:00
|
|
|
st.perform(obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call the method on the object if guard is a string' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'test'}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2008-02-21 10:46:06 -05:00
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2008-02-21 10:46:06 -05:00
|
|
|
obj.should_receive(:test)
|
2009-04-09 01:25:16 -04:00
|
|
|
|
2008-02-21 10:46:06 -05:00
|
|
|
st.perform(obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
it '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}}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2008-02-21 10:46:06 -05:00
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2008-02-21 10:46:06 -05:00
|
|
|
obj.should_receive(:test)
|
|
|
|
|
|
|
|
st.perform(obj)
|
|
|
|
end
|
|
|
|
end
|
2010-10-01 13:33:35 -04:00
|
|
|
|
2013-02-21 22:02:40 -05:00
|
|
|
describe AASM::Transition, '- when executing the transition with a Proc' do
|
2010-10-01 13:33:35 -04:00
|
|
|
it 'should call a Proc on the object with args' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :on_transition => Proc.new {|o| o.test}}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2010-10-01 13:33:35 -04:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2010-10-01 13:33:35 -04:00
|
|
|
|
|
|
|
opts[:on_transition].should_receive(:call).with(any_args)
|
|
|
|
|
|
|
|
st.execute(obj, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call a Proc on the object without args' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :on_transition => Proc.new {||}}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2010-10-01 13:33:35 -04:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2010-10-01 13:33:35 -04:00
|
|
|
|
|
|
|
opts[:on_transition].should_receive(:call).with(no_args)
|
|
|
|
|
|
|
|
st.execute(obj, args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-21 22:02:40 -05:00
|
|
|
describe AASM::Transition, '- when executing the transition with an :on_transtion method call' do
|
2010-10-01 13:33:35 -04:00
|
|
|
it 'should accept a String for the method name' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :on_transition => 'test'}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2010-10-01 13:33:35 -04:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2010-10-01 13:33:35 -04:00
|
|
|
|
|
|
|
obj.should_receive(:test)
|
|
|
|
|
2011-11-26 14:49:46 -05:00
|
|
|
st.execute(obj, args)
|
2010-10-01 13:33:35 -04:00
|
|
|
end
|
2011-11-26 14:49:46 -05:00
|
|
|
|
2010-10-01 13:33:35 -04:00
|
|
|
it 'should accept a Symbol for the method name' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :on_transition => :test}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2010-10-01 13:33:35 -04:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2010-10-01 13:33:35 -04:00
|
|
|
|
|
|
|
obj.should_receive(:test)
|
|
|
|
|
2011-11-26 14:49:46 -05:00
|
|
|
st.execute(obj, args)
|
2010-10-01 13:33:35 -04:00
|
|
|
end
|
2011-11-26 14:49:46 -05:00
|
|
|
|
2010-10-01 13:33:35 -04:00
|
|
|
it 'should pass args if the target method accepts them' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :on_transition => :test}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2010-10-01 13:33:35 -04:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2010-10-01 13:33:35 -04:00
|
|
|
|
|
|
|
obj.class.class_eval do
|
|
|
|
define_method(:test) {|*args| 'success'}
|
|
|
|
end
|
|
|
|
|
|
|
|
return_value = st.execute(obj, args)
|
|
|
|
|
|
|
|
return_value.should == 'success'
|
|
|
|
end
|
2011-11-26 14:49:46 -05:00
|
|
|
|
2010-10-01 13:33:35 -04:00
|
|
|
it 'should NOT pass args if the target method does NOT accept them' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :on_transition => :test}
|
2013-02-21 22:02:40 -05:00
|
|
|
st = AASM::Transition.new(opts)
|
2010-10-01 13:33:35 -04:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2010-10-01 13:33:35 -04:00
|
|
|
|
|
|
|
obj.class.class_eval do
|
|
|
|
define_method(:test) {|*args| 'success'}
|
|
|
|
end
|
|
|
|
|
|
|
|
return_value = st.execute(obj, args)
|
|
|
|
|
|
|
|
return_value.should == 'success'
|
|
|
|
end
|
2011-11-26 14:49:46 -05:00
|
|
|
|
2010-10-01 13:33:35 -04:00
|
|
|
end
|