Skip to content

Commit

Permalink
Use Html.raw/1 for more errors
Browse files Browse the repository at this point in the history
We use `Html.raw/1` for formatting more errors. The output is much nicer
and easier to understand at a glance.

We also extract the `ignore_whitespace/1` helper that was introduced
into a `TestHelpers` module.
  • Loading branch information
germsvel committed Feb 11, 2024
1 parent 4c39920 commit 82e7415
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 36 deletions.
8 changes: 3 additions & 5 deletions lib/phoenix_test/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,16 @@ defmodule PhoenixTest.Query do
end

defp format_potential_matches(elements) do
Enum.map_join(elements, "\n", fn
{tag, _, [content]} -> "<#{tag}> tag with content #{inspect(content)}"
end)
Enum.map_join(elements, "\n", &Html.raw/1)
end

defp format_find_one_of_elements_for_error(selectors_and_text) do
Enum.map_join(selectors_and_text, "\n", fn
{selector, text} ->
"Selector #{inspect(selector)} with content #{inspect(text)}"
"- #{inspect(selector)} with content #{inspect(text)}"

selector ->
"Selector #{inspect(selector)}"
"- #{inspect(selector)}"
end)
end
end
11 changes: 7 additions & 4 deletions lib/phoenix_test/static.ex
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Static do

defp verify_field_presence([], expected_field) do
raise ArgumentError, """
Expected form to have #{inspect(expected_field)} form field, but found no
existing fields.
Expected form to have #{inspect(expected_field)} form field, but found none.
"""
end

Expand All @@ -176,12 +175,16 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Static do
Found the following fields:
- #{Enum.map_join(existing_fields, "\n - ", &format_field_error/1)}
#{format_existing_fields_errors(existing_fields)}
"""
end
end

defp format_existing_fields_errors(fields) do
Enum.map_join(fields, "\n", &format_field_error/1)
end

defp format_field_error(field) do
"#{field["type"]} with name=#{inspect(field["name"])}"
Html.raw({field["type"], [{"name", field["name"]}], []})
end
end
13 changes: 2 additions & 11 deletions test/phoenix_test/assertions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule PhoenixTest.AssertionsTest do
use ExUnit.Case, async: true

import PhoenixTest
import PhoenixTest.TestHelpers

alias ExUnit.AssertionError

setup do
Expand Down Expand Up @@ -185,15 +187,4 @@ defmodule PhoenixTest.AssertionsTest do
end
end
end

# converts a multi-line string into a whitespace-forgiving regex
defp ignore_whitespace(string) do
string
|> String.split("\n")
|> Enum.map(&String.trim/1)
|> Enum.reject(fn s -> s == "" end)
|> Enum.map(fn s -> "\\s*" <> s <> "\\s*" end)
|> Enum.join("\n")
|> Regex.compile!([:dotall])
end
end
31 changes: 20 additions & 11 deletions test/phoenix_test/query_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule PhoenixTest.QueryTest do
use ExUnit.Case, async: true

import PhoenixTest.TestHelpers

alias PhoenixTest.Query

describe "find!/2" do
Expand Down Expand Up @@ -216,8 +218,8 @@ defmodule PhoenixTest.QueryTest do
I was looking for an element with one of these selectors:
Selector "h2" with content "Hi"
Selector "h3"
- "h2" with content "Hi"
- "h3"
"""

assert_raise ArgumentError, msg, fn ->
Expand All @@ -231,19 +233,26 @@ defmodule PhoenixTest.QueryTest do
<h2>Greetings</h2>
"""

msg = """
Could not find an element with given selectors.
msg =
"""
Could not find an element with given selectors.
I was looking for an element with one of these selectors:
I was looking for an element with one of these selectors:
Selector "h2" with content "Hi"
Selector "h3"
- "h2" with content "Hi"
- "h3"
I found some elements that match the selector but not the content:
I found some elements that match the selector but not the content:
<h2> tag with content "Hello"
<h2> tag with content "Greetings"
"""
<h2>
Hello
</h2>
<h2>
Greetings
</h2>
"""
|> ignore_whitespace()

assert_raise ArgumentError, msg, fn ->
Query.find_one_of!(html, [{"h2", "Hi"}, "h3"])
Expand Down
11 changes: 6 additions & 5 deletions test/phoenix_test/static_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,14 @@ defmodule PhoenixTest.StaticTest do
end

test "raises an error when form input cannot be found", %{conn: conn} do
message = """
Expected form to have "location[user][name]" form field, but found none.
message =
"""
Expected form to have "location[user][name]" form field, but found none.
Found the following fields:
Found the following fields:
- input with name="user[name]"
"""
<input name="user[name]"/>\n
"""

assert_raise ArgumentError, message, fn ->
conn
Expand Down
16 changes: 16 additions & 0 deletions test/support/test_helpers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule PhoenixTest.TestHelpers do
@moduledoc false

@doc """
Converts a multi-line string into a whitespace-forgiving regex
"""
def ignore_whitespace(string) do
string
|> String.split("\n")
|> Enum.map(&String.trim/1)
|> Enum.reject(fn s -> s == "" end)
|> Enum.map(fn s -> "\\s*" <> s <> "\\s*" end)
|> Enum.join("\n")
|> Regex.compile!([:dotall])
end
end

0 comments on commit 82e7415

Please sign in to comment.