Skip to content

Commit c6e64ab

Browse files
author
Yuki Ito
committed
Add tests for warning
1 parent 231d65e commit c6e64ab

File tree

1 file changed

+148
-2
lines changed

1 file changed

+148
-2
lines changed

lib/elixir/test/elixir/kernel/warning_test.exs

Lines changed: 148 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,164 @@ defmodule Kernel.WarningTest do
55

66
import ExUnit.CaptureIO
77

8-
test :invalid_token do
8+
test :unused_variable do
99
assert capture_io(fn ->
10+
Code.eval_string """
1011
defmodule Sample do
12+
def hello(arg), do: nil
13+
end
14+
"""
15+
end) =~ %r"variable arg is unused"
16+
after
17+
purge Sample
18+
end
19+
20+
test :unused_function do
21+
assert capture_io(fn ->
22+
Code.eval_string """
23+
defmodule Sample1 do
24+
defp hello, do: nil
25+
end
26+
"""
27+
end) =~ %r"function hello/0 is unused"
28+
29+
assert capture_io(fn ->
30+
Code.eval_string """
31+
defmodule Sample2 do
1132
defp hello(0), do: hello(1)
1233
defp hello(1), do: :ok
1334
end
35+
"""
1436
end) =~ %r"function hello/1 is unused"
37+
38+
assert capture_io(fn ->
39+
Code.eval_string """
40+
defmodule Sample3 do
41+
def a, do: nil
42+
def b, do: d(10)
43+
defp c(x, y // 1), do: [x, y]
44+
defp d(x), do: x
45+
end
46+
"""
47+
end) =~ %r"function c/2 is unused"
48+
49+
after
50+
purge [Sample1, Sample2, Sample3]
51+
end
52+
53+
test :unused_macro do
54+
assert capture_io(fn ->
55+
Code.eval_string """
56+
defmodule Sample do
57+
defmacrop hello, do: nil
58+
end
59+
"""
60+
end) =~ %r"macro hello/0 is unused"
1561
after
1662
purge Sample
1763
end
1864

19-
defp purge(module) do
65+
test :unsued_default_args do
66+
assert capture_io(fn ->
67+
Code.eval_string """
68+
defmodule Sample1 do
69+
def a, do: b(1,2,3)
70+
defp b(arg1 // 1, arg2 // 2, arg3 // 3), do: [arg1, arg2, arg3]
71+
end
72+
"""
73+
end) =~ %r"default arguments in b/3 are never used"
74+
75+
assert capture_io(fn ->
76+
Code.eval_string """
77+
defmodule Sample2 do
78+
def a, do: b(1,2)
79+
defp b(arg1 // 1, arg2 // 2, arg3 // 3), do: [arg1, arg2, arg3]
80+
end
81+
"""
82+
end) =~ %r"the first 2 default arguments in b/3 are never used"
83+
84+
assert capture_io(fn ->
85+
Code.eval_string """
86+
defmodule Sample3 do
87+
def a, do: b(1)
88+
defp b(arg1 // 1, arg2 // 2, arg3 // 3), do: [arg1, arg2, arg3]
89+
end
90+
"""
91+
end) =~ %r"the first default argument in b/3 is never used"
92+
93+
assert capture_io(fn ->
94+
Code.eval_string """
95+
defmodule Sample4 do
96+
def a, do: b(1)
97+
defp b(arg1 // 1, arg2, arg3 // 3), do: [arg1, arg2, arg3]
98+
end
99+
"""
100+
end) == nil
101+
after
102+
purge [Sample1, Sample2, Sample3, Sample4]
103+
end
104+
105+
test :unused_import do
106+
assert capture_io(fn ->
107+
Code.eval_string """
108+
defmodule Sample1 do
109+
def hello, do: nil
110+
end
111+
112+
defmodule Sample2 do
113+
import Sample1
114+
def a, do: nil
115+
end
116+
"""
117+
end) =~ %r"unused import Sample1"
118+
after
119+
purge [Sample1, Sample2]
120+
end
121+
122+
test :clause_not_match do
123+
assert capture_io(fn ->
124+
Code.eval_string """
125+
defmodule Sample do
126+
def hello, do: nil
127+
def hello, do: nil
128+
end
129+
"""
130+
end) =~ %r"this clause cannot match because a previous clause at line 2 always matches"
131+
after
132+
purge Sample
133+
end
134+
135+
test :clause_with_defaults_should_be_first do
136+
assert capture_io(fn ->
137+
Code.eval_string """
138+
defmodule Sample do
139+
def hello(arg), do: nil
140+
def hello(arg // 0), do: nil
141+
end
142+
"""
143+
end) =~ %r"clause with defaults should be the first clause in function hello/1"
144+
after
145+
purge Sample
146+
end
147+
148+
test :multiple_clauses_with_defaults do
149+
assert capture_io(fn ->
150+
Code.eval_string """
151+
defmodule Sample do
152+
def hello(arg // 0), do: nil
153+
def hello(arg // 1), do: nil
154+
end
155+
"""
156+
end) =~ %r"function hello/1 has default values and multiple clauses, use a separate clause for declaring defaults"
157+
after
158+
purge Sample
159+
end
160+
161+
defp purge(list) when is_list(list) do
162+
Enum.each list, purge(&1)
163+
end
164+
165+
defp purge(module) when is_atom(module) do
20166
:code.delete module
21167
:code.purge module
22168
end

0 commit comments

Comments
 (0)