2012-11-05 17:38:50 -05:00
|
|
|
require 'spec_helper'
|
2008-01-07 14:11:38 -05:00
|
|
|
|
2012-11-05 17:38:50 -05:00
|
|
|
describe 'adding an event' do
|
2012-12-09 04:32:32 -05:00
|
|
|
let(:event) do
|
2014-11-28 16:22:43 -05:00
|
|
|
AASM::Core::Event.new(:close_order, {:success => :success_callback}) do
|
2013-02-02 03:22:52 -05:00
|
|
|
before :before_callback
|
|
|
|
after :after_callback
|
2008-01-07 14:11:38 -05:00
|
|
|
transitions :to => :closed, :from => [:open, :received]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set the name' do
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(event.name).to eq(:close_order)
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
|
|
|
|
2012-12-09 04:32:32 -05:00
|
|
|
it 'should set the success callback' do
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(event.options[:success]).to eq(:success_callback)
|
2008-05-20 15:27:35 -04:00
|
|
|
end
|
|
|
|
|
2013-02-02 03:22:52 -05:00
|
|
|
it 'should set the after callback' do
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(event.options[:after]).to eq([:after_callback])
|
2013-02-02 03:22:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set the before callback' do
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(event.options[:before]).to eq([:before_callback])
|
2013-02-02 03:22:52 -05:00
|
|
|
end
|
|
|
|
|
2012-12-09 04:32:32 -05:00
|
|
|
it 'should create transitions' do
|
2013-11-30 16:49:39 -05:00
|
|
|
transitions = event.transitions
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(transitions[0].from).to eq(:open)
|
|
|
|
expect(transitions[0].to).to eq(:closed)
|
|
|
|
expect(transitions[1].from).to eq(:received)
|
|
|
|
expect(transitions[1].to).to eq(:closed)
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-05 17:51:05 -05:00
|
|
|
describe 'transition inspection' do
|
2012-12-09 04:32:32 -05:00
|
|
|
let(:event) do
|
2014-11-28 16:22:43 -05:00
|
|
|
AASM::Core::Event.new(:run) do
|
2012-11-05 17:51:05 -05:00
|
|
|
transitions :to => :running, :from => :sleeping
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-09 04:32:32 -05:00
|
|
|
it 'should support inspecting transitions from other states' do
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(event.transitions_from_state(:sleeping).map(&:to)).to eq([:running])
|
2014-12-14 08:53:57 -05:00
|
|
|
expect(event.transitions_from_state?(:sleeping)).to be_truthy
|
2012-11-05 17:51:05 -05:00
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(event.transitions_from_state(:cleaning).map(&:to)).to eq([])
|
2014-12-14 08:53:57 -05:00
|
|
|
expect(event.transitions_from_state?(:cleaning)).to be_falsey
|
2012-11-05 17:51:05 -05:00
|
|
|
end
|
|
|
|
|
2012-12-09 04:32:32 -05:00
|
|
|
it 'should support inspecting transitions to other states' do
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(event.transitions_to_state(:running).map(&:from)).to eq([:sleeping])
|
2014-12-14 08:53:57 -05:00
|
|
|
expect(event.transitions_to_state?(:running)).to be_truthy
|
2012-11-05 17:51:05 -05:00
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(event.transitions_to_state(:cleaning).map(&:to)).to eq([])
|
2014-12-14 08:53:57 -05:00
|
|
|
expect(event.transitions_to_state?(:cleaning)).to be_falsey
|
2012-11-05 17:51:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-13 15:05:14 -04:00
|
|
|
describe 'transition inspection without from' do
|
|
|
|
let(:event) do
|
2014-11-28 16:22:43 -05:00
|
|
|
AASM::Core::Event.new(:run) do
|
2014-06-13 15:05:14 -04:00
|
|
|
transitions :to => :running
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should support inspecting transitions from other states' do
|
|
|
|
expect(event.transitions_from_state(:sleeping).map(&:to)).to eq([:running])
|
2014-12-14 08:53:57 -05:00
|
|
|
expect(event.transitions_from_state?(:sleeping)).to be_truthy
|
2014-06-13 15:05:14 -04:00
|
|
|
|
|
|
|
expect(event.transitions_from_state(:cleaning).map(&:to)).to eq([:running])
|
2014-12-14 08:53:57 -05:00
|
|
|
expect(event.transitions_from_state?(:cleaning)).to be_truthy
|
2014-06-13 15:05:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-11-05 17:38:50 -05:00
|
|
|
describe 'firing an event' do
|
2011-11-26 14:34:25 -05:00
|
|
|
it 'should return nil if the transitions are empty' do
|
2013-11-30 18:08:50 -05:00
|
|
|
obj = double('object', :aasm => double('aasm', :current_state => 'open'))
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2014-11-28 16:22:43 -05:00
|
|
|
event = AASM::Core::Event.new(:event)
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(event.fire(obj)).to be_nil
|
2008-02-21 10:16:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the state of the first matching transition it finds' do
|
2014-11-28 16:22:43 -05:00
|
|
|
event = AASM::Core::Event.new(:event) do
|
2008-02-21 10:16:08 -05:00
|
|
|
transitions :to => :closed, :from => [:open, :received]
|
|
|
|
end
|
|
|
|
|
2013-11-30 18:08:50 -05:00
|
|
|
obj = double('object', :aasm => double('aasm', :current_state => :open))
|
2008-02-21 10:16:08 -05:00
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(event.fire(obj)).to eq(:closed)
|
2008-02-21 10:16:08 -05:00
|
|
|
end
|
2011-03-21 23:01:45 -04:00
|
|
|
|
|
|
|
it 'should call the guard with the params passed in' do
|
2014-11-28 16:22:43 -05:00
|
|
|
event = AASM::Core::Event.new(:event) do
|
2011-03-21 23:01:45 -04:00
|
|
|
transitions :to => :closed, :from => [:open, :received], :guard => :guard_fn
|
|
|
|
end
|
|
|
|
|
2013-11-30 18:08:50 -05:00
|
|
|
obj = double('object', :aasm => double('aasm', :current_state => :open))
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(obj).to receive(:guard_fn).with('arg1', 'arg2').and_return(true)
|
2011-03-21 23:01:45 -04:00
|
|
|
|
2014-11-19 17:34:39 -05:00
|
|
|
expect(event.fire(obj, {}, nil, 'arg1', 'arg2')).to eq(:closed)
|
2011-03-21 23:01:45 -04:00
|
|
|
end
|
|
|
|
|
2008-02-21 10:16:08 -05:00
|
|
|
end
|
2008-10-09 10:53:39 -04:00
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
describe 'should fire callbacks' do
|
|
|
|
describe 'success' do
|
|
|
|
it "if it's a symbol" do
|
|
|
|
ThisNameBetterNotBeInUse.instance_eval {
|
2013-11-30 15:36:28 -05:00
|
|
|
aasm do
|
|
|
|
event :with_symbol, :success => :symbol_success_callback do
|
|
|
|
transitions :to => :symbol, :from => [:initial]
|
|
|
|
end
|
2013-02-21 02:22:48 -05:00
|
|
|
end
|
|
|
|
}
|
2008-10-09 10:53:39 -04:00
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
model = ThisNameBetterNotBeInUse.new
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(model).to receive(:symbol_success_callback)
|
2013-02-21 02:22:48 -05:00
|
|
|
model.with_symbol!
|
|
|
|
end
|
2008-10-09 10:53:39 -04:00
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
it "if it's a string" do
|
|
|
|
ThisNameBetterNotBeInUse.instance_eval {
|
2013-11-30 15:36:28 -05:00
|
|
|
aasm do
|
|
|
|
event :with_string, :success => 'string_success_callback' do
|
|
|
|
transitions :to => :string, :from => [:initial]
|
|
|
|
end
|
2013-02-21 02:22:48 -05:00
|
|
|
end
|
|
|
|
}
|
2008-10-09 10:53:39 -04:00
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
model = ThisNameBetterNotBeInUse.new
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(model).to receive(:string_success_callback)
|
2013-02-21 02:22:48 -05:00
|
|
|
model.with_string!
|
|
|
|
end
|
2008-10-09 10:53:39 -04:00
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
it "if passed an array of strings and/or symbols" do
|
|
|
|
ThisNameBetterNotBeInUse.instance_eval {
|
2013-11-30 15:36:28 -05:00
|
|
|
aasm do
|
|
|
|
event :with_array, :success => [:success_callback1, 'success_callback2'] do
|
|
|
|
transitions :to => :array, :from => [:initial]
|
|
|
|
end
|
2013-02-21 02:22:48 -05:00
|
|
|
end
|
|
|
|
}
|
2008-10-09 10:53:39 -04:00
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
model = ThisNameBetterNotBeInUse.new
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(model).to receive(:success_callback1)
|
|
|
|
expect(model).to receive(:success_callback2)
|
2013-02-21 02:22:48 -05:00
|
|
|
model.with_array!
|
|
|
|
end
|
2008-10-09 10:53:39 -04:00
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
it "if passed an array of strings and/or symbols and/or procs" do
|
|
|
|
ThisNameBetterNotBeInUse.instance_eval {
|
2013-11-30 15:36:28 -05:00
|
|
|
aasm do
|
|
|
|
event :with_array_including_procs, :success => [:success_callback1, 'success_callback2', lambda { proc_success_callback }] do
|
|
|
|
transitions :to => :array, :from => [:initial]
|
|
|
|
end
|
2013-02-21 02:22:48 -05:00
|
|
|
end
|
|
|
|
}
|
2008-10-09 10:53:39 -04:00
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
model = ThisNameBetterNotBeInUse.new
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(model).to receive(:success_callback1)
|
|
|
|
expect(model).to receive(:success_callback2)
|
|
|
|
expect(model).to receive(:proc_success_callback)
|
2013-02-21 02:22:48 -05:00
|
|
|
model.with_array_including_procs!
|
|
|
|
end
|
2009-04-09 01:14:02 -04:00
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
it "if it's a proc" do
|
|
|
|
ThisNameBetterNotBeInUse.instance_eval {
|
2013-11-30 15:36:28 -05:00
|
|
|
aasm do
|
|
|
|
event :with_proc, :success => lambda { proc_success_callback } do
|
|
|
|
transitions :to => :proc, :from => [:initial]
|
|
|
|
end
|
2013-02-21 02:22:48 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
model = ThisNameBetterNotBeInUse.new
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(model).to receive(:proc_success_callback)
|
2013-02-21 02:22:48 -05:00
|
|
|
model.with_proc!
|
|
|
|
end
|
2009-04-09 01:14:02 -04:00
|
|
|
end
|
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
describe 'after' do
|
|
|
|
it "if they set different ways" do
|
|
|
|
ThisNameBetterNotBeInUse.instance_eval do
|
2013-11-30 15:36:28 -05:00
|
|
|
aasm do
|
|
|
|
event :with_afters, :after => :do_one_thing_after do
|
|
|
|
after do
|
|
|
|
do_another_thing_after_too
|
|
|
|
end
|
|
|
|
after do
|
|
|
|
do_third_thing_at_last
|
|
|
|
end
|
|
|
|
transitions :to => :proc, :from => [:initial]
|
2013-02-21 02:22:48 -05:00
|
|
|
end
|
|
|
|
end
|
2008-10-09 10:53:39 -04:00
|
|
|
end
|
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
model = ThisNameBetterNotBeInUse.new
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(model).to receive(:do_one_thing_after).once.ordered
|
|
|
|
expect(model).to receive(:do_another_thing_after_too).once.ordered
|
|
|
|
expect(model).to receive(:do_third_thing_at_last).once.ordered
|
2013-02-21 02:22:48 -05:00
|
|
|
model.with_afters!
|
|
|
|
end
|
2008-10-09 10:53:39 -04:00
|
|
|
end
|
2013-02-20 23:54:43 -05:00
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
describe 'before' do
|
|
|
|
it "if it's a proc" do
|
|
|
|
ThisNameBetterNotBeInUse.instance_eval do
|
2013-11-30 15:36:28 -05:00
|
|
|
aasm do
|
|
|
|
event :before_as_proc do
|
|
|
|
before do
|
|
|
|
do_something_before
|
|
|
|
end
|
|
|
|
transitions :to => :proc, :from => [:initial]
|
2013-02-21 02:22:48 -05:00
|
|
|
end
|
2013-02-20 23:54:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
model = ThisNameBetterNotBeInUse.new
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(model).to receive(:do_something_before).once
|
2013-02-21 02:22:48 -05:00
|
|
|
model.before_as_proc!
|
|
|
|
end
|
2013-02-20 23:54:43 -05:00
|
|
|
end
|
|
|
|
|
2013-02-21 02:22:48 -05:00
|
|
|
it 'in right order' do
|
2013-02-20 23:54:43 -05:00
|
|
|
ThisNameBetterNotBeInUse.instance_eval do
|
2013-11-30 15:36:28 -05:00
|
|
|
aasm do
|
|
|
|
event :in_right_order, :after => :do_something_after do
|
|
|
|
before do
|
|
|
|
do_something_before
|
|
|
|
end
|
|
|
|
transitions :to => :proc, :from => [:initial]
|
2013-02-20 23:54:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
model = ThisNameBetterNotBeInUse.new
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(model).to receive(:do_something_before).once.ordered
|
|
|
|
expect(model).to receive(:do_something_after).once.ordered
|
2013-02-21 02:22:48 -05:00
|
|
|
model.in_right_order!
|
2013-02-20 23:54:43 -05:00
|
|
|
end
|
2008-10-09 10:53:39 -04:00
|
|
|
end
|
2012-11-28 04:42:41 -05:00
|
|
|
|
2014-09-12 05:42:01 -04:00
|
|
|
describe 'current event' do
|
|
|
|
let(:pe) {ParametrisedEvent.new}
|
|
|
|
|
|
|
|
it 'if no event has been triggered' do
|
|
|
|
expect(pe.aasm.current_event).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'if a event has been triggered' do
|
|
|
|
pe.wakeup
|
|
|
|
expect(pe.aasm.current_event).to eql :wakeup
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'if no event has been triggered' do
|
|
|
|
pe.wakeup!
|
|
|
|
expect(pe.aasm.current_event).to eql :wakeup!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-28 04:42:41 -05:00
|
|
|
describe 'parametrised events' do
|
2012-12-02 02:54:58 -05:00
|
|
|
let(:pe) {ParametrisedEvent.new}
|
2012-11-28 04:42:41 -05:00
|
|
|
|
|
|
|
it 'should transition to specified next state (sleeping to showering)' do
|
2012-12-02 02:54:58 -05:00
|
|
|
pe.wakeup!(:showering)
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(pe.aasm.current_state).to eq(:showering)
|
2012-11-28 04:42:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should transition to specified next state (sleeping to working)' do
|
2012-12-02 02:54:58 -05:00
|
|
|
pe.wakeup!(:working)
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(pe.aasm.current_state).to eq(:working)
|
2012-11-28 04:42:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should transition to default (first or showering) state' do
|
2012-12-02 02:54:58 -05:00
|
|
|
pe.wakeup!
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(pe.aasm.current_state).to eq(:showering)
|
2012-11-28 04:42:41 -05:00
|
|
|
end
|
|
|
|
|
2014-09-12 08:30:36 -04:00
|
|
|
it 'should transition to default state when :after transition invoked' do
|
2012-12-02 02:54:58 -05:00
|
|
|
pe.dress!(nil, 'purple', 'dressy')
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(pe.aasm.current_state).to eq(:working)
|
2012-11-28 04:42:41 -05:00
|
|
|
end
|
|
|
|
|
2014-09-12 08:30:36 -04:00
|
|
|
it 'should call :after transition method with args' do
|
2012-12-02 02:54:58 -05:00
|
|
|
pe.wakeup!(:showering)
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(pe).to receive(:wear_clothes).with('blue', 'jeans')
|
2012-12-02 02:54:58 -05:00
|
|
|
pe.dress!(:working, 'blue', 'jeans')
|
2012-11-28 04:42:41 -05:00
|
|
|
end
|
|
|
|
|
2014-09-12 08:30:36 -04:00
|
|
|
it 'should call :after transition proc' do
|
2012-12-02 02:54:58 -05:00
|
|
|
pe.wakeup!(:showering)
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(pe).to receive(:wear_clothes).with('purple', 'slacks')
|
2012-12-02 02:54:58 -05:00
|
|
|
pe.dress!(:dating, 'purple', 'slacks')
|
2012-11-28 04:42:41 -05:00
|
|
|
end
|
|
|
|
|
2014-09-12 08:30:36 -04:00
|
|
|
it 'should call :after transition with an array of methods' do
|
2012-12-02 02:54:58 -05:00
|
|
|
pe.wakeup!(:showering)
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(pe).to receive(:condition_hair)
|
|
|
|
expect(pe).to receive(:fix_hair)
|
2012-12-02 02:54:58 -05:00
|
|
|
pe.dress!(:prettying_up)
|
2012-11-28 04:42:41 -05:00
|
|
|
end
|
|
|
|
end
|
2012-12-03 22:27:47 -05:00
|
|
|
|
|
|
|
describe 'event firing without persistence' do
|
|
|
|
it 'should attempt to persist if aasm_write_state is defined' do
|
|
|
|
foo = Foo.new
|
|
|
|
def foo.aasm_write_state; end
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(foo).to be_open
|
2012-12-03 22:27:47 -05:00
|
|
|
|
2014-01-07 13:35:51 -05:00
|
|
|
expect(foo).to receive(:aasm_write_state_without_persistence)
|
2012-12-03 22:27:47 -05:00
|
|
|
foo.close
|
|
|
|
end
|
|
|
|
end
|