Skip to content

Commit a79d965

Browse files
committed
Rewrite tests for helpers to use capture_iex and capture_io sparingly
1 parent 958550c commit a79d965

File tree

2 files changed

+57
-53
lines changed

2 files changed

+57
-53
lines changed

lib/iex/test/iex/helpers_test.exs

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Code.require_file "../test_helper.exs", __DIR__
33
defmodule IEx.HelpersTest do
44
use IEx.Case
55

6+
import IEx.Helpers
7+
68
@doc """
79
Test function 1
810
"""
@@ -20,57 +22,54 @@ defmodule IEx.HelpersTest do
2022

2123
test "h helper module" do
2224
assert "# IEx.Helpers\n\nWelcome to Interactive Elixir" <> _
23-
= capture_iex("h IEx.Helpers")
24-
assert capture_iex("h :whatever") == "Could not load module :whatever: nofile\n:ok"
25+
= capture_io(fn -> h IEx.Helpers end)
26+
27+
assert capture_io(fn -> h :whatever end)
28+
== "Could not load module :whatever: nofile\n"
2529
end
2630

2731
test "h helper function" do
28-
doc_1 = "* def test_fun_1()\n\nTest function 1\n:ok"
29-
doc_2 = "* def test_fun_1(arg)\n\nTest function 2\n:ok"
32+
doc_1 = "* def test_fun_1()\n\nTest function 1\n"
33+
doc_2 = "* def test_fun_1(arg)\n\nTest function 2\n"
3034

31-
assert capture_iex("h IEx.HelpersTest.test_fun_1/0") == doc_1
32-
assert capture_iex("h IEx.HelpersTest.test_fun_1/1") == doc_2
35+
assert capture_io(fn -> h IEx.HelpersTest.test_fun_1/0 end) == doc_1
36+
assert capture_io(fn -> h IEx.HelpersTest.test_fun_1/1 end) == doc_2
3337

34-
output = capture_iex("h IEx.HelpersTest.test_fun_1")
38+
output = capture_io(fn -> h IEx.HelpersTest.test_fun_1 end)
3539
assert :binary.match(output, doc_1)
3640
assert :binary.match(output, doc_2)
3741

38-
assert capture_iex("h pwd") == "* def pwd()\n\nPrints the current working directory.\n\n:ok"
42+
assert capture_io(fn -> h pwd end)
43+
== "* def pwd()\n\nPrints the current working directory.\n\n"
3944
end
4045

4146
test "t helper" do
42-
assert "** (UndefinedFunctionError) undefined function: IEx.Helpers.t/0" <> _
43-
= capture_iex("t")
44-
45-
assert capture_iex("t ExUnit") == "No types for ExUnit have been found\n:ok"
47+
assert capture_io(fn -> t ExUnit end) == "No types for ExUnit have been found\n"
4648

4749
# Test that it shows at least two types
48-
assert Enum.count(capture_iex("t Enum") |> String.split("\n"), fn line ->
50+
assert Enum.count(capture_io(fn -> t Enum end) |> String.split("\n"), fn line ->
4951
String.starts_with? line, "@type"
5052
end) >= 2
5153

5254
assert "@type t() :: " <> _
53-
= capture_iex("t Enum.t")
54-
assert capture_iex("t Enum.t") == capture_iex("t Enum.t/0")
55+
= capture_io(fn -> t Enum.t end)
56+
assert capture_io(fn -> t Enum.t end) == capture_io(fn -> t Enum.t/0 end)
5557
end
5658

5759
test "s helper" do
58-
assert "** (UndefinedFunctionError) undefined function: IEx.Helpers.s/0" <> _
59-
= capture_iex("s")
60-
61-
assert capture_iex("s ExUnit") == "No specs for ExUnit have been found\n:ok"
60+
assert capture_io(fn -> s ExUnit end) == "No specs for ExUnit have been found\n"
6261

6362
# Test that it shows at least two specs
64-
assert Enum.count(capture_iex("s Enum") |> String.split("\n"), fn line ->
63+
assert Enum.count(capture_io(fn -> s Enum end) |> String.split("\n"), fn line ->
6564
String.starts_with? line, "@spec"
6665
end) >= 2
6766

68-
assert Enum.count(capture_iex("s Enum.all?") |> String.split("\n"), fn line ->
67+
assert Enum.count(capture_io(fn -> s Enum.all? end) |> String.split("\n"), fn line ->
6968
String.starts_with? line, "@spec"
7069
end) >= 2
7170

72-
assert capture_iex("s Enum.all?/1") == "@spec all?(t()) :: boolean()\n:ok"
73-
assert capture_iex("s list_to_binary") == "@spec list_to_binary(iolist()) :: binary()\n:ok"
71+
assert capture_io(fn -> s Enum.all?/1 end) == "@spec all?(t()) :: boolean()\n"
72+
assert capture_io(fn -> s list_to_binary end) == "@spec list_to_binary(iolist()) :: binary()\n"
7473
end
7574

7675
test "v helper" do
@@ -94,20 +93,20 @@ defmodule IEx.HelpersTest do
9493
end
9594

9695
test "flush helper" do
97-
assert capture_iex("self() <- :hello\nflush") == ":hello\n:hello\n:ok"
96+
assert capture_io(fn -> self() <- :hello; flush end) == ":hello\n"
9897
end
9998

10099
test "pwd helper" do
101-
assert capture_iex("pwd") =~ %r"lib[\\/]iex\n:ok$"
100+
assert capture_io(fn -> pwd end) =~ %r"lib[\\/]iex\n$"
102101
end
103102

104103
test "ls helper" do
105-
assert [":ok", "ebin", "lib", "mix.exs", "test"]
106-
= capture_iex("ls")
104+
assert ["ebin", "lib", "mix.exs", "test"]
105+
= capture_io(fn -> ls end)
107106
|> String.split
108107
|> Enum.map(String.strip(&1))
109108
|> Enum.sort
110-
assert capture_iex("ls \"~\"") == capture_iex("ls System.user_home")
109+
assert capture_io(fn -> ls "~" end) == capture_io(fn -> ls System.user_home end)
111110
end
112111

113112
test "import_file helper" do
@@ -149,19 +148,21 @@ defmodule IEx.HelpersTest do
149148
%r/^Kernel\s+.+Elixir\.Kernel\.beam$/,
150149
]
151150

152-
assert Enum.count(capture_iex("m") |> String.split("\n"), fn line ->
151+
assert Enum.count(capture_io(fn -> m end) |> String.split("\n"), fn line ->
153152
Enum.any? regexes, fn re ->
154153
Regex.match? re, line
155154
end
156155
end) >= 2
157156
end
158157

159158
test "c helper" do
160-
assert "** (UndefinedFunctionError) undefined function: Helpers_test_module.run/0" <> _
161-
= capture_iex("Helpers_test_module.run")
159+
assert_raise UndefinedFunctionError, "undefined function: Helpers_test_module.run/0", fn ->
160+
Helpers_test_module.run
161+
end
162162

163163
File.write! "test-module-code.ex", test_module_code
164-
assert capture_iex("c \"test-module-code.ex\"\nHelpers_test_module.run") == "[Helpers_test_module]\nrun!\n:ok"
164+
assert c("test-module-code.ex") == [Helpers_test_module]
165+
assert Helpers_test_module.run == :run
165166
after
166167
File.rm "test-module-code.ex"
167168
File.rm! "Elixir.Helpers_test_module.beam"
@@ -171,8 +172,9 @@ defmodule IEx.HelpersTest do
171172

172173
test "c helper multiple modules" do
173174
File.write! "test-module-code.ex", test_module_code <> "\n" <> another_test_module
174-
assert capture_iex("c(\"test-module-code.ex\") |> Enum.sort\nHelpers_test_module.run\nAnother_test_module.hello")
175-
== "[Another_test_module,Helpers_test_module]\nrun!\n:ok\nworld\n:ok"
175+
assert c("test-module-code.ex") |> Enum.sort == [Another_test_module,Helpers_test_module]
176+
assert Helpers_test_module.run == :run
177+
assert Another_test_module.hello == :world
176178
after
177179
File.rm "test-module-code.ex"
178180
File.rm "Elixir.Helpers_test_module.beam"
@@ -187,8 +189,10 @@ defmodule IEx.HelpersTest do
187189
test "c helper list" do
188190
File.write! "test-module-code-1.ex", test_module_code
189191
File.write! "test-module-code-2.ex", another_test_module
190-
assert capture_iex("c([\"test-module-code-1.ex\", \"test-module-code-2.ex\"]) |> Enum.sort\nHelpers_test_module.run\nAnother_test_module.hello")
191-
== "[Another_test_module,Helpers_test_module]\nrun!\n:ok\nworld\n:ok"
192+
assert c(["test-module-code-1.ex", "test-module-code-2.ex"]) |> Enum.sort
193+
== [Another_test_module,Helpers_test_module]
194+
assert Helpers_test_module.run == :run
195+
assert Another_test_module.hello == :world
192196
after
193197
File.rm "test-module-code-1.ex"
194198
File.rm "test-module-code-2.ex"
@@ -203,16 +207,13 @@ defmodule IEx.HelpersTest do
203207
end
204208

205209
test "l helper" do
206-
File.write! "test-module-code.ex", test_module_code
207-
input = """
208-
c "test-module-code.ex"
209-
File.write! "test-module-code.ex", "defmodule Helpers_test_module do end"
210-
l Helpers_test_module
211-
Helpers_test_module.run
212-
"""
213-
assert capture_iex(input) == "[Helpers_test_module]\n:ok\n{:module,Helpers_test_module}\nrun!\n:ok"
210+
assert l(:non_existent_module) == {:error,:nofile}
214211

215-
assert capture_iex("l :non_existent_module") == "{:error,:nofile}"
212+
File.write! "test-module-code.ex", test_module_code
213+
assert c("test-module-code.ex") == [Helpers_test_module]
214+
#File.write! "test-module-code.ex", "defmodule Helpers_test_module do end"
215+
assert l(Helpers_test_module) == {:module, Helpers_test_module}
216+
assert Helpers_test_module.run == :run
216217
after
217218
File.rm "test-module-code.ex"
218219
File.rm! "Elixir.Helpers_test_module.beam"
@@ -224,16 +225,18 @@ defmodule IEx.HelpersTest do
224225
end
225226

226227
test "r helper" do
227-
assert capture_iex("r") == "[]"
228-
assert capture_iex("r Kernel") == ":nosource"
229-
assert "** (UndefinedFunctionError) undefined function: :non_existent_module.module_info/1" <> _
230-
= capture_iex("r :non_existent_module")
228+
assert r == []
229+
assert r(Kernel) == :nosource
230+
assert_raise UndefinedFunctionError, "undefined function: :non_existent_module.module_info/1", fn ->
231+
r :non_existent_module
232+
end
231233

232234
File.write! "test-module-code.ex", test_module_code
233235
# FIXME: `r Helpers_test_module` returns :nosource
234-
assert capture_iex("c \"test-module-code.ex\"\nr Helpers_test_module") == "[Helpers_test_module]\n[Helpers_test_module]"
236+
assert c("test-module-code.ex") == [Helpers_test_module]
237+
assert r(Helpers_test_module) == [Helpers_test_module]
235238

236-
assert capture_iex("r") == "[Helpers_test_module]"
239+
assert r == [Helpers_test_module]
237240
after
238241
File.rm "test-module-code.ex"
239242
File.rm! "Elixir.Helpers_test_module.beam"
@@ -248,7 +251,7 @@ defmodule IEx.HelpersTest do
248251
"""
249252
defmodule Helpers_test_module do
250253
def run do
251-
IO.puts "run!"
254+
:run
252255
end
253256
end
254257
"""
@@ -258,7 +261,7 @@ defmodule IEx.HelpersTest do
258261
"""
259262
defmodule Another_test_module do
260263
def hello do
261-
IO.puts "world"
264+
:world
262265
end
263266
end
264267
"""

lib/iex/test/test_helper.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule IEx.Case do
55
defmacro __using__(_) do
66
quote do
77
use ExUnit.Case, async: false
8+
import ExUnit.CaptureIO
89
import unquote(__MODULE__)
910

1011
setup do

0 commit comments

Comments
 (0)