Packages
double_down
0.66.0
0.69.0
0.68.0
0.66.0
0.65.0
0.64.1
0.64.0
0.63.3
0.63.2
0.63.1
0.63.0
0.62.1
0.61.0
0.60.4
0.60.3
0.60.2
0.60.1
0.60.0
0.59.0
0.58.0
0.57.0
0.56.1
0.56.0
0.55.0
0.54.0
0.53.0
0.52.3
0.52.2
0.52.1
0.52.0
0.51.0
0.50.1
0.50.0
0.49.0
0.48.1
0.48.0
0.47.2
0.47.1
0.47.0
0.46.3
0.46.2
0.46.1
0.46.0
0.45.0
0.44.0
0.43.0
0.42.0
0.41.1
0.41.0
0.40.0
0.39.0
0.38.0
0.37.2
0.37.0
0.35.0
0.34.0
0.33.0
0.32.0
0.31.1
0.31.0
0.30.1
0.30.0
0.29.0
0.28.1
0.28.0
0.27.0
0.26.0
0.24.0
Builds on the Mox pattern — generates behaviours and dispatch facades from `defcallback` declarations — and adds stateful test doubles powerful enough to test Ecto.Repo operations without a database.
Current section
Files
Jump to
Current section
Files
lib/double_down/dynamic_facade/shader.ex
defmodule DoubleDown.DynamicFacade.Shader do
@moduledoc false
@doc false
def rename_module(module, new_name, cover_enabled?) do
beam_code =
case :code.get_object_code(module) do
{^module, binary, _path} -> binary
:error -> raise "Failed to get object code for #{inspect(module)}"
end
{:ok, {_, [{:abstract_code, {:raw_abstract_v1, forms}}]}} =
:beam_lib.chunks(beam_code, [:abstract_code])
forms = rename_module_attribute(forms, new_name)
compiler_opts =
module.module_info(:compile)
|> Keyword.get(:options, [])
|> Enum.filter(&(&1 != :from_core))
|> then(&[:return_errors, :debug_info | &1])
binary =
case :compile.forms(forms, compiler_opts) do
{:ok, _module_name, binary} -> binary
{:ok, _module_name, binary, _warnings} -> binary
end
{:module, ^new_name} = :code.load_binary(new_name, ~c"", binary)
if cover_enabled? do
apply(:cover, :compile_beams, [[{new_name, binary}]])
end
end
defp rename_module_attribute([{:attribute, line, :module, {_, vars}} | t], new_name) do
[{:attribute, line, :module, {new_name, vars}} | t]
end
defp rename_module_attribute([{:attribute, line, :module, _} | t], new_name) do
[{:attribute, line, :module, new_name} | t]
end
defp rename_module_attribute([h | t], new_name) do
[h | rename_module_attribute(t, new_name)]
end
defp rename_module_attribute([], _new_name), do: []
end