Last active
December 24, 2015 11:59
-
-
Save ka8725/6794542 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This custom matcher can be used to test state machine | |
# | |
# Examples | |
# | |
# transition = OpenStruct.new(:name => :operation1, :state_field => :status, :from => :state1, :to => :state2) | |
# it { should have_transition(transition) } | |
# | |
# transition = {:name => :operation2, :state_field => :status, :from => :state2, :to => :state3} | |
# it { should have_transition(transition) } | |
RSpec::Matchers.define :have_transition do |transition| | |
match do |model| | |
transition = OpenStruct(transition) | |
events = model.class.state_machines[transition.state_field].events | |
event = events[transition.name] | |
events.valid_for(model, :from => transition.from, :to => transition.to) == [event] | |
end | |
def OpenStruct(params) | |
params.is_a?(OpenStruct) ? params : OpenStruct.new(params) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GearBox | |
state_machine :gear, :initial => :P do | |
event :switch_to_r do | |
transition [:P, :N] => :R | |
end | |
event :switch_to_n do | |
transition [:R, :D] => :N | |
end | |
event :switch_to_d do | |
transition :N => :D | |
end | |
event :switch_to_p do | |
transition :R => :P | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe GearBox do | |
context '#gear' | |
let(:from_p_to_r) do | |
OpenStruct.new({ | |
:state_field => :gear, | |
:name => :switch_to_r, | |
:from => :P, | |
:to => :R | |
}) | |
end | |
let(:from_n_to_r) do | |
OpenStruct.new({ | |
:state_field => :gear, | |
:name => :switch_to_r, | |
:from => :N, | |
:to => :R | |
}) | |
end | |
let(:from_r_to_n) do | |
OpenStruct.new({ | |
:state_field => :gear, | |
:name => :switch_to_n, | |
:from => :R, | |
:to => :N | |
}) | |
end | |
let(:from_d_to_n) do | |
OpenStruct.new({ | |
:state_field => :gear, | |
:name => :switch_to_n, | |
:from => :D, | |
:to => :N | |
}) | |
end | |
let(:from_n_to_d) do | |
OpenStruct.new({ | |
:state_field => :gear, | |
:name => :switch_to_d, | |
:from => :n, | |
:to => :d | |
}) | |
end | |
let(:from_r_to_p) do | |
OpenStruct.new({ | |
:state_field => :gear, | |
:name => :switch_to_p, | |
:from => :R, | |
:to => :P | |
}) | |
end | |
it { should be_p } # check initial gear | |
it { should have_transition from_p_to_r } | |
it { should have_transition from_n_to_r } | |
it { should have_transition from_r_to_n } | |
it { should have_transition from_d_to_n } | |
it { should have_transition from_n_to_d } | |
it { should have_transition from_r_to_p } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment