1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
local T
local expect = MiniTest.expect
local child = MiniTest.new_child_neovim()
local source = debug.getinfo(1, 'S').source
local root = vim.fn.fnamemodify(source:sub(2), ':p:h:h')
local function write_config(lines)
child.lua_func(function(config_lines)
local path = vim.env.HERDR_SPLITS_CONFIG
vim.fn.mkdir(vim.fn.fnamemodify(path, ':h'), 'p')
vim.fn.writefile(config_lines, path)
end, lines)
end
T = MiniTest.new_set({
hooks = {
pre_case = function()
child.restart({ '--noplugin', '-u', 'NONE', '-i', 'NONE' })
child.cmd('set runtimepath^=' .. vim.fn.fnameescape(root))
child.lua([[
local test_dir = vim.fn.tempname()
vim.fn.mkdir(test_dir, 'p')
vim.g.herdr_splits_test_dir = test_dir
vim.env.HOME = test_dir .. '/home'
vim.env.XDG_CONFIG_HOME = test_dir .. '/xdg'
vim.env.HERDR_SPLITS_CONFIG = test_dir .. '/nested/herdr-splits.conf'
vim.env.HERDR_BIN_PATH = test_dir .. '/missing-herdr'
vim.env.HERDR_PLUGIN_CONFIG_DIR = nil
]])
end,
post_case = function()
local test_dir = child.g.herdr_splits_test_dir
child.stop()
vim.fn.delete(test_dir, 'rf')
end,
},
})
T['to_herdr translates key notation'] = function()
local translate = function(value)
return child.lua_func(function(key)
return require('herdr-splits.conf').to_herdr(key)
end, value)
end
expect.equality(translate('<C-M-Left>'), 'ctrl+alt+left')
expect.equality(translate('<A-Return>'), 'alt+enter')
expect.equality(translate('<S-Space>'), 'shift+space')
expect.equality(translate('H'), 'h')
expect.equality(translate('<Escape>'), 'esc')
expect.equality(translate(''), vim.NIL)
expect.equality(translate(false), vim.NIL)
expect.equality(translate('<C>'), vim.NIL)
end
T['read_managed handles a missing file'] = function()
local result = child.lua_func(function()
local values, adopted = require('herdr-splits.conf').read_managed()
return { adopted = adopted, count = vim.tbl_count(values) }
end)
expect.equality(result, { adopted = false, count = 0 })
end
T['read_managed adopts managed legacy values with last value winning'] = function()
write_config({
'# legacy config',
' nav_key_left = ctrl+old',
'nav_key_left=ctrl+new # last value wins',
'resize_key_up = alt+up',
'unzoom_on_nav=false',
'nav_at_edge=stop',
'unknown_key=ignored',
})
local result = child.lua_func(function()
local values, adopted = require('herdr-splits.conf').read_managed()
return {
adopted = adopted,
nav_left = values.nav_keys.left,
resize_up = values.resize_keys.up,
unzoom_on_nav = values.unzoom_on_nav,
nav_at_edge = values.nav_at_edge,
unknown = values.unknown_key,
}
end)
expect.equality(result, {
adopted = true,
nav_left = 'ctrl+new',
resize_up = 'alt+up',
unzoom_on_nav = false,
nav_at_edge = 'stop',
})
end
T['write creates generated content without leftover temporary files'] = function()
local result = child.lua_func(function()
local conf = require('herdr-splits.conf')
local ok, err = conf.write({
nav_keys = { left = 'ctrl+h', down = 'ctrl+j', up = 'ctrl+k', right = 'ctrl+l' },
resize_keys = { left = 'alt+h', down = 'alt+j', up = 'alt+k', right = 'alt+l' },
unzoom_on_nav = false,
nav_at_edge = 'stop',
})
local values, adopted = conf.read_managed()
return {
ok = ok,
err = err,
adopted = adopted,
adopted_count = vim.tbl_count(values),
}
end)
expect.equality(result, { ok = true, adopted = false, adopted_count = 0 })
local content = child.lua_func(function()
local file = assert(io.open(vim.env.HERDR_SPLITS_CONFIG, 'r'))
local bytes = file:read('*a')
file:close()
return bytes
end)
expect.equality(content, table.concat({
'# herdr-splits-generated-v1',
'# herdr-splits plugin config — GENERATED by herdr-splits.nvim setup().',
'# Regenerated from defaults + setup() opts on every setup(); do not edit by hand.',
'# A headerless/legacy conf is adopted once on migration (see the startup',
'# notice), then overwritten thereafter — remove an opt in setup() to revert.',
'nav_key_left=ctrl+h',
'nav_key_down=ctrl+j',
'nav_key_up=ctrl+k',
'nav_key_right=ctrl+l',
'resize_key_left=alt+h',
'resize_key_down=alt+j',
'resize_key_up=alt+k',
'resize_key_right=alt+l',
'unzoom_on_nav=false',
'nav_at_edge=stop',
'',
}, '\n'))
expect.equality(child.lua_get([[vim.fn.glob(vim.env.HERDR_SPLITS_CONFIG .. '.tmp.*', false, true)]]), {})
end
T['config setup applies precedence and resets omitted managed options'] = function()
write_config({
'nav_key_left=ctrl+legacy',
'resize_key_left=alt+legacy',
'unzoom_on_nav=false',
'nav_at_edge=stop',
})
local result = child.lua_func(function()
vim.notify = function() end
local config = require('herdr-splits.config')
config.setup({
nav_keys = { left = '<C-x>' },
unzoom_on_nav = true,
})
local first = {
nav_left = config.nav_keys.left,
resize_left = config.resize_keys.left,
unzoom_on_nav = config.unzoom_on_nav,
nav_at_edge = config.nav_at_edge,
}
config.setup({ nav_keys = { right = '<C-r>' } })
local second = {
nav_left = config.nav_keys.left,
nav_right = config.nav_keys.right,
resize_left = config.resize_keys.left,
unzoom_on_nav = config.unzoom_on_nav,
nav_at_edge = config.nav_at_edge,
}
return { first = first, second = second }
end)
expect.equality(result, {
first = {
nav_left = 'ctrl+x',
resize_left = 'alt+legacy',
unzoom_on_nav = true,
nav_at_edge = 'stop',
},
second = {
nav_left = 'ctrl+h',
nav_right = 'ctrl+r',
resize_left = 'alt+h',
unzoom_on_nav = true,
nav_at_edge = 'wrap',
},
})
end
return T
|