Skip to content

Commit

Permalink
Implement custom test framework (#63)
Browse files Browse the repository at this point in the history
* WIP

* first test working

* typo

* update test playground files

* rolling my own test runner

* create debug logger

* create debug logger

* a bit of cleanup

* 4 tests working

* more WIP

* test framework beta ready

* unit tests

* help spec

* various things

* typo

* add TEST_README.md

* update test readme

* almost there

* implement global test queue

* move test framework into its own directory

* delete submodules

* update validate.sh

* formatting

---------

Co-authored-by: Simon McLean <simonmclean@192.168.1.116>
  • Loading branch information
simonmclean and Simon McLean committed Sep 12, 2024
1 parent c11c540 commit 84ee0bf
Show file tree
Hide file tree
Showing 48 changed files with 1,271 additions and 1,416 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
git_ignored_file
1 change: 0 additions & 1 deletion lib/busted
Submodule busted deleted from 5ed85d
1 change: 0 additions & 1 deletion lib/luassert
Submodule luassert deleted from d3528b
4 changes: 1 addition & 3 deletions lua/triptych/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ end
---@param State TriptychState
---@param refresh_view fun(): nil
function Actions.new(State, refresh_view)
local vim = _G.triptych_mock_vim or vim

local M = {}

---@return nil
Expand Down Expand Up @@ -308,7 +306,7 @@ function Actions.new(State, refresh_view)
for _, item in ipairs(State.copy_list) do
local destination = get_copy_path(u.path_join(destination_dir, item.display_name))
M.duplicate_file_or_dir(item, destination)
autocmds.publish_file_created(destination)
autocmds.publish_did_delete_node(destination)
end

view.jump_cursor_to(State, destination_dir)
Expand Down
5 changes: 3 additions & 2 deletions lua/triptych/autocmds.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local log = require 'triptych.logger'

local au_group_internal = vim.api.nvim_create_augroup('TriptychEventsInternal', { clear = true })
local au_group_public = vim.api.nvim_create_augroup('TriptychEvents', { clear = true })

Expand All @@ -11,7 +13,6 @@ local AutoCommands = {}
---@param Git? Git
---@return AutoCommands
function AutoCommands.new(event_handlers, State, Diagnostics, Git)
local vim = _G.triptych_mock_vim or vim
local instance = {}
setmetatable(instance, { __index = AutoCommands })

Expand Down Expand Up @@ -55,7 +56,6 @@ end
M.new = AutoCommands.new

function AutoCommands:destroy_autocommands()
local vim = _G.triptych_mock_vim or vim
for _, autocmd in pairs(self.autocmds) do
vim.api.nvim_del_autocmd(autocmd)
end
Expand Down Expand Up @@ -125,6 +125,7 @@ end

---@param win_type WinType
function M.publish_did_update_window(win_type)
log.debug('publish_did_update_window', { win_type = win_type })
exec_public_autocmd('TriptychDidUpdateWindow', {
win_type = win_type,
})
Expand Down
1 change: 1 addition & 0 deletions lua/triptych/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ end
---@return TriptychConfig
local function default_config()
return {
debug = false,
mappings = {
-- Everything below is buffer-local, meaning it will only apply to Triptych windows
show_help = 'g?',
Expand Down
1 change: 0 additions & 1 deletion lua/triptych/diagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Diagnostics.get_sign = function(severity)
end

Diagnostics.new = function()
local vim = _G.triptych_mock_vim or vim
local instance = {}
setmetatable(instance, { __index = Diagnostics })

Expand Down
15 changes: 9 additions & 6 deletions lua/triptych/event_handlers.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
local u = require 'triptych.utils'
local log = require 'triptych.logger'
local float = require 'triptych.float'
local autocmds = require 'triptych.autocmds'
local view = require 'triptych.view'

local M = {}

---When the cursor has moved trigger an update of the child/preview window
---@param State TriptychState
---@return nil
function M.handle_cursor_moved(State)
local vim = _G.triptych_mock_vim or vim
local view = _G.triptych_mock_view or require 'triptych.view'
log.debug 'handle_cursor_moved'
local target = view.get_target_under_cursor(State)
local current_dir = State.windows.current.path
local line_number = vim.api.nvim_win_get_cursor(0)[1]
Expand Down Expand Up @@ -42,8 +43,7 @@ end
---@param Git? Git
---@return nil
function M.handle_dir_read(State, path_details, win_type, Diagnostics, Git)
local vim = _G.triptych_mock_vim or vim
local view = _G.triptych_mock_view or require 'triptych.view'
log.debug('handle_cursor_moved', { win_type = win_type })
view.set_parent_or_primary_window_lines(State, path_details, win_type, Diagnostics, Git)

-- Set cursor position
Expand Down Expand Up @@ -79,8 +79,11 @@ end
---@param path string
---@param lines string[]
function M.handle_file_read(child_win_buf, path, lines)
float.set_child_window_file_preview(child_win_buf, path, lines)
autocmds.publish_did_update_window 'child'
log.debug('handle_file_read', { path = path })
if vim.g.triptych_is_open then
float.set_child_window_file_preview(child_win_buf, path, lines)
autocmds.publish_did_update_window 'child'
end
end

---@return nil
Expand Down
11 changes: 0 additions & 11 deletions lua/triptych/float.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ local M = {}
---@param fn fun(): nil
---@return nil
local function modify_locked_buffer(buf, fn)
local vim = _G.triptych_mock_vim or vim
vim.api.nvim_buf_set_option(buf, 'readonly', false)
vim.api.nvim_buf_set_option(buf, 'modifiable', true)
fn()
Expand All @@ -21,7 +20,6 @@ end
---@param lines string[]
---@return nil
function M.buf_set_lines(buf, lines)
local vim = _G.triptych_mock_vim or vim
modify_locked_buffer(buf, function()
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
end)
Expand All @@ -32,7 +30,6 @@ end
---@param highlights HighlightDetails[]
---@return nil
function M.buf_apply_highlights(buf, highlights)
local vim = _G.triptych_mock_vim or vim
-- Apply icon highlight
for i, highlight in ipairs(highlights) do
vim.api.nvim_buf_add_highlight(buf, 0, highlight.icon.highlight_name, i - 1, 0, highlight.icon.length)
Expand All @@ -46,7 +43,6 @@ end
---@param lines string[]
---@param attempt_scroll_top? boolean
function M.win_set_lines(win, lines, attempt_scroll_top)
local vim = _G.triptych_mock_vim or vim
local buf = vim.api.nvim_win_get_buf(win)
M.buf_set_lines(buf, lines)
if attempt_scroll_top then
Expand All @@ -63,7 +59,6 @@ end
---@param postfix? string
---@return nil
function M.win_set_title(win, title, icon, highlight, postfix)
local vim = _G.triptych_mock_vim or vim
vim.api.nvim_win_call(win, function()
local maybe_icon = ''
if vim.g.triptych_config.options.file_icons.enabled and icon then
Expand Down Expand Up @@ -96,15 +91,13 @@ end

---@return number
local function create_new_buffer()
local vim = _G.triptych_mock_vim or vim
local buf = vim.api.nvim_create_buf(false, true)
return buf
end

---@param config FloatingWindowConfig
---@return number
local function create_floating_window(config)
local vim = _G.triptych_mock_vim or vim
local buf = create_new_buffer()
local win = vim.api.nvim_open_win(buf, true, {
width = config.width,
Expand Down Expand Up @@ -132,7 +125,6 @@ end
---@param winblend number
---@return number
local function create_backdrop(winblend)
local vim = _G.triptych_mock_vim or vim
local buf = create_new_buffer()
local win = vim.api.nvim_open_win(buf, false, {
width = vim.o.columns,
Expand Down Expand Up @@ -174,8 +166,6 @@ function M.create_three_floating_windows(
margin_x,
margin_y
)
local vim = _G.triptych_mock_vim or vim

local screen_height = vim.o.lines

local screen_width = vim.o.columns
Expand Down Expand Up @@ -304,7 +294,6 @@ end
---@param wins number[]
---@return nil
function M.close_floats(wins)
local vim = _G.triptych_mock_vim or vim
for _, win in ipairs(wins) do
local buf = vim.api.nvim_win_get_buf(win)
vim.api.nvim_buf_delete(buf, { force = true })
Expand Down
2 changes: 0 additions & 2 deletions lua/triptych/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ local M = {}
---@param path string
---@return number
function M.get_file_size_in_kb(path)
local vim = _G.triptych_mock_vim or vim
local bytes = vim.fn.getfsize(path)
return bytes / 1000
end
Expand Down Expand Up @@ -43,7 +42,6 @@ end, 2)
---@param show_hidden boolean
---@param callback fun(path_details: PathDetails): nil
function M.get_path_details(_path, show_hidden, callback)
local vim = _G.triptych_mock_vim or vim
local path = vim.fs.normalize(_path)

local tree = {
Expand Down
1 change: 0 additions & 1 deletion lua/triptych/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ end
M.Git = {}

function M.Git.new()
local vim = _G.triptych_mock_vim or vim
local instance = {}
setmetatable(instance, { __index = M.Git })

Expand Down
1 change: 0 additions & 1 deletion lua/triptych/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

---@return string[]
local function help_lines()
local vim = _G.triptych_mock_vim or vim
local mappings = vim.g.triptych_config.mappings
local lines = {}
local left_col_length = 0 -- Used for padding and alignment
Expand Down
4 changes: 0 additions & 4 deletions lua/triptych/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ end
---@param dir? string Path of directory to open. If omitted will be the directory containing the current buffer
---@return fun()|nil
local function toggle_triptych(dir)
local vim = _G.triptych_mock_vim or vim

if dir and not vim.fn.isdirectory(dir) then
return warn(tostring(dir) .. ' is not a directory')
end
Expand Down Expand Up @@ -142,8 +140,6 @@ end

---@param user_config? table
local function setup(user_config)
local vim = _G.triptych_mock_vim or vim

if vim.fn.has 'nvim-0.9.0' ~= 1 then
return warn 'triptych.nvim requires Neovim >= 0.9.0'
end
Expand Down
13 changes: 13 additions & 0 deletions lua/triptych/logger.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
return {
---@param function_name string
---@param data? table
debug = function(function_name, data)
if vim.g.triptych_config.debug then
local log_line = '[triptych][' .. function_name .. '] '
if data then
log_line = log_line .. ' ' .. vim.inspect(data)
end
vim.print(log_line)
end
end,
}
1 change: 0 additions & 1 deletion lua/triptych/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local Mappings = {}
---@param actions unknown
---@param refresh_fn fun(): nil
function Mappings.new(State, actions, refresh_fn)
local vim = _G.triptych_mock_vim or vim
local mappings = vim.g.triptych_config.mappings
local extension_mappings = vim.g.triptych_config.extension_mappings

Expand Down
3 changes: 0 additions & 3 deletions lua/triptych/syntax_highlighting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ local M = {}
---@param buf number
---@return nil
M.stop = function(buf)
local vim = _G.triptych_mock_vim or vim
vim.treesitter.stop(buf)
vim.api.nvim_buf_set_option(buf, 'syntax', 'off')
end
Expand All @@ -14,8 +13,6 @@ end
---@param filetype? string
---@return nil
M.start = function(buf, filetype)
local vim = _G.triptych_mock_vim or vim

-- Because this function will be debounced we need to check that the buffer still exists
if not vim.api.nvim_buf_is_valid(buf) then
return
Expand Down
2 changes: 2 additions & 0 deletions lua/triptych/types.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---@class TriptychConfig
---@field debug boolean
---@field config boolean
---@field mappings TriptychConfigMappings
---@field extension_mappings { [string]: ExtensionMapping }
---@field options TriptychConfigOptions
Expand Down
11 changes: 0 additions & 11 deletions lua/triptych/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ end
---@return string[] # Lines including icons
---@return HighlightDetails[]
local function path_details_to_lines(State, path_details)
local vim = _G.triptych_mock_vim or vim
local config_options = vim.g.triptych_config.options
local icons_enabled = config_options.file_icons.enabled
local lines = {}
Expand Down Expand Up @@ -163,7 +162,6 @@ end
---@param State TriptychState
---@return PathDetails?
function M.get_target_under_cursor(State)
local vim = _G.triptych_mock_vim or vim
local line_number = vim.api.nvim_win_get_cursor(0)[1]
local contents = State.windows.current.contents
if contents then
Expand All @@ -175,7 +173,6 @@ end
---@param State TriptychState
---@return PathDetails[]
function M.get_targets_in_selection(State)
local vim = _G.triptych_mock_vim or vim
local from = vim.fn.getpos('v')[2]
local to = vim.api.nvim_win_get_cursor(0)[1]
local results = {}
Expand Down Expand Up @@ -208,7 +205,6 @@ end
---@param path string
---@return string?
local function get_title_postfix(path)
local vim = _G.triptych_mock_vim or vim
if path == vim.fn.getcwd() then
return '(cwd)'
end
Expand All @@ -230,7 +226,6 @@ end
---@param group string # see :h sign-group
---@return nil
local function set_sign_columns(buf, children, group)
local vim = _G.triptych_mock_vim or vim
vim.fn.sign_unplace(group)
for index, entry in ipairs(children) do
if entry.git_status then
Expand All @@ -250,8 +245,6 @@ end
---@param target_dir string
---@return nil
function M.set_primary_and_parent_window_targets(State, target_dir)
local vim = _G.triptych_mock_vim or vim

local focused_win = State.windows.current.win
local parent_win = State.windows.parent.win
local child_win = State.windows.child.win
Expand Down Expand Up @@ -296,8 +289,6 @@ end
---@param Git? Git
---@return nil
function M.set_parent_or_primary_window_lines(State, path_details, win_type, Diagnostics, Git)
local vim = _G.triptych_mock_vim or vim

local state = u.eval(function()
if win_type == 'parent' then
return State.windows.parent
Expand Down Expand Up @@ -354,7 +345,6 @@ end
---@param path_details PathDetails
---@return nil
function M.set_child_window_target(State, path_details)
local vim = _G.triptych_mock_vim or vim
local buf = vim.api.nvim_win_get_buf(State.windows.child.win)

-- TODO: Can we make path_details mandatory to avoid the repeated checks
Expand Down Expand Up @@ -448,7 +438,6 @@ end
---@param path string
---@return nil
function M.jump_cursor_to(State, path)
local vim = _G.triptych_mock_vim or vim
local line_num
for index, item in ipairs(State.windows.current.contents.children) do
if item.path == path then
Expand Down
Loading

0 comments on commit 84ee0bf

Please sign in to comment.