-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Dich0tomy/ref/test-system
test: add an automatic test runner
- Loading branch information
Showing
10 changed files
with
200 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,5 @@ | |
!*.rockspec | ||
|
||
# Plugin test & lint | ||
!spec/ | ||
!.busted | ||
!.luacheckrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.