Common library code for other vc*.nvim projects.

Add test utils.

+79
+79
lua/vclib/testing.lua
··· 1 + local M = {} 2 + 3 + local function _run_test_suite(suite_name, test_suite) 4 + local suite_failed = 0 5 + local suite_total = 0 6 + local test_cases = test_suite.test_cases 7 + local test_function = test_suite.test 8 + for case_name, case in pairs(test_cases) do 9 + local full_test_name = string.format("%s__%s", suite_name, case_name) 10 + 11 + local status, err = pcall(function() 12 + test_function(case) 13 + end) 14 + suite_total = suite_total + 1 15 + if not status then 16 + suite_failed = suite_failed + 1 17 + print(string.format("Test %s failed: %s", full_test_name, err)) 18 + end 19 + end 20 + 21 + if suite_failed == 0 then 22 + print( 23 + string.format( 24 + "All tests in %s passed (%d tests)", 25 + suite_name, 26 + suite_total 27 + ) 28 + ) 29 + else 30 + print( 31 + string.format( 32 + "%d/%d tests failed in %s", 33 + suite_failed, 34 + suite_total, 35 + suite_name 36 + ) 37 + ) 38 + end 39 + return suite_failed, suite_total 40 + end 41 + 42 + function M.run_tests(test_modules) 43 + local failed = 0 44 + local total = 0 45 + for _, test_module_name in ipairs(test_modules) do 46 + local test_module = require(test_module_name) 47 + print(string.format("=== Running tests in %s ===", test_module_name)) 48 + for suite_name, test_suite in pairs(test_module) do 49 + local suite_failed, suite_total = _run_test_suite(suite_name, test_suite) 50 + failed = failed + suite_failed 51 + total = total + suite_total 52 + end 53 + end 54 + print "--------------------------------" 55 + if failed == 0 then 56 + print(string.format("All tests passed (%d tests)", total)) 57 + else 58 + print(string.format("%d/%d tests failed", failed, total)) 59 + end 60 + end 61 + 62 + function M.assert_list_eq(actual, expected) 63 + assert( 64 + #actual == #expected, 65 + string.format("Lists have different lengths: %d vs %d", #actual, #expected) 66 + ) 67 + local diff = "" 68 + for i = 1, #expected do 69 + if actual[i] ~= expected[i] then 70 + diff = diff 71 + .. string.format("\n[%d]: %s ~= %s", i, actual[i], expected[i]) 72 + end 73 + end 74 + if diff ~= "" then 75 + error("Lists differ:" .. diff) 76 + end 77 + end 78 + 79 + return M