Added unittest + basic python and lua unittests (#174)

---------

Co-authored-by: Danymat <d.danymat@gmail.com>
This commit is contained in:
Colin Kennedy
2024-07-23 12:18:14 -04:00
committed by GitHub
parent 6de0add480
commit fef1ab3932
9 changed files with 1027 additions and 2 deletions

47
tests/utils/specs.lua Normal file
View File

@@ -0,0 +1,47 @@
local textmate = require("tests.utils.textmate")
local neogen = require("neogen")
local M = {}
--- Make a docstring for given language and return the `neogen` result.
---@param source string Pseudo-source-code to call. It contains `"|cursor|"` which is the expected user position.
---@param filetype string Buffer filetype.
---@param neogen_opts table? Custom Neogen opts passed during generate command.
---@return string? # The same source code, after calling neogen.
function M.make_docstring(source, filetype, neogen_opts)
local result = textmate.extract_cursors(source)
if not result then
vim.notify(
string.format("Source\n\n%s\n\nwas not parsable. Does it have a |cursor| defined?", source),
vim.log.levels.ERROR
)
return nil
end
local cursors, code = unpack(result)
local buffer = vim.api.nvim_create_buf(true, true)
vim.bo[buffer].filetype = filetype
vim.cmd.buffer(buffer)
local window = vim.fn.win_getid() -- We just created the buffer so the current window works
local strict_indexing = false
vim.api.nvim_buf_set_lines(buffer, 0, -1, strict_indexing, vim.fn.split(code, "\n"))
-- IMPORTANT: Because we are adding docstrings in the buffer, we must start
-- from the bottom docstring up. Otherwise the row/column positions of the
-- next `cursor` in `cursors` will be out of date.
for index = #cursors, 1, -1 do
local cursor = cursors[index]
vim.api.nvim_win_set_cursor(window, cursor)
neogen_opts = vim.tbl_deep_extend("force", { snippet_engine = "nvim" }, neogen_opts or {})
neogen.generate(neogen_opts)
end
return table.concat(vim.api.nvim_buf_get_lines(buffer, 0, -1, strict_indexing), "\n")
end
return M

65
tests/utils/textmate.lua Normal file
View File

@@ -0,0 +1,65 @@
--- A file to make dealing with text in Lua a little easier.
---
--- @module 'tests.textmate'
local _CURSOR_MARKER = "|cursor|"
local M = {}
---@class Cursor
--- A fake position in-space where a user's cursor is meant to be.
---@field [1] number
--- A 1-or-more value indicating the buffer line value.
---@field [2] number
--- A 1-or-more value indicating the buffer column value.
--- Find all lines marked with `"|cursor|"` and return their row / column positions.
---
---@param source string Pseudo-Python source-code to call. It contains `"|cursor|"` which is the expected user position.
---@return Cursor[] # The row and column cursor position in `source`.
---@return string # The same source code but with `"|cursor|"` stripped out.
function M.extract_cursors(source)
local index = 1
local count = #source
local cursors = {}
local cursor_marker_offset = #_CURSOR_MARKER - 1
local code = ""
local current_row = 1
local current_column = 1
while index <= count do
local character = source:sub(index, index)
if character == "\n" then
current_row = current_row + 1
current_column = 1
else
current_column = current_column + 1
end
if character == "|" then
if source:sub(index, index + cursor_marker_offset) == _CURSOR_MARKER then
index = index + cursor_marker_offset
table.insert(cursors, { current_row, current_column })
else
code = code .. character
end
else
code = code .. character
end
index = index + 1
end
if index <= count then
-- If this happens, is because the while loop called `break` early
-- This is very likely so we add the last character(s) to the output
local remainder = source:sub(index, #source)
code = code .. remainder
end
return { cursors, code }
end
return M