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
|
--- Ensure all module bindings have descriptions.
--
-- @copyright 2017 Aidan Holm <aidanholm@gmail.com>
local test = require("tests.lib")
local lousy = require("lousy")
local T = {}
local modes = require "modes"
local clear_all_mode_bindings = function ()
local mode_list = modes.get_modes()
for mode_name in pairs(mode_list) do
local mode = modes.get_mode(mode_name)
mode.binds = nil
end
end
local get_mode_bindings_for_module = function (mod)
-- First require() loads the module and all dependencies
-- Second require() loads just the module
require(mod)
clear_all_mode_bindings()
package.loaded[mod] = nil
require(mod)
local ret = {}
local mode_list = modes.get_modes()
for mode_name in pairs(mode_list) do
local mode = modes.get_mode(mode_name)
for _, m in pairs(mode.binds or {}) do
local b, a = unpack(m)
table.insert(ret, {
name = lousy.bind.bind_to_string(b),
desc = a.desc,
})
end
end
return ret
end
local function add_file_error(errors, file, error, ...)
table.insert(errors, { file = file, err = string.format(error, ...) })
end
T.test_module_binds_have_descriptions = function ()
-- settings.lua must be excluded because re-requiring it will clear all
-- registered settings, causing modules tested after it to crash
local excludes = {"_wm%.lua$", "modes%.lua", "unique_instance%.lua", "settings%.lua"}
local files = test.find_files({"lib/"}, ".+%.lua$", excludes)
local errors = {}
for _, file in ipairs(files) do
local pkg = file:gsub("^%a+/", ""):gsub("%.lua$", ""):gsub("/", ".")
for _, b in ipairs(get_mode_bindings_for_module(pkg)) do
if not b.desc or b.desc == "" then
add_file_error(errors, file, "No description for binding %s", b.name)
end
if b.desc and not b.desc:match("%.$") then
add_file_error(errors, file, "Description for binding %s doesn't end in a full stop.", b.name)
end
if b.desc and b.desc:match("^%l") then
add_file_error(errors, file, "Description for binding %s isn't capitalized.", b.name)
end
end
end
if #errors > 0 then
error("Some bindings are missing descriptions:\n" .. test.format_file_errors(errors))
end
end
return T
-- vim: et:sw=4:ts=8:sts=4:tw=80
|