Skip to content

Commit

Permalink
Apply some code review suggestions about test
Browse files Browse the repository at this point in the history
  • Loading branch information
scottming committed Oct 16, 2023
1 parent 2aba95b commit 576e148
Showing 1 changed file with 77 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
alias Lexical.Document
alias Lexical.Document.Position
alias Lexical.Project
alias Lexical.RemoteControl.CodeIntelligence.Rename
alias Lexical.RemoteControl.Search.Indexer
alias Lexical.RemoteControl.Search.Store
alias Lexical.RemoteControl.Search.Store.Backends.Ets

alias Lexical.Test.CodeSigil
alias Lexical.Test.CursorSupport
Expand All @@ -18,16 +18,13 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do

setup_all do
start_supervised(Document.Store)
:ok
end

setup do
project = project()
delete_indexes(project)
start_supervised!({Store, [project, &default_create/1, &default_update/2]})
Lexical.RemoteControl.set_project(project)
destroy_backend(project)

on_exit(fn ->
delete_indexes(project)
destroy_backend(project)
end)

{:ok, project: project}
Expand All @@ -44,14 +41,16 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
end

describe "rename exact module" do
setup :with_a_started_ets_store

test "succeeds when the cursor on the definition" do
{:ok, result} =
~q[
defmodule |Foo do
end
] |> rename("Bar")
] |> rename("Renamed")

assert result =~ ~S[defmodule Bar do]
assert result =~ ~S[defmodule Renamed do]
end

test "succeeds when the cursor on the alias" do
Expand All @@ -60,9 +59,9 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
defmodule Baz do
alias |Foo
end
] |> rename("Bar")
] |> rename("Renamed")

assert result =~ ~S[alias Bar]
assert result =~ ~S[alias Renamed]
end

test "succeeds when the definition in a nested module" do
Expand All @@ -76,10 +75,18 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
defmodule TopLevelTest do
alias TopLevel.|Foo
end
] |> rename("Bar")
] |> rename("Renamed")

assert result == ~q[
defmodule TopLevel do
defmodule Renamed do
end
end
assert result =~ ~S[ defmodule Bar do]
assert result =~ ~S[ alias TopLevel.Bar]
defmodule TopLevelTest do
alias TopLevel.Renamed
end
]
end

test "succeeds when the cursor in the multiple aliases off of single alias" do
Expand All @@ -92,9 +99,9 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
}
|Second
end
] |> rename("Bar")
] |> rename("Renamed")

assert result =~ ~S[ First, Bar,]
assert result =~ ~S[ First, Renamed,]
end

test "only rename the aliased when the cursor at the aliased" do
Expand All @@ -105,10 +112,10 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
|FooBar
end
]
|> rename("FooBarBar")
|> rename("Renamed")

assert result =~ ~S[alias Foo.Bar, as: FooBarBar]
assert result =~ ~S[ FooBarBar]
assert result =~ ~S[alias Foo.Bar, as: Renamed]
assert result =~ ~S[ Renamed]
end

test "succeeds when the cursor at the alias_ased child" do
Expand All @@ -122,10 +129,30 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
Parent.|Bar
end
]
|> rename("Baz")
|> rename("Renamed")

assert result =~ ~S[defmodule TopLevel.Foo.Renamed]

assert result =~ ~S[ Parent.Renamed]
end

test "only rename aliased when the cursor at the alias_ased" do
{:ok, result} =
~q[
defmodule TopLevel.Foo.Bar do
end
defmodule TopLevel.Another do
alias TopLevel.Foo, as: Parent
|Parent.Bar
end
]
|> rename("Renamed")

assert result =~ ~S[defmodule TopLevel.Foo.Baz]
assert result =~ ~S[Parent.Baz]
assert result =~ ~S[defmodule TopLevel.Foo.Bar do]

assert result =~ ~S[alias TopLevel.Foo, as: Renamed]
assert result =~ ~S[ Renamed.Bar]
end

test "shouldn't rename the relative module" do
Expand All @@ -136,7 +163,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
defmodule FooTest do
end
] |> rename("ShouldNotRenameTest")
] |> rename("Renamed")

assert result =~ ~S[defmodule FooTest do]
end
Expand All @@ -149,26 +176,28 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
defmodule TopLevel.Module.Another do # x
alias TopLevel.|Module
end
] |> rename("Baz")
] |> rename("Renamed")

refute result =~ ~S[defmodule TopLevel.Baz.Another]
refute result =~ ~S[defmodule TopLevel.Renamed.Another]

assert result =~ ~S[defmodule TopLevel.Baz do]
assert result =~ ~S[alias TopLevel.Baz]
assert result =~ ~S[defmodule TopLevel.Renamed do]
assert result =~ ~S[alias TopLevel.Renamed]
end
end

describe "rename descendants" do
setup :with_a_started_ets_store

test "in the middle of definition" do
{:ok, result} =
~q[
defmodule TopLevel.|Middle.Module do
alias TopLevel.Middle.Module
end
] |> rename("Foo")
] |> rename("Renamed")

assert result =~ ~S[defmodule TopLevel.Foo.Module]
assert result =~ ~S[alias TopLevel.Foo.Module]
assert result =~ ~S[defmodule TopLevel.Renamed.Module]
assert result =~ ~S[alias TopLevel.Renamed.Module]
end

test "in the middle of reference" do
Expand All @@ -177,27 +206,10 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
defmodule TopLevel.Middle.Module do
alias TopLevel.|Middle.Module
end
] |> rename("Foo")
] |> rename("Renamed")

assert result =~ ~S[defmodule TopLevel.Foo.Module]
assert result =~ ~S[alias TopLevel.Foo.Module]
end

test "succeeds when the cursor at the alias_ased reference" do
{:ok, result} =
~q[
defmodule TopLevel.Foo.Bar do
end
defmodule TopLevel.Another do
alias TopLevel.Foo, as: Parent
|Parent.Bar
end
]
|> rename("AsedParent")

assert result =~ ~S[alias TopLevel.Foo, as: AsedParent]
assert result =~ ~S[AsedParent.Bar]
assert result =~ ~S[defmodule TopLevel.Renamed.Module]
assert result =~ ~S[alias TopLevel.Renamed.Module]
end

test "succeeds when there are same module name in the cursor neighborhood" do
Expand All @@ -212,15 +224,17 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
defmodule TopLevel.Another do
alias TopLevel.Foo.|Foo
end
] |> rename("Bar")
] |> rename("Renamed")

assert result =~ ~S[defmodule TopLevel.Foo do]
assert result =~ ~S[defmodule TopLevel.Foo.Bar do]
assert result =~ ~S[alias TopLevel.Foo.Bar]
assert result =~ ~S[defmodule TopLevel.Foo.Renamed do]
assert result =~ ~S[alias TopLevel.Foo.Renamed]
end
end

describe "unsupported" do
setup :with_a_started_ets_store

test "rename a function" do
assert {:error, {:unsupported, {:local_or_var, ~c"bar"}}} ==
~q[
Expand All @@ -234,12 +248,10 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do

defp rename(%Project{} = project \\ project(), source, new_name) do
uri = subject_uri(project)
{:ok, document} = open_document(uri, strip_cursor(source))

position =
source |> cursor_position() |> then(&Position.new(document, elem(&1, 0), elem(&1, 1)))

with :ok <- index(document),
with {position, text} <- pop_cursor(source),
{:ok, document} <- open_document(uri, text),
:ok <- index(document),
{:ok, uri_with_changes} <- Rename.rename(document, position, new_name) do
changes = uri_with_changes |> Map.values() |> List.flatten()
{:ok, apply_edits(document, changes)}
Expand Down Expand Up @@ -285,10 +297,12 @@ defmodule Lexical.RemoteControl.CodeIntelligence.RenameTest do
{:ok, [], []}
end

defp delete_indexes(project) do
project
|> Store.State.index_path()
|> Path.dirname()
|> File.rm_rf()
defp destroy_backend(project) do
Ets.destroy(project)
end

defp with_a_started_ets_store(%{project: project}) do
start_supervised!({Store, [project, &default_create/1, &default_update/2, Ets]})
:ok
end
end

0 comments on commit 576e148

Please sign in to comment.