Skip to content

Commit 7044f9c

Browse files
committed
Add a test for leaking vars
1 parent ec83279 commit 7044f9c

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

lib/ex_unit/test/ex_unit/doc_test_test.exs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,6 @@ defmodule ExUnit.DocTestTest.ExceptionModule do
4444
def two_exceptions_in_single_context
4545
end
4646

47-
defmodule ExUnit.DocTestTest.LeakCheckModule do
48-
@doc """
49-
iex> a = 1
50-
1
51-
52-
iex> a + 1
53-
2
54-
"""
55-
def no_leak
56-
end
57-
5847
defmodule ExUnit.DocTestTest.SomewhatGoodModule do
5948
@doc """
6049
iex> test_fun
@@ -111,7 +100,36 @@ defmodule ExUnit.DocTestTest do
111100
doctest ExUnit.DocTestTest.ExceptionModule
112101
end
113102

114-
# FIXME: is it possible to test this?
115-
# ** (CompileError) .../doc_test_test.exs:55: function a/0 undefined
116-
#doctest ExUnit.DocTestTest.LeakCheckModule
103+
test :var_leak do
104+
assert "nofile:9: function '_a'/0 undefined" = format_rescue("""
105+
defmodule M do
106+
@doc \"\"\"
107+
iex> _a = 1
108+
1
109+
110+
iex> _a + 1
111+
2
112+
\"\"\"
113+
def no_leak
114+
end
115+
116+
defmodule M.Test do
117+
require ExUnit.DocTest
118+
119+
ExUnit.DocTest.doctest M
120+
end
121+
"""
122+
)
123+
end
124+
125+
defp format_rescue(expr) do
126+
result = try do
127+
:elixir.eval(to_char_list(expr), [])
128+
nil
129+
rescue
130+
error -> error.message
131+
end
132+
133+
result || raise(ExUnit.AssertionError, message: "Expected function given to format_rescue to fail")
134+
end
117135
end

0 commit comments

Comments
 (0)