Skip to content

Commit 85781b4

Browse files
authored
Merge pull request #10 from togethercomputer/aj/eng-36100-add-common-assertions-for-asserting-items-in-collections
2 parents 1ea91e1 + 53b22a0 commit 85781b4

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

lib/together/test/assertions.ex

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,45 @@ defmodule Together.Test.Assertions do
179179
defp var_context({name, meta, context}) do
180180
{name, meta[:counter] || context}
181181
end
182+
183+
@doc """
184+
Assert a collection contains an item
185+
186+
## Example
187+
188+
my_function() # returns [1, 2]
189+
|> assert_contains(2)
190+
191+
"""
192+
defmacro assert_contains(collection, item) do
193+
quote do
194+
collection = unquote(collection)
195+
assert unquote(item) in collection
196+
collection
197+
end
198+
end
199+
200+
@doc """
201+
Assert a collection contains an item matching the given pattern
202+
203+
## Example
204+
205+
my_function() # returns [%{"key" => "value"}, %{"other_key" => "other"}]
206+
|> assert_contains_match(%{"key" => _})
207+
208+
"""
209+
defmacro assert_contains_match(collection, pattern) do
210+
quote do
211+
collection = unquote(collection)
212+
213+
assert Enum.any?(collection, fn item ->
214+
case item do
215+
unquote(pattern) -> true
216+
_ -> false
217+
end
218+
end)
219+
220+
collection
221+
end
222+
end
182223
end
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
defmodule Together.Test.AssertionsTest do
2+
use ExUnit.Case
3+
4+
import Together.Test.Assertions
5+
6+
describe "assert_equal/2" do
7+
test "asserts that two values are equal" do
8+
assert_equal(2, 2)
9+
10+
assert_raise ExUnit.AssertionError, fn ->
11+
assert_equal(2, 3)
12+
end
13+
end
14+
15+
test "allows chaining assertions" do
16+
2
17+
|> assert_equal(2)
18+
|> Kernel.+(1)
19+
|> assert_equal(3)
20+
end
21+
end
22+
23+
describe "assert_match/2" do
24+
test "asserts that the first argument matches the given pattern" do
25+
assert_match(%{"key" => "value"}, %{"key" => _})
26+
27+
assert_raise ExUnit.AssertionError, fn ->
28+
assert_match(%{"key" => "value"}, %{"key" => "other"})
29+
end
30+
end
31+
32+
test "allows chaining assertions" do
33+
%{"key" => "value"}
34+
|> assert_match(%{"key" => _})
35+
|> Map.put("new_key", "new_value")
36+
|> assert_match(%{"new_key" => _})
37+
end
38+
39+
test "allows pinning in patterns" do
40+
value = "value"
41+
42+
assert_match(%{"key" => "value"}, %{"key" => ^value})
43+
44+
assert_raise ExUnit.AssertionError, fn ->
45+
assert_match(%{"key" => "other"}, %{"key" => ^value})
46+
end
47+
end
48+
49+
test "allows extracting values in patterns" do
50+
assert_match(%{"key" => "value"}, %{"key" => extracted_value})
51+
assert extracted_value == "value"
52+
end
53+
end
54+
55+
describe "assert_recent/2" do
56+
test "asserts that a timestamp is recent" do
57+
assert_recent(DateTime.utc_now())
58+
59+
assert_raise ExUnit.AssertionError, fn ->
60+
assert_recent(DateTime.add(DateTime.utc_now(), -20, :second), 10)
61+
end
62+
end
63+
end
64+
65+
describe "assert_set_equal/2" do
66+
test "asserts that two sets are equal" do
67+
assert_set_equal([1, 2, 3], [2, 3, 1])
68+
assert_set_equal([1, 2, 3], [2, 3, 1, 1])
69+
70+
assert_raise ExUnit.AssertionError, fn ->
71+
assert_set_equal([1, 2, 3], [1, 2])
72+
end
73+
74+
assert_raise ExUnit.AssertionError, fn ->
75+
assert_set_equal([1, 2, 3], [1, 2, 4])
76+
end
77+
78+
assert_raise ExUnit.AssertionError, fn ->
79+
assert_set_equal([1, 2, 3], [1, 2, 3, 4])
80+
end
81+
end
82+
end
83+
84+
describe "assert_set_match/2" do
85+
test "asserts that the first set matches the given pattern" do
86+
assert_set_match([1, 2, 3], [2, 3, _])
87+
88+
assert_raise ExUnit.AssertionError, fn ->
89+
assert_set_match([1, 2, 3], [1, 2])
90+
end
91+
92+
assert_raise ExUnit.AssertionError, fn ->
93+
assert_set_match([1, 2, 3], [1, 2, 4])
94+
end
95+
96+
assert_raise ExUnit.AssertionError, fn ->
97+
assert_set_match([1, 2, 3], [1, 2, 3, 4])
98+
end
99+
end
100+
101+
test "allows pinning in patterns" do
102+
value = 1
103+
assert_set_match([1, 2, 3], [^value, 2, _])
104+
end
105+
end
106+
107+
describe "assert_contains/2" do
108+
test "asserts that a collection contains an item" do
109+
assert_contains([1, 2, 3], 2)
110+
111+
assert_raise ExUnit.AssertionError, fn ->
112+
assert_contains([1, 2, 3], 4)
113+
end
114+
end
115+
116+
test "allows chaining assertions" do
117+
[1, 2]
118+
|> assert_contains(2)
119+
|> Enum.map(&(&1 * 2))
120+
|> assert_contains(4)
121+
end
122+
end
123+
124+
describe "assert_contains_match/2" do
125+
test "asserts that a collection contains an item matching the pattern" do
126+
assert_contains_match([%{"key" => "value"}, %{"other_key" => "other"}], %{"key" => _})
127+
128+
assert_raise ExUnit.AssertionError, fn ->
129+
assert_contains_match([%{"key" => "value"}], %{"key" => "other"})
130+
end
131+
end
132+
133+
test "allows pinning in patterns" do
134+
value = "value"
135+
assert_contains_match([%{"key" => "value"}, %{"other_key" => "other"}], %{"key" => ^value})
136+
end
137+
138+
test "allows chaining assertions" do
139+
[%{"key" => "value"}, %{"other_key" => "other"}]
140+
|> assert_contains_match(%{"key" => _})
141+
|> Enum.map(&Map.new(&1, fn {k, v} -> {String.upcase(k), v} end))
142+
|> assert_contains_match(%{"KEY" => _})
143+
end
144+
end
145+
end

0 commit comments

Comments
 (0)