| 1 |
--[[ |
| 2 |
The MIT License |
| 3 |
|
| 4 |
Copyright (c) 2009-2012 [Norman Clarke](mailto:[email protected]) |
| 5 |
|
| 6 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 |
this software and associated documentation files (the "Software"), to deal in |
| 8 |
the Software without restriction, including without limitation the rights to |
| 9 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 11 |
so, subject to the following conditions: |
| 12 |
|
| 13 |
The above copyright notice and this permission notice shall be included in all |
| 14 |
copies or substantial portions of the Software. |
| 15 |
|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
SOFTWARE. |
| 23 |
]]-- |
| 24 |
|
| 25 |
--- Telescope is a test library for Lua that allows for flexible, declarative |
| 26 |
-- tests. The documentation produced here is intended largely for developers |
| 27 |
-- working on Telescope. For information on using Telescope, please visit the |
| 28 |
-- project homepage at: <a href="http://github.com/norman/telescope">http://github.com/norman/telescope#readme</a>. |
| 29 |
-- @release 0.6 |
| 30 |
-- @class module |
| 31 |
-- @module 'telescope' |
| 32 |
local _M = {} |
| 33 |
|
| 34 |
local compat_env = require 'compat_env' |
| 35 |
|
| 36 |
local getfenv = _G.getfenv or compat_env.getfenv |
| 37 |
local setfenv = _G.setfenv or compat_env.setfenv |
| 38 |
|
| 39 |
-- Lua 5.2+ compatibility: unpack was moved to table.unpack |
| 40 |
local unpack = _G.unpack or table.unpack |
| 41 |
|
| 42 |
|
| 43 |
local _VERSION = "0.6.0" |
| 44 |
|
| 45 |
--- The status codes that can be returned by an invoked test. These should not be overridden. |
| 46 |
-- @name status_codes |
| 47 |
-- @class table |
| 48 |
-- @field err - This is returned when an invoked test results in an error |
| 49 |
-- rather than a passed or failed assertion. |
| 50 |
-- @field fail - This is returned when an invoked test contains one or more failing assertions. |
| 51 |
-- @field pass - This is returned when all of a test's assertions pass. |
| 52 |
-- @field pending - This is returned when a test does not have a corresponding function. |
| 53 |
-- @field unassertive - This is returned when an invoked test does not produce |
| 54 |
-- errors, but does not contain any assertions. |
| 55 |
local status_codes = { |
| 56 |
err = 2, |
| 57 |
fail = 4, |
| 58 |
pass = 8, |
| 59 |
pending = 16, |
| 60 |
unassertive = 32 |
| 61 |
} |
| 62 |
|
| 63 |
--- Labels used to show the various <tt>status_codes</tt> as a single character. |
| 64 |
-- These can be overridden if you wish. |
| 65 |
-- @name status_labels |
| 66 |
-- @class table |
| 67 |
-- @see status_codes |
| 68 |
-- @field status_codes.err 'E' |
| 69 |
-- @field status_codes.fail 'F' |
| 70 |
-- @field status_codes.pass 'P' |
| 71 |
-- @field status_codes.pending '?' |
| 72 |
-- @field status_codes.unassertive 'U' |
| 73 |
|
| 74 |
local status_labels = { |
| 75 |
[status_codes.err] = 'E', |
| 76 |
[status_codes.fail] = 'F', |
| 77 |
[status_codes.pass] = 'P', |
| 78 |
[status_codes.pending] = '?', |
| 79 |
[status_codes.unassertive] = 'U' |
| 80 |
} |
| 81 |
|
| 82 |
--- The default names for context blocks. It defaults to "context", "spec" and |
| 83 |
-- "describe." |
| 84 |
-- @name context_aliases |
| 85 |
-- @class table |
| 86 |
local context_aliases = {"context", "describe", "spec"} |
| 87 |
--- The default names for test blocks. It defaults to "test," "it", "expect", |
| 88 |
-- "they" and "should." |
| 89 |
-- @name test_aliases |
| 90 |
-- @class table |
| 91 |
local test_aliases = {"test", "it", "expect", "should", "they"} |
| 92 |
|
| 93 |
--- The default names for "before" blocks. It defaults to "before" and "setup." |
| 94 |
-- The function in the before block will be run before each sibling test function |
| 95 |
-- or context. |
| 96 |
-- @name before_aliases |
| 97 |
-- @class table |
| 98 |
local before_aliases = {"before", "setup"} |
| 99 |
|
| 100 |
--- The default names for "after" blocks. It defaults to "after" and "teardown." |
| 101 |
-- The function in the after block will be run after each sibling test function |
| 102 |
-- or context. |
| 103 |
-- @name after_aliases |
| 104 |
-- @class table |
| 105 |
local after_aliases = {"after", "teardown"} |
| 106 |
|
| 107 |
-- Prefix to place before all assertion messages. Used by make_assertion(). |
| 108 |
local assertion_message_prefix = "Assert failed: expected " |
| 109 |
|
| 110 |
--- The default assertions. |
| 111 |
-- These are the assertions built into telescope. You can override them or |
| 112 |
-- create your own custom assertions using <tt>make_assertion</tt>. |
| 113 |
-- <ul> |
| 114 |
-- <tt><li>assert_blank(a)</tt> - true if a is nil, or the empty string</li> |
| 115 |
-- <tt><li>assert_empty(a)</tt> - true if a is an empty table</li> |
| 116 |
-- <tt><li>assert_equal(a, b)</tt> - true if a == b</li> |
| 117 |
-- <tt><li>assert_error(f)</tt> - true if function f produces an error</li> |
| 118 |
-- <tt><li>assert_false(a)</tt> - true if a is false</li> |
| 119 |
-- <tt><li>assert_greater_than(a, b)</tt> - true if a > b</li> |
| 120 |
-- <tt><li>assert_gte(a, b)</tt> - true if a >= b</li> |
| 121 |
-- <tt><li>assert_less_than(a, b)</tt> - true if a < b</li> |
| 122 |
-- <tt><li>assert_lte(a, b)</tt> - true if a <= b</li> |
| 123 |
-- <tt><li>assert_match(a, b)</tt> - true if b is a string that matches pattern a</li> |
| 124 |
-- <tt><li>assert_nil(a)</tt> - true if a is nil</li> |
| 125 |
-- <tt><li>assert_true(a)</tt> - true if a is true</li> |
| 126 |
-- <tt><li>assert_type(a, b)</tt> - true if a is of type b</li> |
| 127 |
-- <tt><li>assert_not_blank(a)</tt> - true if a is not nil and a is not the empty string</li> |
| 128 |
-- <tt><li>assert_not_empty(a)</tt> - true if a is a table, and a is not empty</li> |
| 129 |
-- <tt><li>assert_not_equal(a, b)</tt> - true if a ~= b</li> |
| 130 |
-- <tt><li>assert_not_error(f)</tt> - true if function f does not produce an error</li> |
| 131 |
-- <tt><li>assert_not_false(a)</tt> - true if a is not false</li> |
| 132 |
-- <tt><li>assert_not_greater_than(a, b)</tt> - true if not (a > b)</li> |
| 133 |
-- <tt><li>assert_not_gte(a, b)</tt> - true if not (a >= b)</li> |
| 134 |
-- <tt><li>assert_not_less_than(a, b)</tt> - true if not (a < b)</li> |
| 135 |
-- <tt><li>assert_not_lte(a, b)</tt> - true if not (a <= b)</li> |
| 136 |
-- <tt><li>assert_not_match(a, b)</tt> - true if the string b does not match the pattern a</li> |
| 137 |
-- <tt><li>assert_not_nil(a)</tt> - true if a is not nil</li> |
| 138 |
-- <tt><li>assert_not_true(a)</tt> - true if a is not true</li> |
| 139 |
-- <tt><li>assert_not_type(a, b)</tt> - true if a is not of type b</li> |
| 140 |
-- </ul> |
| 141 |
-- @see make_assertion |
| 142 |
-- @name assertions |
| 143 |
-- @class table |
| 144 |
local assertions = {} |
| 145 |
|
| 146 |
--- Create a custom assertion. |
| 147 |
-- This creates an assertion along with a corresponding negative assertion. It |
| 148 |
-- is used internally by telescope to create the default assertions. |
| 149 |
-- @param name The base name of the assertion. |
| 150 |
-- <p> |
| 151 |
-- The name will be used as the basis of the positive and negative assertions; |
| 152 |
-- i.e., the name <tt>equal</tt> would be used to create the assertions |
| 153 |
-- <tt>assert_equal</tt> and <tt>assert_not_equal</tt>. |
| 154 |
-- </p> |
| 155 |
-- @param message The base message that will be shown. |
| 156 |
-- <p> |
| 157 |
-- The assertion message is what is shown when the assertion fails. It will be |
| 158 |
-- prefixed with the string in <tt>telescope.assertion_message_prefix</tt>. |
| 159 |
-- The variables passed to <tt>telescope.make_assertion</tt> are interpolated |
| 160 |
-- in the message string using <tt>string.format</tt>. When creating the |
| 161 |
-- inverse assertion, the message is reused, with <tt>" to be "</tt> replaced |
| 162 |
-- by <tt>" not to be "</tt>. Hence a recommended format is something like: |
| 163 |
-- <tt>"%s to be similar to %s"</tt>. |
| 164 |
-- </p> |
| 165 |
-- @param func The assertion function itself. |
| 166 |
-- <p> |
| 167 |
-- The assertion function can have any number of arguments. |
| 168 |
-- </p> |
| 169 |
-- @usage <tt>make_assertion("equal", "%s to be equal to %s", function(a, b) |
| 170 |
-- return a == b end)</tt> |
| 171 |
-- @function make_assertion |
| 172 |
local function make_assertion(name, message, func) |
| 173 |
local num_vars = 0 |
| 174 |
-- if the last vararg ends up nil, we'll need to pad the table with nils so |
| 175 |
-- that string.format gets the number of args it expects |
| 176 |
local format_message |
| 177 |
if type(message) == "function" then |
| 178 |
format_message = message |
| 179 |
else |
| 180 |
for _, _ in message:gmatch("%%s") do num_vars = num_vars + 1 end |
| 181 |
format_message = function(message, ...) |
| 182 |
local a = {} |
| 183 |
local args = {...} |
| 184 |
local nargs = select('#', ...) |
| 185 |
if nargs > num_vars then |
| 186 |
local userErrorMessage = args[num_vars+1] |
| 187 |
if type(userErrorMessage) == "string" then |
| 188 |
return(assertion_message_prefix .. userErrorMessage) |
| 189 |
else |
| 190 |
error(string.format('assert_%s expected %d arguments but got %d', name, num_vars, #args)) |
| 191 |
end |
| 192 |
end |
| 193 |
for i = 1, nargs do a[i] = tostring(args[i]) end |
| 194 |
for i = nargs+1, num_vars do a[i] = 'nil' end |
| 195 |
return (assertion_message_prefix .. message):format(unpack(a)) |
| 196 |
end |
| 197 |
end |
| 198 |
|
| 199 |
assertions["assert_" .. name] = function(...) |
| 200 |
if assertion_callback then assertion_callback(...) end |
| 201 |
if not func(...) then |
| 202 |
error({format_message(message, ...), debug.traceback()}) |
| 203 |
end |
| 204 |
end |
| 205 |
end |
| 206 |
|
| 207 |
--- (local) Return a table with table t's values as keys and keys as values. |
| 208 |
-- @param t The table. |
| 209 |
local function invert_table(t) |
| 210 |
local t2 = {} |
| 211 |
for k, v in pairs(t) do t2[v] = k end |
| 212 |
return t2 |
| 213 |
end |
| 214 |
|
| 215 |
-- (local) Truncate a string "s" to length "len", optionally followed by the |
| 216 |
-- string given in "after" if truncated; for example, truncate_string("hello |
| 217 |
-- world", 3, "...") |
| 218 |
-- @param s The string to truncate. |
| 219 |
-- @param len The desired length. |
| 220 |
-- @param after A string to append to s, if it is truncated. |
| 221 |
local function truncate_string(s, len, after) |
| 222 |
if #s <= len then |
| 223 |
return s |
| 224 |
else |
| 225 |
local s = s:sub(1, len):gsub("%s*$", '') |
| 226 |
if after then return s .. after else return s end |
| 227 |
end |
| 228 |
end |
| 229 |
|
| 230 |
--- (local) Filter a table's values by function. This function iterates over a |
| 231 |
-- table , returning only the table entries that, when passed into function f, |
| 232 |
-- yield a truthy value. |
| 233 |
-- @param t The table over which to iterate. |
| 234 |
-- @param f The filter function. |
| 235 |
local function filter(t, f) |
| 236 |
local a, b |
| 237 |
return function() |
| 238 |
repeat a, b = next(t, a) |
| 239 |
if not b then return end |
| 240 |
if f(a, b) then return a, b end |
| 241 |
until not b |
| 242 |
end |
| 243 |
end |
| 244 |
|
| 245 |
--- (local) Finds the value in the contexts table indexed with i, and returns a table |
| 246 |
-- of i's ancestor contexts. |
| 247 |
-- @param i The index in the <tt>contexts</tt> table to get ancestors for. |
| 248 |
-- @param contexts The table in which to find the ancestors. |
| 249 |
local function ancestors(i, contexts) |
| 250 |
if i == 0 then return end |
| 251 |
local a = {} |
| 252 |
local function func(j) |
| 253 |
if contexts[j].parent == 0 then return nil end |
| 254 |
table.insert(a, contexts[j].parent) |
| 255 |
func(contexts[j].parent) |
| 256 |
end |
| 257 |
func(i) |
| 258 |
return a |
| 259 |
end |
| 260 |
|
| 261 |
make_assertion("blank", "'%s' to be blank", function(a) return a == '' or a == nil end) |
| 262 |
make_assertion("empty", "'%s' to be an empty table", function(a) return not next(a) end) |
| 263 |
make_assertion("equal", "'%s' to be equal to '%s'", function(a, b) return a == b end) |
| 264 |
make_assertion("error", "result to be an error", function(f) return not pcall(f) end) |
| 265 |
make_assertion("false", "'%s' to be false", function(a) return a == false end) |
| 266 |
make_assertion("greater_than", "'%s' to be greater than '%s'", function(a, b) return a > b end) |
| 267 |
make_assertion("gte", "'%s' to be greater than or equal to '%s'", function(a, b) return a >= b end) |
| 268 |
make_assertion("less_than", "'%s' to be less than '%s'", function(a, b) return a < b end) |
| 269 |
make_assertion("lte", "'%s' to be less than or equal to '%s'", function(a, b) return a <= b end) |
| 270 |
make_assertion("match", "'%s' to be a match for %s", function(a, b) return (tostring(b)):match(a) end) |
| 271 |
make_assertion("nil", "'%s' to be nil", function(a) return a == nil end) |
| 272 |
make_assertion("true", "'%s' to be true", function(a) return a == true end) |
| 273 |
make_assertion("type", "'%s' to be a %s", function(a, b) return type(a) == b end) |
| 274 |
|
| 275 |
make_assertion("not_blank", "'%s' not to be blank", function(a) return a ~= '' and a ~= nil end) |
| 276 |
make_assertion("not_empty", "'%s' not to be an empty table", function(a) return not not next(a) end) |
| 277 |
make_assertion("not_equal", "'%s' not to be equal to '%s'", function(a, b) return a ~= b end) |
| 278 |
make_assertion("not_error", "result not to be an error", function(f) return not not pcall(f) end) |
| 279 |
make_assertion("not_match", "'%s' not to be a match for %s", function(a, b) return not (tostring(b)):match(a) end) |
| 280 |
make_assertion("not_nil", "'%s' not to be nil", function(a) return a ~= nil end) |
| 281 |
make_assertion("not_type", "'%s' not to be a %s", function(a, b) return type(a) ~= b end) |
| 282 |
|
| 283 |
--- Build a contexts table from the test file or function given in <tt>target</tt>. |
| 284 |
-- If the optional <tt>contexts</tt> table argument is provided, then the |
| 285 |
-- resulting contexts will be added to it. |
| 286 |
-- <p> |
| 287 |
-- The resulting contexts table's structure is as follows: |
| 288 |
-- </p> |
| 289 |
-- <code> |
| 290 |
-- { |
| 291 |
-- {parent = 0, name = "this is a context", context = true}, |
| 292 |
-- {parent = 1, name = "this is a nested context", context = true}, |
| 293 |
-- {parent = 2, name = "this is a test", test = function}, |
| 294 |
-- {parent = 2, name = "this is another test", test = function}, |
| 295 |
-- {parent = 0, name = "this is test outside any context", test = function}, |
| 296 |
-- } |
| 297 |
-- </code> |
| 298 |
-- @param contexts A optional table in which to collect the resulting contexts |
| 299 |
-- and function. |
| 300 |
-- @function load_contexts |
| 301 |
local function load_contexts(target, contexts) |
| 302 |
local env = {} |
| 303 |
local current_index = 0 |
| 304 |
local context_table = contexts or {} |
| 305 |
|
| 306 |
local function context_block(name, func) |
| 307 |
table.insert(context_table, {parent = current_index, name = name, context = true}) |
| 308 |
local previous_index = current_index |
| 309 |
current_index = #context_table |
| 310 |
func() |
| 311 |
current_index = previous_index |
| 312 |
end |
| 313 |
|
| 314 |
local function test_block(name, func) |
| 315 |
local test_table = {name = name, parent = current_index, test = func or true} |
| 316 |
if current_index ~= 0 then |
| 317 |
test_table.context_name = context_table[current_index].name |
| 318 |
else |
| 319 |
test_table.context_name = 'top level' |
| 320 |
end |
| 321 |
table.insert(context_table, test_table) |
| 322 |
end |
| 323 |
|
| 324 |
local function before_block(func) |
| 325 |
context_table[current_index].before = func |
| 326 |
end |
| 327 |
|
| 328 |
local function after_block(func) |
| 329 |
context_table[current_index].after = func |
| 330 |
end |
| 331 |
|
| 332 |
for _, v in ipairs(after_aliases) do env[v] = after_block end |
| 333 |
for _, v in ipairs(before_aliases) do env[v] = before_block end |
| 334 |
for _, v in ipairs(context_aliases) do env[v] = context_block end |
| 335 |
for _, v in ipairs(test_aliases) do env[v] = test_block end |
| 336 |
|
| 337 |
-- Set these functions in the module's meta table to allow accessing |
| 338 |
-- telescope's test and context functions without env tricks. This will |
| 339 |
-- however add tests to a context table used inside the module, so multiple |
| 340 |
-- test files will add tests to the same top-level context, which may or may |
| 341 |
-- not be desired. |
| 342 |
setmetatable(_M, {__index = env}) |
| 343 |
|
| 344 |
setmetatable(env, {__index = _G}) |
| 345 |
|
| 346 |
local func, err = type(target) == 'string' and assert(loadfile(target)) or target |
| 347 |
if err then error(err) end |
| 348 |
setfenv(func, env)() |
| 349 |
return context_table |
| 350 |
end |
| 351 |
|
| 352 |
-- in-place table reverse. |
| 353 |
function table.reverse(t) |
| 354 |
local len = #t+1 |
| 355 |
for i=1, (len-1)/2 do |
| 356 |
t[i], t[len-i] = t[len-i], t[i] |
| 357 |
end |
| 358 |
end |
| 359 |
|
| 360 |
--- Run all tests. |
| 361 |
-- This function will exectute each function in the contexts table. |
| 362 |
-- @param contexts The contexts created by <tt>load_contexts</tt>. |
| 363 |
-- @param callbacks A table of callback functions to be invoked before or after |
| 364 |
-- various test states. |
| 365 |
-- <p> |
| 366 |
-- There is a callback for each test <tt>status_code</tt>, and callbacks to run |
| 367 |
-- before or after each test invocation regardless of outcome. |
| 368 |
-- </p> |
| 369 |
-- <ul> |
| 370 |
-- <li>after - will be invoked after each test</li> |
| 371 |
-- <li>before - will be invoked before each test</li> |
| 372 |
-- <li>err - will be invoked after each test which results in an error</li> |
| 373 |
-- <li>fail - will be invoked after each failing test</li> |
| 374 |
-- <li>pass - will be invoked after each passing test</li> |
| 375 |
-- <li>pending - will be invoked after each pending test</li> |
| 376 |
-- <li>unassertive - will be invoked after each test which doesn't assert |
| 377 |
-- anything</li> |
| 378 |
-- </ul> |
| 379 |
-- <p> |
| 380 |
-- Callbacks can be used, for example, to drop into a debugger upon a failed |
| 381 |
-- assertion or error, for profiling, or updating a GUI progress meter. |
| 382 |
-- </p> |
| 383 |
-- @param test_filter A function to filter tests that match only conditions that you specify. |
| 384 |
-- <p> |
| 385 |
-- For example, the folling would allow you to run only tests whose name matches a pattern: |
| 386 |
-- </p> |
| 387 |
-- <p> |
| 388 |
-- <code> |
| 389 |
-- function(t) return t.name:match("%s* lexer") end |
| 390 |
-- </code> |
| 391 |
-- </p> |
| 392 |
-- @return A table of result tables. Each result table has the following |
| 393 |
-- fields: |
| 394 |
-- <ul> |
| 395 |
-- <li>assertions_invoked - the number of assertions the test invoked</li> |
| 396 |
-- <li>context - the name of the context</li> |
| 397 |
-- <li>message - a table with an error message and stack trace</li> |
| 398 |
-- <li>name - the name of the test</li> |
| 399 |
-- <li>status_code - the resulting status code</li> |
| 400 |
-- <li>status_label - the label for the status_code</li> |
| 401 |
-- </ul> |
| 402 |
-- @see load_contexts |
| 403 |
-- @see status_codes |
| 404 |
-- @function run |
| 405 |
local function run(contexts, callbacks, test_filter) |
| 406 |
|
| 407 |
local results = {} |
| 408 |
local status_names = invert_table(status_codes) |
| 409 |
local test_filter = test_filter or function(a) return a end |
| 410 |
|
| 411 |
-- Setup a new environment suitable for running a new test |
| 412 |
local function newEnv() |
| 413 |
local env = {} |
| 414 |
|
| 415 |
-- Make sure globals are accessible in the new environment |
| 416 |
setmetatable(env, {__index = _G}) |
| 417 |
|
| 418 |
-- Setup all the assert functions in the new environment |
| 419 |
for k, v in pairs(assertions) do |
| 420 |
setfenv(v, env) |
| 421 |
env[k] = v |
| 422 |
end |
| 423 |
|
| 424 |
return env |
| 425 |
end |
| 426 |
|
| 427 |
local env = newEnv() |
| 428 |
|
| 429 |
local function invoke_callback(name, test) |
| 430 |
if not callbacks then return end |
| 431 |
if type(callbacks[name]) == "table" then |
| 432 |
for _, c in ipairs(callbacks[name]) do c(test) end |
| 433 |
elseif callbacks[name] then |
| 434 |
callbacks[name](test) |
| 435 |
end |
| 436 |
end |
| 437 |
|
| 438 |
local function invoke_test(func) |
| 439 |
local assertions_invoked = 0 |
| 440 |
env.assertion_callback = function() |
| 441 |
assertions_invoked = assertions_invoked + 1 |
| 442 |
end |
| 443 |
setfenv(func, env) |
| 444 |
local result, message = xpcall(func, debug.traceback) |
| 445 |
if result and assertions_invoked > 0 then |
| 446 |
return status_codes.pass, assertions_invoked, nil |
| 447 |
elseif result then |
| 448 |
return status_codes.unassertive, 0, nil |
| 449 |
elseif type(message) == "table" then |
| 450 |
return status_codes.fail, assertions_invoked, message |
| 451 |
else |
| 452 |
return status_codes.err, assertions_invoked, {message, debug.traceback()} |
| 453 |
end |
| 454 |
end |
| 455 |
|
| 456 |
for i, v in filter(contexts, function(i, v) return v.test and test_filter(v) end) do |
| 457 |
env = newEnv() -- Setup a new environment for this test |
| 458 |
|
| 459 |
local ancestors = ancestors(i, contexts) |
| 460 |
local context_name = 'Top level' |
| 461 |
if contexts[i].parent ~= 0 then |
| 462 |
context_name = contexts[contexts[i].parent].name |
| 463 |
end |
| 464 |
local result = { |
| 465 |
assertions_invoked = 0, |
| 466 |
name = contexts[i].name, |
| 467 |
context = context_name, |
| 468 |
test = i |
| 469 |
} |
| 470 |
table.sort(ancestors) |
| 471 |
-- this "before" is the test callback passed into the runner |
| 472 |
invoke_callback("before", result) |
| 473 |
|
| 474 |
-- run all the "before" blocks/functions |
| 475 |
for _, a in ipairs(ancestors) do |
| 476 |
if contexts[a].before then |
| 477 |
setfenv(contexts[a].before, env) |
| 478 |
contexts[a].before() |
| 479 |
end |
| 480 |
end |
| 481 |
|
| 482 |
-- check if it's a function because pending tests will just have "true" |
| 483 |
if type(v.test) == "function" then |
| 484 |
result.status_code, result.assertions_invoked, result.message = invoke_test(v.test) |
| 485 |
invoke_callback(status_names[result.status_code], result) |
| 486 |
else |
| 487 |
result.status_code = status_codes.pending |
| 488 |
invoke_callback("pending", result) |
| 489 |
end |
| 490 |
result.status_label = status_labels[result.status_code] |
| 491 |
|
| 492 |
-- Run all the "after" blocks/functions |
| 493 |
table.reverse(ancestors) |
| 494 |
for _, a in ipairs(ancestors) do |
| 495 |
if contexts[a].after then |
| 496 |
setfenv(contexts[a].after, env) |
| 497 |
contexts[a].after() |
| 498 |
end |
| 499 |
end |
| 500 |
|
| 501 |
invoke_callback("after", result) |
| 502 |
results[i] = result |
| 503 |
end |
| 504 |
|
| 505 |
return results |
| 506 |
|
| 507 |
end |
| 508 |
|
| 509 |
--- Return a detailed report for each context, with the status of each test. |
| 510 |
-- @param contexts The contexts returned by <tt>load_contexts</tt>. |
| 511 |
-- @param results The results returned by <tt>run</tt>. |
| 512 |
-- @function test_report |
| 513 |
local function test_report(contexts, results) |
| 514 |
|
| 515 |
local buffer = {} |
| 516 |
local leading_space = " " |
| 517 |
local level = 0 |
| 518 |
local line_char = "-" |
| 519 |
local previous_level = 0 |
| 520 |
local status_format_len = 3 |
| 521 |
local status_format = "[%s]" |
| 522 |
local width = 72 |
| 523 |
local context_name_format = "%-" .. width - status_format_len .. "s" |
| 524 |
local function_name_format = "%-" .. width - status_format_len .. "s" |
| 525 |
|
| 526 |
local function space() |
| 527 |
return leading_space:rep(level - 1) |
| 528 |
end |
| 529 |
|
| 530 |
local function add_divider() |
| 531 |
table.insert(buffer, line_char:rep(width)) |
| 532 |
end |
| 533 |
add_divider() |
| 534 |
for i, item in ipairs(contexts) do |
| 535 |
local ancestors = ancestors(i, contexts) |
| 536 |
previous_level = level or 0 |
| 537 |
level = #ancestors |
| 538 |
-- the 4 here is the length of "..." plus one space of padding |
| 539 |
local name = truncate_string(item.name, width - status_format_len - 4 - #ancestors, '...') |
| 540 |
if previous_level ~= level and level == 0 then add_divider() end |
| 541 |
if item.context then |
| 542 |
table.insert(buffer, context_name_format:format(space() .. name .. ':')) |
| 543 |
elseif results[i] then |
| 544 |
table.insert(buffer, function_name_format:format(space() .. name) .. |
| 545 |
status_format:format(results[i].status_label)) |
| 546 |
end |
| 547 |
end |
| 548 |
add_divider() |
| 549 |
return table.concat(buffer, "\n") |
| 550 |
|
| 551 |
end |
| 552 |
|
| 553 |
--- Return a table of stack traces for tests which produced a failure or an error. |
| 554 |
-- @param contexts The contexts returned by <tt>load_contexts</tt>. |
| 555 |
-- @param results The results returned by <tt>run</tt>. |
| 556 |
-- @function error_report |
| 557 |
local function error_report(contexts, results) |
| 558 |
local buffer = {} |
| 559 |
for _, r in filter(results, function(i, r) return r.message end) do |
| 560 |
local name = contexts[r.test].name |
| 561 |
table.insert(buffer, name .. ":\n" .. r.message[1] .. "\n" .. r.message[2]) |
| 562 |
end |
| 563 |
if #buffer > 0 then return table.concat(buffer, "\n") end |
| 564 |
end |
| 565 |
|
| 566 |
--- Get a one-line report and a summary table with the status counts. The |
| 567 |
-- counts given are: total tests, assertions, passed tests, failed tests, |
| 568 |
-- pending tests, and tests which didn't assert anything. |
| 569 |
-- @return A report that can be printed |
| 570 |
-- @return A table with the various counts. Its fields are: |
| 571 |
-- <tt>assertions</tt>, <tt>errors</tt>, <tt>failed</tt>, <tt>passed</tt>, |
| 572 |
-- <tt>pending</tt>, <tt>tests</tt>, <tt>unassertive</tt>. |
| 573 |
-- @param contexts The contexts returned by <tt>load_contexts</tt>. |
| 574 |
-- @param results The results returned by <tt>run</tt>. |
| 575 |
-- @function summary_report |
| 576 |
local function summary_report(contexts, results) |
| 577 |
local r = { |
| 578 |
assertions = 0, |
| 579 |
errors = 0, |
| 580 |
failed = 0, |
| 581 |
passed = 0, |
| 582 |
pending = 0, |
| 583 |
tests = 0, |
| 584 |
unassertive = 0 |
| 585 |
} |
| 586 |
for _, v in pairs(results) do |
| 587 |
r.tests = r.tests + 1 |
| 588 |
r.assertions = r.assertions + v.assertions_invoked |
| 589 |
if v.status_code == status_codes.err then r.errors = r.errors + 1 |
| 590 |
elseif v.status_code == status_codes.fail then r.failed = r.failed + 1 |
| 591 |
elseif v.status_code == status_codes.pass then r.passed = r.passed + 1 |
| 592 |
elseif v.status_code == status_codes.pending then r.pending = r.pending + 1 |
| 593 |
elseif v.status_code == status_codes.unassertive then r.unassertive = r.unassertive + 1 |
| 594 |
end |
| 595 |
end |
| 596 |
local buffer = {} |
| 597 |
for _, k in ipairs({"tests", "passed", "assertions", "failed", "errors", "unassertive", "pending"}) do |
| 598 |
local number = r[k] |
| 599 |
local label = k |
| 600 |
if number == 1 then |
| 601 |
label = label:gsub("s$", "") |
| 602 |
end |
| 603 |
table.insert(buffer, ("%d %s"):format(number, label)) |
| 604 |
end |
| 605 |
return table.concat(buffer, " "), r |
| 606 |
end |
| 607 |
|
| 608 |
_M.after_aliases = after_aliases |
| 609 |
_M.make_assertion = make_assertion |
| 610 |
_M.assertion_message_prefix = assertion_message_prefix |
| 611 |
_M.before_aliases = before_aliases |
| 612 |
_M.context_aliases = context_aliases |
| 613 |
_M.error_report = error_report |
| 614 |
_M.load_contexts = load_contexts |
| 615 |
_M.run = run |
| 616 |
_M.test_report = test_report |
| 617 |
_M.status_codes = status_codes |
| 618 |
_M.status_labels = status_labels |
| 619 |
_M.summary_report = summary_report |
| 620 |
_M.test_aliases = test_aliases |
| 621 |
_M.version = _VERSION |
| 622 |
_M._VERSION = _VERSION |
| 623 |
|
| 624 |
return _M |