elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24k stars 3.33k forks source link

ExUnit's assert_receive passes in cases where it should fail (related to using variables in pattern matching) #1724

Closed knewter closed 10 years ago

knewter commented 10 years ago

I've whittled this down to a tiny test case showing the incorrect behaviour.

The general problem is that one of these two tests fail, when both should:

defmodule Pinger do
  def start do
    await()
  end

  def await do
    receive do
      {:ping, pid} -> pong(pid)
      await
    end
  end

  defp pong(pid) do
    pid <- {:pong, 2}
  end
end

defmodule AssertReceiveBugTest do
  use ExUnit.Case

  test "assert_receive doesn't fail properly when passing a bound variable in as part of a pattern" do
    pinger = spawn_link(Pinger, :start, [])
    pinger <- {:ping, self()}
    expected = 1
    assert_receive({:pong, expected})
  end

  test "assert_receive fails fine if you don't pass a bound variable though..." do
    pinger = spawn_link(Pinger, :start, [])
    pinger <- {:ping, self()}
    assert_receive({:pong, 1})
  end
end
alco commented 10 years ago

assert_receive({:pong, ^expected}) will fail as expected.

josevalim commented 10 years ago

Exactly. assert_receive, assert_received and assert left = right all expect a pattern, so you should pass a pattern. Maybe we should change the name somehow of the first two to make it explicit? Suggestions?

knewter commented 10 years ago

Thanks for this. It feels odd and I'm still new to elixir. Maybe some documentation to help with other newbs? I'd be glad to write it, if you think it's sensible. On Sep 25, 2013 3:01 AM, "Alexei Sholik" notifications@github.com wrote:

assert_receive({:pong, ^expected}) will fail as expected.

— Reply to this email directly or view it on GitHubhttps://github.com/elixir-lang/elixir/issues/1724#issuecomment-25068197 .

josevalim commented 10 years ago

@alco already sent a patch to improve the docs. If you would like to extend it further, another pull request is definitely welcome!

knewter commented 10 years ago

was that in elixir or elixir-lang.github.com?

On Wed, Sep 25, 2013 at 8:47 AM, José Valim notifications@github.comwrote:

@alco https://github.com/alco already sent a patch to improve the docs. If you would like to extend it further, another pull request is definitely welcome!

— Reply to this email directly or view it on GitHubhttps://github.com/elixir-lang/elixir/issues/1724#issuecomment-25086904 .

Josh Adams CTO | isotope|eleven http://www.isotope11.com cell 215-3957 work 476-8671 x201

josevalim commented 10 years ago

Elixir, it is linked here in this github issue. :)

knewter commented 10 years ago

Derp, didn't see that for some reason. Thanks for the hand-holding :)

On Wed, Sep 25, 2013 at 9:00 AM, José Valim notifications@github.comwrote:

Elixir, it is linked here in this github issue. :)

— Reply to this email directly or view it on GitHubhttps://github.com/elixir-lang/elixir/issues/1724#issuecomment-25087897 .

Josh Adams CTO | isotope|eleven http://www.isotope11.com cell 215-3957 work 476-8671 x201