Skip to content

Commit

Permalink
Avoid reusing modules across tests, see #13774
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Aug 10, 2024
1 parent 1b4ec98 commit 02a1f07
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/elixir/test/elixir/module/types/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,8 @@ defmodule Module.Types.IntegrationTest do

Map.new(modules, fn module ->
{^module, binary, _filename} = :code.get_object_code(module)
:code.purge(module)
:code.delete(module)
:code.purge(module)
{module, binary}
end)
end
Expand Down
29 changes: 12 additions & 17 deletions lib/elixir/test/elixir/typespec_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,32 +1283,32 @@ defmodule TypespecTest do
end

test "spec_to_quoted with maps with __struct__ key" do
defmodule A do
defmodule StructA do
defstruct [:key]
end

defmodule B do
defmodule StructB do
defstruct [:key]
end

bytecode =
test_module do
@spec single_struct(%A{}) :: :ok
@spec single_struct(%StructA{}) :: :ok
def single_struct(arg), do: {:ok, arg}

@spec single_struct_key(%{__struct__: A}) :: :ok
@spec single_struct_key(%{__struct__: StructA}) :: :ok
def single_struct_key(arg), do: {:ok, arg}

@spec single_struct_key_type(%{__struct__: atom()}) :: :ok
def single_struct_key_type(arg), do: {:ok, arg}

@spec union_struct(%A{} | %B{}) :: :ok
@spec union_struct(%StructA{} | %StructB{}) :: :ok
def union_struct(arg), do: {:ok, arg}

@spec union_struct_key(%{__struct__: A | B}) :: :ok
@spec union_struct_key(%{__struct__: StructA | StructB}) :: :ok
def union_struct_key(arg), do: {:ok, arg}

@spec union_struct_key_type(%{__struct__: atom() | A | binary()}) :: :ok
@spec union_struct_key_type(%{__struct__: atom() | StructA | binary()}) :: :ok
def union_struct_key_type(arg), do: {:ok, arg}
end

Expand All @@ -1323,31 +1323,26 @@ defmodule TypespecTest do

assert Code.Typespec.spec_to_quoted(:single_struct, ast_single_struct)
|> Macro.to_string() ==
"single_struct(%TypespecTest.A{key: term()}) :: :ok"
"single_struct(%TypespecTest.StructA{key: term()}) :: :ok"

assert Code.Typespec.spec_to_quoted(:single_struct_key, ast_single_struct_key)
|> Macro.to_string() ==
"single_struct_key(%TypespecTest.A{}) :: :ok"
"single_struct_key(%TypespecTest.StructA{}) :: :ok"

assert Code.Typespec.spec_to_quoted(:single_struct_key_type, ast_single_struct_key_type)
|> Macro.to_string() ==
"single_struct_key_type(%{__struct__: atom()}) :: :ok"

assert Code.Typespec.spec_to_quoted(:union_struct, ast_union_struct) |> Macro.to_string() ==
"union_struct(%TypespecTest.A{key: term()} | %TypespecTest.B{key: term()}) :: :ok"
"union_struct(%TypespecTest.StructA{key: term()} | %TypespecTest.StructB{key: term()}) :: :ok"

assert Code.Typespec.spec_to_quoted(:union_struct_key, ast_union_struct_key)
|> Macro.to_string() ==
"union_struct_key(%{__struct__: TypespecTest.A | TypespecTest.B}) :: :ok"
"union_struct_key(%{__struct__: TypespecTest.StructA | TypespecTest.StructB}) :: :ok"

assert Code.Typespec.spec_to_quoted(:union_struct_key_type, ast_union_struct_key_type)
|> Macro.to_string() ==
"union_struct_key_type(%{__struct__: atom() | TypespecTest.A | binary()}) :: :ok"
after
for mod <- [A, B] do
:code.purge(mod)
:code.delete(mod)
end
"union_struct_key_type(%{__struct__: atom() | TypespecTest.StructA | binary()}) :: :ok"
end

test "non-variables are given as arguments" do
Expand Down

0 comments on commit 02a1f07

Please sign in to comment.