@@ -3,6 +3,8 @@ Code.require_file "../test_helper.exs", __DIR__
3
3
defmodule IEx.HelpersTest do
4
4
use IEx.Case
5
5
6
+ import IEx.Helpers
7
+
6
8
@ doc """
7
9
Test function 1
8
10
"""
@@ -20,57 +22,54 @@ defmodule IEx.HelpersTest do
20
22
21
23
test "h helper module" do
22
24
assert "# IEx.Helpers\n \n Welcome 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 "
25
29
end
26
30
27
31
test "h helper function" do
28
- doc_1 = "* def test_fun_1()\n \n Test function 1\n :ok "
29
- doc_2 = "* def test_fun_1(arg)\n \n Test function 2\n :ok "
32
+ doc_1 = "* def test_fun_1()\n \n Test function 1\n "
33
+ doc_2 = "* def test_fun_1(arg)\n \n Test function 2\n "
30
34
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
33
37
34
- output = capture_iex ( " h IEx.HelpersTest.test_fun_1" )
38
+ output = capture_io ( fn -> h IEx.HelpersTest . test_fun_1 end )
35
39
assert :binary . match ( output , doc_1 )
36
40
assert :binary . match ( output , doc_2 )
37
41
38
- assert capture_iex ( "h pwd" ) == "* def pwd()\n \n Prints the current working directory.\n \n :ok"
42
+ assert capture_io ( fn -> h pwd end )
43
+ == "* def pwd()\n \n Prints the current working directory.\n \n "
39
44
end
40
45
41
46
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 "
46
48
47
49
# 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 ->
49
51
String . starts_with? line , "@type"
50
52
end ) >= 2
51
53
52
54
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 )
55
57
end
56
58
57
59
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 "
62
61
63
62
# 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 ->
65
64
String . starts_with? line , "@spec"
66
65
end ) >= 2
67
66
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 ->
69
68
String . starts_with? line , "@spec"
70
69
end ) >= 2
71
70
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 "
74
73
end
75
74
76
75
test "v helper" do
@@ -94,20 +93,20 @@ defmodule IEx.HelpersTest do
94
93
end
95
94
96
95
test "flush helper" do
97
- assert capture_iex ( " self() <- :hello\n flush" ) == ":hello\n :hello \n :ok "
96
+ assert capture_io ( fn -> self ( ) <- :hello ; flush end ) == ":hello\n "
98
97
end
99
98
100
99
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 $"
102
101
end
103
102
104
103
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 )
107
106
|> String . split
108
107
|> Enum . map ( String . strip ( & 1 ) )
109
108
|> 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 )
111
110
end
112
111
113
112
test "import_file helper" do
@@ -149,19 +148,21 @@ defmodule IEx.HelpersTest do
149
148
% r / ^ Kernel \s+. + Elixir \. Kernel \. beam $/ ,
150
149
]
151
150
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 ->
153
152
Enum . any? regexes , fn re ->
154
153
Regex . match? re , line
155
154
end
156
155
end ) >= 2
157
156
end
158
157
159
158
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
162
162
163
163
File . write! "test-module-code.ex" , test_module_code
164
- assert capture_iex ( "c \" test-module-code.ex\" \n Helpers_test_module.run" ) == "[Helpers_test_module]\n run!\n :ok"
164
+ assert c ( "test-module-code.ex" ) == [ Helpers_test_module ]
165
+ assert Helpers_test_module . run == :run
165
166
after
166
167
File . rm "test-module-code.ex"
167
168
File . rm! "Elixir.Helpers_test_module.beam"
@@ -171,8 +172,9 @@ defmodule IEx.HelpersTest do
171
172
172
173
test "c helper multiple modules" do
173
174
File. write! "test-module-code.ex" , test_module_code <> "\n " <> another_test_module
174
- assert capture_iex ( "c(\" test-module-code.ex\" ) |> Enum.sort\n Helpers_test_module.run\n Another_test_module.hello" )
175
- == "[Another_test_module,Helpers_test_module]\n run!\n :ok\n world\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
176
178
after
177
179
File. rm "test-module-code.ex"
178
180
File. rm "Elixir.Helpers_test_module.beam"
@@ -187,8 +189,10 @@ defmodule IEx.HelpersTest do
187
189
test "c helper list" do
188
190
File . write! "test-module-code-1.ex" , test_module_code
189
191
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\n Helpers_test_module.run\n Another_test_module.hello" )
191
- == "[Another_test_module,Helpers_test_module]\n run!\n :ok\n world\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
192
196
after
193
197
File . rm "test-module-code-1.ex"
194
198
File . rm "test-module-code-2.ex"
@@ -203,16 +207,13 @@ defmodule IEx.HelpersTest do
203
207
end
204
208
205
209
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}\n run!\n :ok"
210
+ assert l( :non_existent_module ) == { :error , :nofile }
214
211
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
216
217
after
217
218
File. rm "test-module-code.ex"
218
219
File. rm! "Elixir.Helpers_test_module.beam"
@@ -224,16 +225,18 @@ defmodule IEx.HelpersTest do
224
225
end
225
226
226
227
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
231
233
232
234
File . write! "test-module-code.ex" , test_module_code
233
235
# FIXME: `r Helpers_test_module` returns :nosource
234
- assert capture_iex ( "c \" test-module-code.ex\" \n r 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 ]
235
238
236
- assert capture_iex ( "r" ) == " [Helpers_test_module]"
239
+ assert r == [ Helpers_test_module ]
237
240
after
238
241
File . rm "test-module-code.ex"
239
242
File . rm! "Elixir.Helpers_test_module.beam"
@@ -248,7 +251,7 @@ defmodule IEx.HelpersTest do
248
251
"""
249
252
defmodule Helpers_test_module do
250
253
def run do
251
- IO.puts " run!"
254
+ : run
252
255
end
253
256
end
254
257
"""
@@ -258,7 +261,7 @@ defmodule IEx.HelpersTest do
258
261
"""
259
262
defmodule Another_test_module do
260
263
def hello do
261
- IO.puts " world"
264
+ : world
262
265
end
263
266
end
264
267
"""
0 commit comments