Skip to content

Commit

Permalink
first tests for state machines with multiple state attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed May 26, 2015
1 parent 3ea6068 commit 8d0cc35
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 48 deletions.
89 changes: 45 additions & 44 deletions spec/models/complex_multiple_example.rb
Expand Up @@ -47,58 +47,59 @@ class ComplexMultipleExample
end
end

# aasm(:right) do
# state :passive
# state :pending, :initial => true, :before_enter => :make_activation_code
# state :active, :before_enter => :do_activate
# state :suspended
# state :deleted, :before_enter => :do_delete#, :exit => :do_undelete
# state :waiting

# event :register do
# transitions :from => :passive, :to => :pending do
# guard do
# can_register?
# end
# end
# end

# event :activate do
# transitions :from => :pending, :to => :active
# end

# event :suspend do
# transitions :from => [:passive, :pending, :active], :to => :suspended
# end

# event :delete do
# transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
# end

# # a dummy event that can never happen
# event :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?
# transitions :from => :suspended, :to => :passive
# end

# event :wait do
# transitions :from => :suspended, :to => :waiting, :guard => :if_polite?
# end
# end # right
aasm(:right) do
state :passive
state :pending, :initial => true, :before_enter => :make_activation_code
state :active, :before_enter => :do_activate
state :suspended
state :deleted, :before_enter => :do_delete#, :exit => :do_undelete
state :waiting

event :right_register do
transitions :from => :passive, :to => :pending do
guard do
can_register?
end
end
end

event :right_activate do
transitions :from => :pending, :to => :active
end

event :right_suspend do
transitions :from => [:passive, :pending, :active], :to => :suspended
end

event :right_delete do
transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
end

# a dummy event that can never happen
event :right_unpassify do
transitions :from => :passive, :to => :active, :guard => Proc.new {|u| false }
end

event :right_unsuspend do
transitions :from => :suspended, :to => :active, :guard => Proc.new { has_activated? }
transitions :from => :suspended, :to => :pending, :guard => :has_activation_code?
transitions :from => :suspended, :to => :passive
end

event :right_wait do
transitions :from => :suspended, :to => :waiting, :guard => :if_polite?
end
end # right

def initialize
# the AR backend uses a before_validate_on_create :aasm_ensure_initial_state
# lets do something similar here for testing purposes.
aasm(:left).enter_initial_state
aasm(:right).enter_initial_state
end

def make_activation_code
@activation_code = 'moo'
@activation_code = @activation_code ? @activation_code + '2' : '1'
end

def do_activate
Expand Down
23 changes: 19 additions & 4 deletions spec/unit/complex_multiple_example_spec.rb
Expand Up @@ -5,32 +5,45 @@

it 'should be in the pending state' do
expect(auth.aasm(:left).current_state).to eq(:pending)
expect(auth.aasm(:right).current_state).to eq(:pending)
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.activation_code).to eq '12'
end
end

describe 'when being unsuspended' do
let(:auth) {ComplexMultipleExample.new}

it 'should be able to be unsuspended' do
it 'should be able to unsuspend' do
auth.left_activate!
auth.left_suspend!
expect(auth.may_left_unsuspend?).to be true

auth.right_activate!
auth.right_suspend!
expect(auth.may_right_unsuspend?).to be true
end

it 'should not be able to be unsuspended into active' do
it 'should not be able to unsuspend into active' do
auth.left_suspend!
expect(auth.may_left_unsuspend?(:active)).not_to be true

auth.right_activate!
auth.right_suspend!
expect(auth.may_right_unsuspend?(:active)).to be true
end

it 'should be able to be unsuspended into active if polite' do
it 'should be able to wait into waiting if polite' do
auth.left_suspend!
expect(auth.may_left_wait?(:waiting, :please)).to be true
auth.left_wait!(nil, :please)

auth.right_suspend!
expect(auth.may_right_wait?(:waiting)).to be false
auth.right_wait!(nil, :please)
end

it 'should not be able to be unsuspended into active if not polite' do
Expand Down Expand Up @@ -75,10 +88,12 @@

it "should be able to fire known events" do
expect(auth.aasm(:left).may_fire_event?(:left_activate)).to be true
expect(auth.aasm(:right).may_fire_event?(:right_activate)).to be true
end

it "should not be able to fire unknown events" do
expect(auth.aasm(:left).may_fire_event?(:unknown)).to be false
expect(auth.aasm(:right).may_fire_event?(:unknown)).to be false
end

end

0 comments on commit 8d0cc35

Please sign in to comment.