Skip to content

Commit eb5393b

Browse files
authored
perf(test): improve unit tests (#145)
perf(configs): rename 'bufferchanged' to 'filetype' (#145)
1 parent 11b836e commit eb5393b

File tree

5 files changed

+94
-43
lines changed

5 files changed

+94
-43
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ There're 3 types of filter configs:
282282
283283
- `"startup"`: Choose a color on nvim's start.
284284
- `"interval"`: Choose a color after a fixed interval time.
285-
- `"bufferchanged"`: Choose a color when buffer changed.
285+
- `"filetype"`: Choose a color by file type.
286286
287287
- `policy`:
288288
@@ -298,7 +298,7 @@ There're 3 types of filter configs:
298298
- `seconds`: Fixed interval time by seconds.
299299
- `implement`: Internal policy implementation, e.g. `shuffle`, `in_order`, `reverse_order`, `single` builtin policies.
300300
301-
- By filetype policy, works with `timing = "bufferchanged"`.
301+
- By filetype policy, works with `timing = "filetype"`.
302302
303303
- `mapping`: A lua table to map file type to colorscheme.
304304
- `fallback`: Default colorscheme when file type is not mapped.
@@ -351,7 +351,7 @@ require('colorbox').setup({
351351
},
352352
fallback = "solarized8",
353353
},
354-
timing = "bufferchanged",
354+
timing = "filetype",
355355
})
356356
```
357357

lua/colorbox.lua

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ local Defaults = {
2222
--- @type colorbox.PolicyConfig
2323
policy = "shuffle",
2424

25-
--- @type "startup"|"interval"|"bufferchanged"
25+
--- @type "startup"|"interval"|"filetype"
2626
timing = "startup",
2727

2828
-- (Optional) filters that disable some colors that you don't want.
@@ -235,7 +235,7 @@ end
235235

236236
local function _policy_shuffle()
237237
if #FilteredColorNamesList > 0 then
238-
local i = numbers.random(#FilteredColorNamesList)
238+
local i = numbers.random(#FilteredColorNamesList) --[[@as integer]]
239239
local color = _get_next_color_name_by_idx(i)
240240
logging.get("colorbox"):debug(
241241
"|_policy_shuffle| color:%s, FilteredColorNamesList:%s (%d), i:%d",
@@ -244,22 +244,16 @@ local function _policy_shuffle()
244244
vim.inspect(#FilteredColorNamesList),
245245
vim.inspect(i)
246246
)
247-
local ok, err = pcall(
248-
vim.cmd --[[@as function]],
249-
string.format([[color %s]], color)
250-
)
251-
assert(ok, err)
247+
vim.cmd(string.format([[color %s]], color))
252248
end
253249
end
254250

255251
local function _policy_in_order()
256252
if #FilteredColorNamesList > 0 then
257253
local previous_track = _load_previous_track() --[[@as colorbox.PreviousTrack]]
258-
local i = previous_track ~= nil and previous_track.color_number or 1
254+
local i = previous_track ~= nil and previous_track.color_number or 0
259255
local color = _get_next_color_name_by_idx(i)
260-
---@diagnostic disable-next-line: param-type-mismatch
261-
local ok, err = pcall(vim.cmd, string.format([[color %s]], color))
262-
assert(ok, err)
256+
vim.cmd(string.format([[color %s]], color))
263257
end
264258
end
265259

@@ -269,25 +263,17 @@ local function _policy_reverse_order()
269263
local i = previous_track ~= nil and previous_track.color_number
270264
or (#FilteredColorNamesList + 1)
271265
local color = _get_prev_color_name_by_idx(i)
272-
---@diagnostic disable-next-line: param-type-mismatch
273-
local ok, err = pcall(vim.cmd, string.format([[color %s]], color))
274-
assert(ok, err)
266+
vim.cmd(string.format([[color %s]], color))
275267
end
276268
end
277269

278270
local function _policy_single()
279271
if #FilteredColorNamesList > 0 then
280272
local previous_track = _load_previous_track() --[[@as colorbox.PreviousTrack]]
281-
local color = nil
282-
if previous_track then
283-
color = previous_track.color_name
284-
else
285-
color = _get_next_color_name_by_idx(0)
286-
end
273+
local color = previous_track ~= nil and previous_track.color_name
274+
or _get_next_color_name_by_idx(0)
287275
if color ~= vim.g.colors_name then
288-
---@diagnostic disable-next-line: param-type-mismatch
289-
local ok, err = pcall(vim.cmd, string.format([[color %s]], color))
290-
assert(ok, err)
276+
vim.cmd(string.format([[color %s]], color))
291277
end
292278
end
293279
end
@@ -340,18 +326,15 @@ local function _policy_by_filetype()
340326

341327
if Configs.policy.mapping[ft] then
342328
local ok, err = pcall(
343-
---@diagnostic disable-next-line: param-type-mismatch
344-
vim.cmd,
329+
vim.cmd --[[@as function]],
345330
string.format([[color %s]], Configs.policy.mapping[ft])
346331
)
347332
assert(ok, err)
348333
else
349-
local ok, err =
350-
---@diagnostic disable-next-line: param-type-mismatch
351-
pcall(
352-
vim.cmd,
353-
string.format([[color %s]], Configs.policy.fallback)
354-
)
334+
local ok, err = pcall(
335+
vim.cmd --[[@as function]],
336+
string.format([[color %s]], Configs.policy.fallback)
337+
)
355338
assert(ok, err)
356339
end
357340
_force_sync_syntax()
@@ -374,7 +357,8 @@ local function _policy()
374357
_policy_fixed_interval()
375358
elseif
376359
Configs.timing == "bufferchanged"
377-
and _is_by_filetype_policy(Configs.policy)
360+
or Configs.timing == "filetype"
361+
and _is_by_filetype_policy(Configs.policy)
378362
then
379363
_policy_by_filetype()
380364
end
@@ -386,7 +370,7 @@ local function _timing_startup()
386370
})
387371
end
388372

389-
local function _timing_buffer_changed()
373+
local function _timing_filetype()
390374
vim.api.nvim_create_autocmd({ "BufNew", "BufReadPre", "BufNewFile" }, {
391375
callback = _policy,
392376
})
@@ -404,15 +388,17 @@ local function _timing()
404388
)
405389
)
406390
_policy_fixed_interval()
407-
elseif Configs.timing == "bufferchanged" then
391+
elseif
392+
Configs.timing == "bufferchanged" or Configs.timing == "filetype"
393+
then
408394
assert(
409395
_is_by_filetype_policy(Configs.policy),
410396
string.format(
411-
"invalid policy %s for 'bufferchanged' timing!",
397+
"invalid policy %s for 'filetype' timing!",
412398
vim.inspect(Configs.policy)
413399
)
414400
)
415-
_timing_buffer_changed()
401+
_timing_filetype()
416402
else
417403
error(string.format("invalid timing %s!", vim.inspect(Configs.timing)))
418404
end
@@ -794,6 +780,14 @@ local function setup(opts)
794780
_timing()
795781
end
796782

783+
local function _get_filtered_color_names_list()
784+
return FilteredColorNamesList
785+
end
786+
787+
local function _get_filtered_color_name_to_index_map()
788+
return FilteredColorNameToIndexMap
789+
end
790+
797791
local M = {
798792
setup = setup,
799793
update = update,
@@ -803,6 +797,15 @@ local M = {
803797
_force_sync_syntax = _force_sync_syntax,
804798
_save_track = _save_track,
805799
_load_previous_track = _load_previous_track,
800+
_get_next_color_name_by_idx = _get_next_color_name_by_idx,
801+
_get_prev_color_name_by_idx = _get_prev_color_name_by_idx,
802+
_get_filtered_color_names_list = _get_filtered_color_names_list,
803+
_get_filtered_color_name_to_index_map = _get_filtered_color_name_to_index_map,
804+
_policy_shuffle = _policy_shuffle,
805+
_policy_in_order = _policy_in_order,
806+
_policy_reverse_order = _policy_reverse_order,
807+
_policy_single = _policy_single,
808+
_policy = _policy,
806809
}
807810

808811
return M

lua/colorbox/commons/numbers.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,21 @@ end
126126

127127
--- @param m integer?
128128
--- @param n integer?
129-
--- @return number?, string?
129+
--- @return number
130130
M.random = function(m, n)
131131
local rand_result, rand_err = require("colorbox.commons.uv").random(4)
132132
if rand_result == nil then
133-
return nil, rand_err
133+
if m == nil and n == nil then
134+
return math.random()
135+
elseif m ~= nil and n == nil then
136+
return math.random(m)
137+
else
138+
return math.random(m --[[@as integer]], n --[[@as integer]])
139+
end
134140
end
135-
local bytes = { string.byte(rand_result, 1, -1) }
141+
local bytes = {
142+
string.byte(rand_result --[[@as string]], 1, -1),
143+
}
136144
local total = 0
137145
for _, b in ipairs(bytes) do
138146
total = M.mod(total * 256 + b, M.INT32_MAX)

lua/colorbox/commons/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.4.0
1+
3.4.1

test/colorbox_spec.lua

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,44 @@ describe("colorbox", function()
7474
end
7575
end)
7676
end)
77+
describe(
78+
"[_get_next_color_name_by_idx/_get_prev_color_name_by_idx]",
79+
function()
80+
it("_get_next_color_name_by_idx", function()
81+
local colornames = colorbox._get_filtered_color_names_list()
82+
local colorindexes =
83+
colorbox._get_filtered_color_name_to_index_map()
84+
for i, c in ipairs(colornames) do
85+
local actual = colorbox._get_next_color_name_by_idx(i)
86+
if i == #colornames then
87+
assert_eq(colorindexes[actual], 1)
88+
else
89+
assert_eq(i + 1, colorindexes[actual])
90+
end
91+
end
92+
end)
93+
it("_get_prev_color_name_by_idx", function()
94+
local colornames = colorbox._get_filtered_color_names_list()
95+
local colorindexes =
96+
colorbox._get_filtered_color_name_to_index_map()
97+
for i, c in ipairs(colornames) do
98+
local actual = colorbox._get_prev_color_name_by_idx(i)
99+
if i == 1 then
100+
assert_eq(colorindexes[actual], #colornames)
101+
else
102+
assert_eq(i - 1, colorindexes[actual])
103+
end
104+
end
105+
end)
106+
end
107+
)
108+
describe("[_policy]", function()
109+
it("test", function()
110+
colorbox._policy_shuffle()
111+
colorbox._policy_in_order()
112+
colorbox._policy_reverse_order()
113+
colorbox._policy_single()
114+
colorbox._policy()
115+
end)
116+
end)
77117
end)

0 commit comments

Comments
 (0)