1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

first tests for state machines with multiple state attributes

This commit is contained in:
Thorsten Böttger 2015-05-18 21:43:18 +12:00
parent 6db4c04da8
commit 4b58b5829a
2 changed files with 59 additions and 59 deletions

View file

@ -1,49 +1,49 @@
class ComplexMultipleExample
include AASM
attr_accessor :activation_code, :activated_at, :deleted_at
attr_accessor :left_activation_code, :left_activated_at, :left_deleted_at
aasm(:left) do
state :passive
state :pending, :initial => true, :before_enter => :make_activation_code
state :active, :before_enter => :do_activate
state :pending, :initial => true, :before_enter => :make_left_activation_code
state :active, :before_enter => :do_left_activate
state :suspended
state :deleted, :before_enter => :do_delete#, :exit => :do_undelete
state :deleted, :before_enter => :do_left_delete#, :exit => :do_left_undelete
state :waiting
event :register do
event :left_register do
transitions :from => :passive, :to => :pending do
guard do
can_register?
can_left_register?
end
end
end
event :activate do
event :left_activate do
transitions :from => :pending, :to => :active
end
event :suspend do
event :left_suspend do
transitions :from => [:passive, :pending, :active], :to => :suspended
end
event :delete do
event :left_delete do
transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
end
# a dummy event that can never happen
event :unpassify do
event :left_unpassify do
transitions :from => :passive, :to => :active, :guard => Proc.new {|u| false }
end
event :unsuspend do
transitions :from => :suspended, :to => :active, :guard => Proc.new { has_activated? }
transitions :from => :suspended, :to => :pending, :guard => :has_activation_code?
event :left_unsuspend do
transitions :from => :suspended, :to => :active, :guard => Proc.new { has_left_activated? }
transitions :from => :suspended, :to => :pending, :guard => :has_left_activation_code?
transitions :from => :suspended, :to => :passive
end
event :wait do
transitions :from => :suspended, :to => :waiting, :guard => :if_polite?
event :left_wait do
transitions :from => :suspended, :to => :waiting, :guard => :if_left_polite?
end
end
@ -97,36 +97,36 @@ class ComplexMultipleExample
aasm(:left).enter_initial_state
end
def make_activation_code
@activation_code = 'moo'
def make_left_activation_code
@left_activation_code = 'moo'
end
def do_activate
@activated_at = Time.now
@activation_code = nil
def do_left_activate
@left_activated_at = Time.now
@left_activation_code = nil
end
def do_delete
@deleted_at = Time.now
def do_left_delete
@left_deleted_at = Time.now
end
def do_undelete
@deleted_at = false
def do_left_undelete
@left_deleted_at = false
end
def can_register?
def can_left_register?
true
end
def has_activated?
!!@activated_at
def has_left_activated?
!!@left_activated_at
end
def has_activation_code?
!!@activation_code
def has_left_activation_code?
!!@left_activation_code
end
def if_polite?(phrase = nil)
def if_left_polite?(phrase = nil)
phrase == :please
end
end

View file

@ -8,8 +8,8 @@ describe 'on initialization' do
end
it 'should have an activation code' do
expect(auth.has_activation_code?).to be_truthy
expect(auth.activation_code).not_to be_nil
expect(auth.has_left_activation_code?).to be_truthy
expect(auth.left_activation_code).not_to be_nil
end
end
@ -17,64 +17,64 @@ describe 'when being unsuspended' do
let(:auth) {ComplexMultipleExample.new}
it 'should be able to be unsuspended' do
auth.activate!
auth.suspend!
expect(auth.may_unsuspend?).to be true
auth.left_activate!
auth.left_suspend!
expect(auth.may_left_unsuspend?).to be true
end
it 'should not be able to be unsuspended into active' do
auth.suspend!
expect(auth.may_unsuspend?(:active)).not_to be true
auth.left_suspend!
expect(auth.may_left_unsuspend?(:active)).not_to be true
end
it 'should be able to be unsuspended into active if polite' do
auth.suspend!
expect(auth.may_wait?(:waiting, :please)).to be true
auth.wait!(nil, :please)
auth.left_suspend!
expect(auth.may_left_wait?(:waiting, :please)).to be true
auth.left_wait!(nil, :please)
end
it 'should not be able to be unsuspended into active if not polite' do
auth.suspend!
expect(auth.may_wait?(:waiting)).not_to be true
expect(auth.may_wait?(:waiting, :rude)).not_to be true
expect {auth.wait!(nil, :rude)}.to raise_error(AASM::InvalidTransition)
expect {auth.wait!}.to raise_error(AASM::InvalidTransition)
auth.left_suspend!
expect(auth.may_left_wait?(:waiting)).not_to be true
expect(auth.may_left_wait?(:waiting, :rude)).not_to be true
expect {auth.left_wait!(nil, :rude)}.to raise_error(AASM::InvalidTransition)
expect {auth.left_wait!}.to raise_error(AASM::InvalidTransition)
end
it 'should not be able to be unpassified' do
auth.activate!
auth.suspend!
auth.unsuspend!
auth.left_activate!
auth.left_suspend!
auth.left_unsuspend!
expect(auth.may_unpassify?).not_to be true
expect {auth.unpassify!}.to raise_error(AASM::InvalidTransition)
expect(auth.may_left_unpassify?).not_to be true
expect {auth.left_unpassify!}.to raise_error(AASM::InvalidTransition)
end
it 'should be active if previously activated' do
auth.activate!
auth.suspend!
auth.unsuspend!
auth.left_activate!
auth.left_suspend!
auth.left_unsuspend!
expect(auth.aasm(:left).current_state).to eq(:active)
end
it 'should be pending if not previously activated, but an activation code is present' do
auth.suspend!
auth.unsuspend!
auth.left_suspend!
auth.left_unsuspend!
expect(auth.aasm(:left).current_state).to eq(:pending)
end
it 'should be passive if not previously activated and there is no activation code' do
auth.activation_code = nil
auth.suspend!
auth.unsuspend!
auth.left_activation_code = nil
auth.left_suspend!
auth.left_unsuspend!
expect(auth.aasm(:left).current_state).to eq(:passive)
end
it "should be able to fire known events" do
expect(auth.aasm(:left).may_fire_event?(:activate)).to be true
expect(auth.aasm(:left).may_fire_event?(:left_activate)).to be true
end
it "should not be able to fire unknown events" do