Skip to content

Commit

Permalink
4 tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon McLean committed Sep 3, 2024
1 parent 1a6d9fb commit 8491f90
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
31 changes: 29 additions & 2 deletions tests/test_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local TIMEOUT_SECONDS = 3

---@class Test
---@field name string
---@field only boolean
---@field run fun(callback: fun(passed: boolean, fail_message?: string))

---@param description string
Expand All @@ -15,6 +16,7 @@ M.test = function(description, test_body)
---@type Test
return {
name = description,
only = false,
run = function(callback)
local success, err = pcall(test_body, function(assertions)
local passed, failed = pcall(assertions)
Expand All @@ -33,6 +35,15 @@ M.test = function(description, test_body)
}
end

---@param description string
---@param test_body fun(done: function)
---@return Test
M.test_only = function(description, test_body)
local test = M.test(description, test_body)
test.only = true
return test
end

---@param test_name string
---@param passed boolean
---@param fail_message? string
Expand All @@ -47,6 +58,22 @@ end

---@param tests Test[]
M.run_tests = function(tests)
---@type Test[]
local tests_marked_only = {}
for _, test in ipairs(tests) do
if test.only then
table.insert(tests_marked_only, test)
end
end

---@type Test[]
local tests_to_run
if #tests_marked_only > 0 then
tests_to_run = tests_marked_only
else
tests_to_run = tests
end

local timer = vim.loop.new_timer()

---@param remaining_tests Test[]
Expand All @@ -55,7 +82,7 @@ M.run_tests = function(tests)
local current_test = table.remove(remaining_tests, #remaining_tests)

if current_test then
timer:start(1000 * TIMEOUT_SECONDS, 0, function ()
timer:start(1000 * TIMEOUT_SECONDS, 0, function()
output_result(current_test.name, false, 'Timeout after ' .. TIMEOUT_SECONDS .. ' seconds')
end)
current_test.run(function(passed, fail_message)
Expand All @@ -68,7 +95,7 @@ M.run_tests = function(tests)
end
end

run_tests(u.reverse_list(tests))
run_tests(u.reverse_list(tests_to_run))
end

return M
48 changes: 46 additions & 2 deletions tests/tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local u = require 'tests.utils'
local test_runner = require 'tests.test_runner'
local run_tests = test_runner.run_tests
local test = test_runner.test
local test_only = test_runner.test_only

local function open_triptych(callback)
local cwd = vim.fn.getcwd()
Expand Down Expand Up @@ -57,8 +58,34 @@ run_tests {
}

open_triptych(function()
-- Nav right
u.on_all_wins_updated(function()
local result = u.get_state()
close_triptych(function()
done(function()
assert.same(expected_lines, result.lines)
assert.same(expected_winbars, result.winbars)
end)
end)
end)
u.press_key 'l'
end)
end),

test('navigates up the filesystem', function(done)
local expected_lines = {
child = { 'level_4/', 'level_3_file_1.md' },
primary = { 'level_3/', 'level_2_file_1.lua' },
parent = { 'level_2/', 'level_1_file_1.lua' },
}

local expected_winbars = {
child = '%#WinBar#%=%#WinBar#level_3/%=',
primary = '%#WinBar#%=%#WinBar#level_2%=',
parent = '%#WinBar#%=%#WinBar#level_1%=',
}

open_triptych(function()
u.press_key 'h'
u.on_all_wins_updated(function()
local result = u.get_state()
close_triptych(function()
Expand All @@ -70,9 +97,26 @@ run_tests {
end)
end)
end),

test('opens a file', function(done)
-- Used to return to this buffer, after the file is opened
local current_buf = vim.api.nvim_get_current_buf()

open_triptych(function()
u.on_event('TriptychDidClose', function()
done(function()
assert.same(vim.g.triptych_is_open, false)
vim.api.nvim_set_current_buf(current_buf)
end)
end)
u.press_key 'j'
u.on_child_window_updated(function()
u.press_key 'l'
end)
end)
end),
}

-- it('navigates up the filesystem', function() end)
--
-- it('opens a file', function() end)
--
Expand Down
17 changes: 17 additions & 0 deletions tests/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function M.on_event(event, callback, once)
})
end

function M.on_child_window_updated(callback)
M.on_event('TriptychDidUpdateWindow', callback)
end

function M.on_all_wins_updated(callback)
local wins_updated = {
child = false,
Expand Down Expand Up @@ -118,6 +122,19 @@ function M.press_key(k)
api.nvim_exec2('norm! ' .. k, {})
end

function M.key_sequence(keys)
local function key_sequence(remaining_keys)
local k = table.remove(remaining_keys, #remaining_keys)
M.press_key(k)
if #remaining_keys > 0 then
vim.schedule(function()
M.key_sequence(remaining_keys)
end)
end
end
key_sequence(M.reverse_list(keys))
end

function M.reverse_list(list)
local reversed = {}
for i = #list, 1, -1 do
Expand Down

0 comments on commit 8491f90

Please sign in to comment.