mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
little clean-up for test classes
This commit is contained in:
parent
a8af74a3cf
commit
bde1780eb6
7 changed files with 210 additions and 205 deletions
|
@ -1,129 +0,0 @@
|
|||
class CallbackNewDsl
|
||||
include AASM
|
||||
|
||||
def initialize(options={})
|
||||
@fail_event_guard = options[:fail_event_guard]
|
||||
@fail_transition_guard = options[:fail_transition_guard]
|
||||
@log = options[:log]
|
||||
end
|
||||
|
||||
aasm do
|
||||
state :open, :initial => true,
|
||||
:before_enter => :before_enter_open,
|
||||
:enter => :enter_open,
|
||||
:after_enter => :after_enter_open,
|
||||
:before_exit => :before_exit_open,
|
||||
:exit => :exit_open,
|
||||
:after_exit => :after_exit_open
|
||||
|
||||
state :closed,
|
||||
:before_enter => :before_enter_closed,
|
||||
:enter => :enter_closed,
|
||||
:after_enter => :after_enter_closed,
|
||||
:before_exit => :before_exit_closed,
|
||||
:exit => :exit_closed,
|
||||
:after_exit => :after_exit_closed
|
||||
|
||||
event :close, :before => :before, :after => :after, :guard => :event_guard do
|
||||
transitions :to => :closed, :from => [:open], :guard => :transition_guard, :after => :transitioning
|
||||
end
|
||||
|
||||
event :open, :before => :before, :after => :after do
|
||||
transitions :to => :open, :from => :closed
|
||||
end
|
||||
end
|
||||
|
||||
def log(text)
|
||||
puts text if @log
|
||||
end
|
||||
|
||||
def before_enter_open; log('before_enter_open'); end
|
||||
def enter_open; log('enter_open'); end
|
||||
def before_exit_open; log('before_exit_open'); end
|
||||
def after_enter_open; log('after_enter_open'); end
|
||||
def exit_open; log('exit_open'); end
|
||||
def after_exit_open; log('after_exit_open'); end
|
||||
|
||||
def before_enter_closed; log('before_enter_closed'); end
|
||||
def enter_closed; log('enter_closed'); end
|
||||
def before_exit_closed; log('before_exit_closed'); end
|
||||
def exit_closed; log('exit_closed'); end
|
||||
def after_enter_closed; log('after_enter_closed'); end
|
||||
def after_exit_closed; log('after_exit_closed'); end
|
||||
|
||||
def event_guard; log('event_guard'); !@fail_event_guard; end
|
||||
def transition_guard; log('transition_guard'); !@fail_transition_guard; end
|
||||
def transitioning; log('transitioning'); end
|
||||
|
||||
def before; log('before'); end
|
||||
def after; log('after'); end
|
||||
end
|
||||
|
||||
class CallbackNewDslArgs
|
||||
include AASM
|
||||
|
||||
aasm do
|
||||
state :open, :initial => true,
|
||||
:before_enter => :before_enter_open,
|
||||
:after_enter => :after_enter_open,
|
||||
:before_exit => :before_exit_open,
|
||||
:after_exit => :after_exit_open
|
||||
|
||||
state :closed,
|
||||
:before_enter => :before_enter_closed,
|
||||
:after_enter => :after_enter_closed,
|
||||
:before_exit => :before_exit_closed,
|
||||
:after_exit => :after_exit_closed
|
||||
|
||||
event :close, :before => :before, :after => :after do
|
||||
transitions :to => :closed, :from => [:open], :after => :transition_proc
|
||||
end
|
||||
|
||||
event :open, :before => :before, :after => :after do
|
||||
transitions :to => :open, :from => :closed
|
||||
end
|
||||
end
|
||||
|
||||
def log(text)
|
||||
# puts text
|
||||
end
|
||||
|
||||
def before_enter_open; log('before_enter_open'); end
|
||||
def before_exit_open; log('before_exit_open'); end
|
||||
def after_enter_open; log('after_enter_open'); end
|
||||
def after_exit_open; log('after_exit_open'); end
|
||||
|
||||
def before_enter_closed; log('before_enter_closed'); end
|
||||
def before_exit_closed; log('before_enter_closed'); end
|
||||
def after_enter_closed; log('after_enter_closed'); end
|
||||
def after_exit_closed; log('after_exit_closed'); end
|
||||
|
||||
def before(*args); log('before'); end
|
||||
def transition_proc(arg1, arg2); log('transition_proc'); end
|
||||
def after(*args); log('after'); end
|
||||
end
|
||||
|
||||
class CallbackWithStateArg
|
||||
|
||||
include AASM
|
||||
|
||||
aasm do
|
||||
state :open, :inital => true
|
||||
state :closed
|
||||
state :out_to_lunch
|
||||
|
||||
event :close, :before => :before_method, :after => :after_method do
|
||||
transitions :to => :closed, :from => [:open], :after => :transition_method
|
||||
transitions :to => :out_to_lunch, :from => [:open], :after => :transition_method2
|
||||
end
|
||||
end
|
||||
|
||||
def before_method(arg); end
|
||||
|
||||
def after_method(arg); end
|
||||
|
||||
def transition_method(arg); end
|
||||
|
||||
def transition_method2(arg); end
|
||||
|
||||
end
|
62
spec/models/callbacks/basic.rb
Normal file
62
spec/models/callbacks/basic.rb
Normal file
|
@ -0,0 +1,62 @@
|
|||
module Callbacks
|
||||
class Basic
|
||||
include AASM
|
||||
|
||||
def initialize(options={})
|
||||
@fail_event_guard = options[:fail_event_guard]
|
||||
@fail_transition_guard = options[:fail_transition_guard]
|
||||
@log = options[:log]
|
||||
end
|
||||
|
||||
aasm do
|
||||
state :open, :initial => true,
|
||||
:before_enter => :before_enter_open,
|
||||
:enter => :enter_open,
|
||||
:after_enter => :after_enter_open,
|
||||
:before_exit => :before_exit_open,
|
||||
:exit => :exit_open,
|
||||
:after_exit => :after_exit_open
|
||||
|
||||
state :closed,
|
||||
:before_enter => :before_enter_closed,
|
||||
:enter => :enter_closed,
|
||||
:after_enter => :after_enter_closed,
|
||||
:before_exit => :before_exit_closed,
|
||||
:exit => :exit_closed,
|
||||
:after_exit => :after_exit_closed
|
||||
|
||||
event :close, :before => :before, :after => :after, :guard => :event_guard do
|
||||
transitions :to => :closed, :from => [:open], :guard => :transition_guard, :after => :transitioning
|
||||
end
|
||||
|
||||
event :open, :before => :before, :after => :after do
|
||||
transitions :to => :open, :from => :closed
|
||||
end
|
||||
end
|
||||
|
||||
def log(text)
|
||||
puts text if @log
|
||||
end
|
||||
|
||||
def before_enter_open; log('before_enter_open'); end
|
||||
def enter_open; log('enter_open'); end
|
||||
def before_exit_open; log('before_exit_open'); end
|
||||
def after_enter_open; log('after_enter_open'); end
|
||||
def exit_open; log('exit_open'); end
|
||||
def after_exit_open; log('after_exit_open'); end
|
||||
|
||||
def before_enter_closed; log('before_enter_closed'); end
|
||||
def enter_closed; log('enter_closed'); end
|
||||
def before_exit_closed; log('before_exit_closed'); end
|
||||
def exit_closed; log('exit_closed'); end
|
||||
def after_enter_closed; log('after_enter_closed'); end
|
||||
def after_exit_closed; log('after_exit_closed'); end
|
||||
|
||||
def event_guard; log('event_guard'); !@fail_event_guard; end
|
||||
def transition_guard; log('transition_guard'); !@fail_transition_guard; end
|
||||
def transitioning; log('transitioning'); end
|
||||
|
||||
def before; log('before'); end
|
||||
def after; log('after'); end
|
||||
end
|
||||
end
|
66
spec/models/callbacks/guard_within_block.rb
Normal file
66
spec/models/callbacks/guard_within_block.rb
Normal file
|
@ -0,0 +1,66 @@
|
|||
module Callbacks
|
||||
class GuardWithinBlock
|
||||
include AASM
|
||||
|
||||
def initialize(options={})
|
||||
@fail_event_guard = options[:fail_event_guard]
|
||||
@fail_transition_guard = options[:fail_transition_guard]
|
||||
@log = options[:log]
|
||||
end
|
||||
|
||||
aasm do
|
||||
state :open, :initial => true,
|
||||
:before_enter => :before_enter_open,
|
||||
:enter => :enter_open,
|
||||
:after_enter => :after_enter_open,
|
||||
:before_exit => :before_exit_open,
|
||||
:exit => :exit_open,
|
||||
:after_exit => :after_exit_open
|
||||
|
||||
state :closed,
|
||||
:before_enter => :before_enter_closed,
|
||||
:enter => :enter_closed,
|
||||
:after_enter => :after_enter_closed,
|
||||
:before_exit => :before_exit_closed,
|
||||
:exit => :exit_closed,
|
||||
:after_exit => :after_exit_closed
|
||||
|
||||
event :close, :before => :before, :after => :after, :guard => :event_guard do
|
||||
transitions :to => :closed, :from => [:open], :after => :transitioning do
|
||||
guard do
|
||||
transition_guard
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
event :open, :before => :before, :after => :after do
|
||||
transitions :to => :open, :from => :closed
|
||||
end
|
||||
end
|
||||
|
||||
def log(text)
|
||||
puts text if @log
|
||||
end
|
||||
|
||||
def before_enter_open; log('before_enter_open'); end
|
||||
def enter_open; log('enter_open'); end
|
||||
def before_exit_open; log('before_exit_open'); end
|
||||
def after_enter_open; log('after_enter_open'); end
|
||||
def exit_open; log('exit_open'); end
|
||||
def after_exit_open; log('after_exit_open'); end
|
||||
|
||||
def before_enter_closed; log('before_enter_closed'); end
|
||||
def enter_closed; log('enter_closed'); end
|
||||
def before_exit_closed; log('before_exit_closed'); end
|
||||
def exit_closed; log('exit_closed'); end
|
||||
def after_enter_closed; log('after_enter_closed'); end
|
||||
def after_exit_closed; log('after_exit_closed'); end
|
||||
|
||||
def event_guard; log('event_guard'); !@fail_event_guard; end
|
||||
def transition_guard; log('transition_guard'); !@fail_transition_guard; end
|
||||
def transitioning; log('transitioning'); end
|
||||
|
||||
def before; log('before'); end
|
||||
def after; log('after'); end
|
||||
end
|
||||
end
|
45
spec/models/callbacks/with_args.rb
Normal file
45
spec/models/callbacks/with_args.rb
Normal file
|
@ -0,0 +1,45 @@
|
|||
module Callbacks
|
||||
class WithArgs
|
||||
include AASM
|
||||
|
||||
aasm do
|
||||
state :open, :initial => true,
|
||||
:before_enter => :before_enter_open,
|
||||
:after_enter => :after_enter_open,
|
||||
:before_exit => :before_exit_open,
|
||||
:after_exit => :after_exit_open
|
||||
|
||||
state :closed,
|
||||
:before_enter => :before_enter_closed,
|
||||
:after_enter => :after_enter_closed,
|
||||
:before_exit => :before_exit_closed,
|
||||
:after_exit => :after_exit_closed
|
||||
|
||||
event :close, :before => :before, :after => :after do
|
||||
transitions :to => :closed, :from => [:open], :after => :transition_proc
|
||||
end
|
||||
|
||||
event :open, :before => :before, :after => :after do
|
||||
transitions :to => :open, :from => :closed
|
||||
end
|
||||
end
|
||||
|
||||
def log(text)
|
||||
# puts text
|
||||
end
|
||||
|
||||
def before_enter_open; log('before_enter_open'); end
|
||||
def before_exit_open; log('before_exit_open'); end
|
||||
def after_enter_open; log('after_enter_open'); end
|
||||
def after_exit_open; log('after_exit_open'); end
|
||||
|
||||
def before_enter_closed; log('before_enter_closed'); end
|
||||
def before_exit_closed; log('before_enter_closed'); end
|
||||
def after_enter_closed; log('after_enter_closed'); end
|
||||
def after_exit_closed; log('after_exit_closed'); end
|
||||
|
||||
def before(*args); log('before'); end
|
||||
def transition_proc(arg1, arg2); log('transition_proc'); end
|
||||
def after(*args); log('after'); end
|
||||
end
|
||||
end
|
26
spec/models/callbacks/with_state_args.rb
Normal file
26
spec/models/callbacks/with_state_args.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
module Callbacks
|
||||
class WithStateArg
|
||||
|
||||
include AASM
|
||||
|
||||
aasm do
|
||||
state :open, :inital => true
|
||||
state :closed
|
||||
state :out_to_lunch
|
||||
|
||||
event :close, :before => :before_method, :after => :after_method do
|
||||
transitions :to => :closed, :from => [:open], :after => :transition_method
|
||||
transitions :to => :out_to_lunch, :from => [:open], :after => :transition_method2
|
||||
end
|
||||
end
|
||||
|
||||
def before_method(arg); end
|
||||
|
||||
def after_method(arg); end
|
||||
|
||||
def transition_method(arg); end
|
||||
|
||||
def transition_method2(arg); end
|
||||
|
||||
end
|
||||
end
|
|
@ -1,64 +0,0 @@
|
|||
class GuardWithinBlock
|
||||
include AASM
|
||||
|
||||
def initialize(options={})
|
||||
@fail_event_guard = options[:fail_event_guard]
|
||||
@fail_transition_guard = options[:fail_transition_guard]
|
||||
@log = options[:log]
|
||||
end
|
||||
|
||||
aasm do
|
||||
state :open, :initial => true,
|
||||
:before_enter => :before_enter_open,
|
||||
:enter => :enter_open,
|
||||
:after_enter => :after_enter_open,
|
||||
:before_exit => :before_exit_open,
|
||||
:exit => :exit_open,
|
||||
:after_exit => :after_exit_open
|
||||
|
||||
state :closed,
|
||||
:before_enter => :before_enter_closed,
|
||||
:enter => :enter_closed,
|
||||
:after_enter => :after_enter_closed,
|
||||
:before_exit => :before_exit_closed,
|
||||
:exit => :exit_closed,
|
||||
:after_exit => :after_exit_closed
|
||||
|
||||
event :close, :before => :before, :after => :after, :guard => :event_guard do
|
||||
transitions :to => :closed, :from => [:open], :after => :transitioning do
|
||||
guard do
|
||||
transition_guard
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
event :open, :before => :before, :after => :after do
|
||||
transitions :to => :open, :from => :closed
|
||||
end
|
||||
end
|
||||
|
||||
def log(text)
|
||||
puts text if @log
|
||||
end
|
||||
|
||||
def before_enter_open; log('before_enter_open'); end
|
||||
def enter_open; log('enter_open'); end
|
||||
def before_exit_open; log('before_exit_open'); end
|
||||
def after_enter_open; log('after_enter_open'); end
|
||||
def exit_open; log('exit_open'); end
|
||||
def after_exit_open; log('after_exit_open'); end
|
||||
|
||||
def before_enter_closed; log('before_enter_closed'); end
|
||||
def enter_closed; log('enter_closed'); end
|
||||
def before_exit_closed; log('before_exit_closed'); end
|
||||
def exit_closed; log('exit_closed'); end
|
||||
def after_enter_closed; log('after_enter_closed'); end
|
||||
def after_exit_closed; log('after_exit_closed'); end
|
||||
|
||||
def event_guard; log('event_guard'); !@fail_event_guard; end
|
||||
def transition_guard; log('transition_guard'); !@fail_transition_guard; end
|
||||
def transitioning; log('transitioning'); end
|
||||
|
||||
def before; log('before'); end
|
||||
def after; log('after'); end
|
||||
end
|
|
@ -1,9 +1,10 @@
|
|||
require 'spec_helper'
|
||||
Dir[File.dirname(__FILE__) + "/../models/callbacks/*.rb"].sort.each { |f| require File.expand_path(f) }
|
||||
|
||||
describe 'callbacks for the new DSL' do
|
||||
|
||||
it "be called in order" do
|
||||
callback = CallbackNewDsl.new
|
||||
callback = Callbacks::Basic.new
|
||||
callback.aasm.current_state
|
||||
|
||||
expect(callback).to receive(:before).once.ordered
|
||||
|
@ -26,7 +27,7 @@ describe 'callbacks for the new DSL' do
|
|||
end
|
||||
|
||||
it "does not run any state callback if the event guard fails" do
|
||||
callback = CallbackNewDsl.new(:log => false)
|
||||
callback = Callbacks::Basic.new(:log => false)
|
||||
callback.aasm.current_state
|
||||
|
||||
expect(callback).to receive(:before).once.ordered
|
||||
|
@ -50,7 +51,7 @@ describe 'callbacks for the new DSL' do
|
|||
context "if the transition guard fails" do
|
||||
it "does not run any state callback if guard is defined inline" do
|
||||
show_debug_log = false
|
||||
callback = CallbackNewDsl.new(:log => show_debug_log, :fail_transition_guard => true)
|
||||
callback = Callbacks::Basic.new(:log => show_debug_log, :fail_transition_guard => true)
|
||||
callback.aasm.current_state
|
||||
|
||||
unless show_debug_log
|
||||
|
@ -74,8 +75,6 @@ describe 'callbacks for the new DSL' do
|
|||
end
|
||||
|
||||
it "does not run transition_guard twice for multiple permitted transitions" do
|
||||
require 'models/callbacks/multiple_transitions_transition_guard'
|
||||
|
||||
show_debug_log = false
|
||||
callback = Callbacks::MultipleTransitionsTransitionGuard.new(:log => show_debug_log, :fail_transition_guard => true)
|
||||
callback.aasm.current_state
|
||||
|
@ -102,7 +101,7 @@ describe 'callbacks for the new DSL' do
|
|||
end
|
||||
|
||||
it "does not run any state callback if guard is defined with block" do
|
||||
callback = GuardWithinBlock.new #(:log => true, :fail_transition_guard => true)
|
||||
callback = Callbacks::GuardWithinBlock.new #(:log => true, :fail_transition_guard => true)
|
||||
callback.aasm.current_state
|
||||
|
||||
expect(callback).to receive(:before).once.ordered
|
||||
|
@ -125,7 +124,7 @@ describe 'callbacks for the new DSL' do
|
|||
end
|
||||
|
||||
it "should properly pass arguments" do
|
||||
cb = CallbackNewDslArgs.new
|
||||
cb = Callbacks::WithArgs.new
|
||||
|
||||
# TODO: use expect syntax here
|
||||
cb.should_receive(:before).with(:arg1, :arg2).once.ordered
|
||||
|
@ -141,14 +140,14 @@ describe 'callbacks for the new DSL' do
|
|||
end
|
||||
|
||||
it "should call the callbacks given the to-state as argument" do
|
||||
cb = CallbackWithStateArg.new
|
||||
cb = Callbacks::WithStateArg.new
|
||||
cb.should_receive(:before_method).with(:arg1).once.ordered
|
||||
cb.should_receive(:transition_method).never
|
||||
cb.should_receive(:transition_method2).with(:arg1).once.ordered
|
||||
cb.should_receive(:after_method).with(:arg1).once.ordered
|
||||
cb.close!(:out_to_lunch, :arg1)
|
||||
|
||||
cb = CallbackWithStateArg.new
|
||||
cb = Callbacks::WithStateArg.new
|
||||
some_object = double('some object')
|
||||
cb.should_receive(:before_method).with(some_object).once.ordered
|
||||
cb.should_receive(:transition_method2).with(some_object).once.ordered
|
||||
|
@ -157,14 +156,14 @@ describe 'callbacks for the new DSL' do
|
|||
end
|
||||
|
||||
it "should call the proper methods just with arguments" do
|
||||
cb = CallbackWithStateArg.new
|
||||
cb = Callbacks::WithStateArg.new
|
||||
cb.should_receive(:before_method).with(:arg1).once.ordered
|
||||
cb.should_receive(:transition_method).with(:arg1).once.ordered
|
||||
cb.should_receive(:transition_method).never
|
||||
cb.should_receive(:after_method).with(:arg1).once.ordered
|
||||
cb.close!(:arg1)
|
||||
|
||||
cb = CallbackWithStateArg.new
|
||||
cb = Callbacks::WithStateArg.new
|
||||
some_object = double('some object')
|
||||
cb.should_receive(:before_method).with(some_object).once.ordered
|
||||
cb.should_receive(:transition_method).with(some_object).once.ordered
|
||||
|
@ -191,7 +190,7 @@ describe 'event callbacks' do
|
|||
it "should run error_callback if an exception is raised and error_callback defined" do
|
||||
def @foo.error_callback(e); end
|
||||
|
||||
allow(@foo).to receive(:before_enter).and_raise(e=StandardError.new)
|
||||
allow(@foo).to receive(:before_enter).and_raise(e = StandardError.new)
|
||||
expect(@foo).to receive(:error_callback).with(e)
|
||||
|
||||
@foo.safe_close!
|
||||
|
|
Loading…
Reference in a new issue