Skip to content
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
32 changes: 30 additions & 2 deletions lib/mimic/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ defmodule Mimic.Module do
mimic_info = module_mimic_info(opts)
mimic_behaviours = generate_mimic_behaviours(module)
mimic_functions = generate_mimic_functions(module)
mimic_macros = generate_mimic_macros(module)
mimic_struct = generate_mimic_struct(module)
quoted = [mimic_info, mimic_struct | mimic_behaviours ++ mimic_functions]
quoted = [mimic_info, mimic_struct | mimic_behaviours ++ mimic_functions ++ mimic_macros]
Module.create(module, quoted, Macro.Env.location(__ENV__))
module
end
Expand Down Expand Up @@ -175,7 +176,14 @@ defmodule Mimic.Module do
defp generate_mimic_functions(module) do
internal_functions = [__info__: 1, module_info: 0, module_info: 1]

for {fn_name, arity} <- module.module_info(:exports),
functions =
if function_exported?(module, :__info__, 1) do
module.__info__(:functions)
else
module.module_info(:exports)
end

for {fn_name, arity} <- functions,
{fn_name, arity} not in internal_functions do
args = Macro.generate_arguments(arity, module)

Expand All @@ -187,6 +195,26 @@ defmodule Mimic.Module do
end
end

defp generate_mimic_macros(module) do
macros =
if function_exported?(module, :__info__, 1) do
module.__info__(:macros)
else
[]
end

for {macro_name, arity} <- macros do
args = Macro.generate_arguments(arity, module)
macro_fun = String.to_existing_atom("MACRO-#{macro_name}")

quote do
defmacro unquote(macro_name)(unquote_splicing(args)) do
Server.apply(__MODULE__, unquote(macro_fun), [__CALLER__ | unquote(args)])
end
end
end
end

defp generate_mimic_behaviours(module) do
module.module_info(:attributes)
|> Keyword.get_values(:behaviour)
Expand Down
11 changes: 11 additions & 0 deletions test/mimic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,17 @@ defmodule Mimic.Test do
expect(Calculator, :add, 0, fn x, y -> x + y end)
end
end

test "macros" do
expect(Calculator, :add, fn x, _y -> x + 2 end)

quote do
require Calculator

assert Calculator.add(4, 2) == Calculator.add_macro(4, 2)
end
|> Code.eval_quoted()
end
end

describe "expect/4 global mode" do
Expand Down
3 changes: 3 additions & 0 deletions test/support/test_modules.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule AddAdapter do
@moduledoc false
@callback add(number(), number()) :: number()
@macrocallback add_macro(Macro.t(), Macro.t()) :: Macro.t()
@optional_callbacks add_macro: 2
end

defmodule MultAdapter do
Expand All @@ -13,6 +15,7 @@ defmodule Calculator do
@behaviour AddAdapter
@behaviour MultAdapter
def add(x, y), do: x + y
defmacro add_macro(x, y), do: {:+, [], [x, y]}
def mult(x, y), do: x * y
end

Expand Down
Loading