Skip to content

Commit

Permalink
Merge pull request #6 from Dich0tomy/ref/test-system
Browse files Browse the repository at this point in the history
test: add an automatic test runner
  • Loading branch information
Dich0tomy authored Jul 24, 2024
2 parents 0e90b8b + 7566388 commit 864a627
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 160 deletions.
6 changes: 4 additions & 2 deletions .busted
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
return {
_all = {
coverage = false,
lpath = "lua/?.lua;lua/?/init.lua",
lua = "nlua",
lpath = 'lua/?.lua;lua/?/init.lua',
pattern = '^auto_test_runner.lua$',
lua = 'nlua',
ROOT = { 'lua/' }
},
default = {
verbose = true
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@
!*.rockspec

# Plugin test & lint
!spec/
!.busted
!.luacheckrc
2 changes: 1 addition & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
std = "luajit"
cache = true
include_files = {"spec/**.lua", "lua/**.lua", "*.rockspec", ".luacheckrc"}
include_files = {"lua/**.lua", "*.rockspec", ".luacheckrc"}

read_globals = {
"vim",
Expand Down
45 changes: 45 additions & 0 deletions lua/cpp-tools/auto_test_runner.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--- This is a special file which gets called by busted.
--- This file scans all the requirable `lua/` files from here
--- and runs their __test functions with busted test context.

local current_filename = debug.getinfo(1).source:match('([%w_%.]+)$')

local function project_lua_files(path, type)
return type == 'file' and vim.endswith(path, 'lua') and not vim.endswith(path, current_filename)
end

local function path_to_requirable(path)
return path:match('lua/(.*)'):gsub('/init%.lua', ''):gsub('%.lua', ''):gsub('/', '.')
end

local function run_module_test(name)
local ok, module = pcall(require, name)
if not ok then
describe(name, function()
it('Has an error!', function()
assert.message(('Requiring this module failed with the following error:\n%s'):format(module)).truthy(false)
end)
end)
return
end

-- This module is something else, we don't fw it
if type(module) ~= 'table' then
return
end

local test_func = vim.tbl_get(module, '__test')
if test_func then
setfenv(test_func, getfenv())()
end
end

local function is_lua_dir(dir)
return vim.startswith(dir, 'lua')
end

vim
.iter(vim.fs.dir('.', { depth = 10, skip = is_lua_dir }))
:filter(project_lua_files)
:map(path_to_requirable)
:each(run_module_test)
2 changes: 1 addition & 1 deletion lua/cpp-tools/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ local function ensure_dependencies()
:each(function(not_found_dep) end)
end

local M
local M = {}

function M.check()
validate_config()
Expand Down
49 changes: 0 additions & 49 deletions lua/cpp-tools/lib/iter.lua

This file was deleted.

149 changes: 149 additions & 0 deletions lua/cpp-tools/lib/iter/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
local M = {}

---Counts the lines in a string
---@param str string the string to count lines for
---@return number line count # The number of lines
function M.line_count(str)
local count = 1
local length = #str
for i = 1, length - 1 do
local c = str:sub(i, i)
if c == '\n' then
if i ~= (length - 2) then
count = count + 1
end
end
end

return count
end

---Splits the string into lines and immediately returns the amount as well
---@param str string the string to split
---@return string[] lines # An array of lines
---@return number # The amount of lines in the array
function M.lines(str)
local lines = vim.split(str, '\n')
return lines, #lines
end

---Checks if two array tables have equal values
---@generic T
---@param arr1 `T`[] first array
---@param arr2 T[] second array
---@return boolean # are the arrays equal
function M.arrays_equal(arr1, arr2)
if arr1 == arr2 then
return true
elseif #arr1 ~= #arr2 then
return false
end

for i = 1, #arr1 do
if arr1[i] ~= arr2[i] then
return false
end
end

return true
end

function M.__test()
describe('array_equals', function()
local iter = require('lua.cpp-tools.lib.iter.init')

it('Two same references to empty are equal', function()
local empty = {}
local a = empty
local b = empty

assert.is.truthy(iter.arrays_equal(a, b))
end)

it('Two different references to empty are equal', function()
local a = {}
local b = {}

assert.is.truthy(iter.arrays_equal(a, b))
end)

it('Two same values are equal', function()
local a = { 1, 2 }
local b = { 1, 2 }

assert.is.truthy(iter.arrays_equal(a, b))
end)

it('Two same values with different order are not equal', function()
local a = { 1, 2 }
local b = { 2, 1 }

assert.is.falsy(iter.arrays_equal(a, b))
end)

it('Two same values with different values', function()
local a = { '' }
local b = { 1 }

assert.is.falsy(iter.arrays_equal(a, b))
end)
end)

describe('line_count', function()
local iter = require('lua.cpp-tools.lib.iter.init')

it('Returns 1 for an empty string', function()
assert.are.equal(iter.line_count(''), 1)
end)

it('Returns 1 for a non empty string with one line', function()
assert.are.equal(iter.line_count('Hello there'), 1)
end)

it('Returns 1 for a string with a trailing newlien', function()
assert.are.equal(iter.line_count('Foo\n'), 1)
end)

it('Returns 2 for a string with a trailing newline and content after it', function()
assert.are.equal(iter.line_count('Foo\nBar'), 2)
end)

it('Returns X for a string with X lines (ignoring last newline)', function()
local three_lined_string = [[
first line
second line
third line]]

assert.are.equal(iter.line_count(three_lined_string), 3)
end)
end)

describe('lines', function()
local iter = require('lua.cpp-tools.lib.iter.init')

it('Returns one empty line untouched', function()
local empty_line = ''
local lines = iter.lines(empty_line)

assert.are.equal(#lines, 1)
assert.are.equal(lines[1], empty_line)
end)

it('Returns one non empty line untouched', function()
local empty_line = 'asdasdasdasdasd'
local lines = iter.lines(empty_line)

assert.are.equal(#lines, 1)
assert.are.equal(lines[1], empty_line)
end)

it('Returns X lines for a string with X lines', function()
local lines_arr = { 'line1', 'line2', 'line3' }
local lines_str = vim.fn.join(lines_arr, '\n')

assert.is.truthy(iter.arrays_equal(iter.lines(lines_str), lines_arr))
end)
end)
end

return M
1 change: 0 additions & 1 deletion nix/checks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
inherit root;
fileset = fs.unions [
(root + /ftplugin)
(root + /spec)
(root + /lua)
(root + /.busted)
(fs.fileFilter (f: f.hasExt "rockspec") root)
Expand Down
10 changes: 0 additions & 10 deletions spec/dummy_spec.lua

This file was deleted.

95 changes: 0 additions & 95 deletions spec/iter_spec.lua

This file was deleted.

0 comments on commit 864a627

Please sign in to comment.