/
copeiro.ex
154 lines (127 loc) · 3.63 KB
/
copeiro.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
defmodule Copeiro do
@moduledoc """
The Copeiro package provides assertion functions that will enhance your testing experience in Elixir
"""
@doc """
Asserts that two lists matches
## Examples
For the following examples `left` and `right` will be used to describe the expression `assert_lists left OPERATOR right`
### All elements of `left` are also elements of `right`
```
iex> assert_lists [1, 2] in [0, 2, 1, 3]
true
iex> assert_lists [{:a, 1}, {:c, 3}] in [{:a, 1}, {:b, 2}, {:c, 3}]
true
```
### `left` and `right` has no element in common
```
iex> assert_lists [1, 2] not in [3, 4]
true
iex> assert_lists [%{c: 3}, %{d: 4}] not in [%{a: 1}, %{b: 2}]
true
```
### Asserts that two lists match in any order
```
iex> assert_lists [1, 2, 3] == [2, 1, 3], any_order: true
true
iex> assert_lists [{:a, 0}, {:b, 1}, {:c, 3}] == [{:a, 0}, {:c, 3}, {:b, 1}], any_order: true
true
```
### Asserting lists of maps/structs
```
iex> assert_lists [%{a: 1}, %{a: 2}] in [%{a: 1, b: 1}, %{a: 2, b: 2}, %{a: 3, b: 3}], keys: [:a]
true
iex> assert_lists [%{a: 1}, %{a: 2}] == [%{a: 2, b: 2}, %{a: 1, b: 1}], keys: [:a], any_order: true
true
```
"""
defmacro assert_lists(expr, opts \\ []) do
{op, left, right} =
case expr do
{:not, _, [{:in, _, [left, right]}]} -> {:not_in, left, right}
{op, _, [left, right]} -> {op, left, right}
end
quote bind_quoted: [op: op, left: left, right: right, opts: opts], location: :keep do
{left, right, opts} = Copeiro.__access_keys__(left, right, opts)
Copeiro.__assert_lists__(op, left, right, opts)
end
end
@doc false
def __assert_lists__(:==, left, right, any_order: true) do
left
|> Copeiro.Comparator.match_lists_in_any_order(right)
|> case do
{:error, {direction, value}} ->
ExUnit.Assertions.flunk("""
assertion failed, lists does not match
value-#{direction}: #{inspect(value)}
left: #{inspect(left)}
right: #{inspect(right)}
""")
:ok ->
true
end
end
def __assert_lists__(:==, left, right, _opts) do
if left == right do
true
else
ExUnit.Assertions.flunk("""
Comparison (using ==) failed in:
left: #{inspect(left)}
right: #{inspect(right)}
""")
end
end
def __assert_lists__(:in, left, right, _opts) do
left
|> Enum.reduce_while(:ok, fn l, _ ->
if l in right do
{:cont, :ok}
else
{:halt, {:error, l}}
end
end)
|> case do
{:error, value} ->
ExUnit.Assertions.flunk("""
assertion failed, value not found
value: #{inspect(value)}
left: #{inspect(left)}
right: #{inspect(right)}
""")
:ok ->
true
end
end
def __assert_lists__(:not_in, left, right, _opts) do
left
|> Enum.reduce_while(true, fn l, _ ->
case l not in right do
true -> {:cont, true}
false -> {:halt, {:error, l}}
end
end)
|> case do
{:error, value} ->
ExUnit.Assertions.flunk("""
match succeeded, but should have failed
value: #{inspect(value)}
left: #{inspect(left)}
right: #{inspect(right)}
""")
_ ->
true
end
end
@doc false
def __access_keys__(left, right, opts) do
{keys, opts} = Keyword.pop(opts, :keys, [])
if keys == [] do
{left, right, opts}
else
{do_access_keys(left, keys), do_access_keys(right, keys), opts}
end
end
defp do_access_keys(lst, keys), do: Enum.map(lst, &Map.take(&1, keys))
end