patch icon indicating copy to clipboard operation
patch copied to clipboard

Patching local function does not work in a string interpolation

Open ollien opened this issue 8 months ago • 1 comments

👋

In my testing, it looks like patching does not work if a method is called from within a string interpolation expression. I took a look at the "limitations" section of the docs, and didn't see this mentioned, so I think it's a bug.

Module:

defmodule Patchtest do
  def greet() do
    "Hello, world!"
  end

  def format_greeting_1() do
    greeting = greet()
    "The program says: #{greeting}"
  end

  def format_greeting_2() do
    "The program says: #{greet()}"
  end
end

Test:

defmodule PatchtestTest do
  use ExUnit.Case
  use Patch
  doctest Patchtest

  test "greets the world" do
    patch(Patchtest, :greet, "Hello, patched!")

    assert Patchtest.format_greeting_1() == "The program says: Hello, patched!"
    assert Patchtest.format_greeting_2() == "The program says: Hello, patched!"
  end
end

This fails with:

  1) test greets the world (PatchtestTest)
     test/patchtest_test.exs:6
     Assertion with == failed
     code:  assert Patchtest.format_greeting_2() == "The program says: Hello, patched!"
     left:  "The program says: Hello, world!"
     right: "The program says: Hello, patched!"
     stacktrace:
       test/patchtest_test.exs:10: (test)

ollien avatar Feb 18 '25 21:02 ollien