/
having_test.exs
166 lines (149 loc) · 2.8 KB
/
having_test.exs
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
defmodule HavingTest do
use ExUnit.Case
import Having.TestHelper
import Having
doctest Having
test "simple expression with binding" do
assert 1 = having a
| a = 1
end
test "expands simple expression with binding" do
actual = expanded do
having a
| a = 1
end
expected = quoted do
with(a = 1, do: a)
end
assert expected == actual
end
test "expands two bindings into single with" do
actual = expanded do
having {a, b}
| a = 1
| b = 2
end
expected = quoted do
with(b = 2, a = 1, do: {a, b})
end
assert expected == actual
end
test "expands three bindings into single with" do
actual = expanded do
having {b, c}
| c = b * b
| b = a * a
| a = 2
end
expected = quoted do
with(a = 2, b = a * a, c = b * b, do: {b, c})
end
assert expected == actual
end
defp pows(x), do: having {m, n}
| n = m * m
| m = x * x
test "using having on a function def" do
assert {4, 16} = pows(2)
end
test "can use match" do
actual = expanded do
having a
|( a when is_number(a) <- 1 )
end
expected = quoted do
with(a when is_number(a) <- 1) do
a
end
end
assert expected == actual
end
test "can be piped into" do
actual = expanded do
{a, b}
|> having do
a = 1
b = 2
end
end
expected = quoted do
with(b = 2, a = 1, do: {a, b})
end
assert expected == actual
end
test "can pipe into keywords" do
actual = expanded do
{a, b} |> having(a: 1, b: 2)
end
expected = quoted do
with(b = 2, a = 1, do: {a, b})
end
assert expected == actual
end
test "can have else on piped having" do
actual = expanded do
a
|> having do
b
else
_ -> c
end
end
expected = quoted do
with(b) do
a
else
_ -> c
end
end
assert expected == actual
end
test "nested piped having" do
actual = expanded do
a
|> having(b when b < 10 <- 2)
|> having(c = 2)
end
expected = quoted do
with(c = 2) do
with(b when b < 10 <- 2) do
a
end
end
end
assert expected == actual
end
test "nested piped having else" do
actual = expanded do
a
|> having do
b
else
_ -> x
end
|> having(c)
end
expected = quoted do
with(c) do
with(b) do
a
else
_ -> x
end
end
end
assert expected == actual
end
test "pipes into with" do
actual = expanded do
having a
|> with(b, c)
end
expected = quoted do
with(b, c) do
a
end
end
assert expected == actual
end
end