stealth 0.9.8 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,228 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
4
-
5
- describe "transition callbacks" do
6
-
7
- class KonamiCodeFlow
8
- include Stealth::Flow
9
-
10
- attr_reader :code, :before_transition_count, :transition_count
11
-
12
- def initialize
13
- @code = []
14
- @before_transition_count = 0
15
- @transition_count = 0
16
- end
17
-
18
- flow do
19
- state :up do
20
- event :upward, :transitions_to => :up
21
- event :downward, :transitions_to => :down
22
- end
23
-
24
- state :down do
25
- event :downward, :transitions_to => :down
26
- event :leftward, :transitions_to => :left
27
- end
28
-
29
- state :left do
30
- event :rightward, :transitions_to => :right
31
- end
32
-
33
- state :right do
34
- event :leftward, :transitions_to => :left
35
- event :bward, :transitions_to => :b
36
- end
37
-
38
- state :a
39
-
40
- state :b do
41
- event :award, :transitions_to => :a
42
- end
43
-
44
- before_transition do |from, to, triggering_event, *event_args|
45
- @before_transition_count += 1
46
- end
47
-
48
- after_transition do |from, to, triggering_event, *event_args|
49
- @code << to.to_s
50
- end
51
-
52
- on_transition {
53
- @transition_count += 1
54
- }
55
- end
56
- end
57
-
58
- let(:flow) { KonamiCodeFlow.new }
59
-
60
- before(:each) do
61
- flow.upward!
62
- flow.upward!
63
- flow.downward!
64
- flow.downward!
65
- flow.leftward!
66
- flow.rightward!
67
- flow.leftward!
68
- flow.rightward!
69
- flow.bward!
70
- flow.award!
71
- end
72
-
73
- it "should have a correct transition count" do
74
- expect(flow.transition_count).to eq 10
75
- end
76
-
77
- it "should have a correct before_transition count" do
78
- expect(flow.before_transition_count).to eq 10
79
- end
80
-
81
- it "should have generated the correct code via after_transition" do
82
- expect(flow.code).to eq(['up', 'up', 'down', 'down', 'left', 'right', 'left', 'right', 'b', 'a'])
83
- end
84
-
85
- describe "on_error callbacks" do
86
- class ErrorFlow
87
- include Stealth::Flow
88
-
89
- attr_reader :errors
90
-
91
- def initialize
92
- @errors = {}
93
- end
94
-
95
- flow do
96
- state :first do
97
- event :advance, :transitions_to => :second do
98
- raise "uh oh"
99
- end
100
- end
101
-
102
- state :second
103
-
104
- on_error do |error, from, to, event, *args|
105
- @errors.merge!({
106
- error: error.class,
107
- from: from,
108
- to: to,
109
- event: event,
110
- args: args
111
- })
112
- end
113
- end
114
- end
115
-
116
- let(:error_flow) { ErrorFlow.new }
117
-
118
- it "should not advance to the next state" do
119
- error_flow.advance!
120
- expect(error_flow.current_state).to eq :first
121
- end
122
-
123
- it "should call the on_error block" do
124
- error_flow.advance!
125
- expect(error_flow.errors).to eq({ error: RuntimeError, from: :first, to: :second, event: :advance, args: [] })
126
- end
127
- end
128
-
129
- describe "on_entry and on_exit callbacks" do
130
- class PostFlow
131
- include Stealth::Flow
132
-
133
- attr_reader :email_reviewer, :tweet_link
134
-
135
- def initialize
136
- @email_reviewer = false
137
- @tweet_link = false
138
- end
139
-
140
- flow do
141
- state :draft do
142
- event :submit_for_review, :transitions_to => :in_review
143
-
144
- on_exit do
145
- @email_reviewer = true
146
- end
147
- end
148
-
149
- state :in_review do
150
- event :approve, :transitions_to => :live
151
- event :reject, :transitions_to => :draft
152
- end
153
-
154
- state :live do
155
- on_entry do
156
- @tweet_link = true
157
- end
158
- end
159
- end
160
- end
161
-
162
- let(:post_flow) { PostFlow.new }
163
-
164
- it "should email the reviewer when flow transitions (on_exit) to in_review" do
165
- expect {
166
- post_flow.submit_for_review!
167
- }.to change(post_flow, :email_reviewer).from(false).to(true)
168
- end
169
-
170
- it "should tweet the post link when flow transitions (on_entry) to live" do
171
- post_flow.submit_for_review!
172
-
173
- expect {
174
- post_flow.approve!
175
- }.to change(post_flow, :tweet_link).from(false).to(true)
176
- end
177
- end
178
-
179
- describe "on_entry and on_exit method-style callbacks" do
180
- class ConcisePostFlow
181
- include Stealth::Flow
182
-
183
- attr_reader :email_reviewer, :tweet_link
184
-
185
- def initialize
186
- @email_reviewer = false
187
- @tweet_link = false
188
- end
189
-
190
- flow do
191
- state :draft do
192
- event :submit_for_review, :transitions_to => :in_review
193
- end
194
-
195
- state :in_review do
196
- event :approve, :transitions_to => :live
197
- event :reject, :transitions_to => :draft
198
- end
199
-
200
- state :live
201
- end
202
-
203
- def on_draft_exit(new_state, event, *args)
204
- @email_reviewer = true
205
- end
206
-
207
- def on_live_entry(prior_state, event, *args)
208
- @tweet_link = true
209
- end
210
- end
211
-
212
- let(:post_flow) { ConcisePostFlow.new }
213
-
214
- it "should email the reviewer when flow transitions (on_exit) to in_review" do
215
- expect {
216
- post_flow.submit_for_review!
217
- }.to change(post_flow, :email_reviewer).from(false).to(true)
218
- end
219
-
220
- it "should tweet the post link when flow transitions (on_entry) to live" do
221
- post_flow.submit_for_review!
222
-
223
- expect {
224
- post_flow.approve!
225
- }.to change(post_flow, :tweet_link).from(false).to(true)
226
- end
227
- end
228
- end