Skip to content

Commit

Permalink
tests: Add more
Browse files Browse the repository at this point in the history
Add and update tests.
  • Loading branch information
ckoch-cars committed Aug 20, 2022
1 parent 93fe933 commit 6a6da40
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 178 deletions.
36 changes: 2 additions & 34 deletions test/ex_factor/cli_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ defmodule ExFactor.CLITest do

test "with --no-format" do
opts = [
module: "ExFactor.Support.ExampleFive",
sourcepath: "test/support/example_five.ex",
module: "ExFactor.Support.ExampleSeven",
sourcepath: "test/support/example_seven.ex",
targetpath: "test/support/example_six.ex",
target: "ExFactor.Modified.ExampleSix",
function: :all_funcs,
Expand All @@ -60,38 +60,6 @@ defmodule ExFactor.CLITest do
assert exit_status == 0

assert cli_output =~ "Message: --dry_run changes to make"

# content = """
# defmodule ExFactorSampleModule do
# @somedoc "This is somedoc"
# # a comment and no aliases
# _docp = "here's an arbitrary module underscore"
# @spec pub1(term()) :: term()
# def pub1(arg1) do
# :pub1_ok
# end
# end
# """

# File.write("test/tmp/source_module.ex", content)
# target_path = "test/tmp/target_module.ex"

# opts = [
# target_path: target_path,
# target: "ExFactor.NewMod",
# module: "ExFactorSampleModule",
# source_path: "test/tmp/source_module.ex",
# function: :pub1,
# arity: 1,
# format: false
# ]

# argv = OptionParser.to_argv(opts)

# {_cli_output, exit_status} = System.cmd("mix", ["ex_factor" | argv])
# assert exit_status == 0
# file = File.read!(target_path)
# assert file =~ "\n@spec pub1(term()) :: term()\ndef pub1(arg1) do\n :pub1_ok\nend\nend"
end

test "with --moduleonly" do
Expand Down
113 changes: 10 additions & 103 deletions test/ex_factor/mix_task_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,104 +26,23 @@ defmodule ExFactor.MixTaskTest do
end

test "write a new file with the function" do
# content = """
# defmodule ExFactorSampleModule do
# @somedoc "This is somedoc"
# # a comment and no aliases
# _docp = "here's an arbitrary module underscore"
# @spec pub1(term()) :: term()
# def pub1(arg1) do
# :pub1_ok
# end
# end
# """

# File.write("test/tmp/source_module.ex", content)
# target_path = "test/tmp/target_module.ex"

opts = [
target: "ExFactor.Tmp.NeighborsMoveOut",
module: "ExFactor.Neighbors",
function: :walk,
arity: 3,
dry_run: true
dryrun: true,
verbosde: true
]

argv = OptionParser.to_argv(opts)

run_message = capture_io(fn -> Mix.Tasks.ExFactor.run(argv) end)
# |> IO.inspect(label: "ExFactor.run io")
assert run_message =~ "Module: ExFactor.Tmp.NeighborsMoveOut"
assert run_message =~ "Path: lib/ex_factor/tmp/neighbors_move_out.ex"
end

test "write a new file add a moduledoc comment" do
content = """
defmodule ExFactorSampleModule do
def pub1(arg1) do
:pub1_ok
end
end
"""

File.write("test/tmp/source_module.ex", content)
target_path = "test/tmp/target_module.ex"

opts = [
target_path: target_path,
target: "ExFactor.NewMod",
module: "ExFactorSampleModule",
source_path: "test/tmp/source_module.ex",
function: :pub1,
arity: 1
]

argv = OptionParser.to_argv(opts)
capture_io(fn -> Mix.Tasks.ExFactor.run(argv) end)

file = File.read!(target_path)
assert file =~ "def pub1(arg1)"
assert file =~ "@moduledoc \"This module created with ExFactor\""
end

test " with dry_run option, don't write the file." do
content = """
defmodule ExFactorSampleModule do
@somedoc "This is somedoc"
# a comment and no aliases
_docp = "here's an arbitrary module underscore"
@spec pub1(term()) :: term()
def pub1(arg1) do
:ok
end
end
"""

File.write("test/tmp/source_module.ex", content)
target_path = "test/tmp/target_module.ex"

opts = [
target_path: target_path,
target: "ExFactor.NewMod",
module: "ExFactorSampleModule",
source_path: "test/tmp/source_module.ex",
function: :pub1,
arity: 1,
dryrun: true
]

argv = OptionParser.to_argv(opts)

output = capture_io(fn -> Mix.Tasks.ExFactor.run(argv) end)

# no new file gets written
assert {:error, :enoent} = File.read(target_path)
# assert output.file_contents
assert output =~ "--dry_run changes to make"
assert output =~ "ExFactor.NewMod"
end

@tag :skip
test "write multiple functions and their docs, into an existing module" do
content = """
defmodule ExFactor.Tmp.SourceModule do
Expand Down Expand Up @@ -185,32 +104,20 @@ defmodule ExFactor.MixTaskTest do
end

test "with --moduleonly" do
content = """
defmodule ExFactor.Module do
def pub1(arg1) do
ExFactorSampleModule.call_some_function(arg1)
end
end
"""

File.write("lib/ex_factor/tmp/source_module.ex", content)

opts = [
target: "ExFactor.NewMod",
module: "ExFactorSampleModule",
moduleonly: true
target: "ExFactor.Tmp.My.Neighbors.Moved",
module: "ExFactor.Neighbors",
moduleonly: true,
dryrun: true
]

argv = OptionParser.to_argv(opts)

capture_io(fn ->
run_io = capture_io(fn ->
Mix.Tasks.ExFactor.run(argv)
end)

file = File.read!("lib/ex_factor/tmp/source_module.ex")
assert file =~ "def pub1(arg1) do\n NewMod.call_some_function(arg1)\n end"
assert file =~ "alias ExFactor.NewMod"
assert run_io =~ " Module: Elixir.ExFactor.Extractor"
assert run_io =~ "Path: lib/ex_factor/extractor.ex"
end

end
end
74 changes: 33 additions & 41 deletions test/ex_factor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@ defmodule ExFactorTest do
end

describe "refactor/1" do
test "works with another one-liner fn" do
opts = [
source_module: "ExFactor.Support.ExampleSeven",
source_path: "test/support/example_seven.ex",
target_path: "test/support/example_six.ex",
target_module: "ExFactor.Modified.ExampleSix",
source_function: :all_functions,
arity: 1,
dry_run: true
]
%{additions: _, changes: _, removals: _} = _results = ExFactor.refactor(opts)
end

test "works with a one-liner fn" do
opts = [
source_module: "ExFactor.Support.ExampleFive",
source_path: "test/support/example_five.ex",
target_path: "test/support/example_six.ex",
target_module: "ExFactor.Modified.ExampleSix",
source_function: :all_funcs,
arity: 1,
dry_run: true
]
%{additions: _, changes: _, removals: _} = _results = ExFactor.refactor(opts)


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 Expand Up @@ -177,40 +204,10 @@ defmodule ExFactorTest do

describe "refactor_module/1" do
test "it refactors the refs to a module name only" do
File.rm("lib/ex_factor/tmp/source_module.ex")
File.rm("lib/ex_factor/tmp/target_module.ex")

content = """
defmodule ExFactor.Tmp.SourceModule do
@doc "this is some documentation for refactor1/1"
def refactor1([]) do
ExFactor.Tmp.TargetModule.pub_exists({})
end
def refactor1(arg1) do
ExFactor.Tmp.TargetModule.pub_exists(arg1)
end
end
"""

File.write("lib/ex_factor/tmp/source_module.ex", content)

content = """
defmodule ExFactor.Tmp.TargetModule do
@doc "some docs"
def pub_exists(:error) do
:error
end
def pub_exists(arg_exists) do
arg_exists
end
end
"""

File.write("lib/ex_factor/tmp/target_module.ex", content)

opts = [
target_module: "ExFactor.Tmp.NewModule",
source_module: "ExFactor.Tmp.TargetModule"
target_module: "ExFactor.Tmp.My.Neughbors.Moved",
source_module: "ExFactor.Neighbors",
dry_run: true
]

%{additions: additions, changes: [changes], removals: removals} =
Expand All @@ -220,15 +217,10 @@ defmodule ExFactorTest do
assert removals == %ExFactor{}

assert %ExFactor{
module: ExFactor.Tmp.SourceModule,
path: "lib/ex_factor/tmp/source_module.ex",
state: [:alias_added, :changed, :changed]
module: ExFactor.Extractor,
path: "lib/ex_factor/extractor.ex",
state: [:dry_run, :alias_added, :changed, :changed]
} = changes

file = File.read!("lib/ex_factor/tmp/source_module.ex")
assert file =~ "alias ExFactor.Tmp.NewModule"
assert file =~ "NewModule.pub_exists({})"
assert file =~ "NewModule.pub_exists(arg1)"
end
end
end

0 comments on commit 6a6da40

Please sign in to comment.