Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorate all functions inside behaviour-implementation module #54

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/hammox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ defmodule Hammox do
"""
defdelegate verify_on_exit!(context \\ %{}), to: Mox

@doc since: "0.1.0"
@doc """
Same as `protect/3`, but decorate all public functions inside the module.

Example:

```elixir
defmodule Calculator do
@callback add(integer(), integer()) :: integer()
def add(a, b), do: a + b
@callback add(integer(), integer(), integer()) :: integer()
def add(a, b, c), do: a + b + c
@callback multiply(integer(), integer()) :: integer()
def multiply(a, b), do: a * b
end

%{
add_2: add_2,
add_3: add_3,
multiply_2: multiply_2
} = Hammox.protect(Calculator)
"""
@spec protect(module_name :: module()) :: map()
def protect(module_name) when is_atom(module_name) do
funs = module_name.__info__(:functions)
protect(module_name, module_name, funs)
end

@doc since: "0.1.0"
@doc """
Takes the function provided by a module, function, arity tuple and
Expand Down
7 changes: 7 additions & 0 deletions test/hammox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ defmodule HammoxTest do

defmock(TestMock, for: Hammox.Test.Behaviour)

describe "protect/1" do
test "decorate all functions inside the module" do
assert %{other_foo_0: _, other_foo_1: _, foo_0: _} =
Hammox.protect(Hammox.Test.BehaviourImplementation)
end
end

describe "protect/2" do
test "returns function protected from contract errors" do
fun = Hammox.protect({Hammox.Test.SmallImplementation, :foo, 0}, Hammox.Test.SmallBehaviour)
Expand Down