Skip to content

Commit

Permalink
testing: cover defdelegate
Browse files Browse the repository at this point in the history
Add test case for defdelegate.
  • Loading branch information
ckoch-cars committed Aug 20, 2022
1 parent 6a6da40 commit 0490486
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
12 changes: 12 additions & 0 deletions lib/ex_factor/changer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions test/ex_factor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions test/support/example_five.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
29 changes: 26 additions & 3 deletions test/support/trace.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 0490486

Please sign in to comment.