Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelroses committed Mar 28, 2024
1 parent 9d5da07 commit 556ea35
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.tests
result
result-*
103 changes: 103 additions & 0 deletions tests/charm-freeze/test_all.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
local ne = MiniTest.expect.no_error
local e = MiniTest.expect.error
local child = MiniTest.new_child_neovim()

local T = MiniTest.new_set({
hooks = {
pre_case = function()
child.restart()
child.lua([[P = require('.///////lua/charm-freeze')]])
end,
-- Stop once all test cases are finished
post_once = child.stop,
},
})

-- We need to allow no options to be passed
T["no_options"] = function()
ne(function()
child.lua([[P.setup({})]])
end)
end

-- These are invalid options, errors should be thrown by these
T["invalid_options.string"] = function()
e(function()
child.lua([[P.setup({command = 1})]])
end)
end

T["invalid_options.number"] = function()
e(function()
child.lua([[P.setup({line_height = "chesee"})]])
end)
end

T["invalid_options.boolean"] = function()
e(function()
child.lua([[P.setup({line_numbers = "chesee"})]])
end)
end

T["invalid_options.table"] = function()
e(function()
child.lua([[P.setup({border = {1, 2, 3}})]])
end)
end

T["invalid_options.array"] = function()
e(function()
child.lua([[P.setup({padding = { "l" = "something", "r" = "something"}})]])
end)
end

T["invalid_options.function"] = function()
e(function()
child.lua([[P.setup({padding = function() return "./" .. os.date("%Y-%m-%d") .. "_freeze.png" end})]])
end)
end

T["invalid_options.unknown"] = function()
e(function()
child.lua([[P.setup({unknown = 1})]])
end)
end

-- Check valid options, no errors should be thrown by these
T["vaild_options.string"] = function()
ne(function()
child.lua([[P.setup({theme = "charm"})]])
end)
end

T["valid_options.number"] = function()
ne(function()
child.lua([[P.setup({line_height = 1.2})]])
end)
end

T["valid_options.boolean"] = function()
ne(function()
child.lua([[P.setup({line_numbers = false})]])
end)
end

T["valid_options.array"] = function()
ne(function()
child.lua([[P.setup({padding = {20, 40, 20, 20}})]])
end)
end

T["valid_options.table"] = function()
ne(function()
child.lua([[P.setup({border = {radius = 8}})]])
end)
end

T["valid_options.function"] = function()
ne(function()
child.lua([[P.setup({output = function() return "./" .. os.date("%Y-%m-%d") .. "_freeze.png" end})]])
end)
end

return T
40 changes: 40 additions & 0 deletions tests/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local M = {}

function M.root(root)
local f = debug.getinfo(1, "S").source:sub(2)
return vim.fn.fnamemodify(f, ":p:h:h") .. "/" .. (root or "")
end

---@param plugin string
function M.load(plugin)
local name = plugin:match(".*/(.*)")
local package_root = M.root(".tests/site/pack/deps/start/")
if not vim.loop.fs_stat(package_root .. name) then
print("Installing " .. plugin)
vim.fn.mkdir(package_root, "p")
vim.fn.system({
"git",
"clone",
"--depth=1",
"https://github.com/" .. plugin .. ".git",
package_root .. "/" .. name,
})
end
end

function M.setup()
vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.opt.runtimepath:append(M.root())
vim.opt.packpath = { M.root(".tests/site") }

-- Load a testing framework
M.load("echasnovski/mini.nvim")
require("mini.test").setup()

vim.env.XDG_CONFIG_HOME = M.root(".tests/config")
vim.env.XDG_DATA_HOME = M.root(".tests/data")
vim.env.XDG_STATE_HOME = M.root(".tests/state")
vim.env.XDG_CACHE_HOME = M.root(".tests/cache")
end

M.setup()
3 changes: 3 additions & 0 deletions tests/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

nvim --headless --noplugin -u tests/init.lua -c "lua MiniTest.run()"

0 comments on commit 556ea35

Please sign in to comment.