Something went wrong. Try again.
Library of 45+ independent Lua modules improving Neovim experience with minimal effort nvim-mini.org/mini.nvim
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767-- This is intended to mostly cover general API. In a sense, all tests in this-- plugin also test 'mini.test'.local helpers = dofile('tests/helpers.lua')
local child = helpers.new_child_neovim()local expect, eq = helpers.expect, helpers.expect.equalitylocal new_set, finally = MiniTest.new_set, MiniTest.finally
-- Helpers with child processeslocal load_module = function(config) child.mini_load('test', config) endlocal unload_module = function() child.mini_unload('test') endlocal set_cursor = function(...) return child.set_cursor(...) endlocal set_lines = function(...) return child.set_lines(...) endlocal get_lines = function(...) return child.get_lines(...) end
local get_latest_message = function() return child.cmd_capture('1messages') end
local get_ref_path = function(name) return string.format('tests/dir-test/%s', name) end
local get_current_all_cases = function() -- Encode functions inside child. Works only for "simple" functions. local command = [[vim.tbl_map(function(case) case.hooks.pre = vim.tbl_map(string.dump, case.hooks.pre) case.hooks.post = vim.tbl_map(string.dump, case.hooks.post) case.test = string.dump(case.test) return case end, MiniTest.current.all_cases)]] local res = child.lua_get(command)
-- Decode functions in current process res = vim.tbl_map(function(case) case.hooks.pre = vim.tbl_map(loadstring, case.hooks.pre) case.hooks.post = vim.tbl_map(loadstring, case.hooks.post) ---@diagnostic disable-next-line: param-type-mismatch case.test = loadstring(case.test) return case end, res)
-- Update array to enable getting element by last entry of `desc` field return setmetatable(res, { __index = function(t, key) return vim.tbl_filter(function(case_output) local last_desc = case_output.desc[#case_output.desc] return last_desc == key end, t)[1] end, })end
local wait_till_not_executing = function() child.lua('vim.wait(100000, function() return not MiniTest.is_executing() end, 20)')end
local testrun_ref_file = function(name) local find_files_command = string.format([[_G.find_files = function() return { '%s' } end]], get_ref_path(name)) child.lua(find_files_command) child.lua([[ _G.reporter_log = {} _G.report_in_log = { start = function(...) table.insert(_G.reporter_log, { 'start', ... }) end, update = function(...) table.insert(_G.reporter_log, { 'update', vim.deepcopy(MiniTest.current.case.exec.state), ... }) end, finish = function(...) table.insert(_G.reporter_log, { 'finish', ... }) end, } ]]) child.lua([[MiniTest.run({ collect = { find_files = _G.find_files }, execute = { reporter = _G.report_in_log } })]]) wait_till_not_executing() return get_current_all_cases()end
local filter_by_desc = function(cases, id, value) return vim.tbl_filter(function(c) return c.desc[id] == value end, cases)end
local expect_all_state = function(cases, state) local res = true for _, c in ipairs(cases) do if type(c.exec) ~= 'table' or c.exec.state ~= state then res = false end end
eq(res, true)end
-- Time constantslocal terminal_wait = helpers.get_time_const(500)local wait_time = helpers.get_time_const(100)
-- Output test setlocal T = new_set({ hooks = { pre_case = function() child.setup() -- Mock UI so that default reporter is as if used interactively child.lua('vim.api.nvim_list_uis = function() return { 1 } end') load_module() end, post_once = child.stop, }, n_retry = helpers.get_n_retry(1),})
-- Unit tests =================================================================T['setup()'] = new_set()
T['setup()']['creates side effects'] = function() -- Global variable eq(child.lua_get('type(_G.MiniTest)'), 'table')
-- Autocommand group eq(child.fn.exists('#MiniTest'), 1)
-- Highlight groups child.cmd('hi clear') load_module() expect.match(child.cmd_capture('hi MiniTestFail'), 'gui=bold') expect.match(child.cmd_capture('hi MiniTestPass'), 'gui=bold') expect.match(child.cmd_capture('hi MiniTestEmphasis'), 'gui=bold')end
T['setup()']['creates `config` field'] = function() eq(child.lua_get('type(_G.MiniTest.config)'), 'table')
-- Check default values local expect_config = function(field, value) eq(child.lua_get('MiniTest.config.' .. field), value) end
expect_config('collect.emulate_busted', true) eq(child.lua_get('type(_G.MiniTest.config.collect.find_files)'), 'function') eq(child.lua_get('type(_G.MiniTest.config.collect.filter_cases)'), 'function') expect_config('execute.reporter', vim.NIL) expect_config('execute.stop_on_error', false) expect_config('script_path', 'scripts/minitest.lua') expect_config('silent', false)end
T['setup()']['respects `config` argument'] = function() unload_module() load_module({ script_path = 'a' }) eq(child.lua_get('MiniTest.config.script_path'), 'a')end
T['setup()']['validates `config` argument'] = function() unload_module()
local expect_config_error = function(config, name, target_type) expect.error(function() load_module(config) end, vim.pesc(name) .. '.*' .. vim.pesc(target_type)) end
expect_config_error('a', 'config', 'table') expect_config_error({ collect = 'a' }, 'collect', 'table') expect_config_error({ collect = { emulate_busted = 'a' } }, 'collect.emulate_busted', 'boolean') expect_config_error({ collect = { find_files = 'a' } }, 'collect.find_files', 'function') expect_config_error({ collect = { filter_cases = 'a' } }, 'collect.filter_cases', 'function') expect_config_error({ execute = 'a' }, 'execute', 'table') expect_config_error({ execute = { reporter = 'a' } }, 'execute.reporter', 'function') expect_config_error({ execute = { stop_on_error = 'a' } }, 'execute.stop_on_error', 'boolean') expect_config_error({ script_path = 1 }, 'script_path', 'string') expect_config_error({ silent = 1 }, 'silent', 'boolean')end
T['setup()']['ensures colors'] = function() child.cmd('colorscheme default') expect.match(child.cmd_capture('hi MiniTestFail'), 'gui=bold guifg=#ffc0b9') expect.match(child.cmd_capture('hi MiniTestPass'), 'gui=bold guifg=#b3f6c0') expect.match(child.cmd_capture('hi MiniTestEmphasis'), 'gui=bold')
-- Changing bg with enabled colorscheme should recompute computed attributes child.cmd('set bg=light') expect.match(child.cmd_capture('hi MiniTestFail'), 'gui=bold guifg=#590008') expect.match(child.cmd_capture('hi MiniTestPass'), 'gui=bold guifg=#005523')end
T['new_set()'] = new_set()
T['new_set()']['tracks field order'] = function() local res = testrun_ref_file('testref_new-set.lua')
-- Check order local order_cases = vim.tbl_map( function(c) return c.desc[#c.desc] end, vim.tbl_filter(function(c) return c.desc[2] == 'order' end, res) ) eq(order_cases, { 'From initial call', 'zzz First added', 'aaa Second added', 1 })end
T['new_set()']['stores `opts`'] = function() local opts = { parametrize = { { 'a' } }, n_retry = 10 } child.lua([[_G.set = MiniTest.new_set(...)]], { opts }) eq(child.lua_get([[getmetatable(_G.set).opts]]), opts)end
T['case helpers'] = new_set()
T['case helpers']['work'] = function() local res = testrun_ref_file('testref_case-helpers.lua')
local validate = function(name, state, notes) ---@diagnostic disable-next-line: undefined-field eq(res[name].exec.state, state) ---@diagnostic disable-next-line: undefined-field eq(res[name].exec.notes, notes) end
-- `finally()` validate('finally() with error; check', 'Pass', {}) validate('finally() no error; check', 'Pass', {}) eq(child.lua_get('_G.finally_log'), { 'one', 'two' })
-- `skip()` validate('skip(); no message', 'Pass with notes', { 'Skip test' }) validate('skip(); with message', 'Pass with notes', { 'This is a custom skip message' }) validate('skip() can be called from helper', 'Pass with notes', { 'Skip from helper' }) validate('skip one', 'Pass with notes', { 'Should skip case' }) validate('skip two', 'Pass with notes', { 'Should skip case' })
validate('skip() in other hooks', 'Fail', {})
-- `add_note()` validate('add_note() case', 'Pass with notes', { 'pre_once', 'pre_case', 'test case', 'post_case', 'post_once' })end
T['run()'] = new_set()
T['run()']['respects `opts` argument'] = function() child.lua('MiniTest.run({ collect = { find_files = function() return {} end } })') eq(#get_current_all_cases(), 0)
-- Should also use buffer local config local general_file = get_ref_path('testref_general.lua') local command = string.format( [[vim.b.minitest_config = { collect = { find_files = function() return { '%s' } end } }]], general_file ) child.lua(command) child.lua('MiniTest.run()') eq(#get_current_all_cases() > 0, true)end
T['run()']['tries to execute script if no arguments are supplied'] = function() local validate = function() local cache_local_config = child.b.minitest_config
eq(child.lua_get('_G.custom_script_result'), vim.NIL) child.lua('MiniTest.run()') eq(child.lua_get('_G.custom_script_result'), 'This actually ran')
-- Global and buffer local config should be restored eq(child.lua_get('type(MiniTest.config.aaa)'), 'nil') eq(child.b.minitest_config, cache_local_config) end
local script_path = get_ref_path('testref_custom-script.lua') child.lua('MiniTest.config.script_path = ' .. vim.inspect(script_path)) validate()
-- Should also use buffer local config child.lua([[MiniTest.config.script_path = '']]) child.lua('_G.custom_script_result = nil') child.b.minitest_config = { script_path = script_path } validate()end
--stylua: ignoreT['run()']['handles `parametrize`'] = function() local res = testrun_ref_file('testref_run-parametrize.lua') eq(#res, 10)
local short_res = vim.tbl_map(function(c) local desc = vim.list_slice(c.desc, 2) return { args = c.args, desc = desc, passed_args = c.exec.fails[1]:match('Passed arguments: (.-)\n Traceback.*$') } end, res)
eq(short_res[1], { args = { 'a' }, desc = { 'parametrize', 'first level' }, passed_args = '"a"' }) eq(short_res[2], { args = { 'b' }, desc = { 'parametrize', 'first level' }, passed_args = '"b"' })
eq(short_res[3], { args = { 'a', 1 }, desc = { 'parametrize', 'nested', 'test' }, passed_args = '"a", 1' }) eq(short_res[4], { args = { 'a', 2 }, desc = { 'parametrize', 'nested', 'test' }, passed_args = '"a", 2' }) eq(short_res[5], { args = { 'b', 1 }, desc = { 'parametrize', 'nested', 'test' }, passed_args = '"b", 1' }) eq(short_res[6], { args = { 'b', 2 }, desc = { 'parametrize', 'nested', 'test' }, passed_args = '"b", 2' })
eq(short_res[7], { args = { 'a', 'a', 1, 1 }, desc = { 'multiple args', 'nested', 'test' }, passed_args = '"a", "a", 1, 1' }) eq(short_res[8], { args = { 'a', 'a', 2, 2 }, desc = { 'multiple args', 'nested', 'test' }, passed_args = '"a", "a", 2, 2' }) eq(short_res[9], { args = { 'b', 'b', 1, 1 }, desc = { 'multiple args', 'nested', 'test' }, passed_args = '"b", "b", 1, 1' }) eq(short_res[10], { args = { 'b', 'b', 2, 2 }, desc = { 'multiple args', 'nested', 'test' }, passed_args = '"b", "b", 2, 2' })end
T['run()']['validates `parametrize`'] = function() expect.error( function() testrun_ref_file('testref_run-parametrize-error.lua') end, [[`parametrize` should have only tables. Got "a"]] )end
T['run()']['handles `data`'] = function() local res = testrun_ref_file('testref_run-data.lua') local short_res = vim.tbl_map(function(c) return { data = c.data, desc = vim.list_slice(c.desc, 2) } end, res)
eq(#short_res, 2) eq(short_res[1], { data = { a = 1, b = 2 }, desc = { 'data', 'first level' }, }) eq(short_res[2], { data = { a = 10, b = 2, c = 30 }, desc = { 'data', 'nested', 'should override' }, })end
T['run()']['handles `hooks`'] = function() local res = testrun_ref_file('testref_run-hooks.lua') --stylua: ignore eq(child.lua_get('_G.log'), { -- Test order "pre_once_1", "pre_case_1", "First level test", "post_case_1", "pre_once_2", "pre_case_1", "pre_case_2", "Nested #1", "post_case_2", "post_case_1", "pre_case_1", "pre_case_2", "Nested #2", "post_case_2", "post_case_1", "post_once_2", "post_once_1",
-- Test skip case on hook error. All hooks should still be called. "pre_case_3", "post_case_3", "post_once_3", "pre_once_4", "post_case_4", "post_once_4",
-- Using same function in `*_once` hooks should still lead to its multiple -- execution. "Same function", "Same function", "Same hook test", "Same function", "Same function", })
-- Skipping test case due to hook errors should add a note expect.match(filter_by_desc(res, 2, 'skip_case_on_hook_error #1')[1].exec.notes[1], '^Skip.*error.*hooks')end
T['run()']['handles `n_retry`'] = function() local res = testrun_ref_file('testref_run-n_retry.lua') --stylua: ignore local ref_log = { 'default', 'should override', 'should override', 'more override', 'more override', 'more override', 'first success #1', 'first success #2', 'first success #3', 'latest error #1', 'latest error #2', 'latest error #3',
-- Retries should NOT be applied separately to hooks 'no retry pre_once', 'no retry pre_case', 'no retry post_case', 'no retry post_once',
-- Should retry failed case with all its `pre_case` and `post_case`. -- The `pre_once` and `post_once` should still be executed only once. 'outer pre_once', 'inner pre_once', 'outer pre_case', 'inner pre_case', 'hook exec case', 'inner post_case', 'outer post_case', 'outer pre_case', 'inner pre_case', 'hook exec case', 'inner post_case', 'outer post_case', 'inner post_once', 'outer post_once',
'screenshot', 'screenshot', 'skip', 'parameter 1', 'parameter 1', 'parameter 2', 'parameter 2' } eq(child.lua_get('_G.log'), ref_log)
-- Should track relevant `n_retry` value eq(vim.tbl_map(function(x) return x.n_retry end, res), { 1, 2, 3, 10, 3, 2, 2, 2, 2, 2, 2, 2 })
-- Should report latest (and only latest) error among all retries local latest_error_case_fails = filter_by_desc(res, 2, 'reports latest error')[1].exec.fails eq(#latest_error_case_fails, 1) expect.match(latest_error_case_fails[1], 'Error #3')
-- Should report hook fails even if `n_retry > 1` eq(#filter_by_desc(res, 2, 'does not retry hooks')[1].exec.fails, 4)
-- Should not generate new screenshots after first try eq(filter_by_desc(res, 2, 'screenshot number')[1].exec.notes, {})
-- Should update state on every retry local update_case_num for i, case in ipairs(res) do if case.desc[2] == 'updates state on every retry' then update_case_num = i end end child.lua('_G.target_case_num = ' .. (update_case_num or #res)) local updates_of_target_case = child.lua([[ local res = {} for _, t in ipairs(_G.reporter_log) do if t[1] == 'update' and t[3] == target_case_num then table.insert(res, t) end end return res ]]) local ref_updates = { { 'update', "Executing 'pre' hook #1", 12 }, { 'update', "Executing 'pre' hook #2", 12 }, { 'update', 'Executing test', 12 }, { 'update', "Executing 'post' hook #1", 12 }, -- Method `update()` of reporter should be called on every retry { 'update', "Executing 'pre' hook #2", 12 }, { 'update', 'Executing test', 12 }, { 'update', "Executing 'post' hook #1", 12 }, { 'update', "Executing 'post' hook #2", 12 }, { 'update', 'Fail', 12 }, } eq(updates_of_target_case, ref_updates)end
T['run()']['respects `stop_on_error` after all retries'] = function() local path = get_ref_path('testref_run-n_retry-stop_on_error.lua') local command = string.format([[_G.cases = MiniTest.collect({ find_files = function() return { '%s' } end })]], path) child.lua(command)
-- Should indeed stop on first "final" error (i.e. ignore errors from tries) child.lua('MiniTest.execute(_G.cases, { stop_on_error = true })') eq(child.lua_get('_G.log'), { 'try #1', 'try #2', 'try #3', 'continue' }) eq(child.lua_get('_G.cases[3].exec'), vim.NIL)end
T['run()']['appends traceback to fails'] = function() local res = testrun_ref_file('testref_general.lua') local ref_path = get_ref_path('testref_general.lua') local n = 0 for _, case in ipairs(res) do if #case.exec.fails > 0 then expect.match(case.exec.fails[1], 'Traceback:%s+' .. vim.pesc(ref_path)) n = n + 1 end end
if n == 0 then error('No actual fails was tested', 0) endend
T['run_file()'] = new_set()
T['run_file()']['works'] = function() child.lua([[MiniTest.run_file(...)]], { get_ref_path('testref_run.lua') }) local last_desc = child.lua_get([[vim.tbl_map(function(case) return case.desc[#case.desc] end, MiniTest.current.all_cases)]]) eq(last_desc, { 'run_at_location()', 'extra case' })end
T['run_file()']['normalizes input path'] = function() child.lua('MiniTest.run_file(...)', { './' .. get_ref_path('testref_run.lua') }) eq(child.lua_get('MiniTest.current.all_cases[1].desc[1]'):gsub('\\', '/'), 'tests/dir-test/testref_run.lua')end
T['run_at_location()'] = new_set()
T['run_at_location()']['works with non-default input'] = new_set({ parametrize = { { 3 }, { 4 }, { 5 } } }, { function(line) local path = get_ref_path('testref_run.lua') local command = string.format([[MiniTest.run_at_location({ file = '%s', line = %s })]], path, line) child.lua(command)
local all_cases = get_current_all_cases() eq(#all_cases, 1) ---@diagnostic disable-next-line: undefined-field eq(all_cases[1].desc, { path, 'run_at_location()' }) end,})
T['run_at_location()']['uses cursor position by default'] = function() local path = get_ref_path('testref_run.lua') child.cmd('edit ' .. path) set_cursor(4, 0) child.lua('MiniTest.run_at_location()')
local all_cases = get_current_all_cases() eq(#all_cases, 1) ---@diagnostic disable-next-line: undefined-field local desc = all_cases[1].desc eq(desc[1]:gsub('\\', '/'), path) eq(desc[2], 'run_at_location()')end
local collect_general = function() local path = get_ref_path('testref_general.lua') local command = string.format([[_G.cases = MiniTest.collect({ find_files = function() return { '%s' } end })]], path) child.lua(command)end
T['collect()'] = new_set()
T['collect()']['works'] = function() child.lua('_G.cases = MiniTest.collect()')
-- Should return array of cases eq(child.lua_get('vim.islist(_G.cases)'), true)
local keys = child.lua_get('vim.tbl_keys(_G.cases[1])') table.sort(keys) eq(keys, { 'args', 'data', 'desc', 'hooks', 'n_retry', 'test' })
local hook_keys = child.lua_get('vim.tbl_keys(_G.cases[1].hooks)') table.sort(hook_keys) eq(hook_keys, { 'post', 'post_source', 'pre', 'pre_source' })end
T['collect()']['respects `emulate_busted` option'] = function() local res = testrun_ref_file('testref_collect-busted.lua')
-- All descriptions should be prepended with file name eq(#filter_by_desc(res, 1, get_ref_path('testref_collect-busted.lua')), #res)
-- `describe()/it()` eq(#filter_by_desc(res, 2, 'describe()/it()'), 3)
-- `setup()/teardown()` expect_all_state(filter_by_desc(res, 2, 'setup()/teardown()'), 'Pass')
-- `before_each()/after_each()` expect_all_state(filter_by_desc(res, 2, 'before_each()/after_each()'), 'Pass')
-- `MiniTest.skip()` expect_all_state(filter_by_desc(res, 2, 'MiniTest.skip()'), 'Pass with notes')
-- `MiniTest.finally()` local cases_finally = filter_by_desc(res, 2, 'MiniTest.finally()') -- all_have_state(filter_by_desc(cases_finally, 3, 'works with no error')) expect_all_state(filter_by_desc(cases_finally, 3, 'works with no error'), 'Pass') expect_all_state(filter_by_desc(cases_finally, 3, 'works with error'), 'Pass')
-- Should also use buffer local config child.lua([[vim.b.minitest_config = { collect = { emulate_busted = false } }]]) local busted_file = get_ref_path('testref_collect-busted.lua') local command = string.format([[MiniTest.collect({ find_files = function() return { '%s' } end })]], busted_file) expect.error(function() child.lua(command) end, 'attempt to call global')end
T['collect()']['respects `find_files` option'] = function() local command = string.format( [[_G.cases = MiniTest.collect({ find_files = function() return { '%s' } end })]], get_ref_path('testref_general.lua') ) child.lua(command) eq(child.lua_get('#_G.cases'), 2) eq(child.lua_get('_G.cases[1].desc[1]'), 'tests/dir-test/testref_general.lua')
child.lua([[vim.b.minitest_config = { collect = { find_files = function() return {} end } }]]) child.lua('_G.cases = MiniTest.collect()') eq(child.lua_get('#_G.cases'), 0)end
T['collect()']['respects `filter_cases` option'] = function() local command = string.format( [[_G.cases = MiniTest.collect({ find_files = function() return { '%s' } end, filter_cases = function(case) return case.desc[2] == 'case 2' end, })]], get_ref_path('testref_general.lua') ) child.lua(command)
eq(child.lua_get('#_G.cases'), 1) eq(child.lua_get('_G.cases[1].desc[2]'), 'case 2')
child.lua([[vim.b.minitest_config = { collect = { filter_cases = function() return false end } }]]) child.lua('_G.cases = MiniTest.collect()') eq(child.lua_get('#_G.cases'), 0)end
T['execute()'] = new_set()
T['execute()']['respects `reporter` option'] = new_set()
T['execute()']['respects `reporter` option']['empty'] = function() collect_general() child.lua('MiniTest.execute(_G.cases, { reporter = {} })')end
T['execute()']['respects `reporter` option']['partial'] = function() collect_general() child.lua([[MiniTest.execute( _G.cases, { reporter = { start = function() _G.was_in_start = true end, finish = function() _G.was_in_finish = true end, } } )]])
eq(child.lua_get('_G.was_in_start'), true) eq(child.lua_get('_G.was_in_finish'), true)
child.lua('vim.b.minitest_config = { execute = { reporter = { update = function() _G.was_in_update = true end } } }') child.lua('MiniTest.execute(_G.cases)') eq(child.lua_get('_G.was_in_update'), true)end
T['execute()']['respects `stop_on_error` option'] = function() collect_general()
child.lua('MiniTest.execute(_G.cases, { stop_on_error = true })')
eq(child.lua_get('type(_G.cases[1].exec)'), 'table') eq(child.lua_get('_G.cases[1].exec.state'), 'Fail')
eq(child.lua_get('type(_G.cases[2].exec)'), 'nil')
-- Should also use buffer local config child.lua('vim.b.minitest_config = { execute = { stop_on_error = false } }') child.lua('MiniTest.execute(_G.cases)') eq(child.lua_get('type(_G.cases[2].exec)'), 'table')end
T['execute()']['properly calls `reporter` methods'] = function() collect_general()
child.lua([[ _G.log = {} _G.reporter = { start = function(all_cases) table.insert(_G.log, { stage = 'start' }) _G.all_cases = all_cases end, update = function(case_num) table.insert(_G.log, { stage = 'update', case_num = case_num, state = _G.all_cases[case_num].exec.state }) end, finish = function() table.insert(_G.log, { stage = 'finish' }) end, }]])
child.lua('MiniTest.execute(_G.cases, { reporter = _G.reporter })') eq(child.lua_get('#_G.all_cases'), 2) eq(child.lua_get('_G.log'), { { stage = 'start' }, { stage = 'update', case_num = 1, state = "Executing 'pre' hook #1" }, { stage = 'update', case_num = 1, state = "Executing 'pre' hook #2" }, { stage = 'update', case_num = 1, state = 'Executing test' }, { stage = 'update', case_num = 1, state = "Executing 'post' hook #1" }, { stage = 'update', case_num = 1, state = 'Fail' }, { stage = 'update', case_num = 2, state = "Executing 'pre' hook #1" }, { stage = 'update', case_num = 2, state = 'Executing test' }, { stage = 'update', case_num = 2, state = "Executing 'post' hook #1" }, { stage = 'update', case_num = 2, state = "Executing 'post' hook #2" }, { stage = 'update', case_num = 2, state = 'Pass' }, { stage = 'finish' }, })
-- Should also call all methods if no cases is supplied child.lua('_G.log, _G.all_cases = {}, {}') child.lua('MiniTest.execute({}, { reporter = _G.reporter })') eq(child.lua_get('#_G.all_cases'), 0) eq(child.lua_get('_G.log'), { { stage = 'start' }, { stage = 'finish' } }) eq(get_latest_message(), '')end
T['execute()']['handles async-adjacent functions'] = function() child.lua('_G.file_path = ' .. vim.inspect(get_ref_path('testref_asynclike.lua'))) child.lua('_G.wait_time = ' .. wait_time) child.lua([[ local cases = MiniTest.collect({ find_files = function() return { _G.file_path } end }) _G.reporter_log = {} local report_in_log = { start = function() table.insert(_G.reporter_log, 'start') end, update = function() table.insert(_G.reporter_log, 'update: ' .. MiniTest.current.case.exec.state) end, finish = function() table.insert(_G.reporter_log, 'finish') end, } MiniTest.execute(cases, { reporter = report_in_log }) ]]) vim.uv.sleep(2 * wait_time)
-- Should execute all steps in order, despite of test case forces other -- scheduled to be executed first eq(child.lua_get('_G.reporter_log'), { 'start', 'update: Executing test', 'update: Fail', 'finish' }) eq(child.lua_get('#MiniTest.current.all_cases[1].exec.fails > 0'), true)end
T['execute()']['respects `config.silent`'] = function() child.lua('MiniTest.config.silent = true') child.lua('MiniTest.execute({})') eq(child.lua_get('MiniTest.current.all_cases'), {})
-- Should not throw message eq(get_latest_message(), '')end
T['stop()'] = new_set()
T['stop()']['works'] = function() collect_general()
child.lua('_G.grandchild = MiniTest.new_child_neovim(); _G.grandchild.start()') child.lua('MiniTest.execute(_G.cases, { reporter = { start = function() MiniTest.stop() end } })')
eq(child.lua_get('type(_G.cases[1].exec)'), 'nil') eq(child.lua_get('type(_G.cases[2].exec)'), 'nil')
-- Should close all opened child processed by default eq(child.lua_get('_G.grandchild.is_running()'), false)end
T['stop()']['respects `close_all_child_neovim` option'] = function() collect_general()
child.lua('_G.grandchild = MiniTest.new_child_neovim(); _G.grandchild.start()') -- Register cleanup finally(function() child.lua('_G.grandchild.stop()') end) child.lua([[MiniTest.execute( _G.cases, { reporter = { start = function() MiniTest.stop({ close_all_child_neovim = false }) end } } )]])
-- Shouldn't close as per option eq(child.lua_get('_G.grandchild.is_running()'), true)end
T['is_executing()'] = new_set()
T['is_executing()']['works'] = function() collect_general()
-- Tests are executing all the time while reporter is active, but not before -- or after eq(child.lua_get('MiniTest.is_executing()'), false)
child.lua([[ _G.executing_states = {} local track_is_executing = function() table.insert(_G.executing_states, MiniTest.is_executing()) end MiniTest.execute( _G.cases, { reporter = { start = track_is_executing, update = track_is_executing, finish = track_is_executing } } )]])
local all_true = true for _, s in ipairs(child.lua_get('_G.executing_states')) do if s ~= true then all_true = false end end eq(all_true, true)
eq(child.lua_get('MiniTest.is_executing()'), false)end
T['expect'] = new_set()
local validate_fail_reason = function(expectation, reason, args) table.insert(args, { fail_reason = reason }) local ok, err = pcall(expectation, unpack(args))
-- Should error with specific pattern eq(ok, false) if vim.is_callable(reason) then reason = reason() end expect.match(err, vim.pesc(reason))
-- Should not show default "Failed expectation ..." text expect.no_match(err, 'Failed expectation')end
T['expect']['equality()'] = new_set()
T['expect']['equality()']['works'] = function() local f, empty_tbl = function() end, {}
-- Pass local validate_pass = function(x, y) eq(MiniTest.expect.equality(x, y), true) end
validate_pass(1, 1) validate_pass('a', 'a') validate_pass(f, f) validate_pass(empty_tbl, empty_tbl)
-- - Tables should be compared "deeply per elements" validate_pass(empty_tbl, {}) validate_pass({ 1 }, { 1 }) validate_pass({ a = 1 }, { a = 1 }) validate_pass({ { b = 2 } }, { { b = 2 } })
-- Fail local validate_fail = function(x, y) expect.error(function() MiniTest.expect.equality(x, y) end, 'equality.*Left: .*Right: ') end
validate_fail(1, 2) validate_fail(1, '1') validate_fail('a', 'b') validate_fail(f, function() end)
-- - Tables should be compared "deeply per elements" validate_fail({ 1 }, { 2 }) validate_fail({ a = 1 }, { a = 2 }) validate_fail({ a = 1 }, { b = 1 }) validate_fail({ a = 1 }, { { a = 1 } }) validate_fail({ { b = 2 } }, { { b = 3 } }) validate_fail({ { b = 2 } }, { { c = 2 } })end
T['expect']['equality()']['provides proper cause'] = function() local validate = function(a, b, cause) expect.error(function() MiniTest.expect.equality(a, b) end, 'Cause: ' .. vim.pesc(cause) .. '\n') end
-- Different types validate({ 1 }, 1, 'different types') validate({ 1 }, 'a', 'different types') validate(1, nil, 'different types') validate(nil, 1, 'different types')
-- Strings validate('a', 'bc', 'different string length') validate('a', 'ab', 'different string length') validate('ab', 'abc', 'different string length') validate('', 'a', 'different string length')
validate('a', 'b', 'different character at position 1, left = "a", right = "b"') validate('aa', 'ab', 'different character at position 2, left = "a", right = "b"') validate('aac', 'abc', 'different character at position 2, left = "a", right = "b"') validate('aa', 'bb', 'different character at position 1, left = "a", right = "b"') validate('aaaa', 'abba', 'different character at position 2, left = "a", right = "b"')
-- Tables validate({ 1 }, {}, 'different values at key 1, left = 1, right = nil') validate({}, { 1 }, 'different values at key 1, left = nil, right = 1') validate({ a = 1 }, {}, 'different values at key "a", left = 1, right = nil') validate({}, { a = 1 }, 'different values at key "a", left = nil, right = 1') validate({ 1, 2 }, { 1 }, 'different values at key 2, left = 2, right = nil') validate({ 1 }, { 1, 2 }, 'different values at key 2, left = nil, right = 2') validate({ a = 1, b = 2 }, { a = 1 }, 'different values at key "b", left = 2, right = nil') validate({ a = 1 }, { a = 1, b = 2 }, 'different values at key "b", left = nil, right = 2')
validate({ 1 }, { 2 }, 'different values at key 1, left = 1, right = 2') validate({ a = 1 }, { a = 2 }, 'different values at key "a", left = 1, right = 2') validate({ 1, 2 }, { 1, 3 }, 'different values at key 2, left = 2, right = 3') validate({ a = 1, b = 2 }, { a = 1, b = 3 }, 'different values at key "b", left = 2, right = 3')
validate({ 1, a = 3 }, { 2, a = 3 }, 'different values at key 1, left = 1, right = 2') validate({ 1, a = 3 }, { 1, a = 4 }, 'different values at key "a", left = 3, right = 4')
validate({ { 1 } }, { {} }, 'different values at key branch 1->1, left = 1, right = nil') validate({ {} }, { { 1 } }, 'different values at key branch 1->1, left = nil, right = 1') validate({ { a = 1 } }, { {} }, 'different values at key branch 1->"a", left = 1, right = nil') validate({ {} }, { { a = 1 } }, 'different values at key branch 1->"a", left = nil, right = 1') validate({ a = { 1 } }, { a = {} }, 'different values at key branch "a"->1, left = 1, right = nil') validate({ a = {} }, { a = { 1 } }, 'different values at key branch "a"->1, left = nil, right = 1') validate({ a = { b = 1 } }, { a = {} }, 'different values at key branch "a"->"b", left = 1, right = nil') validate({ a = {} }, { a = { b = 1 } }, 'different values at key branch "a"->"b", left = nil, right = 1')
validate({ 1, { 2 } }, { 1, 2 }, 'different values at key 2, left = { 2 }, right = 2') validate({ 1, 2 }, { 1, { 2 } }, 'different values at key 2, left = 2, right = { 2 }') validate({ 1, { a = 1 } }, { 1, 2 }, 'different values at key 2, left = { a = 1 }, right = 2') validate({ 1, 2 }, { 1, { a = 1 } }, 'different values at key 2, left = 2, right = { a = 1 }')
validate({ { { { 1 } } } }, { { { { 2 } } } }, 'different values at key branch 1->1->1->1, left = 1, right = 2')
validate({ { 1, 2 } }, { { 1, 3 } }, 'different values at key branch 1->2, left = 2, right = 3') validate({ 1, { 2 } }, { 1, { 3 } }, 'different values at key branch 2->1, left = 2, right = 3')
--->Should prefer consistent/deterministic different key validate({ 1, 2, 3 }, { 1, 3, 4 }, 'different values at key 2, left = 2, right = 3') validate({ 1, { 2 }, 3 }, { 1, { 3 }, 4 }, 'different values at key branch 2->1, left = 2, right = 3') validate({ a = 1, b = 2, c = 3 }, { a = 1, b = 3, c = 4 }, 'different values at key "b", left = 2, right = 3') validate( { a = 1, b = { 2 }, c = 3 }, { a = 1, b = { 3 }, c = 4 }, 'different values at key branch "b"->1, left = 2, right = 3' ) validate({ 1, a = 3 }, { 2, a = 4 }, 'different values at key 1, left = 1, right = 2')
-- Fallback response for other types validate(false, true, 'different values') validate(1, 2, 'different values') -- - Can not compare functions other than by their id (always different) validate(function() end, function() end, 'different values')end
T['expect']['equality()']['respects `opts.fail_reason`'] = function() validate_fail_reason(MiniTest.expect.equality, 'This is test 1', { 1, 2 }) validate_fail_reason(MiniTest.expect.equality, function() return 'This is test 2' end, { 2, 3 })end
T['expect']['no_equality()'] = new_set()
T['expect']['no_equality()']['works'] = function() local f, empty_tbl = function() end, {}
-- Pass local validate_pass = function(x, y) eq(MiniTest.expect.no_equality(x, y), true) end
validate_pass(1, 2) validate_pass(1, '1') validate_pass('a', 'b') validate_pass(f, function() end)
-- - Tables should be compared "deeply per elements" validate_pass({ 1 }, { 2 }) validate_pass({ a = 1 }, { a = 2 }) validate_pass({ a = 1 }, { b = 1 }) validate_pass({ a = 1 }, { { a = 1 } }) validate_pass({ { b = 2 } }, { { b = 3 } }) validate_pass({ { b = 2 } }, { { c = 2 } })
-- Fail local validate_fail = function(x, y) expect.error(function() MiniTest.expect.no_equality(x, y) end, '%*no%* equality.*Object:') end
validate_fail(1, 1) validate_fail('a', 'a') validate_fail(f, f) validate_fail(empty_tbl, empty_tbl)
-- - Tables should be compared "deeply per elements" validate_fail(empty_tbl, {}) validate_fail({ 1 }, { 1 }) validate_fail({ a = 1 }, { a = 1 }) validate_fail({ { b = 2 } }, { { b = 2 } })end
T['expect']['no_equality()']['respects `opts.fail_reason`'] = function() validate_fail_reason(MiniTest.expect.no_equality, 'This is test 3', { 1, 1 }) validate_fail_reason(MiniTest.expect.no_equality, function() return 'This is test 4' end, { 2, 2 })end
T['expect']['error()'] = new_set()
T['expect']['error()']['works'] = function() expect.error(function() MiniTest.expect.error(function() end) end, 'error.*Observed no error')
expect.error(function() MiniTest.expect.error(function() end, 'aa') end, 'error matching pattern "aa".*Observed no error')
expect.error(function() MiniTest.expect.error(error, 'bb') end, 'error matching pattern "bb".*Observed error:')end
T['expect']['error()']['respects `pattern` argument'] = function() ---@diagnostic disable-next-line:param-type-mismatch expect.error(function() MiniTest.expect.error(error, 1) end, 'pattern.*string')
-- `nil` and `''` are placeholders for 'any error' expect.no_error(function() MiniTest.expect.error(error, '') end) expect.no_error(function() MiniTest.expect.error(error, nil) end)end
T['expect']['error()']['returns `true` on success'] = function() eq(MiniTest.expect.error(error), true) end
T['expect']['error()']['respects `opts.fail_reason`'] = function() local f = function() return 1 end validate_fail_reason(MiniTest.expect.error, 'This is test 1', { f, '' }) validate_fail_reason(MiniTest.expect.error, function() return 'This is test 2' end, { f, '' })
local g = function() error('xxx') end validate_fail_reason(MiniTest.expect.error, 'This is test 3', { g, 'yyy' }) validate_fail_reason(MiniTest.expect.error, function() return 'This is test 4' end, { g, 'yyy' })end
T['expect']['no_error()'] = new_set()
T['expect']['no_error()']['works'] = function() expect.error(function() MiniTest.expect.no_error(error) end, '%*no%* error.*Observed error:')
expect.no_error(function() MiniTest.expect.no_error(function() end) end)end
T['expect']['no_error()']['returns `true` on success'] = function() eq(MiniTest.expect.no_error(function() end), true)end
T['expect']['no_error()']['respects `opts.fail_reason`'] = function() local f = function() error('Error msg') end validate_fail_reason(MiniTest.expect.no_error, 'This is test 1', { f }) validate_fail_reason(MiniTest.expect.no_error, function() return 'This is test 2' end, { f })end
T['expect']['reference_screenshot()'] = new_set()
T['expect']['reference_screenshot()']['works'] = function() local path = get_ref_path('reference-screenshot') child.set_size(5, 12)
set_lines({ 'aaa' }) eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), path), true)
set_lines({ 'bbb' }) expect.error( function() MiniTest.expect.reference_screenshot(child.get_screenshot(), path) end, 'screenshot equality to reference at ' .. vim.pesc(vim.inspect(path)) .. '.*Reference:.*Observed:' )
-- Should pass if supplied `nil` (like in case of no reasonable screenshot) eq(MiniTest.expect.reference_screenshot(nil), true)end
T['expect']['reference_screenshot()']['works with double width characters'] = function() if child.fn.has('nvim-0.11') == 0 then MiniTest.skip('Neovim<0.11 has problems working with composing characters') end
local path = get_ref_path('reference-screenshot-double-width') child.set_size(5, 20) set_lines({ '「🙂↔️」' }) eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), path), true)
set_lines({ '「「' }) local pattern = 'different `text` cell at line 1 column 3, reference = "🙂↔️", observed = "「"' expect.error(function() MiniTest.expect.reference_screenshot(child.get_screenshot(), path) end, pattern)end
T['expect']['reference_screenshot()']['locates problem'] = function() local path = get_ref_path('reference-screenshot') local validate = function(screen, pattern) expect.error(function() MiniTest.expect.reference_screenshot(screen, path) end, pattern) end
child.set_size(5, 12) set_lines({ 'aaa' }) local screen = child.get_screenshot()
-- Number of lines local screen_text_lines = vim.deepcopy(screen) table.remove(screen_text_lines.text, 1) validate(screen_text_lines, 'different number of `text` lines%, reference = 5, observed = 4')
local screen_attr_lines = vim.deepcopy(screen) table.remove(screen_attr_lines.attr, 1) validate(screen_attr_lines, 'different number of `attr` lines, reference = 5, observed = 4')
-- Number of columns local screen_text_columns = vim.deepcopy(screen) table.remove(screen_text_columns.text[1], 1) validate(screen_text_columns, 'different number of columns in `text` line 1%, reference = 12, observed = 11')
local screen_attr_columns = vim.deepcopy(screen) table.remove(screen_attr_columns.attr[1], 1) validate(screen_attr_columns, 'different number of columns in `attr` line 1%, reference = 12, observed = 11')
-- Cells local screen_text_cell = vim.deepcopy(screen) screen_text_cell.text[1][2] = 'X' validate(screen_text_cell, 'different `text` cell at line 1 column 2, reference = "a", observed = "X"')
local screen_attr_cell = vim.deepcopy(screen) screen_attr_cell.attr[1][2] = 'X' validate(screen_attr_cell, 'different `attr` cell at line 1 column 2, reference = "0", observed = "X"')end
T['expect']['reference_screenshot()']['correctly infers reference path'] = function() child.set_size(5, 20)
set_lines({ 'This path should be correctly inferred without suffix' }) eq(MiniTest.expect.reference_screenshot(child.get_screenshot()), true)
set_lines({ 'This path should have suffix 002' }) eq(MiniTest.expect.reference_screenshot(child.get_screenshot()), true)
set_lines({ 'This is a call to `reference_screenshot()` with manual path' }) eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), 'tests/dir-test/intermediate-screenshot'), true)
set_lines({ 'This path should have suffix 004' }) eq(MiniTest.expect.reference_screenshot(child.get_screenshot()), true)end
local validate_path_sanitize = function() child.set_size(5, 12) set_lines({ 'Path should be correctly sanitized' })
eq(MiniTest.expect.reference_screenshot(child.get_screenshot()), true)end
local useful_punctuation = [=[_-+{}()[]'"]=]local linux_forbidden = [[/]]local windows_forbidden = [[<>:"/\|?*]]local whitespace = '\t 'local special_characters = string.char(0) .. string.char(1) .. string.char(31)local suffix = useful_punctuation .. linux_forbidden .. windows_forbidden .. whitespace .. special_characters
-- Don't permanently create reference file because its name is very long. This-- might hurt Windows users which are not interested in testing this plugin.T['expect']['reference_screenshot()']['correctly sanitizes path ' .. suffix] = new_set( { parametrize = { { suffix } } }, { test = function() local expected_filename = table.concat({ 'tests/screenshots/', 'tests-test_test.lua---', 'expect---', 'reference_screenshot()---', 'correctly-sanitizes-path-', [[_-+{}()[]''----'-------------]], 'test-+-args-', [[{-'_-+{}()[]'-'-----'-------t--0-1-31'-}]], }, '') finally(function() MiniTest.current.case.exec.notes = {} vim.fn.delete(expected_filename) end) eq(vim.fn.filereadable(expected_filename), 0) validate_path_sanitize() eq(vim.fn.filereadable(expected_filename), 1) end, })
-- Paths should not end with whitespace or dotT['expect']['reference_screenshot()']['correctly sanitizes path for Windows '] = validate_path_sanitizeT['expect']['reference_screenshot()']['correctly sanitizes path for Windows #2.'] = validate_path_sanitize
T['expect']['reference_screenshot()']['creates reference if it does not exist'] = function() local path = get_ref_path('nonexistent-reference-screenshot') child.fn.delete(path) finally(function() child.fn.delete(path) MiniTest.current.case.exec.notes = {} end)
set_lines({ 'nonexistent' }) local screenshot = child.get_screenshot()
eq(MiniTest.expect.reference_screenshot(screenshot, path), true) eq(MiniTest.current.case.exec.notes, { 'Created reference screenshot at path ' .. vim.inspect(path) })
MiniTest.current.case.exec.notes = {} eq(MiniTest.expect.reference_screenshot(screenshot, path), true) eq(MiniTest.current.case.exec.notes, {})end
T['expect']['reference_screenshot()']['respects `opts.force` argument'] = function() local path = get_ref_path('force-reference-screenshot') local notes = { 'Created reference screenshot at path ' .. vim.inspect(path) }
child.fn.delete(path) finally(function() child.fn.delete(path) MiniTest.current.case.exec.notes = {} end)
set_lines({ 'First run' }) eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), path), true) eq(MiniTest.current.case.exec.notes, notes)
MiniTest.current.case.exec.notes = {} set_lines({ 'This should be forced' }) eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), path, { force = true }), true) eq(MiniTest.current.case.exec.notes, notes)end
T['expect']['reference_screenshot()']['respects `opts.ignore_text`'] = function() local path = get_ref_path('reference-screenshot') child.set_size(5, 12) local validate = function(ignore_text, ref) eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), path, { ignore_text = ignore_text }), ref) end
set_lines({ 'aaa' }) validate(nil, true) validate(false, true)
-- Make different text but same highlighting set_lines({ 'aaa', 'bbb' }) local ns_id = child.api.nvim_create_namespace('test-hl') child.api.nvim_buf_set_extmark(0, ns_id, 1, 0, { end_row = 2, end_col = 0, hl_group = 'EndOfBuffer', hl_eol = true })
validate({ 2 }, true) validate({ 1, 2, 3 }, true)
set_lines({ 'ccc', 'bbb' }) expect.error( function() MiniTest.expect.reference_screenshot(child.get_screenshot(), path, { ignore_text = { 2 } }) end, 'screenshot equality to reference at ' .. vim.pesc(vim.inspect(path)) .. '.*different `text` cell at line 1 column 1, reference = "a", observed = "c"' )end
T['expect']['reference_screenshot()']['respects `opts.ignore_attr`'] = function() local path = get_ref_path('reference-screenshot') child.set_size(5, 12) local validate = function(ignore_attr, ref) eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), path, { ignore_attr = ignore_attr }), ref) end
set_lines({ 'aaa' }) validate(nil, true) validate(false, true)
-- Make different attr but same text local ns_id = child.api.nvim_create_namespace('test-hl') child.api.nvim_buf_set_extmark(0, ns_id, 0, 1, { end_row = 0, end_col = 3, hl_group = 'EndOfBuffer' })
validate({ 1 }, true) validate({ 1, 2 }, true)
expect.error( function() MiniTest.expect.reference_screenshot(child.get_screenshot(), path, { ignore_text = { 2 } }) end, 'screenshot equality to reference at ' .. vim.pesc(vim.inspect(path)) .. '.*different `attr` cell at line 1 column 2, reference = "0", observed = "1"' )end
T['expect']['reference_screenshot()']['respects `opts.directory`'] = function() child.set_size(5, 12) set_lines({ 'opts.directory' }) eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), nil, { directory = 'tests/dir-test' }), true) eq(MiniTest.expect.reference_screenshot(child.get_screenshot(), nil, { directory = 'tests/dir-test/' }), true)end
T['expect']['reference_screenshot()']['respects `opts.fail_reason`'] = function() local path = get_ref_path('reference-screenshot') child.set_size(5, 12) set_lines({ 'bbb' }) local screen = child.get_screenshot() validate_fail_reason(MiniTest.expect.reference_screenshot, 'This is test 1', { screen, path }) validate_fail_reason(MiniTest.expect.reference_screenshot, function() return 'This is test 2' end, { screen, path })end
T['expect']['reference_screenshot()']['works with multibyte characters'] = function() child.set_size(5, 12) set_lines({ ' 1 2' }) expect.no_error(function() MiniTest.expect.reference_screenshot(child.get_screenshot()) end)end
T['new_expectation()'] = new_set()
T['new_expectation()']['works'] = function() local expect_truthy = MiniTest.new_expectation( 'truthy', function(x) return x end, function(x) return 'Object: ' .. vim.inspect(x) end )
expect.error(function() expect_truthy(false) end, 'truthy.*Object:') expect.no_error(function() expect_truthy(1) end)end
T['new_expectation()']['allows string or function arguments'] = function() local expect_truthy = MiniTest.new_expectation( function() return 'func_truthy' end, function(x) return x end, 'Not truthy' )
expect.error(function() expect_truthy(false) end, 'func_truthy.*Not truthy') expect.no_error(function() expect_truthy(1) end)end
T['new_child_neovim()'] = new_set()
T['new_child_neovim()']['works'] = function() finally(function() child.lua('_G.grandchild.stop()') end) child.lua('_G.grandchild = MiniTest.new_child_neovim(); _G.grandchild.start()') eq(child.lua_get('_G.grandchild.is_running()'), true)end
T['child'] = new_set()
T['child']['job'] = function() eq(type(child.job), 'table')
child.stop() eq(child.job, nil)end
T['child']['start()'] = new_set()
T['child']['start()']['respects `args` argument'] = function() child.stop()
child.start({ '-c', 'lua _G.inside_args = true' }) eq(child.lua_get('_G.inside_args'), true)end
T['child']['start()']['does nothing if already running'] = function() finally(function() child.lua('_G.grandchild.stop()') end) child.lua('_G.grandchild = MiniTest.new_child_neovim(); _G.grandchild.start()')
child.lua('_G.should_be_present = true') child.lua('_G.grandchild.start()') eq(child.lua_get('_G.should_be_present'), true)
eq(get_latest_message(), '(mini.test) Child process is already running. Use `child.restart()`.')end
T['child']['stop()'] = function() eq(child.is_running(), true) child.stop() eq(child.is_running(), false)end
T['child']['restart()'] = new_set()
T['child']['restart()']['respects `args` argument'] = function() eq(child.lua_get('_G.inside_args'), vim.NIL) child.restart({ '-c', 'lua _G.inside_args = true' }) eq(child.lua_get('_G.inside_args'), true)end
T['child']['restart()']['uses `args` from `start()` by default'] = function() child.stop()
child.start({ '-c', 'lua _G.inside_args = true' }) eq(child.lua_get('_G.inside_args'), true)
child.restart() eq(child.lua_get('_G.inside_args'), true)end
local validate_child_method = function(method, opts) opts = vim.tbl_deep_extend('force', { prevent_hanging = true }, opts or {})
-- Validate presence of method expect.no_error(method)
-- Validate hanging prevention if opts.prevent_hanging then child.type_keys('di') expect.error(method, opts.name .. '.*child process is blocked') -- Unblock for faster test execution child.type_keys('<Esc>') end
-- Validate ensuring running child.stop() expect.error(method, 'Child process is not running')end
local validate_child_field = function(tbl_name, field_name, value) local var = string.format('vim[%s][%s]', vim.inspect(tbl_name), vim.inspect(field_name))
-- Setting child[tbl_name][field_name] = value eq(child.lua_get(var), value)
-- Getting eq(child[tbl_name][field_name], child.lua_get(var))end
T['child']['api'] = function() local method = function() return child.api.nvim_buf_set_lines(0, 0, -1, true, { 'aaa' }) end validate_child_method(method, { prevent_hanging = false })end
T['child']['api_notify'] = function() local method = function() return child.api_notify.nvim_buf_set_lines(0, 0, -1, true, { 'aaa' }) end validate_child_method(method, { prevent_hanging = false })end
T['child']['redirected method tables'] = new_set({ parametrize = { { 'diagnostic', 'get', { 0 } }, { 'fn', 'fnamemodify', { '.', ':p' } }, { 'highlight', 'range', { 0, 1, 'Comment', { 0, 1 }, { 0, 2 } } }, { 'hl', 'range', { 0, 1, 'Comment', { 0, 1 }, { 0, 2 } } }, { 'json', 'encode', { { a = 1 } } }, { 'loop', 'hrtime', {} }, { 'lsp', 'get_clients', {} }, { 'mpack', 'encode', { { a = 1 } } }, --typos: ignore { 'spell', 'check', { 'thouht' } }, { 'treesitter', 'get_parser', {} }, { 'ui', 'select', {} }, { 'uv', 'hrtime', {} }, { 'fs', 'normalize', { '/tmp/' } }, }, hooks = { pre_case = function() -- Mock problematic methods child.lua('vim.treesitter.get_parser = function() end') child.lua('vim.ui.select = function() end') end, },})
T['child']['redirected method tables']['method'] = function(tbl_name, field_name, args) if tbl_name == 'hl' and child.fn.has('nvim-0.11') == 0 then return end local method = function() return child[tbl_name][field_name](unpack(args)) end validate_child_method(method, { name = tbl_name .. '.' .. field_name })end
T['child']['redirected method tables']['field'] = function(tbl_name, field_name, _) if tbl_name == 'hl' and child.fn.has('nvim-0.11') == 0 then return end -- Although being tables, they should be overridable to allow test doubles validate_child_field(tbl_name, field_name, true)end
T['child']['ui'] = function() -- Nothing to actually test due to mandatory function argument eq(type(child.ui), 'table')end
T['child']['scoped variables'] = new_set({ parametrize = { { 'g' }, { 'b' }, { 'w' }, { 't' }, { 'v' }, { 'env' } } })
T['child']['scoped variables']['method'] = function(scope) local method = function() return child[scope].char end validate_child_method(method, { name = scope })end
T['child']['scoped variables']['field'] = function(scope) validate_child_field(scope, 'char', 'a') end
T['child']['scoped options'] = new_set({ parametrize = { { 'o', 'lines', 10 }, { 'go', 'lines', 10 }, { 'bo', 'filetype', 'lua' }, { 'wo', 'number', true } },})
T['child']['scoped options']['method'] = function(tbl_name, field_name, _) local method = function() return child[tbl_name][field_name] end validate_child_method(method, { name = tbl_name })end
T['child']['scoped options']['field'] = function(tbl_name, field_name, value) validate_child_field(tbl_name, field_name, value)end
T['child']['type_keys()'] = new_set()
T['child']['type_keys()']['works'] = function() local method = function() child.type_keys('i', 'abc') end validate_child_method(method, { prevent_hanging = false })end
T['child']['type_keys()']['allows strings and arrays of strings'] = function() child.type_keys('i', { 'H', 'e', 'l', 'l', 'o' }, ' ', { 'World' }, '<Esc>') eq(get_lines(), { 'Hello World' })end
T['child']['type_keys()']['validates input'] = function() local pattern = 'type_keys.*string'
expect.error(function() child.type_keys('a', 1) end, pattern) expect.error(function() child.type_keys('a', { 'a', 1 }) end, pattern)end
T['child']['type_keys()']['throws error explicitly'] = function() expect.error(function() child.type_keys(':aaa<CR>') end, 'E492: Not an editor command: aaa')end
T['child']['type_keys()']['respects `wait` argument'] = function() local delay = helpers.get_time_const(100) local start_time = vim.uv.hrtime() child.type_keys(delay, 'i', 'Hello', { 'w', 'o' }, 'rld') local end_time = vim.uv.hrtime() local duration = (end_time - start_time) * 0.000001 eq(0.9 * 5 * delay <= duration and duration <= 1.1 * 5 * delay, true)end
T['child']['cmd()'] = function() local method = function() child.cmd([[echomsg 'Hello world']]) end
method() eq(child.cmd_capture('1messages'), 'Hello world')
validate_child_method(method, { name = 'cmd' })end
T['child']['cmd_capture()'] = function() local method = function() return child.cmd_capture([[echomsg 'Hello world']]) end
eq(method(), 'Hello world')
validate_child_method(method, { name = 'cmd_capture' })end
T['child']['lua()'] = function() local method = function() return child.lua('_G.n = 0') end
eq(child.lua_get('_G.n'), vim.NIL) method() eq(child.lua_get('_G.n'), 0)
validate_child_method(method, { name = 'lua' })end
T['child']['lua_notify()'] = function() local method = function() return child.lua_notify('_G.n = 0') end
eq(child.lua_get('_G.n'), vim.NIL) method() eq(child.lua_get('_G.n'), 0)
validate_child_method(method, { prevent_hanging = false })end
T['child']['lua_get()'] = function() local method = function() return child.lua_get('1 + 1') end
eq(method(), 2)
validate_child_method(method, { name = 'lua_get' })end
T['child']['lua_func()'] = function() -- Works local method = function() return child.lua_func(function() return 1 + 1 end) end eq(method(), 2)
-- Actually executes function in child neovim child.lua('_G.var = 1') child.lua_func(function() _G.var = 10 end) eq(child.lua_get('_G.var'), 10)
-- Can take arguments eq(child.lua_func(function(a, b) return a + b end, 1, 2), 3)
-- Has no side effects child.lua_func(function() end) eq(child.lua_get('f'), vim.NIL)
-- Can error expect.error(function() return child.lua_func(function() error('test error') end) end, 'test error')
validate_child_method(method, { name = 'lua_func' })end
T['child']['is_blocked()'] = function() eq(child.is_blocked(), false)
child.type_keys('di') eq(child.is_blocked(), true)
child.ensure_normal_mode() validate_child_method(child.is_blocked, { prevent_hanging = false })end
T['child']['is_running()'] = function() eq(child.is_running(), true) child.stop() eq(child.is_running(), false)end
T['child']['ensure_normal_mode()'] = new_set()
T['child']['ensure_normal_mode()']['works'] = new_set({ parametrize = { { 'i' }, { 'v' }, { ':' }, { 'R' } } }, { function(keys) child.type_keys(keys) expect.no_equality(child.api.nvim_get_mode().mode, 'n') child.ensure_normal_mode() eq(child.api.nvim_get_mode().mode, 'n') end,})
T['child']['ensure_normal_mode()']['ensures running'] = function() validate_child_method(child.ensure_normal_mode, { prevent_hanging = false })end
T['child']['get_screenshot()'] = new_set()
T['child']['get_screenshot()']['ensures running'] = function() validate_child_method(child.get_screenshot, { name = 'get_screenshot' })end
T['child']['get_screenshot()']['works'] = function() set_lines({ 'aaa' }) local screenshot = child.get_screenshot()
-- Structure eq(type(screenshot), 'table') eq(vim.islist(screenshot.text), true) eq(vim.islist(screenshot.attr), true)
local n_lines, n_cols = child.o.lines, child.o.columns
eq(#screenshot.text, n_lines) eq(#screenshot.attr, n_lines)
for i = 1, n_lines do eq(vim.islist(screenshot.text[i]), true) eq(vim.islist(screenshot.attr[i]), true)
eq(#screenshot.text[i], n_cols) eq(#screenshot.attr[i], n_cols)
for j = 1, n_cols do eq(type(screenshot.text[i][j]), 'string') eq(type(screenshot.attr[i][j]), 'string') end end
-- Content expect.match(table.concat(screenshot.text[1], ''), '^aaa') expect.no_equality(screenshot.attr[1][1], screenshot.attr[2][1])end
T['child']['get_screenshot()']['respects `opts.redraw`'] = function() -- This blocks redraw until explicitly called child.lua_notify('vim.fn.getchar()') set_lines({ 'Should be visible only after redraw' })
local screenshot = child.get_screenshot({ redraw = false }) expect.match(table.concat(screenshot.text[1]), '^ ') -- - Should be `redraw = true` by default screenshot = child.get_screenshot() expect.match(table.concat(screenshot.text[1]), '^Should be visible')end
T['child']['get_screenshot()']['`tostring()`'] = new_set()
T['child']['get_screenshot()']['`tostring()`']['works'] = function() set_lines({ 'aaa' }) local screenshot = child.get_screenshot() local lines = vim.split(tostring(screenshot), '\n') local n_lines, n_cols = child.o.lines, child.o.columns
-- "Ruler" + "Lines" + "Empty line" + "Ruler" + "Lines" eq(#lines, 2 * n_lines + 3) eq(lines[n_lines + 2], '')
-- Content eq(vim.fn.strchars(lines[2]), 3 + n_cols) expect.match(lines[2], '^01|aaa')
eq(vim.fn.strchars(lines[n_lines + 4]), 3 + n_cols) expect.no_equality(lines[n_lines + 4]:sub(4, 4), lines[n_lines + 5]:sub(4, 4))end
T['child']['get_screenshot()']['`tostring()`']['makes proper rulers'] = function() local validate = function(ref_ruler) local lines = vim.split(tostring(child.get_screenshot()), '\n') eq(lines[1], ref_ruler) local n = 0.5 * (#lines - 3) eq(lines[n + 3], ref_ruler) end
child.set_size(5, 12) validate('-|---------|--')
child.set_size(10, 12) validate('--|---------|--')
child.set_size(100, 20) validate('---|---------|---------|')end
T['child']['get_screenshot()']['`tostring()`']['makes proper line numbers'] = function() local validate = function(...) local lines = vim.split(tostring(child.get_screenshot()), '\n') local n = 0.5 * (#lines - 3)
for _, ref in ipairs({ ... }) do local pattern = '^' .. ref[2] .. '|' expect.match(lines[1 + ref[1]], pattern) expect.match(lines[n + 3 + ref[1]], pattern) end end
child.set_size(5, 12) validate({ 1, '1' }, { 5, '5' })
child.set_size(10, 12) validate({ 1, '01' }, { 10, '10' })
child.set_size(100, 12) validate({ 1, '001' }, { 10, '010' }, { 100, '100' })end
T['child']['get_screenshot()']['works with floating windows'] = function() -- This setup should result into displayed text 'bb a': 'bb ' from floating -- window, 'aa' - from underneath text set_lines({ 'aaaa' })
local buf_id = child.api.nvim_create_buf(true, true) child.api.nvim_buf_set_lines(buf_id, 0, -1, true, { 'bb ' }) child.api.nvim_open_win(buf_id, false, { relative = 'editor', width = 3, height = 1, row = 0, col = 0 })
local screenshot = child.get_screenshot() eq(screenshot.text[1][1], 'b') eq(screenshot.text[1][2], 'b') eq(screenshot.text[1][3], ' ') eq(screenshot.text[1][4], 'a')end
-- Integration tests ==========================================================T['gen_reporter'] = new_set()
T['gen_reporter']['buffer'] = new_set({ hooks = { pre_case = function() child.o.termguicolors = true child.set_size(70, 120)
-- Should work with global 'nomodifiable' child.go.modifiable = false end, }, parametrize = { { '' }, { 'group_depth=2' }, { 'window={width=0.9*vim.o.columns,col=0.05*vim.o.columns}' }, { 'window={border="double",title=""}' }, },}, { test = function(opts_element) local expect_screenshot = function() -- Unify path separator for more robust testing. Rely on search and replace -- to preserve extmark highlighting. if package.config:sub(1, 1) == '\\' then child.bo.modifiable = true local cur_pos = child.api.nvim_win_get_cursor(0) child.cmd([[silent! %s^\S\zs\\^/^g]]) child.cmd('silent! nohlsearch') set_cursor(unpack(cur_pos)) end
child.expect_screenshot() end
-- Should respect non-empty 'winborder' while preferring explicitly -- configured value over it if vim.startswith(opts_element, 'window') then if child.fn.has('nvim-0.11') == 0 then MiniTest.skip("'winborder' option is present on Neovim>=0.11") end child.o.winborder = 'rounded' -- Should properly truncate title. Do not set in parameter to avoid emoji -- in reference screenshot file name. opts_element = opts_element:gsub('title=""', 'title=("a😈ツ"):rep(40)') end
-- Testing "in dynamic" is left for manual approach local path = get_ref_path('testref_reporters.lua') local reporter_command = string.format('_G.reporter = MiniTest.gen_reporter.buffer({ %s })', opts_element) child.lua(reporter_command)
local execute_command = string.format([[MiniTest.run_file('%s', { execute = { reporter = _G.reporter } })]], path) child.lua(execute_command) wait_till_not_executing()
-- Buffer should not be modifiable eq(child.bo.modifiable, false)
expect_screenshot()
-- Should be able to run several times expect.no_error(function() child.lua(execute_command) end) expect.no_error(function() child.lua(execute_command) end)
-- Should use properly named buffer eq(child.api.nvim_buf_get_name(0), 'minitest://' .. child.api.nvim_get_current_buf() .. '/buffer-reporter')
child.cmd('quit')
-- Should work with "string array" 'winborder' if child.fn.has('nvim-0.12') == 0 then MiniTest.skip("String array 'winborder' is present on Neovim>=0.12") end -- - Test only for one parameter that doesn't contain explicit `border` if not (vim.startswith(opts_element, 'window') and opts_element:find('border') == nil) then return end
child.o.winborder = '+,-,+,|,+,-,+,|' child.lua(execute_command) wait_till_not_executing() expect_screenshot() end,})
T['gen_reporter']['stdout'] = new_set({ hooks = { pre_case = function() child.o.termguicolors = true child.set_size(35, 120) child.o.laststatus = 0 end, }, parametrize = { { '' }, { 'TEST_GROUP_DEPTH=2' }, { 'TEST_QUIT_ON_FINISH=false' } },}, { test = function(env_var) helpers.skip_on_windows('Terminal tests are designed for Unix')
-- Testing "in dynamic" is left for manual approach local path = 'tests/dir-test/init_stdout-reporter_works.lua' local command = string.format([[%s %s --headless --clean -n -u %s]], env_var, vim.v.progpath, vim.inspect(path)) child.fn.termopen(command) -- Wait until check is done and possible process is ended vim.uv.sleep(terminal_wait) local ignore_attr = child.fn.has('nvim-0.11') == 1 and {} or { 31, 32, 33, 34, 35 } child.expect_screenshot({ ignore_attr = ignore_attr }) end,})
return T