-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests. Add more functions to begin to find and move target functions
- Loading branch information
1 parent
3eb4b67
commit 27c13dc
Showing
10 changed files
with
399 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,5 @@ ex_factor-*.tar | |
|
||
# Temporary files for e.g. tests | ||
/tmp | ||
|
||
test/support/*_module.ex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule ExFactor.Evaluater do | ||
@moduledoc """ | ||
Documentation for `ExFactor.Evaluater`. | ||
""" | ||
|
||
alias ExFactor.Parser | ||
|
||
def modules_to_refactor(module, func, arity) do | ||
module | ||
|> ExFactor.callers() | ||
|> Enum.map(fn %{filepath: filepath} -> | ||
filepath | ||
|> Parser.public_functions() | ||
|> evaluate_ast(filepath, func, arity) | ||
end) | ||
|> Enum.reject(&is_nil(&1)) | ||
end | ||
|
||
defp evaluate_ast({_ast, fns}, filepath, func, arity) do | ||
fns | ||
|> Enum.find(fn map -> map.name == func && map.arity == arity end) | ||
|> case do | ||
nil -> nil | ||
_ -> filepath | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
defmodule ExFactor.Extractor do | ||
@moduledoc """ | ||
Documentation for `ExFactor.Extractor`. | ||
""" | ||
alias ExFactor.Parser | ||
|
||
def emplace(files, opts) do | ||
source_path = Keyword.get(opts, :source_path) | ||
source_module = Keyword.get(opts, :source_module) | ||
target_module = Keyword.get(opts, :target_module) | ||
target_path = Keyword.get(opts, :target_path) | ||
source_function = Keyword.get(opts, :source_function) | ||
arity = Keyword.get(opts, :arity) | ||
target_function = Keyword.get(opts, :target_function, source_function) | ||
|
||
Macro.underscore(source_module) | ||
# target_path = Macro.underscore(target_module) <> ".ex" | ||
Path.join([Mix.Project.app_path(), target_path]) | ||
# |> IO.inspect(label: "") | ||
|
||
File.exists?(source_path) |> IO.inspect(label: "") | ||
|
||
{_ast, functions} = Parser.public_functions(source_path) | ||
|
||
map = Enum.find(functions, &(&1.name == source_function && &1.arity == arity)) | ||
# |> IO.inspect(label: "source function") | ||
|
||
# map.ast | ||
# |> Macro.to_string() | ||
# |> IO.inspect(label: "source AST") | ||
|
||
case File.exists?(target_path) do | ||
true -> | ||
"somehow we need to add the fn to this file" | ||
|
||
_ -> | ||
content = | ||
quote generated: true do | ||
defmodule unquote(target_module) do | ||
@moduledoc false | ||
unquote(map.ast) | ||
end | ||
end | ||
|> Macro.to_string() | ||
|
||
# |> IO.inspect(label: "quoted") | ||
|
||
File.write(target_path, content) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule ExFactor.EvaluaterTest do | ||
use ExUnit.Case | ||
alias ExFactor.Evaluater | ||
|
||
describe "modules_to_refactor/1" do | ||
test "it should report callers of a module function" do | ||
assert ["test/support/support.ex" | _] = | ||
Evaluater.modules_to_refactor(ExFactor.Parser, :all_functions, 1) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
defmodule ExFactor.ExtractorTest do | ||
use ExUnit.Case | ||
alias ExFactor.Extractor | ||
|
||
setup do | ||
File.rm("test/support/source_module.ex") | ||
File.rm("test/support/target_module.ex") | ||
:ok | ||
end | ||
|
||
test "write a new file with the function" do | ||
content = """ | ||
defmodule ExFactorSampleModule do | ||
@somedoc "This is somedoc" | ||
# no aliases | ||
def pub1(arg1) do | ||
:ok | ||
end | ||
end | ||
""" | ||
|
||
File.write("test/support/source_module.ex", content) | ||
|
||
# target_path = Macro.underscore(target_module) | ||
target_path = "test/support/target_module.ex" | ||
File.rm(target_path) | ||
|
||
opts = [ | ||
target_path: target_path, | ||
target_module: ExFactor.NewMod, | ||
source_module: ExFactorSampleModule, | ||
source_path: "test/support/source_module.ex", | ||
source_function: :pub1, | ||
arity: 1 | ||
] | ||
|
||
path = Path.join([Mix.Project.app_path(), "lib", target_path <> ".ex"]) | ||
Extractor.emplace(["test/support/source_module.ex"], opts) | ||
|
||
file = File.read!(target_path) |> IO.inspect(label: "target_path") | ||
assert file =~ "def(pub1(arg1))" | ||
assert file =~ "defmodule(ExFactor.NewMod) do" | ||
end | ||
|
||
test "write a new file with the function, infer some defaults" do | ||
content = """ | ||
defmodule ExFactorSampleModule do | ||
@somedoc "This is somedoc" | ||
# no aliases | ||
def pub1(arg1) do | ||
:ok | ||
end | ||
end | ||
""" | ||
|
||
File.write("test/support/source_module.ex", content) | ||
target_path = "test/support/target_module.ex" | ||
|
||
opts = [ | ||
target_path: target_path, | ||
target_module: ExFactor.NewMod, | ||
source_module: ExFactorSampleModule, | ||
source_path: "test/support/source_module.ex", | ||
source_function: :pub1, | ||
arity: 1 | ||
] | ||
|
||
path = Path.join([Mix.Project.app_path(), "lib", target_path <> ".ex"]) | ||
Extractor.emplace(["test/support/source_module.ex"], opts) | ||
|
||
file = File.read!(target_path) |> IO.inspect(label: "target_path") | ||
assert file =~ "def(pub1(arg1))" | ||
assert file =~ "defmodule(ExFactor.NewMod) do" | ||
end | ||
end |
Oops, something went wrong.