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
|
2015-03-01 23:08:45 -05:00
|
|
|
expect { process.stop! }.to raise_error do |err|
|
|
|
|
expect(err.class).to eql(AASM::InvalidTransition)
|
2016-01-10 18:13:38 -05:00
|
|
|
expect(err.message).to eql("Event 'stop' cannot transition from 'sleeping'. ")
|
2015-03-01 23:08:45 -05:00
|
|
|
expect(err.object).to eql(process)
|
|
|
|
expect(err.event_name).to eql(:stop)
|
|
|
|
end
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(process).to be_sleeping
|
2011-11-26 14:49:46 -05:00
|
|
|
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
|
2014-12-14 08:53:57 -05:00
|
|
|
expect(silencer.smile!).to be_falsey
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(silencer).to be_silent
|
2011-11-26 14:49:46 -05:00
|
|
|
end
|
|
|
|
|
2012-04-02 05:44:53 -04:00
|
|
|
it 'should not raise an exception when superclass not whiny' do
|
|
|
|
sub = SubClassing.new
|
2014-12-14 08:53:57 -05:00
|
|
|
expect(sub.smile!).to be_falsey
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(sub).to be_silent
|
2012-04-02 05:44:53 -04:00
|
|
|
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
|
2014-12-14 08:53:57 -05:00
|
|
|
expect(silencer.smile_any!).to be_truthy
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(silencer).to be_smiling
|
2012-10-19 02:18:03 -04:00
|
|
|
end
|
2012-12-02 03:45:11 -05:00
|
|
|
|
2015-12-08 04:55:44 -05:00
|
|
|
it 'should call the block on success' do
|
2013-09-03 07:13:53 -04:00
|
|
|
silencer = Silencer.new
|
|
|
|
success = false
|
2014-01-07 13:35:51 -05:00
|
|
|
expect {
|
2013-09-03 07:13:53 -04:00
|
|
|
silencer.smile_any! do
|
|
|
|
success = true
|
|
|
|
end
|
2014-01-07 13:35:51 -05:00
|
|
|
}.to change { success }.to(true)
|
2013-09-03 07:13:53 -04:00
|
|
|
end
|
|
|
|
|
2015-12-08 04:55:44 -05:00
|
|
|
it 'should not call the block on failure' do
|
2013-09-03 07:13:53 -04:00
|
|
|
silencer = Silencer.new
|
|
|
|
success = false
|
2014-01-07 13:35:51 -05:00
|
|
|
expect {
|
2013-09-03 07:13:53 -04:00
|
|
|
silencer.smile! do
|
|
|
|
success = true
|
|
|
|
end
|
2014-12-14 08:53:57 -05:00
|
|
|
}.not_to change { success }
|
2013-09-03 07:13:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-11-28 16:22:43 -05:00
|
|
|
describe AASM::Core::Transition do
|
2015-05-13 06:12:59 -04:00
|
|
|
let(:state_machine) { AASM::StateMachine.new(:name) }
|
|
|
|
let(:event) { AASM::Core::Event.new(:event, state_machine) }
|
|
|
|
|
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'}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2008-01-07 14:11:38 -05:00
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(st.from).to eq(opts[:from])
|
|
|
|
expect(st.to).to eq(opts[:to])
|
|
|
|
expect(st.opts).to eq(opts)
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2014-09-12 08:30:36 -04:00
|
|
|
it 'should set on_transition with deprecation warning' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar'}
|
2014-11-28 16:22:43 -05:00
|
|
|
st = AASM::Core::Transition.allocate
|
2014-12-14 09:05:16 -05:00
|
|
|
expect(st).to receive(:warn).with('[DEPRECATION] :on_transition is deprecated, use :after instead')
|
2014-09-12 08:30:36 -04:00
|
|
|
|
2015-05-13 06:12:59 -04:00
|
|
|
st.send :initialize, event, opts do
|
2014-09-12 08:30:36 -04:00
|
|
|
guard :gg
|
|
|
|
on_transition :after_callback
|
|
|
|
end
|
|
|
|
|
2014-12-14 09:05:16 -05:00
|
|
|
expect(st.opts[:after]).to eql [:after_callback]
|
2014-09-12 08:30:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set after and guard from dsl' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts) do
|
2014-09-12 08:30:36 -04:00
|
|
|
guard :gg
|
|
|
|
after :after_callback
|
|
|
|
end
|
|
|
|
|
2014-12-14 09:05:16 -05:00
|
|
|
expect(st.opts[:guard]).to eql ['g', :gg]
|
|
|
|
expect(st.opts[:after]).to eql [:after_callback] # TODO fix this bad code coupling
|
2014-09-12 08:30:36 -04:00
|
|
|
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'}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2014-01-07 13:35:51 -05:00
|
|
|
allow(obj).to receive(:from).and_return(opts[:from])
|
|
|
|
allow(obj).to receive(:to).and_return(opts[:to])
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(st).to eq(obj)
|
2008-02-21 10:16:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should fail equality check if from are not the same' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2014-01-07 13:35:51 -05:00
|
|
|
allow(obj).to receive(:from).and_return('blah')
|
|
|
|
allow(obj).to receive(:to).and_return(opts[:to])
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(st).not_to eq(obj)
|
2008-02-21 10:16:08 -05:00
|
|
|
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'}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2014-01-07 13:35:51 -05:00
|
|
|
allow(obj).to receive(:from).and_return(opts[:from])
|
|
|
|
allow(obj).to receive(:to).and_return('blah')
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(st).not_to eq(obj)
|
2008-02-21 10:16:08 -05:00
|
|
|
end
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
|
|
|
|
2014-11-28 16:22:43 -05:00
|
|
|
describe AASM::Core::Transition, '- when performing guard checks' do
|
2015-05-13 06:12:59 -04:00
|
|
|
let(:state_machine) { AASM::StateMachine.new(:name) }
|
|
|
|
let(:event) { AASM::Core::Event.new(:event, state_machine) }
|
|
|
|
|
2008-02-21 10:46:06 -05:00
|
|
|
it 'should return true of there is no guard' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar'}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2008-02-21 10:46:06 -05:00
|
|
|
|
2014-12-14 08:53:57 -05:00
|
|
|
expect(st.allowed?(nil)).to be_truthy
|
2008-02-21 10:46:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call the method on the object if guard is a symbol' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => :test}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2008-02-21 10:46:06 -05:00
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(obj).to receive(:test)
|
2009-04-09 01:25:16 -04:00
|
|
|
|
2014-11-08 01:44:24 -05:00
|
|
|
expect(st.allowed?(obj)).to be false
|
|
|
|
end
|
|
|
|
|
2015-10-22 04:21:09 -04:00
|
|
|
it 'should add the name of the failed method calls to the failures instance var' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => :test}
|
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
|
|
|
|
|
|
|
obj = double('object')
|
|
|
|
expect(obj).to receive(:test)
|
|
|
|
|
|
|
|
st.allowed?(obj)
|
|
|
|
expect(st.failures).to eq [:test]
|
|
|
|
end
|
|
|
|
|
2014-11-08 01:44:24 -05:00
|
|
|
it 'should call the method on the object if unless is a symbol' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :unless => :test}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2014-11-08 01:44:24 -05:00
|
|
|
|
|
|
|
obj = double('object')
|
|
|
|
expect(obj).to receive(:test)
|
|
|
|
|
|
|
|
expect(st.allowed?(obj)).to be true
|
2008-02-21 10:46:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call the method on the object if guard is a string' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => 'test'}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2008-02-21 10:46:06 -05:00
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(obj).to receive(:test)
|
2009-04-09 01:25:16 -04:00
|
|
|
|
2014-11-08 01:44:24 -05:00
|
|
|
expect(st.allowed?(obj)).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call the method on the object if unless is a string' do
|
|
|
|
opts = {:from => 'foo', :to => 'bar', :unless => 'test'}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2014-11-08 01:44:24 -05:00
|
|
|
|
|
|
|
obj = double('object')
|
|
|
|
expect(obj).to receive(:test)
|
|
|
|
|
|
|
|
expect(st.allowed?(obj)).to be true
|
2008-02-21 10:46:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call the proc passing the object if the guard is a proc' do
|
2014-09-12 08:30:36 -04:00
|
|
|
opts = {:from => 'foo', :to => 'bar', :guard => Proc.new { test }}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2008-02-21 10:46:06 -05:00
|
|
|
|
2013-11-15 05:19:26 -05:00
|
|
|
obj = double('object')
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(obj).to receive(:test)
|
2008-02-21 10:46:06 -05:00
|
|
|
|
2014-11-08 01:44:24 -05:00
|
|
|
expect(st.allowed?(obj)).to be false
|
2008-02-21 10:46:06 -05:00
|
|
|
end
|
|
|
|
end
|
2010-10-01 13:33:35 -04:00
|
|
|
|
2014-11-28 16:22:43 -05:00
|
|
|
describe AASM::Core::Transition, '- when executing the transition with a Proc' do
|
2015-05-13 06:12:59 -04:00
|
|
|
let(:state_machine) { AASM::StateMachine.new(:name) }
|
|
|
|
let(:event) { AASM::Core::Event.new(:event, state_machine) }
|
|
|
|
|
2010-10-01 13:33:35 -04:00
|
|
|
it 'should call a Proc on the object with args' do
|
2014-09-12 08:30:36 -04:00
|
|
|
opts = {:from => 'foo', :to => 'bar', :after => Proc.new {|a| test(a) }}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2010-10-01 13:33:35 -04:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
2014-01-27 15:51:38 -05:00
|
|
|
obj = double('object', :aasm => 'aasm')
|
2010-10-01 13:33:35 -04:00
|
|
|
|
2014-12-14 09:05:16 -05:00
|
|
|
expect(obj).to receive(:test).with(args)
|
2010-10-01 13:33:35 -04:00
|
|
|
|
|
|
|
st.execute(obj, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should call a Proc on the object without args' do
|
2014-09-29 16:23:25 -04:00
|
|
|
# in order to test that the Proc has been called, we make sure
|
|
|
|
# that after running the :after callback the prc_object is set
|
|
|
|
prc_object = nil
|
|
|
|
prc = Proc.new { prc_object = self }
|
|
|
|
|
2014-09-12 08:30:36 -04:00
|
|
|
opts = {:from => 'foo', :to => 'bar', :after => prc }
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2010-10-01 13:33:35 -04:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
2014-01-27 15:51:38 -05:00
|
|
|
obj = double('object', :aasm => 'aasm')
|
2010-10-01 13:33:35 -04:00
|
|
|
|
|
|
|
st.execute(obj, args)
|
2014-09-29 16:23:25 -04:00
|
|
|
expect(prc_object).to eql obj
|
2010-10-01 13:33:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-28 16:22:43 -05:00
|
|
|
describe AASM::Core::Transition, '- when executing the transition with an :after method call' do
|
2015-05-13 06:12:59 -04:00
|
|
|
let(:state_machine) { AASM::StateMachine.new(:name) }
|
|
|
|
let(:event) { AASM::Core::Event.new(:event, state_machine) }
|
|
|
|
|
2010-10-01 13:33:35 -04:00
|
|
|
it 'should accept a String for the method name' do
|
2014-09-12 08:30:36 -04:00
|
|
|
opts = {:from => 'foo', :to => 'bar', :after => 'test'}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2010-10-01 13:33:35 -04:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
2014-01-27 15:51:38 -05:00
|
|
|
obj = double('object', :aasm => 'aasm')
|
2010-10-01 13:33:35 -04:00
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(obj).to receive(:test)
|
2010-10-01 13:33:35 -04:00
|
|
|
|
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
|
2014-09-12 08:30:36 -04:00
|
|
|
opts = {:from => 'foo', :to => 'bar', :after => :test}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2010-10-01 13:33:35 -04:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
2014-01-27 15:51:38 -05:00
|
|
|
obj = double('object', :aasm => 'aasm')
|
2010-10-01 13:33:35 -04:00
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(obj).to receive(:test)
|
2010-10-01 13:33:35 -04:00
|
|
|
|
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
|
2014-09-12 08:30:36 -04:00
|
|
|
opts = {:from => 'foo', :to => 'bar', :after => :test}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2010-10-01 13:33:35 -04:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
2014-01-27 15:51:38 -05:00
|
|
|
obj = double('object', :aasm => 'aasm')
|
2010-10-01 13:33:35 -04:00
|
|
|
|
2013-11-20 18:18:35 -05:00
|
|
|
def obj.test(args)
|
|
|
|
"arg1: #{args[:arg1]} arg2: #{args[:arg2]}"
|
2010-10-01 13:33:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
return_value = st.execute(obj, args)
|
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(return_value).to eq('arg1: 1 arg2: 2')
|
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 NOT pass args if the target method does NOT accept them' do
|
2014-09-12 08:30:36 -04:00
|
|
|
opts = {:from => 'foo', :to => 'bar', :after => :test}
|
2015-05-13 06:12:59 -04:00
|
|
|
st = AASM::Core::Transition.new(event, opts)
|
2010-10-01 13:33:35 -04:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
2014-01-27 15:51:38 -05:00
|
|
|
obj = double('object', :aasm => 'aasm')
|
2010-10-01 13:33:35 -04:00
|
|
|
|
2013-11-20 18:18:35 -05:00
|
|
|
def obj.test
|
|
|
|
'success'
|
2010-10-01 13:33:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
return_value = st.execute(obj, args)
|
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(return_value).to eq('success')
|
2010-10-01 13:33:35 -04:00
|
|
|
end
|
2011-11-26 14:49:46 -05:00
|
|
|
|
2014-01-27 15:51:38 -05:00
|
|
|
it 'should allow accessing the from_state and the to_state' do
|
2014-09-12 08:30:36 -04:00
|
|
|
opts = {:from => 'foo', :to => 'bar', :after => :test}
|
2015-05-13 06:12:59 -04:00
|
|
|
transition = AASM::Core::Transition.new(event, opts)
|
2014-01-27 15:51:38 -05:00
|
|
|
args = {:arg1 => '1', :arg2 => '2'}
|
|
|
|
obj = double('object', :aasm => AASM::InstanceBase.new('object'))
|
|
|
|
|
|
|
|
def obj.test(args)
|
|
|
|
"from: #{aasm.from_state} to: #{aasm.to_state}"
|
|
|
|
end
|
|
|
|
|
2014-09-12 08:30:36 -04:00
|
|
|
return_value = transition.execute(obj, args)
|
2014-01-27 15:51:38 -05:00
|
|
|
|
|
|
|
expect(return_value).to eq('from: foo to: bar')
|
|
|
|
end
|
|
|
|
|
2010-10-01 13:33:35 -04:00
|
|
|
end
|