Basic Neovim GUI Test Framework

July - 2021

There's probably a better way to do this but I couldn't find it.

This gets around seeming async issues by feeding a table with a list of function to run into a set of two functions that alternate back and forth between themselves with a delay between calling the functions to test.


This needs editing

grimoire = require('grimoire')

local tests_to_run = {}
local log_file_path = 'testing.log'
local current_delay = 500

--- 

local function p()
    local log_file = io.open(log_file_path, "a")
    io.output(log_file)
    io.write("OK: ")
    io.close(log_file)
end

local function f()
    local log_file = io.open(log_file_path, "a")
    io.output(log_file)
    io.write("FAIL: ")
    io.close(log_file)
end

local function log(message)
    local log_file = io.open(log_file_path, "a")
    io.output(log_file)
    io.write(message.."\n")
    io.close(log_file)
end

function tick()
    -- TODO: Reset the timer to the default after 
    -- each adjustment
    if #tests_to_run > 0 then 
        function_to_run = table.remove(tests_to_run, 1)
        _G[function_to_run]()
        if #tests_to_run > 0 then 
            vim.fn.timer_start(current_delay, tock)
        end
    end
end

function tock()
    if #tests_to_run > 0 then 
        function_to_run = table.remove(tests_to_run, 1)
        _G[function_to_run]()
        if #tests_to_run > 0 then 
            vim.fn.timer_start(current_delay, tick)
        end
    end
end

--------------------------------------

function verify_initial_state() 
    if grimoire.state.is_active == false then
        p()
    else
        f()
    end
    log("Pre-launch")

    grimoire.grimoire()
    if grimoire.state.is_active == true then
        p()
    else
        f()
    end
    log("Initial state")
end

function verify_open_windows()
    window_list = vim.api.nvim_list_wins()
    if #window_list == 4 then
        p()
    else
        f()
    end
    log("Initial window count")
end

function verify_current_search()
    vim.api.nvim_feedkeys("apple orange", "i", true)
    current_delay = 2400
end

function verify_closed_windows()
    grimoire.grimoire()
    window_list = vim.api.nvim_list_wins()
    if #window_list == 1 then
        p()
    else
        f()
    end
    log("Windows closed")
end

local function runner() 
    log('---------')
    tests_to_run = {
        'verify_initial_state',
        'verify_open_windows',
        'verify_current_search',
        'verify_closed_windows',
    }
    tick()
end 

return {
    runner = runner
}