|
| 1 | +defmodule Igniter.Code.WithinTest do |
| 2 | + alias Igniter.Code.Common |
| 3 | + alias Sourceror.Zipper |
| 4 | + use ExUnit.Case |
| 5 | + import Igniter.Test |
| 6 | + |
| 7 | + describe "with within" do |
| 8 | + test "performs multiple changes to some module" do |
| 9 | + test_project() |
| 10 | + |> Igniter.create_new_file("lib/some_module.ex", """ |
| 11 | + defmodule SomeModule do |
| 12 | + alias Some.Example |
| 13 | +
|
| 14 | + def some_function(a, b) do |
| 15 | + Example.function(a, b) |
| 16 | + end |
| 17 | + end |
| 18 | + """) |
| 19 | + |> apply_igniter!() |
| 20 | + |> Igniter.Project.Module.find_and_update_module!(SomeModule, fn zipper -> |
| 21 | + zipper |
| 22 | + |> within!(fn zipper -> |
| 23 | + pred = &match?(%Zipper{node: {:alias, _, _}}, &1) |
| 24 | + zipper = Common.remove(zipper, pred) |
| 25 | + line = "import Some.Example, only: [:function]" |
| 26 | + {:ok, Common.add_code(zipper, line, placement: :before)} |
| 27 | + end) |
| 28 | + |> within!(fn zipper -> |
| 29 | + {:ok, zipper} = move_to_function(zipper, :some_function) |
| 30 | + {:ok, zipper} = Common.move_to_do_block(zipper) |
| 31 | + line = "my_private_function(function(a, b))" |
| 32 | + {:ok, Igniter.Code.Common.replace_code(zipper, line)} |
| 33 | + end) |
| 34 | + |> within!(fn zipper -> |
| 35 | + {:ok, zipper} = move_to_function(zipper, :some_function) |
| 36 | + {:ok, zipper} = Common.move_to_do_block(zipper) |
| 37 | + line = "my_private_function!(function(a, b))" |
| 38 | + {:ok, Igniter.Code.Common.replace_code(zipper, line)} |
| 39 | + end) |
| 40 | + |> within!(fn zipper -> |
| 41 | + block = """ |
| 42 | + defp private_function!({:ok, result}), do: result |
| 43 | + defp private_function!(_), do: raise "Something went wrong!" |
| 44 | + """ |
| 45 | + |
| 46 | + {:ok, Common.add_code(zipper, block, placement: :after)} |
| 47 | + end) |
| 48 | + |> then(fn zipper -> {:ok, zipper} end) |
| 49 | + end) |
| 50 | + |> Igniter.Refactors.Rename.rename_function( |
| 51 | + {SomeModule, :some_function}, |
| 52 | + {SomeModule, :some_function!}, |
| 53 | + arity: 2 |
| 54 | + ) |
| 55 | + |> assert_has_patch("lib/some_module.ex", """ |
| 56 | + |defmodule SomeModule do |
| 57 | + - | alias Some.Example |
| 58 | + + | import Some.Example, only: [:function] |
| 59 | + | |
| 60 | + - | def some_function(a, b) do |
| 61 | + - | Example.function(a, b) |
| 62 | + + | def some_function!(a, b) do |
| 63 | + + | my_private_function!(function(a, b)) |
| 64 | + | end |
| 65 | + + | |
| 66 | + + | defp private_function!({:ok, result}), |
| 67 | + + | do: result |
| 68 | + + | |
| 69 | + + | defp private_function!(_), |
| 70 | + + | do: raise("Something went wrong!") |
| 71 | + |end |
| 72 | + """) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + defp within!(zipper, function) do |
| 77 | + case Common.within(zipper, function) do |
| 78 | + {:ok, zipper} -> zipper |
| 79 | + :error -> raise "Error calling within" |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + defp move_to_function(zipper, function) do |
| 84 | + Igniter.Code.Common.move_to(zipper, fn zipper -> |
| 85 | + Igniter.Code.Function.function_call?(zipper, function) |
| 86 | + end) |
| 87 | + end |
| 88 | +end |
0 commit comments