Raise if message in AssertionError is not a binary

Closes #14695.
This commit is contained in:
José Valim
2025-08-31 10:57:43 +02:00
parent 1fadefe25f
commit 71bb017b61
2 changed files with 26 additions and 0 deletions
+10
View File
@@ -38,6 +38,16 @@ defmodule ExUnit.AssertionError do
@no_value
end
@impl true
def exception(opts) do
with {:ok, message} when not is_binary(message) <- Keyword.fetch(opts, :message) do
raise ArgumentError,
":message in ExUnit.Assertion must be a binary, got: #{inspect(message)}"
end
struct(__MODULE__, opts)
end
@impl true
def message(exception) do
"\n\n" <> ExUnit.Formatter.format_assertion_error(exception)
@@ -857,6 +857,22 @@ defmodule ExUnit.AssertionsTest do
"assertion" = error.message
end
test "assert operator with custom options" do
assert 1 > 2, message: "assertion"
flunk("This should never be tested")
rescue
error in [ExUnit.AssertionError] ->
"assertion" = error.message
end
test "assert operator with invalid options" do
assert 1 > 2, message: 123
flunk("This should never be tested")
rescue
error in [ArgumentError] ->
":message in ExUnit.Assertion must be a binary, got: 123" = error.message
end
test "assert lack of equality" do
try do
assert "one" != "one"