Leon4gr45 commited on
Commit
6cb2295
·
verified ·
1 Parent(s): c46ec45

Delete test/support/assert_matches.ex with huggingface_hub

Browse files
Files changed (1) hide show
  1. test/support/assert_matches.ex +0 -406
test/support/assert_matches.ex DELETED
@@ -1,406 +0,0 @@
1
- defmodule Plausible.AssertMatches do
2
- @moduledoc """
3
- Pattern match assertions wrapper macro extending it with checks expressed
4
- directly within the pattern.
5
-
6
- The idea here is that the pin (^) operator does not only rebind existing
7
- binding in the scope but also allows embedding basically any other expression
8
- to match against the given part of the pattern. Normal pattern matching is
9
- also supported and both can be mixed. The only caveat so far is that when
10
- normal patterns fail, only they are listed in the error even if there are
11
- potentially failing expressions. However, once the normal pattern is fixed,
12
- they surface.
13
-
14
- Currently, the following expressions can be pinned:
15
-
16
- * `any(:atom)`
17
- * `any(:string)`
18
- * `any(:binary)`
19
- * `any(:integer)`
20
- * `any(:pos_integer)`
21
- * `any(:number)`
22
- * `any(:float)`
23
- * `any(:boolean)`
24
- * `any(:map)`
25
- * `any(:list)`
26
- * `any(:tuple)`
27
- * `any(:iso8601_date)`
28
- * `any(:iso8601_datetime)`
29
- * `any(:iso8601_naive_datetime)`
30
- * all above variants of any with a one argument predicate function accepting
31
- value and returning a boolean, like: `any(:integer, & &1 > 20)`
32
- * a special case of `any(:string, ~r/regex pattern/)` checking that value is
33
- a string and matches a pattern
34
- * shorthand version of the above, `~r/regex pattern/`
35
- * any artibrary one argument function returning a boolean, like `&is_float/1`
36
- or `&(&1 < 40 or &1 > 300)`
37
- * exactly(expression) where expression is compared using equality, so that can
38
- enforce full equality inside a pattern, like: `exactly(%{foo: 2})` which will
39
- fail if the value is something like `%{foo: 2, other: "something}`
40
- * any other arbitrary expression which is compared the way as if it was wrapped
41
- with `exactly()`; this allows "interpolating" values from schemas and maps without
42
- rebinding like `user.id` (instead of having to rebind to `user_id` first)
43
-
44
- There's also a special pin type, `strict_map(...)` which can wrap around any map
45
- in the pattern. It's enforcing that the pattern has enumerated all the keys
46
- present in the respective map in the pattern matched value. All the above mentioned
47
- pin expressions can be also used inside `strict_map(...)` and `strict_map()` pins
48
- can be nested.
49
-
50
- Usage example:
51
-
52
- n = %{z: 2}
53
-
54
- assert_matches %{
55
- a: ^any(:integer, &(&1 > 2)),
56
- b: ^any(:string, ~r/baz/),
57
- d: [_ | _],
58
- e: ^~r/invalid/,
59
- f: ^n.z,
60
- g: ^(&is_float/1),
61
- h: ^exactly(%{foo: :bar})
62
- } = %{
63
- a: 1,
64
- b: "twofer",
65
- c: :other,
66
- d: [1, 2, 3],
67
- e: "another string",
68
- f: 1,
69
- g: 4.2,
70
- h: %{foo: :bar, other: "stuff"}
71
- }
72
- """
73
-
74
- @doc @moduledoc
75
- defmacro assert_matches({:=, meta, [pattern, value]}) do
76
- {base_strict_pattern, strict_vars} = build_base_strict_pattern(pattern)
77
-
78
- base_strict_pattern = clear_bindings_except(base_strict_pattern, :match_var)
79
-
80
- strict_patterns =
81
- Enum.map(strict_vars, fn strict_var ->
82
- build_strict_pattern(base_strict_pattern, strict_var)
83
- end)
84
-
85
- strict_pattern_matches =
86
- Enum.map(strict_patterns, fn {strict_pattern, _} ->
87
- quote do
88
- assert unquote(strict_pattern) = unquote(value)
89
- end
90
- end)
91
-
92
- strict_pattern_checks =
93
- Enum.map(strict_patterns, fn {strict_pattern, [{strict_var, map_pattern_keys}]} ->
94
- build_strict_pattern_check(strict_pattern, strict_var, map_pattern_keys, pattern, value)
95
- end)
96
-
97
- {var_pattern, pins} = build_var_pattern(pattern)
98
-
99
- clean_pattern =
100
- Enum.reduce(pins, var_pattern, fn {var, _predicate}, pattern ->
101
- Macro.postwalk(pattern, fn
102
- ^var -> {:_, [], __MODULE__}
103
- other -> other
104
- end)
105
- end)
106
-
107
- var_pattern = clear_bindings_except(var_pattern, :assert_match)
108
-
109
- predicate_pattern = build_predicate_pattern(var_pattern, pins)
110
-
111
- quote do
112
- value = unquote(value)
113
- assert unquote(clean_pattern) = value
114
- unquote(strict_pattern_matches)
115
- unquote(strict_pattern_checks)
116
-
117
- assert unquote(var_pattern) = value
118
-
119
- if unquote(length(pins) > 0) do
120
- {errors?, predicate_pattern} = unquote(predicate_pattern)
121
-
122
- if errors? do
123
- raise ExUnit.AssertionError,
124
- message: "match (=) failed",
125
- left: predicate_pattern,
126
- right: value,
127
- expr:
128
- {:assert_matches, unquote(meta),
129
- [{:=, [], [unquote(Macro.escape(pattern)), Macro.escape(value)]}]},
130
- context: {:match, []}
131
- end
132
- end
133
- end
134
- end
135
-
136
- defp build_base_strict_pattern(pattern) do
137
- Macro.postwalk(pattern, [], fn
138
- {:^, _, [{:strict_map, _, _}]} = pin, acc ->
139
- pinned_var = Macro.unique_var(:match, __MODULE__)
140
-
141
- pin = Macro.update_meta(pin, &Keyword.put(&1, :match_var, pinned_var))
142
-
143
- {pin, [pinned_var | acc]}
144
-
145
- {:^, _, _}, acc ->
146
- {{:_, [], __MODULE__}, acc}
147
-
148
- other, acc ->
149
- {other, acc}
150
- end)
151
- end
152
-
153
- defp build_strict_pattern(base_strict_pattern, strict_var) do
154
- Macro.postwalk(base_strict_pattern, [], fn
155
- {:^, meta, [{:strict_map, _, [pinned]}]}, acc ->
156
- if meta[:match_var] == strict_var do
157
- {:%{}, _, map_pattern_values} = pinned
158
- map_pattern_keys = map_pattern_values |> Enum.map(&elem(&1, 0)) |> Enum.sort()
159
- {strict_var, [{strict_var, map_pattern_keys} | acc]}
160
- else
161
- {
162
- Macro.postwalk(pinned, fn
163
- {:^, _, _} ->
164
- {:_, [], __MODULE__}
165
-
166
- other ->
167
- other
168
- end),
169
- acc
170
- }
171
- end
172
-
173
- other, acc ->
174
- {other, acc}
175
- end)
176
- end
177
-
178
- defp build_strict_pattern_check(strict_pattern, strict_var, map_pattern_keys, pattern, value) do
179
- quote bind_quoted: [
180
- pattern: Macro.escape(pattern),
181
- value: value,
182
- strict_pattern: Macro.escape(strict_pattern),
183
- var: strict_var,
184
- escaped_var: Macro.escape(strict_var),
185
- pattern_keys: map_pattern_keys
186
- ] do
187
- var_keys = var |> Map.keys() |> Enum.sort()
188
-
189
- if pattern_keys != var_keys do
190
- missing_keys = var_keys -- pattern_keys
191
-
192
- map_pattern_values =
193
- pattern_keys
194
- |> Enum.map(&{&1, {:_, [], __MODULE__}})
195
- |> Enum.concat(Enum.map(missing_keys, &{&1, :_MISSING_KEY__}))
196
-
197
- error_pattern =
198
- Macro.postwalk(strict_pattern, fn
199
- ^escaped_var -> {:%{}, [], map_pattern_values}
200
- other -> other
201
- end)
202
-
203
- raise ExUnit.AssertionError,
204
- message: "match (=) failed",
205
- left: error_pattern,
206
- right: value,
207
- expr: {:assert_matches, [], [{:=, [], [pattern, Macro.escape(value)]}]},
208
- context: {:match, []}
209
- end
210
- end
211
- end
212
-
213
- defp build_predicate_pattern(var_pattern, pins) do
214
- quote bind_quoted: [
215
- var_pattern: Macro.escape(var_pattern),
216
- escaped_pins: Macro.escape(pins),
217
- pins: pins
218
- ] do
219
- escaped_pins
220
- |> Enum.zip(pins)
221
- |> Enum.reduce({false, var_pattern}, fn {{escaped_var, escaped_predicate}, {var, predicate}},
222
- {errors?, pattern} ->
223
- result =
224
- if is_function(predicate, 1) do
225
- not predicate.(var)
226
- else
227
- predicate != var
228
- end
229
-
230
- if result do
231
- escaped_predicate = Plausible.AssertMatches.Internal.strip_prefix(escaped_predicate)
232
-
233
- {true,
234
- Macro.postwalk(pattern, fn
235
- ^escaped_var -> escaped_predicate
236
- other -> other
237
- end)}
238
- else
239
- {errors?,
240
- Macro.postwalk(pattern, fn
241
- ^escaped_var -> {:_, [], __MODULE__}
242
- other -> other
243
- end)}
244
- end
245
- end)
246
- end
247
- end
248
-
249
- defp build_var_pattern(pattern) do
250
- Macro.postwalk(pattern, [], fn
251
- {:^, _meta, [{pinned, _, module}]} = normal_pin, acc
252
- when is_atom(pinned) and is_atom(module) ->
253
- {normal_pin, acc}
254
-
255
- {:^, _meta, [{:strict_map, _, [pinned]}]}, acc ->
256
- {pinned, acc}
257
-
258
- {:^, _meta, [pinned]}, acc ->
259
- pinned = Plausible.AssertMatches.Internal.transform_predicate(pinned)
260
-
261
- pinned_var =
262
- Macro.unique_var(:match, __MODULE__)
263
- |> Macro.update_meta(&Keyword.put(&1, :assert_match, true))
264
-
265
- {pinned_var, [{pinned_var, pinned} | acc]}
266
-
267
- other, acc ->
268
- {other, acc}
269
- end)
270
- end
271
-
272
- defp clear_bindings_except(pattern, except_meta) do
273
- pattern
274
- |> Macro.postwalk(fn
275
- {:^, _, [{name, meta, module}]} = pin when is_atom(name) and is_atom(module) ->
276
- if meta[except_meta] do
277
- pin
278
- else
279
- {:_, [], __MODULE__}
280
- end
281
-
282
- other ->
283
- other
284
- end)
285
- |> Macro.postwalk(fn
286
- {name, meta, module} = var when is_atom(name) and is_atom(module) ->
287
- if meta[except_meta] do
288
- var
289
- else
290
- {:_, [], __MODULE__}
291
- end
292
-
293
- other ->
294
- other
295
- end)
296
- end
297
-
298
- defmodule Internal do
299
- @moduledoc false
300
-
301
- def transform_predicate({:any, _, [value]}) do
302
- quote do
303
- Plausible.AssertMatches.Internal.any(unquote(value))
304
- end
305
- end
306
-
307
- def transform_predicate({:exactly, _, [value]}) do
308
- quote do
309
- Plausible.AssertMatches.Internal.exactly(unquote(value))
310
- end
311
- end
312
-
313
- def transform_predicate({:any, _, [value, extra_predicate]}) do
314
- quote do
315
- Plausible.AssertMatches.Internal.any(
316
- unquote(value),
317
- unquote(extra_predicate)
318
- )
319
- end
320
- end
321
-
322
- def transform_predicate({:sigil_r, _, _} = regex) do
323
- quote do
324
- Plausible.AssertMatches.Internal.regex(unquote(regex))
325
- end
326
- end
327
-
328
- def transform_predicate(other), do: other
329
-
330
- def strip_prefix({{:., _, [_prefix, f]}, _, args}) when f in [:any, :regex, :exactly] do
331
- {f, [], args}
332
- end
333
-
334
- def strip_prefix(predicate) do
335
- predicate
336
- end
337
-
338
- def any(:atom), do: &is_atom/1
339
- def any(:string), do: &is_binary/1
340
- def any(:binary), do: &is_binary/1
341
- def any(:integer), do: &is_integer/1
342
- def any(:number), do: &is_number/1
343
- def any(:float), do: &is_float/1
344
- def any(:boolean), do: &is_boolean/1
345
- def any(:map), do: &is_map/1
346
- def any(:list), do: &is_list/1
347
- def any(:tuple), do: &is_tuple/1
348
-
349
- def any(:pos_integer) do
350
- fn value ->
351
- is_integer(value) and value > 0
352
- end
353
- end
354
-
355
- def any(:iso8601_date) do
356
- fn value ->
357
- case Date.from_iso8601(value) do
358
- {:ok, _} -> true
359
- _ -> false
360
- end
361
- end
362
- end
363
-
364
- def any(:iso8601_datetime) do
365
- fn value ->
366
- case DateTime.from_iso8601(value) do
367
- {:ok, _, _} -> true
368
- _ -> false
369
- end
370
- end
371
- end
372
-
373
- def any(:iso8601_naive_datetime) do
374
- fn value ->
375
- case NaiveDateTime.from_iso8601(value) do
376
- {:ok, _} -> true
377
- _ -> false
378
- end
379
- end
380
- end
381
-
382
- def any(:string, %Regex{} = regex) do
383
- fn value ->
384
- any(:string).(value) and regex(regex).(value)
385
- end
386
- end
387
-
388
- def any(type, predicate_fn) when is_function(predicate_fn, 1) do
389
- fn value ->
390
- any(type).(value) and predicate_fn.(value)
391
- end
392
- end
393
-
394
- def regex(regex) do
395
- fn value ->
396
- String.match?(value, regex)
397
- end
398
- end
399
-
400
- def exactly(expr) do
401
- fn value ->
402
- value == expr
403
- end
404
- end
405
- end
406
- end