Skip to content

Commit

Permalink
Move the test helpers to NextLS.Support.Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
NJichev committed Apr 24, 2024
1 parent 131f07e commit 3a87d74
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
38 changes: 0 additions & 38 deletions lib/next_ls/test.ex

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule NextLS.CredoExtension.CodeAction.RemoveDebuggerTest do
use ExUnit.Case, async: true

import NextLS.Test
import NextLS.Support.Utils, only: [assert_is_text_edit: 3]

alias GenLSP.Structures.CodeAction
alias GenLSP.Structures.Position
Expand Down
32 changes: 32 additions & 0 deletions test/support/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ defmodule NextLS.Support.Utils do
import ExUnit.Callbacks
import GenLSP.Test

alias GenLSP.Structures.Position
alias GenLSP.Structures.Range
alias GenLSP.Structures.TextEdit

def mix_exs do
"""
defmodule Project.MixProject do
Expand Down Expand Up @@ -172,4 +176,32 @@ defmodule NextLS.Support.Utils do
})
end
end

def apply_edit(code, edit) when is_binary(code), do: apply_edit(String.split(code, "\n"), edit)

def apply_edit(lines, %TextEdit{} = edit) when is_list(lines) do
text = edit.new_text
%Range{start: %Position{line: startl, character: startc}, end: %Position{line: endl, character: endc}} = edit.range

startl_text = Enum.at(lines, startl)
prefix = String.slice(startl_text, 0, startc)

endl_text = Enum.at(lines, endl)
suffix = String.slice(endl_text, endc, String.length(endl_text) - endc)

replacement = prefix <> text <> suffix

new_lines = Enum.slice(lines, 0, startl) ++ [replacement] ++ Enum.slice(lines, endl + 1, Enum.count(lines))

new_lines
|> Enum.join("\n")
|> String.trim()
end

defmacro assert_is_text_edit(code, edit, expected) do
quote do
actual = unquote(__MODULE__).apply_edit(unquote(code), unquote(edit))
assert actual == unquote(expected)
end
end
end

0 comments on commit 3a87d74

Please sign in to comment.