simple_state_machine 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec_helper.rb DELETED
@@ -1,20 +0,0 @@
1
- require "rubygems"
2
- require "bundler"
3
- Bundler.require :test
4
- begin
5
- require 'active_record'
6
- rescue LoadError
7
- puts "Skipping ActiveRecord specs"
8
- end
9
-
10
- ROOT = Pathname(File.expand_path(File.join(File.dirname(__FILE__), '..')))
11
- $LOAD_PATH << File.join(ROOT, 'lib')
12
- $LOAD_PATH << File.join(ROOT, 'spec')
13
- $LOAD_PATH << File.join(ROOT, 'examples')
14
-
15
- require 'simple_state_machine'
16
- require File.join(ROOT, 'examples', 'conversation.rb')
17
- require File.join(ROOT, 'examples', 'lamp.rb')
18
- require File.join(ROOT, 'examples', 'traffic_light.rb')
19
- require File.join(ROOT, 'examples', 'user.rb') if defined? ActiveRecord
20
-
@@ -1,103 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe SimpleStateMachine::StateMachineDefinition do
4
-
5
- let(:klass) do
6
- Class.new do
7
- extend SimpleStateMachine
8
- def initialize(state = 'state1')
9
- @state = state
10
- end
11
- event :event1, :state1 => :state2, :state2 => :state3
12
- end
13
- end
14
- let(:smd) { klass.state_machine_definition }
15
-
16
- it "is inherited by subclasses" do
17
- subject = Class.new(klass).new
18
- expect(subject).to be_state1
19
- subject.event1
20
- expect(subject).to be_state2
21
- subject.event1
22
- expect(subject).to be_state3
23
- end
24
-
25
- describe '#state_method' do
26
- subject do
27
- klass = Class.new do
28
- attr_reader :ssm_state
29
- extend SimpleStateMachine
30
- state_machine_definition.state_method = :ssm_state
31
-
32
- def initialize(state = 'state1')
33
- @ssm_state = state
34
- end
35
- event :event1, :state1 => :state2
36
- event :event2, :state2 => :state3
37
- end
38
- klass.new
39
- end
40
-
41
- it "is used when changing state" do
42
- expect(subject.ssm_state).to eq('state1')
43
- subject.event1
44
- expect(subject.ssm_state).to eq('state2')
45
- subject.event2
46
- expect(subject.ssm_state).to eq('state3')
47
- end
48
-
49
- it "works with state helper methods" do
50
- expect(subject).to be_state1
51
- subject.event1
52
- expect(subject).to be_state2
53
- subject.event2
54
- expect(subject).to be_state3
55
- end
56
-
57
- it "raise an error if an invalid state_transition is called" do
58
- expect { subject.event2 }.to raise_error(SimpleStateMachine::IllegalStateTransitionError, "You cannot 'event2' when state is 'state1'")
59
- end
60
-
61
- end
62
-
63
- describe '#default_error_state' do
64
- subject do
65
- klass = Class.new do
66
- attr_reader :state
67
- extend SimpleStateMachine
68
- state_machine_definition.default_error_state = :failed
69
-
70
- def initialize(state = 'state1')
71
- @state = state
72
- end
73
-
74
- def event1
75
- raise "Some error during event"
76
- end
77
- event :event1, :state1 => :state2
78
- end
79
- klass.new
80
- end
81
-
82
- it "is set when an error occurs during an event" do
83
- expect(subject.state).to eq('state1')
84
- subject.event1
85
- expect(subject.state).to eq('failed')
86
- end
87
- end
88
-
89
- describe "#transitions" do
90
- it "has a list of transitions" do
91
- expect(smd.transitions).to be_a(Array)
92
- expect(smd.transitions.first).to be_a(SimpleStateMachine::Transition)
93
- end
94
- end
95
-
96
- describe "#to_s" do
97
- it "converts to readable string format" do
98
- expect(smd.to_s).to match(Regexp.new("state1.event1! => state2"))
99
- end
100
- end
101
-
102
- end
103
-
@@ -1,60 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe SimpleStateMachine::StateMachine do
4
-
5
- describe "#next_state" do
6
- subject do
7
- klass = Class.new do
8
- extend SimpleStateMachine
9
- def initialize() @state = 'state1' end
10
- event :event1, :state1 => :state2
11
- event :event2, :state2 => :state3
12
- end
13
- klass.new.state_machine
14
- end
15
-
16
- it "returns the next state for the event and current state" do
17
- expect(subject.next_state('event1')).to eq('state2')
18
- end
19
-
20
- it "returns nil if no next state for the event and current state exists" do
21
- expect(subject.next_state('event2')).to be_nil
22
- end
23
- end
24
-
25
- describe "#raised_error" do
26
- context "given an error can be caught" do
27
- let(:class_with_error) do
28
- Class.new do
29
- extend SimpleStateMachine
30
- def initialize(state = 'state1'); @state = state; end
31
- def raise_error
32
- raise "Something went wrong"
33
- end
34
- event :raise_error, :state1 => :state2,
35
- RuntimeError => :failed
36
- event :retry, :failed => :success
37
- end
38
- end
39
-
40
- subject do
41
- class_with_error.new.tap{|i| i.raise_error }
42
- end
43
-
44
- it "the raised error is accessible" do
45
- raised_error = subject.state_machine.raised_error
46
- expect(raised_error).to be_a(RuntimeError)
47
- expect(raised_error.message).to eq("Something went wrong")
48
- end
49
-
50
- it "the raised error is set to nil on the next transition" do
51
- expect(subject.state_machine.raised_error).to be
52
- subject.retry
53
- expect(subject.state_machine.raised_error).not_to be
54
- end
55
-
56
- end
57
- end
58
-
59
- end
60
-
@@ -1,30 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe SimpleStateMachine::Tools::Graphviz do
4
-
5
- before do
6
- @klass = Class.new do
7
- extend SimpleStateMachine
8
- def initialize(state = 'state1')
9
- @state = state
10
- end
11
- event :event1, :state1 => :state2, :state2 => :state3
12
- end
13
- @smd = @klass.state_machine_definition
14
- end
15
-
16
- describe "#to_graphviz_dot" do
17
- it "converts to graphviz dot format" do
18
- expect(@smd.to_graphviz_dot).to eq(%(digraph G {\n"state1"->"state2"[label=event1];\n"state2"->"state3"[label=event1]\n}))
19
- end
20
- end
21
-
22
- describe "#google_chart_url" do
23
- it "shows the state and event dependencies as a Google chart" do
24
- graph = @smd.transitions.map { |t| t.to_graphviz_dot }.sort.join(";")
25
- expect(@smd.google_chart_url).to eq("http://chart.googleapis.com/chart?cht=gv&chl=digraph{#{::CGI.escape graph}}")
26
- end
27
- end
28
- end
29
-
30
-
@@ -1,70 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe SimpleStateMachine::Tools::Inspector do
4
-
5
- describe "#begin_states" do
6
- before do
7
- @klass = Class.new(SimpleStateMachine::StateMachineDefinition) do
8
- def add_events
9
- define_event(:event_a, :state1 => :state2)
10
- define_event(:event_b, :state2 => :state3)
11
- define_event(:event_c, :state1 => :state3)
12
- define_event(:event_c, RuntimeError => :state3)
13
- end
14
-
15
- def decorator_class
16
- SimpleStateMachine::Decorator::Default
17
- end
18
- end
19
- end
20
-
21
- it "returns all 'from' states that aren't 'to' states" do
22
- expect(@klass.new.begin_states).to eq([:state1, RuntimeError])
23
- end
24
- end
25
-
26
- describe "#end_states" do
27
- before do
28
- @klass = Class.new(SimpleStateMachine::StateMachineDefinition) do
29
- def add_events
30
- define_event(:event_a, :state1 => :state2)
31
- define_event(:event_b, :state2 => :state3)
32
- define_event(:event_c, :state1 => :state3)
33
- end
34
-
35
- def decorator_class
36
- SimpleStateMachine::Decorator::Default
37
- end
38
-
39
- end
40
- end
41
-
42
- it "returns all 'to' states that aren't 'from' states" do
43
- expect(@klass.new.end_states).to eq([:state3])
44
- end
45
- end
46
-
47
- describe "#states" do
48
- before do
49
- @klass = Class.new(SimpleStateMachine::StateMachineDefinition) do
50
- def add_events
51
- define_event(:event_a, :state1 => :state2)
52
- define_event(:event_b, :state2 => :state3)
53
- define_event(:event_c, :state1 => :state3)
54
- end
55
-
56
- def decorator_class
57
- SimpleStateMachine::Decorator::Default
58
- end
59
-
60
- end
61
- end
62
-
63
- it "returns all states" do
64
- expect(@klass.new.states.map(&:to_s).sort).to eq(%w{state1 state2 state3})
65
- end
66
- end
67
-
68
- end
69
-
70
-