spec/run.lua (2176 bytes)
1 -- Fallback test runner for a plain Lua interpreter. 2 -- Run it from the plugin folder: lua spec/run.lua 3 -- It gives the small part of the busted API that the spec files use, so the 4 -- same files also run under busted. 5 6 local here = (arg and arg[0] or "spec/run.lua"):gsub("[^/\\]*$", "") 7 package.path = table.concat({ here .. "../?.lua", "./?.lua", package.path }, ";") 8 9 local failed, passed, path = 0, 0, {} 10 11 local function deepEqual(a, b) 12 if a == b then return true end 13 if type(a) ~= "table" or type(b) ~= "table" then return false end 14 for k, v in pairs(a) do if not deepEqual(v, b[k]) then return false end end 15 for k in pairs(b) do if a[k] == nil then return false end end 16 return true 17 end 18 19 local function check(ok, message) 20 if not ok then error(message or "assertion failed", 3) end 21 end 22 23 local A = { equals = function(a, b) check(a == b, tostring(a) .. " ~= " .. tostring(b)) end, 24 same = function(a, b) check(deepEqual(a, b), "tables differ") end, 25 is_nil = function(a) check(a == nil, "expected nil, got " .. tostring(a)) end, 26 is_not_nil = function(a) check(a ~= nil, "expected a value, got nil") end, 27 is_true = function(a) check(a == true, "expected true, got " .. tostring(a)) end, 28 is_false = function(a) check(a == false, "expected false, got " .. tostring(a)) end, 29 near = function(a, b, tol) check(math.abs(a - b) <= tol, tostring(a) .. " not near " .. tostring(b)) end } 30 A.are_equal = A.equals 31 _G.assert = setmetatable(A, { __call = function(_, ok, message) check(ok, message) return ok end }) 32 33 function _G.describe(name, body) path[#path + 1] = name; body(); path[#path] = nil end 34 function _G.it(name, body) 35 local title = table.concat(path, " / ") .. " / " .. name 36 local ok, err = pcall(body) 37 if ok then passed = passed + 1 else failed = failed + 1; print("FAIL " .. title .. ": " .. tostring(err)) end 38 end 39 _G.setup, _G.teardown, _G.before_each, _G.after_each = function(f) f() end, function() end, nil, nil 40 41 for _, name in ipairs({ "text", "srt", "cues", "clock" }) do 42 dofile(here .. name .. "_spec.lua") 43 end 44 print(string.format("%d passed, %d failed", passed, failed)) 45 os.exit(failed == 0 and 0 or 1)