/
value.ex
355 lines (270 loc) · 11.5 KB
/
value.ex
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
defmodule Patch.Mock.Value do
@moduledoc """
Interface for generating mock values.
In a test this module is imported into the test and so using this module directly is not
necessary.
"""
alias Patch.Apply
alias Patch.Mock.Values
@type t ::
Values.Callable.t()
| Values.CallableStack.t()
| Values.Cycle.t()
| Values.Raises.t()
| Values.Scalar.t()
| Values.Sequence.t()
| Values.Throws.t()
| term()
@value_modules [
Values.Callable,
Values.CallableStack,
Values.Cycle,
Values.Raises,
Values.Scalar,
Values.Sequence,
Values.Throws
]
@doc """
Create a new `Values.Callable` to be used as the mock value.
When a patched function has a `Values.Callable` as its mock value, it will invoke the callable
with the arguments to the patched function on every invocation to generate a new value to
return.
```elixir
patch(Example, :example, callable(fn arg -> {:patched, arg} end))
assert Example.example(1) == {:patched, 1} # passes
assert Example.example(2) == {:patched, 2} # passes
assert Example.example(3) == {:patched, 3} # passes
```
Any function literal will automatically be promoted into a `Values.Callable` unless it is
wrapped in a `scalar/1` call.
See `callable/2` for more configuration options. `callable/1` calls use the default
configuration options
- dispatch: :apply
- evaluate: :passthrough
"""
@spec callable(target :: function()) :: Values.Callable.t()
def callable(target) do
callable(target, [])
end
@doc """
`callable/2` allows the test author to provide additional configuration.
There are two options
## `:dispatch`
Controls how the arguments are dispatched to the callable.
- `:apply` is the default. It will call the function with the same arity as the incoming call.
- `:list` will always call the callable with a single argument, a list of all the incoming
arguments.
### Apply Example
```elixir
patch(Example, :example, callable(fn a, b, c -> {:patched, a, b, c} end), :apply)
assert Example.example(1, 2, 3) == {:patched, 1, 2, 3} # passes
```
### List Example
```elixir
patch(Example, :example, callable(fn
[a, b, c] ->
{:patched, a, b, c}
[a] ->
{:patched, a}
end, :list))
assert Example.example(1, 2, 3) == {:patched, 1, 2, 3} # passes
assert Example.example(1) == {:patched, 1} # passes
```
## `:evaluate`
Controls how the callable is evaluated.
- `:passthrough` is the default. It will passthrough to the original function if the provided
callable fails to pattern match to the incoming call
- `:strict` will bubble up any `BadArityError` or `FunctionClauseErrors`.
## Legacy Configuration Behavior (may be deprecated)
This function accepts either a single atom, in which case it will assign that to the `:dispatch`
configuration and use the default `:evaluate` option.
The following calls are equivalent
```elixir
# Using legacy configuration convention
patch(Example, :example, callable(fn args -> {:patched, args}, :apply))
# Using explicit options without evaluate
patch(Example, :example, callable(fn args -> {:patched, args}, dispatch: :apply))
# Using fully specified explicit options
patch(Example, :example, callable(fn args -> {:patched, args}, dispatch: :apply, evaluate: :passthrough))
```
## Multiple Arities
`dispatch: :list` used to be the preferred way to deal with multiple arities, here's an example.
```elixir
patch(Example, :example, callable(fn
[a] ->
{:patched, a}
[a, b, c] ->
{:patched, a, b, c}
end, dispatch: :list))
assert Example.example(1) == {:patched, 1}
assert Example.example(1, 2, 3) == {:patched, 1, 2, 3}
```
Patch now has "Stacked Callables" so the preferred method is to use the equivalent code
```elixir
patch(Example, :example, fn a -> {:patched, a} end)
patch(Example, :example, fn a, b, c -> {:patched, a, b, c} end)
assert Example.example(1) == {:patched, 1}
assert Example.example(1, 2, 3) == {:patched, 1, 2, 3}
```
"""
@spec callable(target :: function(), dispatch :: Values.Callable.dispatch_mode()) :: Values.Callable.t()
def callable(target, dispatch) when is_atom(dispatch) do
callable(target, dispatch: dispatch)
end
@spec callable(target :: function(), options :: [Values.Callable.option()]) :: Values.Callable.t()
def callable(target, options) when is_list(options) do
Values.Callable.new(target, options)
end
@doc """
Create a new `Values.Cycle` to be used as the mock value.
When a patched function has a `Values.Cycle` as its mock value, it will provide the first value
in the cycle and then move the first value to the end of the cycle on every invocation.
Consider a function patched with `cycle([1, 2, 3])` via the following code
```elixir
patch(Example, :example, cycle([1, 2, 3]))
```
| Invocation | Cycle Before Call | Return Value | Cycle After Call |
|------------|-------------------|--------------|------------------|
| 1 | [1, 2, 3] | 1 | [2, 3, 1] |
| 2 | [2, 3, 1] | 2 | [3, 1, 2] |
| 3 | [3, 1, 2] | 3 | [1, 2, 3] |
| 4 | [1, 2, 3] | 1 | [2, 3, 1] |
| 5 | [2, 3, 1] | 2 | [3, 1, 2] |
| 6 | [3, 1, 2] | 3 | [1, 2, 3] |
| 7 | [1, 2, 3] | 1 | [2, 3, 1] |
We could continue the above table forever since the cycle will repeat endlessly. Cycles can
contain `callable/1,2`, `raise/1,2` and `throw/1` mock values.
"""
@spec cycle(values :: [term()]) :: Values.Cycle.t()
defdelegate cycle(values), to: Values.Cycle, as: :new
@doc """
Creates a new `Values.Scalar` to be used as the mock value.
When a patched function has a `Values.Scalar` as its mock value, it will provide the scalar
value on every invocation
```elixir
patch(Example, :example, scalar(:patched))
assert Example.example() == :patched # passes
assert Example.example() == :patched # passes
assert Example.example() == :patched # passes
```
When patching with any term that isn't a function, it will automatically be promoted into a
`Values.Scalar`.
```elixir
patch(Example, :example, :patched)
assert Example.example() == :patched # passes
assert Example.example() == :patched # passes
assert Example.example() == :patched # passes
```
Since functions are always automatically promoted to `Values.Callable`, if a function is meant
as a scalar value it **must** be wrapped in a call to `scalar/1`.
```elixir
patch(Example, :get_name_normalizer, scalar(&String.downcase/1))
assert Example.get_name_normalizer == &String.downcase/1 # passes
```
"""
@spec scalar(value :: term()) :: Values.Scalar.t()
defdelegate scalar(value), to: Values.Scalar, as: :new
@doc """
Creates a new `Values.Sequence` to be used as a mock value.
When a patched function has a `Values.Sequence` as its mock value, it will provide the first
value in the sequence as the return value and then discard the first value. Once the sequence
is down to a final value it will be retained and returned on every subsequent invocation.
Consider a function patched with `sequence([1, 2, 3])` via the following code
```elixir
patch(Example, :example, sequence([1, 2, 3]))
```
| Invocation | Sequence Before Call | Return Value | Sequence After Call |
|------------|----------------------|--------------|---------------------|
| 1 | [1, 2, 3] | 1 | [2, 3] |
| 2 | [2, 3] | 2 | [3] |
| 3 | [3] | 3 | [3] |
| 4 | [3] | 3 | [3] |
| 5 | [3] | 3 | [3] |
We could continue the above table forever since the sequence will continue to return the last
value endlessly. Sequences can contain `callable/1,2`, `raise/1,2` and `throw/1` mock values.
There is one special behavior of sequence, and that's an empty sequence, which always returns
the value `nil` on every invocation.
If the test author would like to simulate an exhaustable sequence, one that returns a set number
of items and then responds to every other call with `nil`, they can simply add a `nil` as the
last element in the sequence
```elixir
patch(Example, :example, sequence([1, 2, 3, nil])
```
| Invocation | Sequence Before Call | Return Value | Sequence After Call |
|------------|----------------------|--------------|---------------------|
| 1 | [1, 2, 3, nil] | 1 | [2, 3, nil] |
| 2 | [2, 3, nil] | 2 | [3, nil] |
| 3 | [3, nil] | 3 | [nil] |
| 4 | [nil] | nil | [nil] |
| 5 | [nil] | nil | [nil] |
"""
@spec sequence(values :: [term()]) :: Values.Sequence.t()
defdelegate sequence(values), to: Values.Sequence, as: :new
@doc """
Guard that checks whether a value is a proper Values module
"""
defguard is_value(module) when module in @value_modules
@doc """
Creates a special value that raises a RuntimeError with the given message.
```elixir
patch(Example, :example, raises("patched"))
assert_raise RuntimeError, "patched", fn ->
Example.example()
end
```
"""
@spec raises(message :: String.t()) :: Values.Raises.t()
defdelegate raises(message), to: Values.Raises, as: :new
@doc """
Creates a special value that raises the given exception with the provided attributes.
```elixir
patch(Example, :example, raises(ArgumentError, message: "patched"))
assert_raise ArgumentError, "patched", fn ->
Example.example()
end
```
"""
@spec raises(exception :: module(), attributes :: Keyword.t()) :: Values.Raises.t()
defdelegate raises(exception, attributes), to: Values.Raises, as: :new
@doc """
Creates a special values that throws the provided value when evaluated.
```elixir
patch(Example, :example, throws(:patched))
assert catch_throw(Example.example()) == :patched
```
"""
@spec throws(value :: term()) :: Values.Throws.t()
defdelegate throws(value), to: Values.Throws, as: :new
@doc """
Advances the given value.
Sequences and Cycles both have meaningful advances, all other values types this acts as a no-op.
"""
@spec advance(value :: t()) :: t()
def advance(%module{} = value) when is_value(module) do
module.advance(value)
end
def advance(value) do
value
end
@doc """
Generate the next return value and advance the underlying value.
"""
@spec next(value :: t(), arguments :: [term()]) :: {:ok, t(), term()} | :error
def next(%Values.Scalar{} = value, arguments) do
Values.Scalar.next(value, arguments)
end
def next(%module{} = value, arguments) when is_value(module) do
with {:ok, next, return_value} <- module.next(value, arguments) do
{:ok, _, return_value} = next(return_value, arguments)
{:ok, next, return_value}
end
end
def next(callable, arguments) when is_function(callable) do
with {:ok, result} <- Apply.safe(callable, arguments) do
{:ok, callable, result}
end
end
def next(scalar, _arguments) do
{:ok, scalar, scalar}
end
end