From 79a2363d05139f26aa2d753149b7502c05f28eb0 Mon Sep 17 00:00:00 2001 From: Yuri Pereira Constante Date: Sun, 23 Jun 2024 10:31:18 -0300 Subject: [PATCH] Move add expectation error handling --- lib/mox.ex | 64 +++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/lib/mox.ex b/lib/mox.ex index 427e9dd..e81e964 100644 --- a/lib/mox.ex +++ b/lib/mox.ex @@ -685,11 +685,8 @@ defmodule Mox do :ok -> :ok - {:error, error} when is_binary(error) -> - raise ArgumentError, error - {:error, error} -> - raise error + raise_cannot_add_expectation!(error, mock) end end @@ -700,14 +697,41 @@ defmodule Mox do :ok -> :ok - {:error, error} when is_binary(error) -> - raise ArgumentError, error - {:error, error} -> - raise error + raise_cannot_add_expectation!(error, mock) end end + defp raise_cannot_add_expectation!( + %NimbleOwnership.Error{reason: {:already_allowed, owner_pid}}, + mock + ) do + inspected = inspect(self()) + + raise ArgumentError, """ + cannot add expectations/stubs to #{inspect(mock)} in the current process (#{inspected}) \ + because the process has been allowed by #{inspect(owner_pid)}. \ + You cannot define expectations/stubs in a process that has been allowed + """ + end + + defp raise_cannot_add_expectation!( + %NimbleOwnership.Error{reason: {:not_shared_owner, global_pid}}, + mock + ) do + inspected = inspect(self()) + + raise ArgumentError, """ + cannot add expectations/stubs to #{inspect(mock)} in the current process (#{inspected}) \ + because Mox is in global mode and the global process is #{inspect(global_pid)}. \ + Only the process that set Mox to global can set expectations/stubs in global mode + """ + end + + defp raise_cannot_add_expectation!(error, _mock) do + raise error + end + @doc """ Allows other processes to share expectations and stubs defined by owner process. @@ -937,28 +961,8 @@ defmodule Mox do update_fun = &{:ok, init_or_merge_expectations(&1, key_expectation_list)} case get_and_update(owner_pid, mock, update_fun) do - {:ok, value} -> - value - - {:error, %NimbleOwnership.Error{reason: {:already_allowed, _}}} -> - inspected = inspect(self()) - - {:error, - """ - cannot add expectations/stubs to #{inspect(mock)} in the current process (#{inspected}) \ - because the process has been allowed by #{inspect(owner_pid)}. \ - You cannot define expectations/stubs in a process that has been allowed - """} - - {:error, %NimbleOwnership.Error{reason: {:not_shared_owner, global_pid}}} -> - inspected = inspect(self()) - - {:error, - """ - cannot add expectations/stubs to #{inspect(mock)} in the current process (#{inspected}) \ - because Mox is in global mode and the global process is #{inspect(global_pid)}. \ - Only the process that set Mox to global can set expectations/stubs in global mode - """} + {:ok, _value} -> + :ok {:error, error} -> {:error, error}