Skip to content

Commit c0ba8a1

Browse files
authored
Use Test Modules in tests instead of Elixir built-in modules (#14383)
1 parent af17940 commit c0ba8a1

File tree

8 files changed

+221
-111
lines changed

8 files changed

+221
-111
lines changed

lib/elixir/test/elixir/exception_test.exs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -556,20 +556,33 @@ defmodule ExceptionTest do
556556
end
557557

558558
test "annotates function clause errors" do
559-
assert blame_message(Access, & &1.fetch(:foo, :bar)) =~ """
560-
no function clause matching in Access.fetch/2
559+
import PathHelpers
560+
561+
write_beam(
562+
defmodule ExampleModule do
563+
def fun(arg1, arg2)
564+
def fun(:one, :one), do: :ok
565+
def fun(:two, :two), do: :ok
566+
end
567+
)
568+
569+
message = blame_message(ExceptionTest.ExampleModule, & &1.fun(:three, :four))
570+
571+
assert message =~ """
572+
no function clause matching in ExceptionTest.ExampleModule.fun/2
561573
562-
The following arguments were given to Access.fetch/2:
574+
The following arguments were given to ExceptionTest.ExampleModule.fun/2:
563575
564576
# 1
565-
:foo
577+
:three
566578
567579
# 2
568-
:bar
580+
:four
569581
570-
Attempted function clauses (showing 5 out of 5):
582+
Attempted function clauses (showing 2 out of 2):
571583
572-
def fetch(-%module{} = container-, key)
584+
def fun(-:one-, -:one-)
585+
def fun(-:two-, -:two-)
573586
"""
574587
end
575588

@@ -858,13 +871,21 @@ defmodule ExceptionTest do
858871

859872
describe "blaming unit tests" do
860873
test "annotates clauses errors" do
861-
args = [%{}, :key, nil]
874+
import PathHelpers
875+
876+
write_beam(
877+
defmodule BlameModule do
878+
def fun(arg), do: arg
879+
end
880+
)
881+
882+
args = [nil]
862883

863884
{exception, stack} =
864-
Exception.blame(:error, :function_clause, [{Keyword, :pop, args, [line: 13]}])
885+
Exception.blame(:error, :function_clause, [{BlameModule, :fun, args, [line: 13]}])
865886

866887
assert %FunctionClauseError{kind: :def, args: ^args, clauses: [_]} = exception
867-
assert stack == [{Keyword, :pop, 3, [line: 13]}]
888+
assert stack == [{BlameModule, :fun, 1, [line: 13]}]
868889
end
869890

870891
test "annotates args and clauses from mfa" do

lib/elixir/test/elixir/module_test.exs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,18 @@ defmodule ModuleTest do
432432
end
433433

434434
test "compiles to core" do
435-
{:ok, {Atom, [{~c"Dbgi", dbgi}]}} = Atom |> :code.which() |> :beam_lib.chunks([~c"Dbgi"])
435+
import PathHelpers
436+
437+
write_beam(
438+
defmodule ExampleModule do
439+
end
440+
)
441+
442+
{:ok, {ExampleModule, [{~c"Dbgi", dbgi}]}} =
443+
ExampleModule |> :code.which() |> :beam_lib.chunks([~c"Dbgi"])
444+
436445
{:debug_info_v1, backend, data} = :erlang.binary_to_term(dbgi)
437-
{:ok, core} = backend.debug_info(:core_v1, Atom, data, [])
446+
{:ok, core} = backend.debug_info(:core_v1, ExampleModule, data, [])
438447
assert is_tuple(core)
439448
end
440449

lib/elixir/test/elixir/protocol/consolidation_test.exs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,18 @@ defmodule Protocol.ConsolidationTest do
247247
end
248248

249249
test "consolidation errors on missing BEAM files" do
250+
import PathHelpers
251+
252+
write_beam(
253+
defmodule ExampleModule do
254+
end
255+
)
256+
250257
defprotocol NoBeam do
251258
def example(arg)
252259
end
253260

254-
assert Protocol.consolidate(String, []) == {:error, :not_a_protocol}
261+
assert Protocol.consolidate(ExampleModule, []) == {:error, :not_a_protocol}
255262
assert Protocol.consolidate(NoBeam, []) == {:error, :no_beam_info}
256263
end
257264

lib/elixir/test/elixir/test_helper.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ defmodule PathHelpers do
5656
File.mkdir_p!(unquote(path))
5757
beam_path = Path.join(unquote(path), Atom.to_string(name) <> ".beam")
5858
File.write!(beam_path, bin)
59+
60+
:code.purge(name)
61+
:code.delete(name)
62+
5963
res
6064
end
6165

lib/iex/test/iex/helpers_test.exs

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -39,53 +39,53 @@ defmodule IEx.HelpersTest do
3939
end
4040

4141
test "sets up a breakpoint with capture syntax" do
42-
assert break!(URI.decode_query() / 2) == 1
43-
assert IEx.Pry.breaks() == [{1, URI, {:decode_query, 2}, 1}]
42+
assert break!(PryExampleModule.two() / 2) == 1
43+
assert IEx.Pry.breaks() == [{1, PryExampleModule, {:two, 2}, 1}]
4444
end
4545

4646
test "sets up a breakpoint with call syntax" do
47-
assert break!(URI.decode_query(_, %{})) == 1
48-
assert IEx.Pry.breaks() == [{1, URI, {:decode_query, 2}, 1}]
47+
assert break!(PryExampleModule.two(_, %{})) == 1
48+
assert IEx.Pry.breaks() == [{1, PryExampleModule, {:two, 2}, 1}]
4949
end
5050

5151
test "sets up a breakpoint with guards syntax" do
52-
assert break!(URI.decode_query(_, map) when is_map(map)) == 1
53-
assert IEx.Pry.breaks() == [{1, URI, {:decode_query, 2}, 1}]
52+
assert break!(PryExampleModule.two(_, map) when is_map(map)) == 1
53+
assert IEx.Pry.breaks() == [{1, PryExampleModule, {:two, 2}, 1}]
5454
end
5555

5656
test "sets up a breakpoint on the given module" do
57-
assert break!(URI, :decode_query, 2) == 1
58-
assert IEx.Pry.breaks() == [{1, URI, {:decode_query, 2}, 1}]
57+
assert break!(PryExampleModule, :two, 2) == 1
58+
assert IEx.Pry.breaks() == [{1, PryExampleModule, {:two, 2}, 1}]
5959
end
6060

6161
test "resets breaks on the given ID" do
62-
assert break!(URI, :decode_query, 2) == 1
62+
assert break!(PryExampleModule, :two, 2) == 1
6363
assert reset_break(1) == :ok
64-
assert IEx.Pry.breaks() == [{1, URI, {:decode_query, 2}, 0}]
64+
assert IEx.Pry.breaks() == [{1, PryExampleModule, {:two, 2}, 0}]
6565
end
6666

6767
test "resets breaks on the given module" do
68-
assert break!(URI, :decode_query, 2) == 1
69-
assert reset_break(URI, :decode_query, 2) == :ok
70-
assert IEx.Pry.breaks() == [{1, URI, {:decode_query, 2}, 0}]
68+
assert break!(PryExampleModule, :two, 2) == 1
69+
assert reset_break(PryExampleModule, :two, 2) == :ok
70+
assert IEx.Pry.breaks() == [{1, PryExampleModule, {:two, 2}, 0}]
7171
end
7272

7373
test "removes breaks in the given module" do
74-
assert break!(URI.decode_query() / 2) == 1
75-
assert remove_breaks(URI) == :ok
74+
assert break!(PryExampleModule.two() / 2) == 1
75+
assert remove_breaks(PryExampleModule) == :ok
7676
assert IEx.Pry.breaks() == []
7777
end
7878

7979
test "removes breaks on all modules" do
80-
assert break!(URI.decode_query() / 2) == 1
80+
assert break!(PryExampleModule.two() / 2) == 1
8181
assert remove_breaks() == :ok
8282
assert IEx.Pry.breaks() == []
8383
end
8484

8585
test "errors when setting up a breakpoint with invalid guard" do
8686
assert capture_io(:stderr, fn ->
8787
assert_raise CompileError, fn ->
88-
break!(URI.decode_query(_, map) when is_whatever(map))
88+
break!(PryExampleModule.two(_, map) when is_whatever(map))
8989
end
9090
end) =~ "cannot find or invoke local is_whatever/1"
9191
end
@@ -98,44 +98,44 @@ defmodule IEx.HelpersTest do
9898

9999
test "errors when setting up a break for unknown function" do
100100
assert_raise RuntimeError,
101-
"could not set breakpoint, unknown function/macro URI.unknown/2",
102-
fn -> break!(URI, :unknown, 2) end
101+
"could not set breakpoint, unknown function/macro #{inspect(PryExampleModule)}.unknown/2",
102+
fn -> break!(PryExampleModule, :unknown, 2) end
103103
end
104104

105105
test "errors for non-Elixir modules" do
106106
assert_raise RuntimeError,
107-
"could not set breakpoint, module :elixir was not written in Elixir",
108-
fn -> break!(:elixir, :unknown, 2) end
107+
"could not set breakpoint, module :maps was not written in Elixir",
108+
fn -> break!(:maps, :unknown, 2) end
109109
end
110110

111111
test "prints table with breaks" do
112-
break!(URI, :decode_query, 2)
112+
break!(PryExampleModule, :two, 2)
113113

114114
assert capture_io(fn -> breaks() end) == """
115115
116-
ID Module.function/arity Pending stops
117-
---- ----------------------- ---------------
118-
1 URI.decode_query/2 1
116+
ID Module.function/arity Pending stops
117+
---- ------------------------ ---------------
118+
1 PryExampleModule.two/2 1
119119
120120
"""
121121

122-
assert capture_io(fn -> URI.decode_query("foo=bar", %{}) end) != ""
122+
assert capture_io(fn -> PryExampleModule.two("foo=bar", %{}) end) != ""
123123

124124
assert capture_io(fn -> breaks() end) == """
125125
126-
ID Module.function/arity Pending stops
127-
---- ----------------------- ---------------
128-
1 URI.decode_query/2 0
126+
ID Module.function/arity Pending stops
127+
---- ------------------------ ---------------
128+
1 PryExampleModule.two/2 0
129129
130130
"""
131131

132-
assert capture_io(fn -> URI.decode_query("foo=bar", %{}) end) == ""
132+
assert capture_io(fn -> PryExampleModule.two("foo=bar", %{}) end) == ""
133133

134134
assert capture_io(fn -> breaks() end) == """
135135
136-
ID Module.function/arity Pending stops
137-
---- ----------------------- ---------------
138-
1 URI.decode_query/2 0
136+
ID Module.function/arity Pending stops
137+
---- ------------------------ ---------------
138+
1 PryExampleModule.two/2 0
139139
140140
"""
141141
end
@@ -152,6 +152,7 @@ defmodule IEx.HelpersTest do
152152
@lists_erl Application.app_dir(:stdlib, "src/lists.erl")
153153
@httpc_erl "src/http_client/httpc.erl"
154154
@editor System.get_env("ELIXIR_EDITOR")
155+
@example_module_path "lib/iex/test/test_helper.exs"
155156

156157
test "opens __FILE__ and __LINE__" do
157158
System.put_env("ELIXIR_EDITOR", "echo __LINE__:__FILE__")
@@ -163,7 +164,8 @@ defmodule IEx.HelpersTest do
163164
end
164165

165166
test "opens Elixir module" do
166-
assert capture_iex("open(IEx.Helpers)") |> maybe_trim_quotes() =~ ~r/#{@iex_helpers}:5$/
167+
assert capture_iex("open(HelperExampleModule)") |> maybe_trim_quotes() =~
168+
~r/#{@example_module_path}:\d+$/
167169
end
168170

169171
test "opens function" do
@@ -176,16 +178,19 @@ defmodule IEx.HelpersTest do
176178
end
177179

178180
test "opens module.function" do
179-
assert capture_iex("open(IEx.Helpers.b)") |> maybe_trim_quotes() =~ ~r/#{@iex_helpers}:\d+$/
180-
assert capture_iex("open(IEx.Helpers.h)") |> maybe_trim_quotes() =~ ~r/#{@iex_helpers}:\d+$/
181+
assert capture_iex("open(HelperExampleModule.fun)") |> maybe_trim_quotes() =~
182+
~r/#{@example_module_path}:\d+$/
183+
184+
assert capture_iex("open(HelperExampleModule.macro)") |> maybe_trim_quotes() =~
185+
~r/#{@example_module_path}:\d+$/
181186
end
182187

183188
test "opens module.function/arity" do
184-
assert capture_iex("open(IEx.Helpers.b/1)") |> maybe_trim_quotes() =~
185-
~r/#{@iex_helpers}:\d+$/
189+
assert capture_iex("open(HelperExampleModule.fun/1)") |> maybe_trim_quotes() =~
190+
~r/#{@example_module_path}:\d+$/
186191

187-
assert capture_iex("open(IEx.Helpers.h/0)") |> maybe_trim_quotes() =~
188-
~r/#{@iex_helpers}:\d+$/
192+
assert capture_iex("open(HelperExampleModule.macro/1)") |> maybe_trim_quotes() =~
193+
~r/#{@example_module_path}:\d+$/
189194
end
190195

191196
test "opens Erlang module" do
@@ -1440,11 +1445,13 @@ defmodule IEx.HelpersTest do
14401445
@tag :capture_log
14411446
test "loads a given module on the given nodes" do
14421447
assert nl([node()], :lists) == {:ok, [{:nonode@nohost, :error, :sticky_directory}]}
1443-
assert nl([node()], Enum) == {:ok, [{:nonode@nohost, :loaded, Enum}]}
1448+
1449+
assert nl([node()], HelperExampleModule) ==
1450+
{:ok, [{:nonode@nohost, :loaded, HelperExampleModule}]}
14441451

14451452
assert nl(:nonexistent_module) == {:error, :nofile}
14461453

1447-
assert nl([:nosuchnode@badhost], Enum) ==
1454+
assert nl([:nosuchnode@badhost], HelperExampleModule) ==
14481455
{:ok, [{:nosuchnode@badhost, :badrpc, :noconnection}]}
14491456
end
14501457
end

lib/iex/test/iex/interaction_test.exs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,30 @@ defmodule IEx.InteractionTest do
232232
end
233233

234234
test "blames function clause error" do
235+
import PathHelpers
236+
237+
write_beam(
238+
defmodule ExampleModule do
239+
def fun(:valid), do: :ok
240+
end
241+
)
242+
243+
content = capture_iex("IEx.InteractionTest.ExampleModule.fun(:invalid)")
244+
245+
assert content =~
246+
"** (FunctionClauseError) no function clause matching in IEx.InteractionTest.ExampleModule.fun/1"
247+
248+
assert content =~
249+
"The following arguments were given to IEx.InteractionTest.ExampleModule.fun/1"
250+
251+
assert content =~ ":invalid"
252+
assert content =~ "def fun(-:valid-)"
253+
254+
assert content =~
255+
~r"test/iex/interaction_test.exs:\d+: IEx\.InteractionTest\.ExampleModule\.fun/1"
256+
235257
content = capture_iex("Access.fetch(:foo, :bar)")
236-
assert content =~ "** (FunctionClauseError) no function clause matching in Access.fetch/2"
237-
assert content =~ "The following arguments were given to Access.fetch/2"
238-
assert content =~ ":foo"
239-
assert content =~ "def fetch(-%module{} = container-, key)"
258+
240259
assert content =~ ~r"\(elixir #{System.version()}\) lib/access\.ex:\d+: Access\.fetch/2"
241260
end
242261

0 commit comments

Comments
 (0)