Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lib/ex_unit/lib/ex_unit/assertions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,24 @@ defmodule ExUnit.Assertions do

@operator [:==, :<, :>, :<=, :>=, :===, :=~, :!==, :!=, :in]

@type_guards [
is_atom: "an atom",
is_binary: "a binary",
is_bitstring: "a bitstring",
is_boolean: "a boolean",
is_float: "a float",
is_function: "a function",
is_integer: "an integer",
is_list: "a list",
is_map: "a map",
is_nil: "nil",
is_number: "a number",
is_pid: "a PID",
is_port: "a port",
is_reference: "a reference",
is_tuple: "a tuple"
]

defp translate_assertion(:assert, {operator, meta, [_, _]} = expr, caller)
when operator in @operator do
if match?([{_, Kernel}], Macro.Env.lookup_import(caller, {operator, 2})) do
Expand All @@ -293,6 +311,20 @@ defmodule ExUnit.Assertions do
end
end

defp translate_assertion(:assert, {guard, _meta, [_]} = expr, caller)
when guard in unquote(Keyword.keys(@type_guards)) do
if match?([{_, Kernel}], Macro.Env.lookup_import(caller, {guard, 1})) do
translate_bool_guard(:assert, expr, "Assertion with #{guard}/1 failed", caller)
end
end

defp translate_assertion(:refute, {guard, _meta, [_]} = expr, caller)
when guard in unquote(Keyword.keys(@type_guards)) do
if match?([{_, Kernel}], Macro.Env.lookup_import(caller, {guard, 1})) do
translate_bool_guard(:refute, expr, "Refute with #{guard}/1 failed", caller)
end
end

defp translate_assertion(_kind, _expected, _caller) do
nil
end
Expand Down Expand Up @@ -343,6 +375,32 @@ defmodule ExUnit.Assertions do
end
end

defp translate_bool_guard(kind, {guard, meta, [arg]} = expr, message, _caller) do
expr = escape_quoted(kind, meta, expr)
call = {guard, meta, [arg]}
call = if kind == :assert, do: call, else: {:not, meta, [call]}

human_type =
case kind do
:assert -> "not #{type_guard_to_human_readable(guard)}"
:refute -> type_guard_to_human_readable(guard)
end

quote do
arg = unquote(arg)

ExUnit.Assertions.assert(unquote(call),
left: arg,
expr: unquote(expr),
message: unquote(message) <> ", the given expression is #{unquote(human_type)}"
)
end
end

for {guard_name, human_name} <- @type_guards do
defp type_guard_to_human_readable(unquote(guard_name)), do: unquote(human_name)
end

@doc false
def __equal__?(left, right) do
left === right
Expand Down
Loading