Skip to content

Commit

Permalink
Merge pull request #9 from AlimovSV/copy-behaviour-attributes
Browse files Browse the repository at this point in the history
Copy behaviour attributes
  • Loading branch information
edgurgel authored Oct 6, 2019
2 parents b127ee6 + 4a7c7d8 commit fd9a85c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/mimic/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ defmodule Mimic.Module do

defp create_mock(module) do
mimic_info = module_mimic_info()
mimic_behaviours = generate_mimic_behaviours(module)
mimic_functions = generate_mimic_functions(module)
Module.create(module, [mimic_info | mimic_functions], Macro.Env.location(__ENV__))
quoted = [mimic_info | [mimic_behaviours ++ mimic_functions]]
Module.create(module, quoted, Macro.Env.location(__ENV__))
module
end

Expand All @@ -115,4 +117,15 @@ defmodule Mimic.Module do
end
end
end

defp generate_mimic_behaviours(module) do
module.module_info(:attributes)
|> Keyword.get_values(:behaviour)
|> List.flatten()
|> Enum.map(fn behaviour ->
quote do
@behaviour unquote(behaviour)
end
end)
end
end
12 changes: 12 additions & 0 deletions test/mimic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -727,4 +727,16 @@ defmodule Mimic.Test do
|> Task.await()
end
end

describe "behaviours" do
test "copies behaviour attributes" do
behaviours =
Calculator.module_info(:attributes)
|> Keyword.get_values(:behaviour)
|> List.flatten()

assert AddAdapter in behaviours
assert MultAdapter in behaviours
end
end
end
12 changes: 12 additions & 0 deletions test/support/test_modules.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
defmodule AddAdapter do
@moduledoc false
@callback add(number(), number()) :: number()
end

defmodule MultAdapter do
@moduledoc false
@callback mult(number(), number()) :: number()
end

defmodule Calculator do
@moduledoc false
@behaviour AddAdapter
@behaviour MultAdapter
def add(x, y), do: x + y
def mult(x, y), do: x * y
end
Expand Down

0 comments on commit fd9a85c

Please sign in to comment.