Skip to content

Commit df7b99a

Browse files
author
Jose Alvarez
committed
test: add missing formatting coverage
1 parent 003e4a4 commit df7b99a

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

lua/null-ls/formatting.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ local apply_edits = a.async_void(function(params)
3535
-- default handler doesn't accept bufnr, so call util directly
3636
lsp.util.apply_text_edits(edits, bufnr)
3737

38-
if c.get().save_after_format and bufnr == api.nvim_get_current_buf() then
39-
vim.cmd("silent noautocmd :update")
40-
end
38+
if not _G._TEST and c.get().save_after_format and bufnr ==
39+
api.nvim_get_current_buf() then vim.cmd("silent noautocmd :update") end
4140
end)
4241

4342
M.handler = function(method, original_params, _, bufnr)

test/files/test-file.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import {User} from "./test-types"

test/spec/e2e_spec.lua

+24-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ local lsp_wait = function() vim.wait(400) end
1616
main.setup()
1717

1818
describe("e2e", function()
19+
_G._TEST = true
1920
after_each(function()
20-
vim.cmd("bufdo! bwipeout!")
21+
vim.cmd("bufdo! bdelete!")
2122
c.reset_sources()
2223
end)
2324

@@ -118,7 +119,7 @@ describe("e2e", function()
118119
end)
119120

120121
it("should combine diagnostics from multiple sources", function()
121-
vim.cmd("bufdo! bwipeout!")
122+
vim.cmd("bufdo! bdelete!")
122123

123124
c.register(builtins._test.mock_diagnostics)
124125
tu.edit_test_file("test-file.md")
@@ -127,6 +128,27 @@ describe("e2e", function()
127128
assert.equals(vim.tbl_count(lsp.diagnostic.get()), 2)
128129
end)
129130
end)
131+
132+
describe("formatting", function()
133+
local formatted = "import { User } from \"./test-types\";\n"
134+
135+
before_each(function()
136+
c.register(builtins.formatting.prettier)
137+
138+
tu.edit_test_file("test-file.js")
139+
-- make sure file wasn't accidentally saved
140+
assert.is_not.equals(u.buf.content(nil, true), formatted)
141+
142+
lsp_wait()
143+
end)
144+
145+
it("should format file", function()
146+
lsp.buf.formatting()
147+
lsp_wait()
148+
149+
assert.equals(u.buf.content(nil, true), formatted)
150+
end)
151+
end)
130152
end)
131153

132154
main.shutdown()

test/spec/helpers_spec.lua

+56
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,62 @@ describe("helpers", function()
260260
end)
261261
end)
262262

263+
describe("formatter_factory", function()
264+
local opts = {key = "val"}
265+
266+
before_each(function() stub(helpers, "generator_factory") end)
267+
after_each(function()
268+
helpers.generator_factory:clear()
269+
helpers.generator_factory:revert()
270+
end)
271+
272+
it("should call generator_factory with enriched opts", function()
273+
helpers.formatter_factory(opts)
274+
275+
assert.stub(helpers.generator_factory).was_called_with(opts)
276+
assert.equals(helpers.generator_factory.calls[1].refs[1]
277+
.ignore_errors, true)
278+
assert.truthy(helpers.generator_factory.calls[1].refs[1].on_output)
279+
end)
280+
281+
describe("on_output", function()
282+
local formatter_done = stub.new()
283+
after_each(function() formatter_done:clear() end)
284+
285+
it("should call done and return if no output", function()
286+
helpers.formatter_factory(opts)
287+
local on_formatter_output =
288+
helpers.generator_factory.calls[1].refs[1].on_output
289+
290+
on_formatter_output({}, formatter_done)
291+
292+
assert.stub(formatter_done).was_called()
293+
end)
294+
295+
it("should call done with edit object", function()
296+
helpers.formatter_factory(opts)
297+
local on_formatter_output =
298+
helpers.generator_factory.calls[1].refs[1].on_output
299+
300+
on_formatter_output({
301+
output = "new text",
302+
content = {"line1", "line"}
303+
}, formatter_done)
304+
305+
assert.stub(formatter_done).was_called_with(
306+
{
307+
{
308+
row = 0,
309+
col = 0,
310+
end_row = 2,
311+
end_col = -1,
312+
text = "new text"
313+
}
314+
})
315+
end)
316+
end)
317+
end)
318+
263319
describe("make_builtin", function()
264320
local opts = {
265321
method = "mockMethod",

0 commit comments

Comments
 (0)