Skip to content

assert fails with error on pattern matching with struct type #9958

@vheathen

Description

@vheathen

Environment

  • Elixir & Erlang/OTP versions:
    Erlang/OTP 22 [erts-10.7] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
    Elixir 1.10.2 (compiled with Erlang/OTP 22)

  • Operating system:
    Ubuntu 18.04 in Windows 10 WSL2

Current behavior

defmodule TestTest do
  use ExUnit.Case

  defmodule T do
    defstruct [:id, :attached]
  end

  setup do
    t = %T{id: 123, attached: false}

    [t: t]
  end

  test "fails without error", %{t: %{id: id} = t} do
    assert %{uuid: ^id, attached: true} = %T{
             attached: false,
             id: 321
           }
  end

  test "fails with error 1", %{t: %type{id: id}} do
    assert %{__struct__: ^type, uuid: ^id, attached: true} = %T{
             attached: false,
             id: 321
           }
  end

  test "fails with error  2", %{t: %type{id: id}} do
    assert %^type{uuid: ^id, attached: true} = %T{
             attached: false,
             id: 321
           }
  end

  test "fails with error 3", %{t: %{id: id}} do
    # type variable isn't pinned
    assert %_type{uuid: ^id, attached: true} = %T{
             attached: false,
             id: 321
           }
  end

end

"fails with error 1" gives:

** (EXIT from #PID<0.94.0>) exited in: GenServer.stop(#PID<0.362.0>, :normal, :infinity)
    ** (EXIT) exited in: :sys.terminate(#PID<0.362.0>, :normal, :infinity)
        ** (EXIT) an exception was raised:
            ** (FunctionClauseError) no function clause matching in Code.ensure_loaded?/1
                (elixir 1.10.2) lib/code.ex:1233: Code.ensure_loaded?({:^, [line: 22], [{:type, [line: 22], nil}]})
                (ex_unit 1.10.2) lib/ex_unit/diff.ex:677: ExUnit.Diff.load_struct/1
                (ex_unit 1.10.2) lib/ex_unit/diff.ex:630: ExUnit.Diff.diff_quoted_struct/4
                (elixir 1.10.2) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
                (elixir 1.10.2) lib/task/supervised.ex:35: Task.Supervised.reply/5
                (stdlib 3.12) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

"fails with error" 2 and 3 give an another error:

 ** (EXIT from #PID<0.94.0>) exited in: GenServer.stop(#PID<0.362.0>, :normal, :infinity)
    ** (EXIT) exited in: :sys.terminate(#PID<0.362.0>, :normal, :infinity)
        ** (EXIT) an exception was raised:
            ** (FunctionClauseError) no function clause matching in Code.Identifier.inspect_as_atom/1
                (elixir 1.10.2) lib/code/identifier.ex:135: Code.Identifier.inspect_as_atom({:type, [line: 36], nil})
                (ex_unit 1.10.2) lib/ex_unit/diff.ex:903: ExUnit.Diff.safe_to_algebra/2
                (ex_unit 1.10.2) lib/ex_unit/diff.ex:1006: ExUnit.Diff.wrap_on_diff/3
                (ex_unit 1.10.2) lib/ex_unit/formatter.ex:377: ExUnit.Formatter.format_sides/6
                (ex_unit 1.10.2) lib/ex_unit/formatter.ex:365: ExUnit.Formatter.format_context/4
                (ex_unit 1.10.2) lib/ex_unit/formatter.ex:158: ExUnit.Formatter.format_assertion_error/6
                (ex_unit 1.10.2) lib/ex_unit/formatter.ex:190: ExUnit.Formatter.format_kind_reason/6
                (ex_unit 1.10.2) lib/ex_unit/formatter.ex:130: anonymous fn/7 in ExUnit.Formatter.format_test_failure/5
                (elixir 1.10.2) lib/enum.ex:1495: anonymous fn/2 in Enum.map_join/3
                (elixir 1.10.2) lib/enum.ex:3359: Enum.map_intersperse_list/3
                (elixir 1.10.2) lib/enum.ex:1495: Enum.map_join/3
                (ex_unit 1.10.2) lib/ex_unit/formatter.ex:129: ExUnit.Formatter.format_test_failure/5
                (ex_unit 1.10.2) lib/ex_unit/cli_formatter.ex:99: ExUnit.CLIFormatter.handle_cast/2
                (stdlib 3.12) gen_server.erl:637: :gen_server.try_dispatch/4
                (stdlib 3.12) gen_server.erl:711: :gen_server.handle_msg/6
                (stdlib 3.12) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

Expected behavior

Tests should fail but without crashing the test suit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions