|
| 1 | +defmodule Hammox.TypeMatchErrorTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + alias Hammox.TypeMatchError |
| 5 | + |
| 6 | + describe "standard error" do |
| 7 | + test "reason" do |
| 8 | + error = TypeMatchError.exception({:error, reason()}) |
| 9 | + |
| 10 | + assert error.message == |
| 11 | + """ |
| 12 | +
|
| 13 | + Returned value {:ok, %{__struct__: Post, body: "post body", post_body: "nil"}} does not match type {:ok, Post.t()} | {:error, any()}. |
| 14 | +
|
| 15 | + Value {:ok, %{__struct__: Post, body: "post body", post_body: "nil"}} does not match type {:ok, Post.t()} | {:error, any()}. |
| 16 | +
|
| 17 | + 1st tuple element :ok does not match 1st element type :error. |
| 18 | +
|
| 19 | + Value :ok does not match type :error. |
| 20 | + """ |
| 21 | + |> String.replace_trailing("\n", "") |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + describe "pretty error" do |
| 26 | + setup do |
| 27 | + Application.put_env(:hammox, :pretty, true) |
| 28 | + |
| 29 | + on_exit(fn -> |
| 30 | + Application.delete_env(:hammox, :pretty) |
| 31 | + end) |
| 32 | + end |
| 33 | + |
| 34 | + test "reason" do |
| 35 | + error = TypeMatchError.exception({:error, reason()}) |
| 36 | + |
| 37 | + assert error.message == |
| 38 | + """ |
| 39 | +
|
| 40 | + Returned value {:ok, %{__struct__: Post, body: \"post body\", post_body: \"nil\"}} does not match type |
| 41 | + {:ok, Post.t()} |
| 42 | + | {:error, any()}. |
| 43 | +
|
| 44 | + Value {:ok, %{__struct__: Post, body: \"post body\", post_body: \"nil\"}} does not match type |
| 45 | + {:ok, Post.t()} |
| 46 | + | {:error, any()}. |
| 47 | +
|
| 48 | + 1st tuple element :ok does not match 1st element type |
| 49 | + :error. |
| 50 | +
|
| 51 | + Value :ok does not match type |
| 52 | + :error. |
| 53 | + """ |
| 54 | + |> String.replace_trailing("\n", "") |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + defp reason do |
| 59 | + [ |
| 60 | + {:return_type_mismatch, |
| 61 | + {:ok, |
| 62 | + %{ |
| 63 | + __struct__: Post, |
| 64 | + body: "post body", |
| 65 | + post_body: "nil" |
| 66 | + }}, |
| 67 | + {:type, 49, :union, |
| 68 | + [ |
| 69 | + {:type, 0, :tuple, |
| 70 | + [ |
| 71 | + {:atom, 0, :ok}, |
| 72 | + {:remote_type, 49, [{:atom, 0, Post}, {:atom, 0, :t}, []]} |
| 73 | + ]}, |
| 74 | + {:type, 0, :tuple, [{:atom, 0, :error}, {:type, 49, :any, []}]} |
| 75 | + ]}}, |
| 76 | + {:type_mismatch, |
| 77 | + {:ok, |
| 78 | + %{ |
| 79 | + __struct__: Post, |
| 80 | + body: "post body", |
| 81 | + post_body: "nil" |
| 82 | + }}, |
| 83 | + {:type, 49, :union, |
| 84 | + [ |
| 85 | + {:type, 0, :tuple, |
| 86 | + [ |
| 87 | + {:atom, 0, :ok}, |
| 88 | + {:remote_type, 49, [{:atom, 0, Post}, {:atom, 0, :t}, []]} |
| 89 | + ]}, |
| 90 | + {:type, 0, :tuple, [{:atom, 0, :error}, {:type, 49, :any, []}]} |
| 91 | + ]}}, |
| 92 | + {:tuple_elem_type_mismatch, 0, :ok, {:atom, 0, :error}}, |
| 93 | + {:type_mismatch, :ok, {:atom, 0, :error}} |
| 94 | + ] |
| 95 | + end |
| 96 | +end |
0 commit comments