Skip to content

Commit

Permalink
test(lsp): add tests for the request_all function
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-z committed Aug 1, 2022
1 parent a5f2bd1 commit 6b36aca
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lua/java_util/lsp/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local rename = require("java_util.lsp._internal.rename")
local rename = require("java_util.lsp.internal.rename").rename

local lsp = {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,9 @@ local string_util = require("java_util.string_util")
local lsp_util = require("java_util.lsp.util")
local ts_utils = require("nvim-treesitter.ts_utils")

local M = {}
local rename = {}

function M.rename(new_name, opts)
opts = opts or {}

if vim.o.filetype == "java" then
local node_at_cursor = ts_utils.get_node_at_cursor(0)
if M._is_field(node_at_cursor) then
M._rename_field({ new_name = new_name, opts = opts, node_at_cursor = node_at_cursor })
return
end
end

vim.lsp.buf.rename(new_name, opts)
end

function M._is_field(node)
local function is_field(node)
local first_parent = node:parent()

if first_parent:type() == "field_access" then
Expand All @@ -32,7 +18,21 @@ function M._is_field(node)
return false
end

function M._with_name(new_name, callback)
function rename.rename(new_name, opts)
opts = opts or {}

if vim.o.filetype == "java" then
local node_at_cursor = ts_utils.get_node_at_cursor(0)
if is_field(node_at_cursor) then
rename._rename_field({ new_name = new_name, opts = opts, node_at_cursor = node_at_cursor })
return
end
end

vim.lsp.buf.rename(new_name, opts)
end

local function with_name(new_name, callback)
local old_name = vim.fn.expand("<cword>")
if new_name ~= nil then
callback(new_name, old_name)
Expand All @@ -45,7 +45,7 @@ function M._with_name(new_name, callback)
end
end

function M._rename_field(opts)
function rename._rename_field(opts)
local params = vim.lsp.util.make_position_params(0)
params.context = { includeDeclaration = true }
lsp_util.request_all({
Expand Down Expand Up @@ -76,7 +76,7 @@ function M._rename_field(opts)
end
end

M._with_name(opts.new_name, function(new_name, old_name)
with_name(opts.new_name, function(new_name, old_name)
local uppercase_old_name = string_util.first_to_upper(old_name)
local uppercase_new_name = string_util.first_to_upper(new_name)
local getter = string.format("%s%s", "get", uppercase_old_name)
Expand Down Expand Up @@ -119,3 +119,5 @@ function M._rename_field(opts)
end)
end)
end

return rename
61 changes: 61 additions & 0 deletions lua/tests/automated/lsp/util_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
local mock = require("luassert.mock")

describe("util", function()
local util = require("java_util.lsp.util")

describe("request_all", function()
local mock_lsp

after_each(function()
mock.revert(mock_lsp)
end)

local function was_called(params, fn)
mock_lsp = mock(vim.lsp, true)
mock_lsp.buf_request.invokes(fn)
local called = false
util.request_all(params, function()
called = true
end)
return called
end

it("given 4 requests when all are succesful then call the callback", function()
local called = was_called({
{ bufnr = 0, method = "textDocument/references", params = {} },
{ bufnr = 0, method = "textDocument/references", params = {} },
{ bufnr = 0, method = "textDocument/references", params = {} },
{ bufnr = 0, method = "textDocument/definition", params = {} },
}, function(_, _, _, fn)
fn()
end)

assert.is_true(called)
end)

it("given 2 requests when one fails then dont call the callback", function()
local count = 0
local called = was_called({
{ bufnr = 0, method = "textDocument/references", params = {} },
{ bufnr = 0, method = "textDocument/definition", params = {} },
}, function(_, _, _, fn)
if count < 1 then
fn()
end
count = count + 1
end)

assert.is_false(called)
end)

it("given 2 requests when all fails then dont call the callback", function()
local count = 0
local called = was_called({
{ bufnr = 0, method = "textDocument/references", params = {} },
{ bufnr = 0, method = "textDocument/definition", params = {} },
}, function(_, _, _, fn) end)

assert.is_false(called)
end)
end)
end)

0 comments on commit 6b36aca

Please sign in to comment.