### Mocking a function with Repatch and ExUnit Source: https://github.com/hissssst/repatch/blob/main/README.md This example demonstrates basic usage of Repatch within an ExUnit test. It shows how to use Repatch.patch/3 to override a function's behavior (e.g., MapSet.new) and then verify if the patched function was called using Repatch.called?/3. ```elixir defmodule ThatsATest do use ExUnit.Case, async: true use Repatch.ExUnit test "that's not a MapSet.new" do Repatch.patch(MapSet, :new, fn _list -> %{thats_not: :a_map_set} end) assert MapSet.new([1, 2, 3]) == %{thats_not: :a_map_set} assert Repatch.called?(MapSet, :new, 1) end end ``` -------------------------------- ### Elixir: Demonstrating Shared Patch Isolation with Repatch.allow/2 Source: https://github.com/hissssst/repatch/blob/main/pages/modes.md This Elixir example illustrates the 'shared' isolation mode in Repatch. It shows how a patch applied in the current process is initially not visible to another process (Server.call), but becomes accessible after explicitly allowing the server process using Repatch.allow/2. It highlights the selective sharing of patches and the interaction with process allowances. ```Elixir pid = Server.start() # Everything works as expected without patch assert %MapSet{} = Server.call(pid, MapSet, :new, []) assert %MapSet{} = MapSet.new() # Now we apply patch and prove that only current process sees it Repatch.patch(MapSet, :new, [mode: :shared], fn -> :hello end) assert %MapSet{} = Server.call(pid, MapSet, :new, []) assert :hello = MapSet.new() # And now we allow the server to have access to this patch and we see # that it resolves to the same patch as the current process Repatch.allow(self(), pid) assert :hello = Server.call(pid, MapSet, :new, []) assert :hello = MapSet.new() ``` -------------------------------- ### Add Repatch to Elixir project dependencies Source: https://github.com/hissssst/repatch/blob/main/README.md This snippet shows how to include Repatch as a dependency in your Elixir project's mix.exs file. Add the :repatch entry with the desired version to your deps list. ```elixir def deps do [ {:repatch, "~> 1.5"} ] end ``` -------------------------------- ### Manually Tracking Patched Function Calls in Elixir Source: https://github.com/hissssst/repatch/blob/main/pages/performance_tips.md This Elixir code demonstrates how to manually track calls to a patched function when automatic history tracking is disabled. It uses an `Agent` to store call details for `MapSet.new/1` after it's patched, allowing for custom assertion against the recorded history. ```elixir history_agent = Agent.start_link(fn -> [] end) Repatch.patch(MapSet, :new, fn list -> result = Repatch.super(MapSet.new(list)) Agent.update(history_agent, fn h -> [{MapSet, :new, [list], result} | h] end) end) MapSet.new([1]) MapSet.new([1, 2]) history = Agent.get(history_agent, &Function.identity/1) assert Enum.any?(history, &match?({MapSet, :new, [1 | _], %MapSet{}}, &1)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.