Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: README.mdtests/helpers.luatests/test_API.lua #15

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,5 @@ Distributed under the MIT License. See `LICENSE.txt` for more information.
[license-shield]: https://img.shields.io/github/license/Cassin01/wf.nvim.svg?style=for-the-badge
[license-url]: https://github.com/Cassin01/wf.nvim/blob/main/LICENSE.txt
[ci-shield]: https://img.shields.io/github/actions/workflow/status/Cassin01/wf.nvim/main.yml?style=for-the-badge
[ci-url]: https://github.com/Cassin01/wf.nvim/.github/workflows/main.yml
[ci-url]: https://github.com/Cassin01/wf.nvim/actions/workflow/status/Cassin01/wf.nvim/main.yml
[product-screenshot]: https://user-images.githubusercontent.com/42632201/213849418-3cddb8bb-7323-4af7-b201-1ce2de07d3b9.png
182 changes: 101 additions & 81 deletions tests/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,144 +6,164 @@ Helpers.expect = vim.deepcopy(MiniTest.expect)

-- The error message returned when a test fails.
local function errorMessage(str, pattern)
return string.format("Pattern: %s\nObserved string: %s", vim.inspect(pattern), str)
return string.format("Pattern: %s\nObserved string: %s", vim.inspect(pattern), str)
end

-- Check equality of a global `field` against `value` in the given `child` process.
-- @usage global_equality(child, "_G.WfLoaded", true)
Helpers.expect.global_equality = MiniTest.new_expectation(
"variable in child process matches",
function(child, field, value)
return Helpers.expect.equality(child.lua_get(field), value)
end,
errorMessage
"variable in child process matches",
function(child, field, value)
return Helpers.expect.equality(child.lua_get(field), value)
end,
errorMessage
)

-- Check type equality of a global `field` against `value` in the given `child` process.
-- @usage global_type_equality(child, "_G.WfLoaded", "boolean")
Helpers.expect.global_type_equality = MiniTest.new_expectation(
"variable type in child process matches",
function(child, field, value)
return Helpers.expect.global_equality(child, "type(" .. field .. ")", value)
end,
errorMessage
"variable type in child process matches",
function(child, field, value)
return Helpers.expect.global_equality(child, "type(" .. field .. ")", value)
end,
errorMessage
)

-- Check equality of a config `field` against `value` in the given `child` process.
-- @usage option_equality(child, "debug", true)
Helpers.expect.config_equality = MiniTest.new_expectation("config option matches", function(child, field, value)
local full_name = require("wf.static").full_name
return Helpers.expect.global_equality(child, 'vim.g["' .. full_name .. "#" .. field .. '"]', value)
-- return Helpers.expect.global_equality(child, "_G.Wf.config." .. field, value)
end, errorMessage)
Helpers.expect.config_equality = MiniTest.new_expectation(
"config option matches",
function(child, field, value)
local full_name = require("wf.static").full_name
return Helpers.expect.global_equality(
child,
'vim.g["' .. full_name .. "#" .. field .. '"]',
value
)
-- return Helpers.expect.global_equality(child, "_G.Wf.config." .. field, value)
end,
errorMessage
)

-- Check type equality of a config `field` against `value` in the given `child` process.
-- @usage config_type_equality(child, "debug", "boolean")
Helpers.expect.config_type_equality = MiniTest.new_expectation(
"config option type matches",
function(child, field, value)
local full_name = require("wf.static").full_name
return Helpers.expect.global_equality(child, 'type(vim.g["' .. full_name .. "#" .. field .. '"])', value)
-- return Helpers.expect.global_equality(child, "type(_G.Wf.config." .. field .. ")", value)
end,
errorMessage
"config option type matches",
function(child, field, value)
local full_name = require("wf.static").full_name
return Helpers.expect.global_equality(
child,
'type(vim.g["' .. full_name .. "#" .. field .. '"])',
value
)
-- return Helpers.expect.global_equality(child, "type(_G.Wf.config." .. field .. ")", value)
end,
errorMessage
)

-- Check equality of a state `field` against `value` in the given `child` process.
-- @usage state_equality(child, "enabled", true)
Helpers.expect.state_equality = MiniTest.new_expectation("state matches", function(child, field, value)
return Helpers.expect.global_equality(child, "_G.Wf.enabled." .. field, value)
end, errorMessage)
Helpers.expect.state_equality = MiniTest.new_expectation(
"state matches",
function(child, field, value)
return Helpers.expect.global_equality(child, "_G.Wf.enabled." .. field, value)
end,
errorMessage
)

-- Check type equality of a state `field` against `value` in the given `child` process.
-- @usage state_type_equality(child, "enabled", "boolean")
Helpers.expect.state_type_equality = MiniTest.new_expectation("state type matches", function(child, field, value)
return Helpers.expect.global_equality(child, "type(_G.Wf.state." .. field .. ")", value)
end, errorMessage)
Helpers.expect.state_type_equality = MiniTest.new_expectation(
"state type matches",
function(child, field, value)
return Helpers.expect.global_equality(child, "type(_G.Wf.state." .. field .. ")", value)
end,
errorMessage
)

Helpers.expect.match = MiniTest.new_expectation("string matching", function(str, pattern)
return str:find(pattern) ~= nil
return str:find(pattern) ~= nil
end, errorMessage)

Helpers.expect.no_match = MiniTest.new_expectation("no string matching", function(str, pattern)
return str:find(pattern) == nil
return str:find(pattern) == nil
end, errorMessage)

-- Monkey-patch `MiniTest.new_child_neovim` with helpful wrappers
Helpers.new_child_neovim = function()
local child = MiniTest.new_child_neovim()
local child = MiniTest.new_child_neovim()

local prevent_hanging = function(method)
local prevent_hanging = function(method)
-- stylua: ignore
if not child.is_blocked() then return end

local msg = string.format("Can not use `child.%s` because child process is blocked.", method)
error(msg)
end
local msg = string.format("Can not use `child.%s` because child process is blocked.", method)
error(msg)
end

child.setup = function()
child.restart({ "-u", "scripts/minimal_init.lua" })
child.setup = function()
child.restart({ "-u", "scripts/minimal_init.lua" })

-- Change initial buffer to be readonly. This not only increases execution
-- speed, but more closely resembles manually opened Neovim.
child.bo.readonly = false
end
-- Change initial buffer to be readonly. This not only increases execution
-- speed, but more closely resembles manually opened Neovim.
child.bo.readonly = false
end

child.set_lines = function(arr, start, finish)
prevent_hanging("set_lines")
child.set_lines = function(arr, start, finish)
prevent_hanging("set_lines")

if type(arr) == "string" then
arr = vim.split(arr, "\n")
end
if type(arr) == "string" then
arr = vim.split(arr, "\n")
end

child.api.nvim_buf_set_lines(0, start or 0, finish or -1, false, arr)
end
child.api.nvim_buf_set_lines(0, start or 0, finish or -1, false, arr)
end

child.get_lines = function(start, finish)
prevent_hanging("get_lines")
child.get_lines = function(start, finish)
prevent_hanging("get_lines")

return child.api.nvim_buf_get_lines(0, start or 0, finish or -1, false)
end
return child.api.nvim_buf_get_lines(0, start or 0, finish or -1, false)
end

child.set_cursor = function(line, column, win_id)
prevent_hanging("set_cursor")
child.set_cursor = function(line, column, win_id)
prevent_hanging("set_cursor")

child.api.nvim_win_set_cursor(win_id or 0, { line, column })
end
child.api.nvim_win_set_cursor(win_id or 0, { line, column })
end

child.get_cursor = function(win_id)
prevent_hanging("get_cursor")
child.get_cursor = function(win_id)
prevent_hanging("get_cursor")

return child.api.nvim_win_get_cursor(win_id or 0)
end
return child.api.nvim_win_get_cursor(win_id or 0)
end

child.set_size = function(lines, columns)
prevent_hanging("set_size")
child.set_size = function(lines, columns)
prevent_hanging("set_size")

if type(lines) == "number" then
child.o.lines = lines
end
if type(lines) == "number" then
child.o.lines = lines
end

if type(columns) == "number" then
child.o.columns = columns
end
end
if type(columns) == "number" then
child.o.columns = columns
end
end

child.get_size = function()
prevent_hanging("get_size")
child.get_size = function()
prevent_hanging("get_size")

return { child.o.lines, child.o.columns }
end
return { child.o.lines, child.o.columns }
end

child.expect_screenshot = function(opts, path, screenshot_opts)
if child.fn.has("nvim-0.8") == 0 then
MiniTest.skip("Screenshots are tested for Neovim>=0.8 (for simplicity).")
end
child.expect_screenshot = function(opts, path, screenshot_opts)
if child.fn.has("nvim-0.8") == 0 then
MiniTest.skip("Screenshots are tested for Neovim>=0.8 (for simplicity).")
end

MiniTest.expect.reference_screenshot(child.get_screenshot(screenshot_opts), path, opts)
end
MiniTest.expect.reference_screenshot(child.get_screenshot(screenshot_opts), path, opts)
end

return child
return child
end

return Helpers
68 changes: 35 additions & 33 deletions tests/test_API.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,62 @@ local helpers = dofile("tests/helpers.lua")

local child = helpers.new_child_neovim()
local eq_global, eq_config, eq_state =
helpers.expect.global_equality, helpers.expect.config_equality, helpers.expect.state_equality
helpers.expect.global_equality, helpers.expect.config_equality, helpers.expect.state_equality
local eq_type_global, eq_type_config, eq_type_state =
helpers.expect.global_type_equality, helpers.expect.config_type_equality, helpers.expect.state_type_equality
helpers.expect.global_type_equality,
helpers.expect.config_type_equality,
helpers.expect.state_type_equality

local T = MiniTest.new_set({
hooks = {
-- This will be executed before every (even nested) case
pre_case = function()
-- Restart child process with custom 'init.lua' script
child.restart({ "-u", "scripts/minimal_init.lua" })
end,
-- This will be executed one after all tests from this set are finished
post_once = child.stop,
},
hooks = {
-- This will be executed before every (even nested) case
pre_case = function()
-- Restart child process with custom 'init.lua' script
child.restart({ "-u", "scripts/minimal_init.lua" })
end,
-- This will be executed one after all tests from this set are finished
post_once = child.stop,
},
})

-- Tests related to the `setup` method.
T["setup()"] = MiniTest.new_set()

T["setup()"]["sets exposed methods and default options value"] = function()
child.lua([[require('wf').setup()]])
child.lua([[require('wf').setup()]])

-- -- global object that holds your plugin information
-- eq_type_global(child, "_G.Wf", "table")
-- -- global object that holds your plugin information
-- eq_type_global(child, "_G.Wf", "table")

-- -- public methods
-- eq_type_global(child, "_G.Wf.toggle", "function")
-- eq_type_global(child, "_G.Wf.disable", "function")
-- eq_type_global(child, "_G.Wf.enable", "function")
-- -- public methods
-- eq_type_global(child, "_G.Wf.toggle", "function")
-- eq_type_global(child, "_G.Wf.disable", "function")
-- eq_type_global(child, "_G.Wf.enable", "function")

-- -- config
-- eq_type_global(child, "_G.Wf.config", "table")
-- -- config
-- eq_type_global(child, "_G.Wf.config", "table")

-- -- assert the value, and the type
-- eq_config(child, "debug", false)
-- eq_type_config(child, "debug", "boolean")
-- -- assert the value, and the type
-- eq_config(child, "debug", false)
-- eq_type_config(child, "debug", "boolean")
end

T["setup()"]["overrides default values"] = function()
child.lua([[require('wf').setup({
child.lua([[require('wf').setup({
-- write all the options with a value different than the default ones
theme = "chad",
})]])
eq_config(child, "theme", "chad")
eq_type_config(child, "theme", "string")
eq_config(child, "theme", "chad")
eq_type_config(child, "theme", "string")

-- child.lua([[require('wf').setup({
-- -- write all the options with a value different than the default ones
-- debug = true,
-- })]])
-- child.lua([[require('wf').setup({
-- -- write all the options with a value different than the default ones
-- debug = true,
-- })]])

-- -- assert the value, and the type
-- eq_config(child, "debug", true)
-- eq_type_config(child, "debug", "boolean")
-- -- assert the value, and the type
-- eq_config(child, "debug", true)
-- eq_type_config(child, "debug", "boolean")
end

return T