Skip to content

Commit

Permalink
Remove conditional statements (#150)
Browse files Browse the repository at this point in the history
* Enum.to_list() |> tl()  -> Enum.drop(1)

* refactored set_max_global/1 to remove if

* refactored stub_with the remove case statements
  • Loading branch information
bradhanks authored Jan 19, 2024
1 parent 877b86f commit 3b376d0
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions lib/mox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,15 @@ defmodule Mox do
setup :set_mox_global
"""
@spec set_mox_global(term()) :: :ok
def set_mox_global(context \\ %{}) do
if Map.get(context, :async) do
raise "Mox cannot be set to global mode when the ExUnit case is async. " <>
"If you want to use Mox in global mode, remove \"async: true\" when using ExUnit.Case"
else
Mox.Server.set_mode(self(), :global)
end
def set_mox_global(context \\ %{})

def set_mox_global(%{async: true}) do
raise "Mox cannot be set to global mode when the ExUnit case is async. " <>
"If you want to use Mox in global mode, remove \"async: true\" when using ExUnit.Case"
end

def set_mox_global(_context), do: Mox.Server.set_mode(self(), :global)

@doc """
Chooses the Mox mode based on context.
Expand Down Expand Up @@ -434,7 +434,7 @@ defmodule Mox do
for behaviour <- behaviours,
{fun, arity} <- behaviour.behaviour_info(:callbacks),
{fun, arity} not in callbacks_to_skip do
args = 0..arity |> Enum.to_list() |> tl() |> Enum.map(&Macro.var(:"arg#{&1}", Elixir))
args = 0..arity |> Enum.drop(1) |> Enum.map(&Macro.var(:"arg#{&1}", Elixir))

quote do
def unquote(fun)(unquote_splicing(args)) do
Expand Down Expand Up @@ -623,24 +623,23 @@ defmodule Mox do
"""
@spec stub_with(mock, module()) :: mock when mock: t()
def stub_with(mock, module) when is_atom(mock) and is_atom(module) do
mock_behaviours = mock.__mock_for__()
behaviours = module_behaviours(module)
behaviours_mock = mock.__mock_for__()
behaviours_common = Enum.filter(behaviours, &(&1 in behaviours_mock))

behaviours =
case module_behaviours(module) do
[] ->
raise ArgumentError, "#{inspect(module)} does not implement any behaviour"

behaviours ->
case Enum.filter(behaviours, &(&1 in mock_behaviours)) do
[] ->
raise ArgumentError,
"#{inspect(module)} and #{inspect(mock)} do not share any behaviour"

common ->
common
end
end
do_stub_with(mock, module, behaviours, behaviours_common)
end

defp do_stub_with(_mock, module, [], _behaviours_common) do
raise ArgumentError, "#{inspect(module)} does not implement any behaviour"
end

defp do_stub_with(mock, module, _behaviours, []) do
raise ArgumentError,
"#{inspect(module)} and #{inspect(mock)} do not share any behaviour"
end

defp do_stub_with(mock, module, behaviours, _behaviours_common) do
for behaviour <- behaviours,
{fun, arity} <- behaviour.behaviour_info(:callbacks),
function_exported?(mock, fun, arity) do
Expand Down

0 comments on commit 3b376d0

Please sign in to comment.