Files
test-samurai.nvim/tests/test_samurai_lua_spec.lua

131 lines
3.6 KiB
Lua

local test_samurai = require("test-samurai")
local lua_runner = require("test-samurai.runners.lua-plenary")
local util = require("test-samurai.util")
local function mkbuf(path, ft, lines)
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, path)
vim.bo[bufnr].filetype = ft
if lines then
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
end
return bufnr
end
describe("test-samurai lua runner (plenary)", function()
it("detects lua spec files by suffix", function()
local bufnr = mkbuf("/tmp/test_samurai_lua_spec_unique_1_spec.lua", "lua")
assert.is_true(lua_runner.is_test_file(bufnr))
end)
it("finds nearest it() when cursor is inside it block and builds filtered command", function()
local bufnr = mkbuf("/tmp/project/tests/test_samurai_lua_nearest_unique_2_spec.lua", "lua", {
"describe('outer', function()",
" it('inner 1', function()",
" local x = 1",
" end)",
"",
" it('inner 2', function()",
" local y = 2",
" end)",
"end)",
})
local orig_find_root = util.find_root
util.find_root = function()
return "/tmp/project"
end
local spec, err = lua_runner.find_nearest(bufnr, 6, 0)
util.find_root = orig_find_root
assert.is_nil(err)
assert.is_not_nil(spec)
assert.equals("inner 2", spec.test_name)
assert.equals("outer inner 2", spec.full_name)
assert.equals("/tmp/project", spec.cwd)
local cmd_spec = lua_runner.build_command(spec)
assert.equals("/tmp/project", cmd_spec.cwd)
assert.are.same({
"nvim",
"--headless",
"-u",
"/tmp/project/tests/minimal_init.lua",
"-c",
'PlenaryBustedFile ' .. spec.file .. ' { busted_args = { "--filter", "inner 2" } }',
"-c",
"qa",
}, cmd_spec.cmd)
end)
it("returns describe block when cursor is between it() calls", function()
local bufnr = mkbuf("/tmp/project/tests/test_samurai_lua_between_unique_3_spec.lua", "lua", {
"describe('outer', function()",
" it('inner 1', function()",
" local x = 1",
" end)",
"",
" it('inner 2', function()",
" local y = 2",
" end)",
"end)",
})
local orig_find_root = util.find_root
util.find_root = function()
return "/tmp/project"
end
local spec, err = lua_runner.find_nearest(bufnr, 4, 0)
util.find_root = orig_find_root
assert.is_nil(err)
assert.is_not_nil(spec)
assert.equals("outer", spec.test_name)
assert.equals("outer", spec.full_name)
assert.equals("describe", spec.kind)
end)
it("builds all command via PlenaryBustedDirectory", function()
local bufnr = mkbuf("/tmp/project/tests/test_samurai_core_spec_unique_4_spec.lua", "lua")
local orig_find_root = util.find_root
util.find_root = function()
return "/tmp/project"
end
local cmd_spec = lua_runner.build_all_command(bufnr)
util.find_root = orig_find_root
assert.are.same({
"nvim",
"--headless",
"-u",
"/tmp/project/tests/minimal_init.lua",
"-c",
"PlenaryBustedDirectory tests",
"-c",
"qa",
}, cmd_spec.cmd)
assert.equals("/tmp/project", cmd_spec.cwd)
end)
it("core selects lua runner for *_spec.lua buffers", function()
test_samurai.setup({
runner_modules = {
"test-samurai.runners.lua-plenary",
},
})
local bufnr = mkbuf("/tmp/project/tests/test_samurai_core_spec_unique_5_spec.lua", "lua")
local runner = require("test-samurai.core").get_runner_for_buf(bufnr)
assert.is_not_nil(runner)
assert.equals("lua-plenary", runner.name)
end)
end)