2015-10-30 06:47:13 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe 'state machine' do
|
|
|
|
let(:simple) { SimpleExample.new }
|
|
|
|
let(:multiple) { SimpleMultipleExample.new }
|
|
|
|
|
|
|
|
describe 'transition_from' do
|
|
|
|
it "works for simple state machines" do
|
|
|
|
expect(simple).to transition_from(:initialised).to(:filled_out).on_event(:fill_out)
|
|
|
|
expect(simple).to_not transition_from(:initialised).to(:authorised).on_event(:fill_out)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works for multiple state machines" do
|
|
|
|
expect(multiple).to transition_from(:standing).to(:walking).on_event(:walk).on(:move)
|
|
|
|
expect(multiple).to_not transition_from(:standing).to(:running).on_event(:walk).on(:move)
|
|
|
|
|
|
|
|
expect(multiple).to transition_from(:sleeping).to(:processing).on_event(:start).on(:work)
|
|
|
|
expect(multiple).to_not transition_from(:sleeping).to(:sleeping).on_event(:start).on(:work)
|
|
|
|
end
|
|
|
|
end
|
2015-10-30 07:07:20 -04:00
|
|
|
|
2015-10-30 07:30:19 -04:00
|
|
|
describe 'allow_transition_to' do
|
|
|
|
it "works for simple state machines" do
|
|
|
|
expect(simple).to allow_transition_to(:filled_out)
|
|
|
|
expect(simple).to_not allow_transition_to(:authorised)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works for multiple state machines" do
|
|
|
|
expect(multiple).to allow_transition_to(:walking).on(:move)
|
|
|
|
expect(multiple).to_not allow_transition_to(:standing).on(:move)
|
|
|
|
|
|
|
|
expect(multiple).to allow_transition_to(:processing).on(:work)
|
|
|
|
expect(multiple).to_not allow_transition_to(:sleeping).on(:work)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-30 07:07:20 -04:00
|
|
|
describe "have_state" do
|
|
|
|
it "works for simple state machines" do
|
|
|
|
expect(simple).to have_state :initialised
|
|
|
|
expect(simple).to_not have_state :filled_out
|
|
|
|
simple.fill_out
|
|
|
|
expect(simple).to have_state :filled_out
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works for multiple state machines" do
|
|
|
|
expect(multiple).to have_state(:standing).on(:move)
|
|
|
|
expect(multiple).to_not have_state(:walking).on(:move)
|
|
|
|
multiple.walk
|
|
|
|
expect(multiple).to have_state(:walking).on(:move)
|
|
|
|
|
|
|
|
expect(multiple).to have_state(:sleeping).on(:work)
|
|
|
|
expect(multiple).to_not have_state(:processing).on(:work)
|
|
|
|
multiple.start
|
|
|
|
expect(multiple).to have_state(:processing).on(:work)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-30 07:19:09 -04:00
|
|
|
describe "allow_event" do
|
|
|
|
it "works for simple state machines" do
|
|
|
|
expect(simple).to allow_event :fill_out
|
|
|
|
expect(simple).to_not allow_event :authorise
|
|
|
|
simple.fill_out
|
|
|
|
expect(simple).to allow_event :authorise
|
|
|
|
end
|
|
|
|
|
2016-11-27 08:36:30 -05:00
|
|
|
it "works with custom arguments" do
|
2017-11-20 01:38:49 -05:00
|
|
|
example = SimpleExampleWithGuardArgs.new
|
|
|
|
expect(example).to allow_event(:fill_out_with_args).with(true)
|
|
|
|
expect(example).to_not allow_event(:fill_out_with_args).with(false)
|
2016-11-27 08:36:30 -05:00
|
|
|
end
|
|
|
|
|
2015-10-30 07:19:09 -04:00
|
|
|
it "works for multiple state machines" do
|
|
|
|
expect(multiple).to allow_event(:walk).on(:move)
|
|
|
|
expect(multiple).to_not allow_event(:hold).on(:move)
|
|
|
|
multiple.walk
|
|
|
|
expect(multiple).to allow_event(:hold).on(:move)
|
|
|
|
|
|
|
|
expect(multiple).to allow_event(:start).on(:work)
|
|
|
|
expect(multiple).to_not allow_event(:stop).on(:work)
|
|
|
|
multiple.start
|
|
|
|
expect(multiple).to allow_event(:stop).on(:work)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-30 06:47:13 -04:00
|
|
|
end
|