Skip to content

Commit

Permalink
Fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Nov 6, 2024
1 parent d68726a commit 4a378f0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 74 deletions.
16 changes: 7 additions & 9 deletions lib/elixir/test/elixir/kernel/raise_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ defmodule Kernel.RaiseTest do
test "function clause error" do
result =
try do
zero(1)
Access.get(:ok, :error)
rescue
x in [FunctionClauseError] -> Exception.message(x)
end

assert result == "no function clause matching in Kernel.RaiseTest.zero/1"
assert result == "no function clause matching in Access.get/3"
end

test "badarg error" do
Expand Down Expand Up @@ -450,7 +450,7 @@ defmodule Kernel.RaiseTest do

result =
try do
^x = zero(0)
^x = Process.get(:unused, 0)
rescue
x in [MatchError] -> Exception.message(x)
end
Expand Down Expand Up @@ -483,7 +483,7 @@ defmodule Kernel.RaiseTest do
test "bad map error" do
result =
try do
%{zero(0) | foo: :bar}
%{Process.get(:unused, 0) | foo: :bar}
rescue
x in [BadMapError] -> Exception.message(x)
end
Expand All @@ -494,7 +494,7 @@ defmodule Kernel.RaiseTest do
test "bad boolean error" do
result =
try do
1 and true
Process.get(:unused, 1) and true
rescue
x in [BadBooleanError] -> Exception.message(x)
end
Expand All @@ -507,7 +507,7 @@ defmodule Kernel.RaiseTest do

result =
try do
case zero(0) do
case Process.get(:unused, 0) do
^x -> nil
end
rescue
Expand All @@ -521,7 +521,7 @@ defmodule Kernel.RaiseTest do
result =
try do
cond do
!zero(0) -> :ok
!Process.get(:unused, 0) -> :ok
end
rescue
x in [CondClauseError] -> Exception.message(x)
Expand Down Expand Up @@ -581,6 +581,4 @@ defmodule Kernel.RaiseTest do
"function DoNotExist.for_sure/0 is undefined (module DoNotExist is not available). " <>
"Make sure the module name is correct and has been specified in full (or that an alias has been defined)"
end

defp zero(0), do: 0
end
137 changes: 75 additions & 62 deletions lib/elixir/test/elixir/kernel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ defmodule KernelTest do
use ExUnit.Case, async: true

# Skip these doctests are they emit warnings
doctest Kernel, except: [===: 2, !==: 2, is_nil: 1]
doctest Kernel,
except:
[===: 2, !==: 2, and: 2, or: 2] ++
[is_exception: 1, is_exception: 2, is_nil: 1, is_struct: 1, is_non_struct_map: 1]

def id(arg), do: arg
def id(arg1, arg2), do: {arg1, arg2}
Expand Down Expand Up @@ -298,8 +301,8 @@ defmodule KernelTest do
assert (false and true) == false
assert (false and 0) == false
assert (false and raise("oops")) == false
assert ((x = true) and not x) == false
assert_raise BadBooleanError, fn -> 0 and 1 end
assert ((x = Process.get(:unused, true)) and not x) == false
assert_raise BadBooleanError, fn -> Process.get(:unused, 0) and 1 end
end

test "or/2" do
Expand All @@ -310,25 +313,27 @@ defmodule KernelTest do
assert (false or false) == false
assert (false or true) == true
assert (false or 0) == 0
assert ((x = false) or not x) == true
assert_raise BadBooleanError, fn -> 0 or 1 end
assert ((x = Process.get(:unused, false)) or not x) == true
assert_raise BadBooleanError, fn -> Process.get(:unused, 0) or 1 end
end

defp struct?(arg) when is_struct(arg), do: true
defp struct?(_arg), do: false
defp delegate_is_struct(arg), do: is_struct(arg)

defp guarded_is_struct(arg) when is_struct(arg), do: true
defp guarded_is_struct(_arg), do: false

defp struct_or_map?(arg) when is_struct(arg) or is_map(arg), do: true
defp struct_or_map?(_arg), do: false

test "is_struct/1" do
assert is_struct(%{}) == false
assert is_struct([]) == false
assert is_struct(%Macro.Env{}) == true
assert is_struct(%{__struct__: "foo"}) == false
assert struct?(%Macro.Env{}) == true
assert struct?(%{__struct__: "foo"}) == false
assert struct?([]) == false
assert struct?(%{}) == false
assert delegate_is_struct(%{}) == false
assert delegate_is_struct([]) == false
assert delegate_is_struct(%Macro.Env{}) == true
assert delegate_is_struct(%{__struct__: "foo"}) == false
assert guarded_is_struct(%Macro.Env{}) == true
assert guarded_is_struct(%{__struct__: "foo"}) == false
assert guarded_is_struct([]) == false
assert guarded_is_struct(%{}) == false
end

test "is_struct/1 and other match works" do
Expand All @@ -337,25 +342,27 @@ defmodule KernelTest do
assert struct_or_map?(10) == false
end

defp struct?(arg, name) when is_struct(arg, name), do: true
defp struct?(_arg, _name), do: false
defp delegate_is_struct(arg, name), do: is_struct(arg, name)

defp guarded_is_struct(arg, name) when is_struct(arg, name), do: true
defp guarded_is_struct(_arg, _name), do: false

defp struct_or_map?(arg, name) when is_struct(arg, name) or is_map(arg), do: true
defp struct_or_map?(_arg, _name), do: false

defp not_atom(), do: "not atom"

test "is_struct/2" do
assert is_struct(%{}, Macro.Env) == false
assert is_struct([], Macro.Env) == false
assert is_struct(%Macro.Env{}, Macro.Env) == true
assert is_struct(%Macro.Env{}, URI) == false
assert struct?(%Macro.Env{}, Macro.Env) == true
assert struct?(%Macro.Env{}, URI) == false
assert struct?(%{__struct__: "foo"}, "foo") == false
assert struct?(%{__struct__: "foo"}, Macro.Env) == false
assert struct?([], Macro.Env) == false
assert struct?(%{}, Macro.Env) == false
assert delegate_is_struct(%{}, Macro.Env) == false
assert delegate_is_struct([], Macro.Env) == false
assert delegate_is_struct(%Macro.Env{}, Macro.Env) == true
assert delegate_is_struct(%Macro.Env{}, URI) == false
assert guarded_is_struct(%Macro.Env{}, Macro.Env) == true
assert guarded_is_struct(%Macro.Env{}, URI) == false
assert guarded_is_struct(%{__struct__: "foo"}, "foo") == false
assert guarded_is_struct(%{__struct__: "foo"}, Macro.Env) == false
assert guarded_is_struct([], Macro.Env) == false
assert guarded_is_struct(%{}, Macro.Env) == false

assert_raise ArgumentError, "argument error", fn ->
is_struct(%{}, not_atom())
Expand All @@ -368,21 +375,23 @@ defmodule KernelTest do
assert struct_or_map?(%Macro.Env{}, Macro.Env) == true
end

defp non_struct_map?(arg) when is_non_struct_map(arg), do: true
defp non_struct_map?(_arg), do: false
defp delegate_is_non_struct_map(arg), do: is_non_struct_map(arg)

defp guarded_is_non_struct_map(arg) when is_non_struct_map(arg), do: true
defp guarded_is_non_struct_map(_arg), do: false

defp non_struct_map_or_struct?(arg) when is_non_struct_map(arg) or is_struct(arg), do: true
defp non_struct_map_or_struct?(_arg), do: false

test "is_non_struct_map/1" do
assert is_non_struct_map(%{}) == true
assert is_non_struct_map([]) == false
assert is_non_struct_map(%Macro.Env{}) == false
assert is_non_struct_map(%{__struct__: "foo"}) == true
assert non_struct_map?(%Macro.Env{}) == false
assert non_struct_map?(%{__struct__: "foo"}) == true
assert non_struct_map?([]) == false
assert non_struct_map?(%{}) == true
assert delegate_is_non_struct_map(%{}) == true
assert delegate_is_non_struct_map([]) == false
assert delegate_is_non_struct_map(%Macro.Env{}) == false
assert delegate_is_non_struct_map(%{__struct__: "foo"}) == true
assert guarded_is_non_struct_map(%Macro.Env{}) == false
assert guarded_is_non_struct_map(%{__struct__: "foo"}) == true
assert guarded_is_non_struct_map([]) == false
assert guarded_is_non_struct_map(%{}) == true
end

test "is_non_struct_map/1 and other match works" do
Expand All @@ -391,21 +400,23 @@ defmodule KernelTest do
assert non_struct_map_or_struct?(10) == false
end

defp exception?(arg) when is_exception(arg), do: true
defp exception?(_arg), do: false
defp delegate_is_exception(arg), do: is_exception(arg)

defp guarded_is_exception(arg) when is_exception(arg), do: true
defp guarded_is_exception(_arg), do: false

defp exception_or_map?(arg) when is_exception(arg) or is_map(arg), do: true
defp exception_or_map?(_arg), do: false

test "is_exception/1" do
assert is_exception(%{}) == false
assert is_exception([]) == false
assert is_exception(%RuntimeError{}) == true
assert is_exception(%{__exception__: "foo"}) == false
assert exception?(%RuntimeError{}) == true
assert exception?(%{__exception__: "foo"}) == false
assert exception?([]) == false
assert exception?(%{}) == false
assert delegate_is_exception(%{}) == false
assert delegate_is_exception([]) == false
assert delegate_is_exception(%RuntimeError{}) == true
assert delegate_is_exception(%{__exception__: "foo"}) == false
assert guarded_is_exception(%RuntimeError{}) == true
assert guarded_is_exception(%{__exception__: "foo"}) == false
assert guarded_is_exception([]) == false
assert guarded_is_exception(%{}) == false
end

test "is_exception/1 and other match works" do
Expand All @@ -414,26 +425,28 @@ defmodule KernelTest do
assert exception_or_map?(10) == false
end

defp exception?(arg, name) when is_exception(arg, name), do: true
defp exception?(_arg, _name), do: false
defp delegate_is_exception(arg, name), do: is_exception(arg, name)

defp guarded_is_exception(arg, name) when is_exception(arg, name), do: true
defp guarded_is_exception(_arg, _name), do: false

defp exception_or_map?(arg, name) when is_exception(arg, name) or is_map(arg), do: true
defp exception_or_map?(_arg, _name), do: false

test "is_exception/2" do
assert is_exception(%{}, RuntimeError) == false
assert is_exception([], RuntimeError) == false
assert is_exception(%RuntimeError{}, RuntimeError) == true
assert is_exception(%RuntimeError{}, Macro.Env) == false
assert exception?(%RuntimeError{}, RuntimeError) == true
assert exception?(%RuntimeError{}, Macro.Env) == false
assert exception?(%{__exception__: "foo"}, "foo") == false
assert exception?(%{__exception__: "foo"}, RuntimeError) == false
assert exception?([], RuntimeError) == false
assert exception?(%{}, RuntimeError) == false
assert delegate_is_exception(%{}, RuntimeError) == false
assert delegate_is_exception([], RuntimeError) == false
assert delegate_is_exception(%RuntimeError{}, RuntimeError) == true
assert delegate_is_exception(%RuntimeError{}, Macro.Env) == false
assert guarded_is_exception(%RuntimeError{}, RuntimeError) == true
assert guarded_is_exception(%RuntimeError{}, Macro.Env) == false
assert guarded_is_exception(%{__exception__: "foo"}, "foo") == false
assert guarded_is_exception(%{__exception__: "foo"}, RuntimeError) == false
assert guarded_is_exception([], RuntimeError) == false
assert guarded_is_exception(%{}, RuntimeError) == false

assert_raise ArgumentError, "argument error", fn ->
is_exception(%{}, not_atom())
delegate_is_exception(%{}, not_atom())
end
end

Expand Down Expand Up @@ -954,15 +967,15 @@ defmodule KernelTest do
end

test "get_in/1" do
users = %{"john" => %{age: 27}, :meg => %{age: 23}}
users = Process.get(:unused, %{"john" => %{age: 27}, :meg => %{age: 23}})
assert get_in(users["john"][:age]) == 27
assert get_in(users["dave"][:age]) == nil
assert get_in(users["john"].age) == 27
assert get_in(users["dave"].age) == nil
assert get_in(users.meg[:age]) == 23
assert get_in(users.meg.age) == 23

is_nil = nil
is_nil = Process.get(:unused, nil)
assert get_in(is_nil.age) == nil

assert_raise KeyError, ~r"key :unknown not found", fn -> get_in(users.unknown) end
Expand Down
6 changes: 3 additions & 3 deletions lib/elixir/test/elixir/macro_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ defmodule MacroTest do
end

defp expand_once_and_clean(quoted, env) do
cleaner = &Keyword.drop(&1, [:counter])
cleaner = &Keyword.drop(&1, [:counter, :type_check])

quoted
|> Macro.expand_once(env)
Expand Down Expand Up @@ -276,7 +276,7 @@ defmodule MacroTest do
end

defp expand_and_clean(quoted, env) do
cleaner = &Keyword.drop(&1, [:counter])
cleaner = &Keyword.drop(&1, [:counter, :type_check])

quoted
|> Macro.expand(env)
Expand Down Expand Up @@ -490,7 +490,7 @@ defmodule MacroTest do
end

test "with case" do
list = [1, 2, 3]
list = List.flatten([1, 2, 3])

{result, formatted} =
dbg_format(
Expand Down

0 comments on commit 4a378f0

Please sign in to comment.