Still working on my neovim configuration.
I use Google test to write my unit tests. Which means that all the unit test begin with TEST(<groupName>, <testName>). This little script see if the cursor is inside a test sets a break point and then runs the test application for that test.
This is useful as I can run my make test from inside neovim that builds and runs the test. If any of the unit tests fail then the error is in the QuickFix list which allows me to jump to the failing test in a couple of key strokes and I can now invoke the test directly.
Any pointers on Bad Lua or bad usage of neovim functions and better ways appreciated.
-- Set the Key <leader>du to run this command
-- Run all the unit tests
vim.cmd.DapRunUnitTests = function() vim.cmd.DapRunUnitTestCode({}) end
-- Set the Key <leader>dt to run this command
-- Run the unit test that the cursor is over.
vim.cmd.DapRunOneTests = function()
-- Search backwards for a line that marks the begining of the TEST.
local lineNo = vim.fn.search('TEST(', 'bn')
if (lineNo == 0) then
return
end
-- Extract the name of the test
local text = vim.fn.getline(lineNo)
local res = { string.match(text, "(%w+)%((%w+), (%w+)%)") }
if (#res == 3 and res[1] == 'TEST') then
-- Set a breakpoint at the current line
-- Then run the unit test code specifying the unit test to run.
require('dap').set_breakpoint()
vim.cmd.DapRunUnitTestCode({'---gtest_filter=' .. res[2] .. '.' .. res[3]})
end
end
-- Function that runs the unit tests.
vim.cmd.DapRunUnitTestCode = function(args)
-- Use make to get runtime environment need to run the
-- unit test application.
local handle = assert(io.popen('make neovimruntime', 'r'))
local output = handle:read('*a')
io.close(handle)
-- Set the runtime environment.
-- Need to modify for non MAC environments.
require('posix').setenv('DYLD_LIBRARY_PATH', output)
-- Set up the config needed.
local config = {
name = 'Launch',
type = 'lldb',
request = 'launch',
program = 'test/coverage/unittest.prog',
cwd = '${workspaceFolder}',
stopOnEntry = false,
args = args,
}
-- Start the debug seasion.
require('dap').run(config)
end