Skip to content

Commit fd00751

Browse files
committed
test: improve coverage
1 parent 1d4fbf4 commit fd00751

9 files changed

Lines changed: 135 additions & 24 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ DEPS_DIR := $(CURDIR)/deps
44

55
# Download mini.nvim if not present, then run all tests
66
test: deps
7-
nvim --headless -u tests/minimal_init.lua -c "lua MiniTest.run()" 2>&1
7+
nvim --headless -u tests/scripts/minimal_init.lua -c "lua MiniTest.run()"
88

99
# Clone mini.nvim into deps/
1010
deps:

‎lua/zen/init.lua‎

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
--- @field filetype Filetype
55

66
--- @class Config
7-
--- @field main { width: number };
8-
--- @field top Integration[];
9-
--- @field right { min_width: number; [number]: Integration[]};
10-
--- @field bottom Integration[];
11-
--- @field left { min_width: number; [number]: Integration[]};
7+
--- @field main? { width?: number };
8+
--- @field top? Integration[];
9+
--- @field right? { min_width?: number; [number]?: Integration[]};
10+
--- @field bottom? Integration[];
11+
--- @field left? { min_width?: number; [number]?: Integration[]};
1212

13-
--- @type Config
13+
--- @type Config?
1414
local opts = {
1515
main = { width = 148 },
1616
top = {},
@@ -21,6 +21,7 @@ local opts = {
2121
local state = {
2222
[vim.api.nvim_get_current_tabpage()] = { left = nil, right = nil },
2323
}
24+
local is_quitting = false
2425

2526
local function create_window(position)
2627
if position == "left" then
@@ -223,7 +224,7 @@ local function close(filetype)
223224
end
224225
end
225226

226-
---@param options Config
227+
---@param options? Config
227228
local function setup(options)
228229
-- Default splitting will cause your main splits to jump when opening an integration.
229230
-- To prevent this, set `splitkeep` to either `screen` or `topline`.
@@ -292,15 +293,30 @@ local function setup(options)
292293
if is_popup_window(vim.api.nvim_get_current_win()) then
293294
return
294295
end
295-
if vim.tbl_count(get_editable_files()) == 1 and not is_buff_integration(args.buf) then
296-
close_side_buffer("left")
297-
close_side_buffer("right")
296+
if is_buff_integration(args.buf) then
297+
return
298+
end
298299

299-
for _, position in ipairs({ "top", "right", "bottom", "left" }) do
300-
for _, integration in pairs(opts[position]) do
301-
if type(integration) == "table" then
302-
close(integration.filetype)
303-
end
300+
local editable_count = 0
301+
for _, win in ipairs(vim.api.nvim_list_wins()) do
302+
if not is_popup_window(win) then
303+
local buf = vim.api.nvim_win_get_buf(win)
304+
if not is_buff_integration(buf) then
305+
editable_count = editable_count + 1
306+
end
307+
end
308+
end
309+
if editable_count ~= 1 then
310+
return
311+
end
312+
313+
close_side_buffer("left")
314+
close_side_buffer("right")
315+
316+
for _, position in ipairs({ "top", "right", "bottom", "left" }) do
317+
for _, integration in pairs(opts[position]) do
318+
if type(integration) == "table" then
319+
close(integration.filetype)
304320
end
305321
end
306322
end

‎tests/scripts/helpers.lua‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
local Helpers = {}
2+
3+
Helpers.expect = vim.deepcopy(MiniTest.expect)
4+
5+
local function error_message(str, pattern)
6+
return string.format("Pattern: %s\nObserved string: %s", vim.inspect(pattern), str)
7+
end
8+
9+
Helpers.expect.layout = MiniTest.new_expectation("window layout matches", function(child, expected)
10+
local layout = child.lua_func(function()
11+
local function enrich(node)
12+
if node[1] == "leaf" then
13+
local win = node[2]
14+
local buf = vim.api.nvim_win_get_buf(win)
15+
return {
16+
type = "leaf",
17+
filetype = vim.bo[buf].filetype,
18+
buftype = vim.bo[buf].buftype,
19+
width = vim.api.nvim_win_get_width(win),
20+
height = vim.api.nvim_win_get_height(win),
21+
}
22+
end
23+
local result = {}
24+
for i, n in ipairs(node[2]) do
25+
result[i] = enrich(n)
26+
end
27+
return { type = node[1], children = result }
28+
end
29+
return enrich(vim.fn.winlayout())
30+
end)
31+
return Helpers.expect.equality(layout, expected)
32+
end, error_message)
33+
34+
return Helpers

‎tests/scripts/init_with_zen.lua‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
vim.cmd([[let &rtp.=','.getcwd()]])
2+
vim.cmd("set rtp+=deps/mini.nvim")
3+
4+
vim.o.columns = 240
5+
vim.o.lines = 52
6+
7+
require("mini.test").setup()
8+
require("zen").setup()

‎tests/test_closing.lua‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local Helpers = dofile("tests/scripts/helpers.lua")
2+
local child = MiniTest.new_child_neovim()
3+
4+
local T = MiniTest.new_set({
5+
hooks = {
6+
pre_case = function()
7+
child.restart({ "-u", "tests/scripts/init_with_zen.lua" })
8+
end,
9+
post_once = child.stop,
10+
},
11+
})
12+
13+
T["close side buffers on last main quit"] = function()
14+
Helpers.expect.layout(child, {
15+
type = "row",
16+
children = {
17+
{ type = "leaf", filetype = "zen-left", buftype = "nofile", width = 46, height = 50 },
18+
{ type = "leaf", filetype = "", buftype = "", width = 146, height = 50 },
19+
{ type = "leaf", filetype = "zen-right", buftype = "nofile", width = 46, height = 50 },
20+
},
21+
})
22+
23+
pcall(child.cmd, "q")
24+
25+
local RUNNING = -1
26+
MiniTest.expect.equality(vim.fn.jobwait({ child.job.id })[1] ~= RUNNING, true)
27+
end
28+
29+
return T

‎tests/test_opening.lua‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local Helpers = dofile("tests/scripts/helpers.lua")
2+
local child = MiniTest.new_child_neovim()
3+
4+
local T = MiniTest.new_set({
5+
hooks = {
6+
pre_case = function()
7+
child.restart({ "-u", "tests/scripts/init_with_zen.lua" })
8+
end,
9+
post_once = child.stop,
10+
},
11+
})
12+
13+
T["open side buffers on startup"] = function()
14+
Helpers.expect.layout(child, {
15+
type = "row",
16+
children = {
17+
{ type = "leaf", filetype = "zen-left", buftype = "nofile", width = 46, height = 50 },
18+
{ type = "leaf", filetype = "", buftype = "", width = 146, height = 50 },
19+
{ type = "leaf", filetype = "zen-right", buftype = "nofile", width = 46, height = 50 },
20+
},
21+
})
22+
end
23+
24+
return T

‎tests/test_setup.lua‎

Lines changed: 0 additions & 8 deletions
This file was deleted.

‎tests/test_splits.lua‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
local T = MiniTest.new_set()
2+
3+
T["close side buffers when a vertical split is opened"] = function()
4+
require("zen").setup({ main = { with = 100 } })
5+
MiniTest.expect.equality(vim.o.splitkeep, "screen")
6+
end
7+
8+
return T

0 commit comments

Comments
 (0)