-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction_spec.rb
178 lines (143 loc) · 4.1 KB
/
action_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require_relative 'spec_helper'
module CrackPipe
class ActionSpec < Minitest::Spec
let(:action) do
Class.new(Action) do
pass { |_, **| false }
step :truthy_value?
step :record_truthy_value_class
fail :record_failure
fail :return_error_code
def truthy_value?(_, value: nil, **)
pass!(value) if value == :short_circuit
fail!(value) if value == :short_circuit!
value
end
def record_truthy_value_class(ctx, value:, **)
ctx[:value_class] = value.class.name
value
end
def record_failure(ctx, value: nil, **)
ctx[:failure_msg] =
"`value` should be a truthy value. Instead it was `#{value}`."
end
def return_error_code(_, **)
:custom_error_code_01
end
end
end
let(:nesting_action) do
klass = action
Class.new(Action) do
step :before
step klass
step :after
fail :after_fail
def before(ctx, **)
ctx[:before] = true
end
def after(ctx, value:, **)
ctx[:after] = true
value.to_s.upcase
end
def after_fail(ctx, **)
:custom_error_code_02
end
end
end
it 'results in a success with a truthy value' do
r = action.(value: 'x')
r.history.size.must_equal(3)
r.history.select { |h| h[:next] == :default }.size.must_equal(3)
assert r.success?
r.output.must_equal('x')
r[:value].must_equal('x')
r[:value_class].must_equal('String')
refute r.context.key?(:failure_msg)
end
it 'results in a failure and uses the fail track with a falsy value' do
r = action.(value: false)
r.history.size.must_equal(4)
r.history.select { |h| h[:next] == :fail }.size.must_equal(3)
assert r.failure?
r.output.must_equal(:custom_error_code_01)
r[:failure_msg].must_match(/false/)
r[:value].must_equal(false)
end
it 'short circuits execution with `pass!`' do
r = action.new.(value: :short_circuit)
r.history.size.must_equal(2)
assert r.success?
r.output.must_equal(:short_circuit)
end
it 'short circuits execution with `fail!`' do
r = action.(value: :short_circuit!)
assert r.failure?
r.output.must_equal(:short_circuit!)
end
it 'nests one action in another' do
r = nesting_action.(value: 'x')
r.history.size.must_equal(5)
r.output.must_equal('X')
r[:after].must_equal(true)
r[:before].must_equal(true)
r[:value_class].must_equal('String')
r = nesting_action.(value: false)
r.history.size.must_equal(6)
r.output.must_equal(:custom_error_code_02)
r[:before].must_equal(true)
r = nesting_action.(value: :short_circuit!)
assert r.failure?
r.output.must_equal(:short_circuit!)
end
it 'works with a simple action with no context' do
action = Class.new(Action) do
step :truthy
def truthy(ctx, **)
ctx[:key] = true
end
end
r = action.call
assert r.success?
r[:key].must_equal(true)
end
it 'executes `after_step` hook' do
a = Class.new(Action) do
step :one
step :two
def one(*)
1
end
def two(*)
:two
end
def after_step(output)
output.is_a?(Symbol) ? output.to_s : super
end
end
r = a.({})
r.history[0][:output].must_equal(1)
r.history[1][:output].must_equal('two')
end
it 'executes `after_flow_control` hook' do
a = Class.new(Action) do
step :one
step :two
def one(*)
:one
end
def two(_, one:, **)
"#{one}!"
end
def after_flow_control(flow_control_hash)
o = flow_control_hash[:output]
flow_control_hash[:context][o] = o.to_s if o.is_a?(Symbol)
super
end
end
r = a.({})
r.history[0][:context][:one].must_equal('one')
r.output.must_equal('one!')
end
end
end