Skip to content

Commit

Permalink
Add some handler tests for rename
Browse files Browse the repository at this point in the history
  • Loading branch information
scottming committed Mar 3, 2024
1 parent 3c12b43 commit 1ce421a
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions apps/server/test/lexical/server/provider/handlers/rename_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
defmodule Lexical.Server.Provider.Handlers.RenameTest do
alias Lexical.Ast
alias Lexical.Document
alias Lexical.Protocol.Requests.Rename
alias Lexical.Protocol.Responses
alias Lexical.Proto.Convert
alias Lexical.RemoteControl

alias Lexical.Server
alias Lexical.Server.Provider.Env
alias Lexical.Server.Provider.Handlers

import Lexical.Test.Protocol.Fixtures.LspProtocol
import Lexical.Test.Fixtures

use ExUnit.Case, async: false
use Patch

setup_all do
start_supervised(Server.Application.document_store_child_spec())
:ok
end

setup do
project = project(:navigations)
path = file_path(project, Path.join("lib", "my_definition.ex"))
uri = Document.Path.ensure_uri(path)
{:ok, project: project, uri: uri}
end

def build_request(path, line, char) do
uri = Document.Path.ensure_uri(path)

params = [
text_document: [uri: uri],
position: [line: line, character: char]
]

with {:ok, _} <- Document.Store.open_temporary(uri),
{:ok, req} <- build(Rename, params) do
Convert.to_native(req)
end
end

def handle(request, project) do
Handlers.Rename.handle(request, %Env{project: project})
end

describe "rename" do
test "returns nil when document can not be analyzed", %{project: project, uri: uri} do
patch(Document.Store, :fetch, fn ^uri, :analysis ->
{:ok, nil, %Ast.Analysis{valid?: false}}
end)

{:ok, request} = build_request(uri, 1, 5)
assert {:reply, response} = handle(request, project)

assert response.error.message == "document can not be analyzed"
end

test "returns nil when there are no changes", %{project: project, uri: uri} do
patch(Document.Store, :fetch, fn ^uri, :analysis ->
{:ok, nil, %Ast.Analysis{valid?: true}}
end)

patch(RemoteControl.Api, :rename, fn ^project, _analysis, _position, _new_name ->
{:ok, %{}}
end)

{:ok, request} = build_request(uri, 1, 5)
assert {:reply, response} = handle(request, project)

assert response == nil
end

test "returns edit when there are changes", %{project: project, uri: uri} do
patch(Document.Store, :fetch, fn ^uri, :analysis ->
{:ok, nil, %Ast.Analysis{valid?: true}}
end)

patch(RemoteControl.Api, :rename, fn ^project, _analysis, _position, _new_name ->
{:ok,
%{
"file:///path/to/file.ex" => [
%{
new_text: "new_text",
range: %{start: %{line: 1, character: 5}, end: %{line: 1, character: 10}}
}
]
}}
end)

{:ok, request} = build_request(uri, 1, 5)

assert {:reply, response} = handle(request, project)

assert response.result.changes == %{
"file:///path/to/file.ex" => [
%{
new_text: "new_text",
range: %{end: %{character: 10, line: 1}, start: %{character: 5, line: 1}}
}
]
}
end
end
end

0 comments on commit 1ce421a

Please sign in to comment.