|
| 1 | +-- imported from https://github.com/echasnovski/mini.nvim |
| 2 | + |
| 3 | +local Helpers = {} |
| 4 | + |
| 5 | +-- Add extra expectations |
| 6 | +Helpers.expect = vim.deepcopy(MiniTest.expect) |
| 7 | + |
| 8 | +Helpers.expect.match = MiniTest.new_expectation("string matching", function(str, pattern) |
| 9 | + return str:find(pattern) ~= nil |
| 10 | +end, function(str, pattern) |
| 11 | + return string.format("Pattern: %s\nObserved string: %s", vim.inspect(pattern), str) |
| 12 | +end) |
| 13 | + |
| 14 | +Helpers.expect.no_match = MiniTest.new_expectation("no string matching", function(str, pattern) |
| 15 | + return str:find(pattern) == nil |
| 16 | +end, function(str, pattern) |
| 17 | + return string.format("Pattern: %s\nObserved string: %s", vim.inspect(pattern), str) |
| 18 | +end) |
| 19 | + |
| 20 | +-- Monkey-patch `MiniTest.new_child_neovim` with helpful wrappers |
| 21 | +Helpers.new_child_neovim = function() |
| 22 | + local child = MiniTest.new_child_neovim() |
| 23 | + |
| 24 | + local prevent_hanging = function(method) |
| 25 | + -- stylua: ignore |
| 26 | + if not child.is_blocked() then return end |
| 27 | + |
| 28 | + local msg = |
| 29 | + string.format("Can not use `child.%s` because child process is blocked.", method) |
| 30 | + error(msg) |
| 31 | + end |
| 32 | + |
| 33 | + child.setup = function() |
| 34 | + child.restart({ "-u", "scripts/minimal_init.lua" }) |
| 35 | + |
| 36 | + -- Change initial buffer to be readonly. This not only increases execution |
| 37 | + -- speed, but more closely resembles manually opened Neovim. |
| 38 | + child.bo.readonly = false |
| 39 | + end |
| 40 | + |
| 41 | + child.set_lines = function(arr, start, finish) |
| 42 | + prevent_hanging("set_lines") |
| 43 | + |
| 44 | + if type(arr) == "string" then |
| 45 | + arr = vim.split(arr, "\n") |
| 46 | + end |
| 47 | + |
| 48 | + child.api.nvim_buf_set_lines(0, start or 0, finish or -1, false, arr) |
| 49 | + end |
| 50 | + |
| 51 | + child.get_lines = function(start, finish) |
| 52 | + prevent_hanging("get_lines") |
| 53 | + |
| 54 | + return child.api.nvim_buf_get_lines(0, start or 0, finish or -1, false) |
| 55 | + end |
| 56 | + |
| 57 | + child.set_cursor = function(line, column, win_id) |
| 58 | + prevent_hanging("set_cursor") |
| 59 | + |
| 60 | + child.api.nvim_win_set_cursor(win_id or 0, { line, column }) |
| 61 | + end |
| 62 | + |
| 63 | + child.get_cursor = function(win_id) |
| 64 | + prevent_hanging("get_cursor") |
| 65 | + |
| 66 | + return child.api.nvim_win_get_cursor(win_id or 0) |
| 67 | + end |
| 68 | + |
| 69 | + child.set_size = function(lines, columns) |
| 70 | + prevent_hanging("set_size") |
| 71 | + |
| 72 | + if type(lines) == "number" then |
| 73 | + child.o.lines = lines |
| 74 | + end |
| 75 | + |
| 76 | + if type(columns) == "number" then |
| 77 | + child.o.columns = columns |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + child.get_size = function() |
| 82 | + prevent_hanging("get_size") |
| 83 | + |
| 84 | + return { child.o.lines, child.o.columns } |
| 85 | + end |
| 86 | + |
| 87 | + --- Assert visual marks |
| 88 | + --- |
| 89 | + --- Useful to validate visual selection |
| 90 | + --- |
| 91 | + ---@param first number|table Table with start position or number to check linewise. |
| 92 | + ---@param last number|table Table with finish position or number to check linewise. |
| 93 | + ---@private |
| 94 | + child.expect_visual_marks = function(first, last) |
| 95 | + child.ensure_normal_mode() |
| 96 | + |
| 97 | + first = type(first) == "number" and { first, 0 } or first |
| 98 | + last = type(last) == "number" and { last, 2147483647 } or last |
| 99 | + |
| 100 | + MiniTest.expect.equality(child.api.nvim_buf_get_mark(0, "<"), first) |
| 101 | + MiniTest.expect.equality(child.api.nvim_buf_get_mark(0, ">"), last) |
| 102 | + end |
| 103 | + |
| 104 | + -- Work with 'mini.nvim': |
| 105 | + -- - `mini_load` - load with "normal" table config |
| 106 | + -- - `mini_load_strconfig` - load with "string" config, which is still a |
| 107 | + -- table but with string values. Final loading is done by constructing |
| 108 | + -- final string table. Needed to be used if one of the config entries is a |
| 109 | + -- function (as currently there is no way to communicate a function object |
| 110 | + -- through RPC). |
| 111 | + -- - `mini_unload` - unload module and revert common side effects. |
| 112 | + child.mini_load = function(name, config) |
| 113 | + local lua_cmd = ([[require('mini.%s').setup(...)]]):format(name) |
| 114 | + child.lua(lua_cmd, { config }) |
| 115 | + end |
| 116 | + |
| 117 | + child.mini_load_strconfig = function(name, strconfig) |
| 118 | + local t = {} |
| 119 | + for key, val in pairs(strconfig) do |
| 120 | + table.insert(t, key .. " = " .. val) |
| 121 | + end |
| 122 | + local str = string.format("{ %s }", table.concat(t, ", ")) |
| 123 | + |
| 124 | + local command = ([[require('mini.%s').setup(%s)]]):format(name, str) |
| 125 | + child.lua(command) |
| 126 | + end |
| 127 | + |
| 128 | + child.mini_unload = function(name) |
| 129 | + local module_name = "mini." .. name |
| 130 | + local tbl_name = "Mini" .. name:sub(1, 1):upper() .. name:sub(2) |
| 131 | + |
| 132 | + -- Unload Lua module |
| 133 | + child.lua(([[package.loaded['%s'] = nil]]):format(module_name)) |
| 134 | + |
| 135 | + -- Remove global table |
| 136 | + child.lua(("_G[%s] = nil"):format(tbl_name)) |
| 137 | + |
| 138 | + -- Remove autocmd group |
| 139 | + if child.fn.exists("#" .. tbl_name) == 1 then |
| 140 | + -- NOTE: having this in one line as `'augroup %s | au! | augroup END'` |
| 141 | + -- for some reason seemed to sometimes not execute `augroup END` part. |
| 142 | + -- That lead to a subsequent bare `au ...` calls to be inside `tbl_name` |
| 143 | + -- group, which gets empty after every `require(<module_name>)` call. |
| 144 | + child.cmd(("augroup %s"):format(tbl_name)) |
| 145 | + child.cmd("au!") |
| 146 | + child.cmd("augroup END") |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + child.expect_screenshot = function(opts, path, screenshot_opts) |
| 151 | + if child.fn.has("nvim-0.8") == 0 then |
| 152 | + MiniTest.skip("Screenshots are tested for Neovim>=0.8 (for simplicity).") |
| 153 | + end |
| 154 | + |
| 155 | + MiniTest.expect.reference_screenshot(child.get_screenshot(screenshot_opts), path, opts) |
| 156 | + end |
| 157 | + |
| 158 | + return child |
| 159 | +end |
| 160 | + |
| 161 | +-- Mark test failure as "flaky" |
| 162 | +Helpers.mark_flaky = function() |
| 163 | + MiniTest.finally(function() |
| 164 | + if #MiniTest.current.case.exec.fails > 0 then |
| 165 | + MiniTest.add_note("This test is flaky.") |
| 166 | + end |
| 167 | + end) |
| 168 | +end |
| 169 | + |
| 170 | +return Helpers |
0 commit comments