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

Validation of behavior and implementation modules #52

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
34 changes: 33 additions & 1 deletion lib/hammox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,13 @@ defmodule Hammox do
do: protect(module_name, module_name, funs)

@spec protect(mfa :: mfa(), behaviour_name :: module()) :: fun()
def protect({module_name, function_name, arity}, behaviour_name)
def protect({module_name, function_name, arity} = mfa, behaviour_name)
when is_atom(module_name) and is_atom(function_name) and is_integer(arity) and
is_atom(behaviour_name) do
module_exist?(module_name)
module_exist?(behaviour_name)
mfa_exist?(mfa)

code = {module_name, function_name}

typespecs = fetch_typespecs!(behaviour_name, function_name, arity)
Expand Down Expand Up @@ -372,4 +376,32 @@ defmodule Hammox do
{:type, _, :fun, [{:type, _, :product, arg_typespecs}, _]} = function_typespec
Enum.at(arg_typespecs, arg_index)
end

defp module_exist?(module_name) do
case Code.ensure_compiled(module_name) do
{:module, _} ->
true

_ ->
raise(ArgumentError,
message:
"Could not find module #{Utils.module_to_string(module_name)}."
)
end
end

defp mfa_exist?({module_name, function_name, arity}) do
case function_exported?(module_name, function_name, arity) do
true ->
true

_ ->
raise(ArgumentError,
message:
"Could not find function #{Utils.module_to_string(module_name)}.#{function_name}/#{
arity
}."
)
end
end
end
29 changes: 28 additions & 1 deletion test/hammox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,34 @@ defmodule HammoxTest do
test "throws when typespec does not exist" do
assert_raise(Hammox.TypespecNotFoundError, fn ->
Hammox.protect(
{Hammox.Test.SmallImplementation, :nonexistent_fuc, 0},
{Hammox.Test.SmallImplementation, :nospec_fun, 0},
Hammox.Test.SmallBehaviour
)
end)
end

test "throws when behaviour module does not exist" do
assert_raise(ArgumentError, fn ->
Hammox.protect(
{Hammox.Test.SmallImplementation, :foo, 0},
NotExistModule
)
end)
end

test "throws when implementation module does not exist" do
assert_raise(ArgumentError, fn ->
Hammox.protect(
{NotExistModule, :foo, 0},
Hammox.Test.SmallBehaviour
)
end)
end

test "throws when implementation function does not exist" do
assert_raise(ArgumentError, fn ->
Hammox.protect(
{Hammox.Test.SmallImplementation, :nonexistent_fun, 0},
Hammox.Test.SmallBehaviour
)
end)
Expand Down
4 changes: 4 additions & 0 deletions test/support/small_implementation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ defmodule Hammox.Test.SmallImplementation do
def other_foo(_) do
1
end

def nospec_fun() do
1
end
end