Skip to content

Commit 3b678a1

Browse files
authored
add assert_moves/3 (#351)
* fix argument name in documentation * add assert_moves/3
1 parent e41f7e0 commit 3b678a1

3 files changed

Lines changed: 84 additions & 1 deletion

File tree

lib/igniter.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ defmodule Igniter do
156156
|> parse_igniter_config()
157157
end
158158

159-
def move_file(igniter, from, from, opts \\ [])
159+
def move_file(igniter, from, to, opts \\ [])
160160
def move_file(igniter, from, from, _opts), do: igniter
161161

162162
def move_file(igniter, from, to, opts) do

lib/igniter/test.ex

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,52 @@ defmodule Igniter.Test do
201201
igniter
202202
end
203203

204+
@doc """
205+
Asserts that a file was moved to a specific location.
206+
207+
## Example
208+
209+
test_project()
210+
|> Igniter.move_file("lib/old_location.ex", "lib/new_location.ex")
211+
|> assert_moves("lib/old_location.ex", "lib/new_location.ex")
212+
"""
213+
@spec assert_moves(Igniter.t(), from :: String.t(), to: String.t()) :: Igniter.t()
214+
def assert_moves(igniter, from, to) do
215+
if Map.has_key?(igniter.moves, from) do
216+
actual_to = Map.get(igniter.moves, from)
217+
218+
assert actual_to == to, """
219+
Expected #{inspect(from)} to have been moved to:
220+
221+
#{to}
222+
223+
But it was moved to:
224+
225+
#{actual_to}
226+
"""
227+
else
228+
flunk("""
229+
Expected #{inspect(from)} to have been moved, but it was not.
230+
#{moved_files(igniter)}
231+
""")
232+
end
233+
234+
igniter
235+
end
236+
237+
defp moved_files(igniter) do
238+
if igniter.moves == %{} do
239+
"\nNo files were moved."
240+
else
241+
files =
242+
Enum.map_join(igniter.moves, "\n", fn {from, to} ->
243+
"* #{from}\n#{to}"
244+
end)
245+
246+
"\nThe following files were moved:\n\n#{files}"
247+
end
248+
end
249+
204250
def assert_rms(igniter, expected_paths) do
205251
actual_rms = Enum.sort(igniter.rms)
206252
expected_rms = Enum.sort(List.wrap(expected_paths))

test/igniter/test_test.exs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,41 @@ defmodule Igniter.TestTest do
7878
end
7979
end
8080
end
81+
82+
describe "assert_moves/3" do
83+
test "passes when file is moved" do
84+
test_project(files: %{"old.exs" => "content"})
85+
|> Igniter.move_file("old.exs", "new.exs")
86+
|> assert_moves("old.exs", "new.exs")
87+
end
88+
89+
test "fails when file is not moved" do
90+
assert_raise ExUnit.AssertionError,
91+
~r/Expected \"old.exs\" to have been moved, but it was not.\n\n No files were moved./,
92+
fn ->
93+
test_project(files: %{"old.exs" => "content"})
94+
|> assert_moves("old.exs", "new.exs")
95+
end
96+
end
97+
98+
test "fails when different file is moved" do
99+
assert_raise ExUnit.AssertionError,
100+
~r/Expected \"one.exs\" to have been moved, but it was not.\n\n The following files were moved:\n\n \* two.exs\n ↳ three.exs/,
101+
fn ->
102+
test_project(files: %{"one.exs" => "content", "two.exs" => "content"})
103+
|> Igniter.move_file("two.exs", "three.exs")
104+
|> assert_moves("one.exs", "three.exs")
105+
end
106+
end
107+
108+
test "fails when file is not moved to a different location" do
109+
assert_raise ExUnit.AssertionError,
110+
~r/Expected \"old.exs\" to have been moved to:\n\n new.exs\n\n But it was moved to:\n\n mature.exs/,
111+
fn ->
112+
test_project(files: %{"old.exs" => "content"})
113+
|> Igniter.move_file("old.exs", "mature.exs")
114+
|> assert_moves("old.exs", "new.exs")
115+
end
116+
end
117+
end
81118
end

0 commit comments

Comments
 (0)