From 04904861cd87e6de2a4faadd34af6cc733a67037 Mon Sep 17 00:00:00 2001 From: Christian Koch Date: Fri, 19 Aug 2022 21:18:26 -0500 Subject: [PATCH] testing: cover defdelegate Add test case for defdelegate. --- lib/ex_factor/changer.ex | 12 ++++++++++++ test/ex_factor_test.exs | 16 +++++++++++----- test/support/example_five.ex | 2 ++ test/support/trace.ex | 29 ++++++++++++++++++++++++++--- 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/lib/ex_factor/changer.ex b/lib/ex_factor/changer.ex index dce2d9e..fb54c76 100644 --- a/lib/ex_factor/changer.ex +++ b/lib/ex_factor/changer.ex @@ -111,6 +111,18 @@ defmodule ExFactor.Changer do fn_line = String.replace(fn_line, source_alias_alt, target_alias) {set_state(state, :changed), fn_line} + String.match?(fn_line, ~r/defdelegate\s*#{source_function}.*#{source_string}/) -> + fn_line = String.replace(fn_line, source_string, target_alias) + {set_state(state, :changed), fn_line} + + String.match?(fn_line, ~r/defdelegate\s*#{source_function}.*#{source_alias}/) -> + fn_line = String.replace(fn_line, source_alias, target_alias) + {set_state(state, :changed), fn_line} + + String.match?(fn_line, ~r/defdelegate\s*#{source_function}.*#{source_alias_alt}/) -> + fn_line = String.replace(fn_line, source_alias_alt, target_alias) + {set_state(state, :changed), fn_line} + true -> {state, fn_line} end diff --git a/test/ex_factor_test.exs b/test/ex_factor_test.exs index 9f9703f..74f5869 100644 --- a/test/ex_factor_test.exs +++ b/test/ex_factor_test.exs @@ -35,18 +35,24 @@ defmodule ExFactorTest do test "works with a one-liner fn" do opts = [ - source_module: "ExFactor.Support.ExampleFive", - source_path: "test/support/example_five.ex", + source_module: "ExFactor.Formatter", target_path: "test/support/example_six.ex", target_module: "ExFactor.Modified.ExampleSix", - source_function: :all_funcs, - arity: 1, + source_function: :format, + arity: 2, dry_run: true ] - %{additions: _, changes: _, removals: _} = _results = ExFactor.refactor(opts) + %{additions: additions, changes: changes, removals: removals} = ExFactor.refactor(opts) + # |> IO.inspect(label: "changes") + assert removals.module == "ExFactor.Formatter" + assert additions.module == "ExFactor.Modified.ExampleSix" + assert additions.path == "test/support/example_six.ex" + five = Enum.find(changes, &(&1.module == ExFactor.Support.ExampleFive)) + assert five.file_contents =~ "defdelegate format(args, opts \\\\ []), to: ExampleSix" end + test "it refactors the functions into a new module, specified in opts" do File.rm("lib/ex_factor/tmp/source_module.ex") File.rm("lib/ex_factor/tmp/target_module.ex") diff --git a/test/support/example_five.ex b/test/support/example_five.ex index 929437c..e3d1b41 100644 --- a/test/support/example_five.ex +++ b/test/support/example_five.ex @@ -15,6 +15,8 @@ defmodule ExFactor.Support.ExampleFive do IO.puts "A functions" end + defdelegate format(args, opts \\ []), to: ExFactor.Formatter + def a_third_func(path) do path |> IO.puts() diff --git a/test/support/trace.ex b/test/support/trace.ex index cbfeace..f397054 100644 --- a/test/support/trace.ex +++ b/test/support/trace.ex @@ -789,17 +789,40 @@ defmodule ExFactor.Support.Trace do {:remote_function, 2, nil, Module, :__put_attribute__, 4} ], {"test/support/example_five.ex", ExFactor.Support.ExampleFive} => [ - {:remote_function, 20, 11, IO, :puts, 1}, - {:remote_function, 21, 11, IO, :inspect, 1}, {:remote_function, 22, 11, IO, :puts, 1}, + {:remote_function, 23, 11, IO, :inspect, 1}, + {:remote_function, 24, 11, IO, :puts, 1}, + {:remote_function, 18, nil, ExFactor.Formatter, :format, 2}, {:remote_function, 15, 8, IO, :puts, 1}, {:remote_function, 12, 36, ExFactor.Parser, :public_functions, 1}, {:remote_function, 11, 29, ExFactor.Parser, :all_functions, 1}, {:imported_function, 11, 29, ExFactor.Parser, :all_functions, 1}, {:remote_function, 10, 33, ExFactor.Callers, :callers, 1}, {:remote_function, 1, 1, :elixir_utils, :noop, 0}, - {:remote_function, 18, nil, :elixir_module, :read_cache, 2}, + {:remote_function, 20, nil, :elixir_module, :read_cache, 2}, + {:remote_function, 20, nil, :elixir_def, :store_definition, 5}, + {:remote_function, 18, nil, :elixir_quote, :list, 2}, + {:remote_function, 18, nil, :elixir_quote, :dot, 5}, {:remote_function, 18, nil, :elixir_def, :store_definition, 5}, + {:remote_function, 18, nil, :erlang, :length, 1}, + {:remote_function, 18, nil, Module, :__put_attribute__, 4}, + {:remote_function, 18, nil, Kernel.Utils, :defdelegate, 2}, + {:remote_function, 18, nil, List, :wrap, 1}, + {:remote_function, 18, nil, Macro.Env, :stacktrace, 1}, + {:remote_function, 18, nil, IO, :warn, 2}, + {:remote_function, 18, nil, :erlang, :"=:=", 2}, + {:remote_function, 18, nil, :erlang, :"=:=", 2}, + {:remote_function, 18, nil, :erlang, :orelse, 2}, + {:remote_function, 18, nil, Keyword, :has_key?, 2}, + {:remote_function, 18, nil, Macro.Env, :stacktrace, 1}, + {:remote_function, 18, nil, IO, :warn, 2}, + {:remote_function, 18, nil, :erlang, :is_list, 1}, + {:remote_function, 18, nil, ArgumentError, :exception, 1}, + {:remote_function, 18, nil, :erlang, :error, 1}, + {:remote_function, 18, nil, :erlang, :"=:=", 2}, + {:remote_function, 18, nil, :erlang, :"=:=", 2}, + {:remote_function, 18, nil, :erlang, :orelse, 2}, + {:remote_function, 18, nil, Keyword, :get, 2}, {:remote_function, 14, nil, :elixir_module, :read_cache, 2}, {:remote_function, 14, nil, :elixir_def, :store_definition, 5}, {:remote_function, 12, nil, :elixir_module, :read_cache, 2},