Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.

Commit 1953535

Browse files
committed
test: refactor documentstore and utils tests
Refactor tests to use MiniTest.new_set structure and child Neovim integration. Update documentstore, format, UUID and LCS test files to remove redundant describe/it blocks. Consolidate test cases and improve line wrapping and readability.
1 parent f01dcd7 commit 1953535

File tree

4 files changed

+160
-149
lines changed

4 files changed

+160
-149
lines changed

tests/test_documentstore.lua

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
1-
local documentstore = require("rzls.documentstore")
2-
31
local eq = MiniTest.expect.equality
2+
local child = MiniTest.new_child_neovim()
3+
4+
local T = MiniTest.new_set()
5+
6+
T["documentstore"] = MiniTest.new_set({
7+
hooks = {
8+
pre_case = function()
9+
child.restart({ "-u", "scripts/minimal_init.lua" })
10+
child.lua([[M = require('rzls')]])
11+
end,
12+
post_once = child.stop,
13+
},
14+
})
15+
16+
T["documentstore"]["create and retreive docs"] = function()
17+
child.lua([[
18+
local documentstore = require("rzls.documentstore")
19+
local path = "tests/rzls/fixtures/test2.razor"
20+
local path_prefix = vim.loop.cwd() .. "/"
21+
local full_path = "file://" .. path_prefix .. path
22+
documentstore.register_vbufs_by_path(path_prefix .. path, true)
23+
]])
24+
local bufs = child.api.nvim_list_bufs()
25+
local names = {}
26+
eq(#bufs, 4)
27+
for _, buf in ipairs(bufs) do
28+
names[buf] = child.api.nvim_buf_get_name(buf)
29+
end
30+
local path_prefix = vim.loop.cwd() .. "/"
31+
eq({
32+
"",
33+
path_prefix .. "tests/rzls/fixtures/test2.razor",
34+
path_prefix .. "tests/rzls/fixtures/test2.razor__virtual.cs",
35+
path_prefix .. "tests/rzls/fixtures/test2.razor__virtual.html",
36+
}, names)
37+
end
438

5-
describe("documentstore", function()
6-
it("create and retreive docs", function()
7-
local path = "tests/rzls/fixtures/test2.razor"
8-
local path_prefix = vim.loop.cwd() .. "/"
9-
local full_path = "file://" .. path_prefix .. path
10-
documentstore.register_vbufs_by_path(path_prefix .. path, true)
11-
for _, lang in pairs({ 1, 2 }) do
12-
local doc = documentstore.get_virtual_document(full_path, lang, "any")
13-
assert(doc, "Could not find virtual document")
14-
eq(doc.kind, lang)
15-
end
16-
local bufs = vim.api.nvim_list_bufs()
17-
local names = {}
18-
MiniTest.skip("Need to use child neovim process to test this")
19-
eq(#bufs, 5)
20-
for _, buf in ipairs(bufs) do
21-
names[buf] = vim.api.nvim_buf_get_name(buf)
22-
end
23-
eq({
24-
"",
25-
path_prefix .. "tests/rzls/fixtures/test2.razor",
26-
path_prefix .. "tests/rzls/fixtures/test2.razor__virtual.cs",
27-
path_prefix .. "tests/rzls/fixtures/test2.razor__virtual.html",
28-
}, names)
29-
end)
30-
end)
39+
return T

tests/utils/test_format.lua

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local format = require("rzls.utils.format")
22
local eq = MiniTest.expect.equality
3+
local T = MiniTest.new_set()
34

45
---@return lsp.TextEdit
56
local function lsp_edit(new_text, start_line, start_char, end_line, end_char)
@@ -18,34 +19,35 @@ local function lsp_edit(new_text, start_line, start_char, end_line, end_char)
1819
}
1920
end
2021

21-
describe("format", function()
22-
it("computes minimal text edits for a buffer", function()
23-
local source_text = [[
22+
T["format"] = MiniTest.new_set()
23+
T["format"]["computes minimal text edits for a buffer"] = function()
24+
local source_text = [[
2425
foo
2526
bar
2627
baz
2728
]]
28-
local source_lines = vim.split(source_text, "\n")
29+
local source_lines = vim.split(source_text, "\n")
2930

30-
local targe_text = [[
31+
local targe_text = [[
3132
foo
3233
bar
3334
baz
3435
]]
35-
local target_lines = vim.split(targe_text, "\n")
36+
local target_lines = vim.split(targe_text, "\n")
3637

37-
-- This edit replaces the full document
38-
local full_replacement_edit =
39-
lsp_edit(table.concat(target_lines, "\n"), 0, 0, #source_lines - 1, source_lines[#source_lines]:len())
38+
-- This edit replaces the full document
39+
local full_replacement_edit =
40+
lsp_edit(table.concat(target_lines, "\n"), 0, 0, #source_lines - 1, source_lines[#source_lines]:len())
4041

41-
local minimal_edits = format.compute_minimal_edits(source_lines, full_replacement_edit)
42+
local minimal_edits = format.compute_minimal_edits(source_lines, full_replacement_edit)
4243

43-
-- Only contain edits that remove spaces of the source document to match the target
44-
local expected = {
45-
lsp_edit("", 1, 0, 1, 4),
46-
lsp_edit("", 2, 0, 2, 8),
47-
}
44+
-- Only contain edits that remove spaces of the source document to match the target
45+
local expected = {
46+
lsp_edit("", 1, 0, 1, 4),
47+
lsp_edit("", 2, 0, 2, 8),
48+
}
49+
50+
eq(expected, minimal_edits)
51+
end
4852

49-
eq(expected, minimal_edits)
50-
end)
51-
end)
53+
return T

tests/utils/test_init.lua

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
local eq = MiniTest.expect.equality
22
local neq = MiniTest.expect.no_equality
3+
local T = MiniTest.new_set()
34

4-
describe("uuid", function()
5-
it("generates unique uuids", function()
6-
local utils = require("rzls.utils")
7-
local uuid1 = utils.uuid()
8-
local uuid2 = utils.uuid()
9-
neq(uuid1, uuid2)
10-
end)
11-
it("generates valid uuids", function()
12-
-- 5f72f119-e7b2-433b-b919-df8a74866e45
13-
local utils = require("rzls.utils")
14-
local uuid = utils.uuid()
15-
eq(36, #uuid)
16-
end)
17-
end)
5+
T["UUID"] = MiniTest.new_set()
6+
T["UUID"]["generates unique uuids"] = function()
7+
local utils = require("rzls.utils")
8+
local uuid1 = utils.uuid()
9+
local uuid2 = utils.uuid()
10+
neq(uuid1, uuid2)
11+
end
12+
13+
T["UUID"]["generates valid uuids"] = function()
14+
local utils = require("rzls.utils")
15+
local uuid = utils.uuid()
16+
eq(36, #uuid)
17+
end
18+
19+
return T

tests/utils/test_lcs.lua

Lines changed: 87 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,124 @@
11
local lcs = require("rzls.utils.lcs")
22
local kind = lcs.edit_kind
33
local eq = MiniTest.expect.equality
4-
5-
describe("lcs", function()
6-
it("calculates diff for saturday -> sunday", function()
7-
local edits = lcs.diff("sunday", "saturday")
8-
9-
---@type rzls.lcs.Edit[]
10-
local expected = {
11-
{ text = "s", kind = kind.unchanged },
12-
{ text = "a", kind = kind.addition },
13-
{ text = "t", kind = kind.addition },
14-
{ text = "u", kind = kind.unchanged },
15-
{ text = "n", kind = kind.removal },
16-
{ text = "r", kind = kind.addition },
17-
{ text = "d", kind = kind.unchanged },
18-
{ text = "a", kind = kind.unchanged },
19-
{ text = "y", kind = kind.unchanged },
20-
}
21-
eq(expected, edits)
22-
end)
23-
24-
---@return lsp.TextEdit
25-
local function lsp_edit(new_text, start_line, start_char, end_line, end_char)
26-
return {
27-
newText = new_text,
4+
local T = MiniTest.new_set()
5+
6+
T["LCS"] = MiniTest.new_set()
7+
T["LCS"]["calculates diff for saturday -> sunday"] = function()
8+
local edits = lcs.diff("sunday", "saturday")
9+
10+
---@type rzls.lcs.Edit[]
11+
local expected = {
12+
{ text = "s", kind = kind.unchanged },
13+
{ text = "a", kind = kind.addition },
14+
{ text = "t", kind = kind.addition },
15+
{ text = "u", kind = kind.unchanged },
16+
{ text = "n", kind = kind.removal },
17+
{ text = "r", kind = kind.addition },
18+
{ text = "d", kind = kind.unchanged },
19+
{ text = "a", kind = kind.unchanged },
20+
{ text = "y", kind = kind.unchanged },
21+
}
22+
eq(expected, edits)
23+
end
24+
25+
T["LCS"]["converts edits to lsp.TextEdit's"] = function()
26+
local source = '<div\n\nclass="foo">'
27+
local target = '<div class="foo">'
28+
29+
local edits = lcs.diff(source, target)
30+
local text_edits = lcs.to_lsp_edits(edits, 0, 0)
31+
32+
---@type lsp.TextEdit[]
33+
local expected = {
34+
-- Replaces "\n\n" with " "
35+
{
36+
newText = " ",
2837
range = {
2938
start = {
30-
line = start_line,
31-
character = start_char,
39+
line = 0,
40+
character = 4,
3241
},
3342
["end"] = {
34-
line = end_line,
35-
character = end_char,
43+
line = 2,
44+
character = 0,
3645
},
3746
},
38-
}
39-
end
40-
41-
it("converts edits to lsp.TextEdit's", function()
42-
local source = '<div\n\nclass="foo">'
43-
local target = '<div class="foo">'
44-
45-
local edits = lcs.diff(source, target)
46-
local text_edits = lcs.to_lsp_edits(edits, 0, 0)
47+
},
48+
}
4749

48-
local expected = {
49-
-- Replaces "\n\n" with " "
50-
lsp_edit(" ", 0, 4, 2, 0),
51-
}
50+
eq(expected, text_edits)
51+
end
5252

53-
eq(expected, text_edits)
54-
end)
53+
T["LCS"]["applies converted lsp.TextEdit's to buffer"] = function()
54+
local source = '<div class="bar">'
55+
local target = '<div\n\nclass="bar">'
5556

56-
it("applies converted lsp.TextEdit's to buffer", function()
57-
local source = '<div class="bar">'
58-
local target = '<div\n\nclass="bar">'
57+
local edits = lcs.diff(source, target)
58+
local text_edits = lcs.to_lsp_edits(edits, 0, 0)
5959

60-
local edits = lcs.diff(source, target)
61-
local text_edits = lcs.to_lsp_edits(edits, 0, 0)
60+
local buf = vim.api.nvim_create_buf(false, true)
61+
vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n"))
62+
vim.lsp.util.apply_text_edits(text_edits, buf, "utf-8")
6263

63-
local buf = vim.api.nvim_create_buf(false, true)
64-
vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n"))
65-
vim.lsp.util.apply_text_edits(text_edits, buf, "utf-8")
64+
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true)
65+
eq(target, table.concat(lines, "\n"))
66+
end
6667

67-
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true)
68-
eq(target, table.concat(lines, "\n"))
69-
end)
68+
T["LCS"]["applies converted lsp.TextEdit's to buffer with CRLF line endings"] = function()
69+
local source = '<div class="bar">'
70+
local target = '<div\r\n\r\nclass="bar">'
7071

71-
it("applies converted lsp.TextEdit's to buffer with CRLF line endings", function()
72-
local source = '<div class="bar">'
73-
local target = '<div\r\n\r\nclass="bar">'
72+
local edits = lcs.diff(source, target)
73+
local text_edits = lcs.to_lsp_edits(edits, 0, 0)
7474

75-
local edits = lcs.diff(source, target)
76-
local text_edits = lcs.to_lsp_edits(edits, 0, 0)
75+
local buf = vim.api.nvim_create_buf(false, true)
76+
vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\r\n"))
77+
vim.lsp.util.apply_text_edits(text_edits, buf, "utf-8")
7778

78-
local buf = vim.api.nvim_create_buf(false, true)
79-
vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\r\n"))
80-
vim.lsp.util.apply_text_edits(text_edits, buf, "utf-8")
79+
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true)
80+
eq(target, table.concat(lines, "\r\n"))
81+
end
8182

82-
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true)
83-
eq(target, table.concat(lines, "\r\n"))
84-
end)
85-
86-
it("applies converted edits to buffer with multiple errors", function()
87-
local source = [[
83+
T["LCS"]["applies converted edits to buffer with multiple errors"] = function()
84+
local source = [[
8885
<div
8986
class = "bar">
9087
<h1> Intentional Leading Space</h1>
9188
9289
9390
</div>
9491
]]
95-
local target = [[
92+
local target = [[
9693
<div class="bar">
9794
<h1> Intentional Leading Space</h1>
9895
</div>
9996
]]
10097

101-
local edits = lcs.diff(source, target)
102-
local text_edits = lcs.to_lsp_edits(edits, 0, 0)
98+
local edits = lcs.diff(source, target)
99+
local text_edits = lcs.to_lsp_edits(edits, 0, 0)
100+
101+
local buf = vim.api.nvim_create_buf(false, true)
102+
vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n"))
103+
vim.lsp.util.apply_text_edits(text_edits, buf, "utf-8")
103104

104-
local buf = vim.api.nvim_create_buf(false, true)
105-
vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n"))
106-
vim.lsp.util.apply_text_edits(text_edits, buf, "utf-8")
105+
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true)
106+
eq(target, table.concat(lines, "\n"))
107+
end
107108

108-
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true)
109-
eq(target, table.concat(lines, "\n"))
110-
end)
109+
T["LCS"]["applies edits to unicode characters"] = function()
110+
local source = " <h1>💩</h1>"
111+
local target = "<h1>💩</h1>"
111112

112-
it("applies edits to unicode characters", function()
113-
local source = " <h1>💩</h1>"
114-
local target = "<h1>💩</h1>"
113+
local edits = lcs.diff(source, target)
114+
local text_edits = lcs.to_lsp_edits(edits, 0, 0)
115115

116-
local edits = lcs.diff(source, target)
117-
local text_edits = lcs.to_lsp_edits(edits, 0, 0)
116+
local buf = vim.api.nvim_create_buf(false, true)
117+
vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n"))
118+
vim.lsp.util.apply_text_edits(text_edits, buf, "utf-16")
118119

119-
local buf = vim.api.nvim_create_buf(false, true)
120-
vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n"))
121-
vim.lsp.util.apply_text_edits(text_edits, buf, "utf-16")
120+
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true)
121+
eq(target, table.concat(lines, "\n"))
122+
end
122123

123-
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true)
124-
eq(target, table.concat(lines, "\n"))
125-
end)
126-
end)
124+
return T

0 commit comments

Comments
 (0)