Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional console colors to report #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 45 additions & 27 deletions telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ local compat_env = require 'telescope.compat_env'
local getfenv = _G.getfenv or compat_env.getfenv
local setfenv = _G.setfenv or compat_env.setfenv

local red = string.char(27) .. '[31m'
local blue = string.char(27) .. '[34m'
local green = string.char(27) .. '[32m'
local yellow = string.char(27) .. '[33m'
local grey = string.char(27) .. '[37m'
local normal = string.char(27) .. '[0m'

local _VERSION = "0.6.0"

Expand Down Expand Up @@ -52,6 +58,14 @@ local status_labels = {
[status_codes.unassertive] = 'U'
}

local status_colors = {
[status_codes.err] = red,
[status_codes.fail] = red,
[status_codes.pass] = green,
[status_codes.pending] = blue,
[status_codes.unassertive] = yellow
}

--- The default names for context blocks. It defaults to "context", "spec" and
-- "describe."
-- @name context_aliases
Expand Down Expand Up @@ -155,7 +169,7 @@ local function make_assertion(name, message, func)
local a = {}
local args = {...}
local nargs = select('#', ...)
if nargs > num_vars then
if nargs > num_vars then
local userErrorMessage = args[num_vars+1]
if type(userErrorMessage) == "string" then
return(assertion_message_prefix .. userErrorMessage)
Expand Down Expand Up @@ -443,12 +457,12 @@ local function run(contexts, callbacks, test_filter)
table.sort(ancestors)
-- this "before" is the test callback passed into the runner
invoke_callback("before", result)

-- run all the "before" blocks/functions
for _, a in ipairs(ancestors) do
if contexts[a].before then
if contexts[a].before then
setfenv(contexts[a].before, env)
contexts[a].before()
contexts[a].before()
end
end

Expand All @@ -465,9 +479,9 @@ local function run(contexts, callbacks, test_filter)
-- Run all the "after" blocks/functions
table.reverse(ancestors)
for _, a in ipairs(ancestors) do
if contexts[a].after then
if contexts[a].after then
setfenv(contexts[a].after, env)
contexts[a].after()
contexts[a].after()
end
end

Expand All @@ -482,8 +496,9 @@ end
--- Return a detailed report for each context, with the status of each test.
-- @param contexts The contexts returned by <tt>load_contexts</tt>.
-- @param results The results returned by <tt>run</tt>.
-- @param use_color Add console colors to report
-- @function test_report
local function test_report(contexts, results)
local function test_report(contexts, results, use_color)

local buffer = {}
local leading_space = " "
Expand Down Expand Up @@ -514,8 +529,9 @@ local function test_report(contexts, results)
if item.context then
table.insert(buffer, context_name_format:format(space() .. name .. ':'))
elseif results[i] then
table.insert(buffer, function_name_format:format(space() .. name) ..
status_format:format(results[i].status_label))
local color = use_color and status_colors[results[i].status_code] or ""
table.insert(buffer, color .. function_name_format:format(space() .. name) ..
status_format:format(results[i].status_label) .. (use_color and normal or ""))
end
end
add_divider()
Expand Down Expand Up @@ -545,35 +561,37 @@ end
-- <tt>pending</tt>, <tt>tests</tt>, <tt>unassertive</tt>.
-- @param contexts The contexts returned by <tt>load_contexts</tt>.
-- @param results The results returned by <tt>run</tt>.
-- @param use_color Add console colors to report
-- @function summary_report
local function summary_report(contexts, results)
local function summary_report(contexts, results, use_color)
local r = {
assertions = 0,
errors = 0,
failed = 0,
passed = 0,
pending = 0,
tests = 0,
unassertive = 0
assertions = { count = 0, color = normal },
errors = { count = 0, color = status_colors[status_codes.err] },
failed = { count = 0, color = status_colors[status_codes.fail] },
passed = { count = 0, color = status_colors[status_codes.pass] },
pending = { count = 0, color = status_colors[status_codes.pending] },
tests = { count = 0, color = normal },
unassertive = { count = 0, color = status_colors[status_codes.unassertive] },
}
for _, v in pairs(results) do
r.tests = r.tests + 1
r.assertions = r.assertions + v.assertions_invoked
if v.status_code == status_codes.err then r.errors = r.errors + 1
elseif v.status_code == status_codes.fail then r.failed = r.failed + 1
elseif v.status_code == status_codes.pass then r.passed = r.passed + 1
elseif v.status_code == status_codes.pending then r.pending = r.pending + 1
elseif v.status_code == status_codes.unassertive then r.unassertive = r.unassertive + 1
r.tests.count = r.tests.count + 1
r.assertions.count = r.assertions.count + v.assertions_invoked
if v.status_code == status_codes.err then r.errors.count = r.errors.count + 1
elseif v.status_code == status_codes.fail then r.failed.count = r.failed.count + 1
elseif v.status_code == status_codes.pass then r.passed.count = r.passed.count + 1
elseif v.status_code == status_codes.pending then r.pending.count = r.pending.count + 1
elseif v.status_code == status_codes.unassertive then r.unassertive.count = r.unassertive.count + 1
end
end
local buffer = {}
for _, k in ipairs({"tests", "passed", "assertions", "failed", "errors", "unassertive", "pending"}) do
local number = r[k]
for _, k in ipairs({ "tests", "passed", "failed", "errors", "assertions", "unassertive", "pending" }) do
local number = r[k].count
local label = k
local color = use_color and r[k].color or ""
if number == 1 then
label = label:gsub("s$", "")
end
table.insert(buffer, ("%d %s"):format(number, label))
table.insert(buffer, ("%s%d %s%s"):format(color, number, label, use_color and normal or ""))
end
return table.concat(buffer, " "), r
end
Expand Down
5 changes: 3 additions & 2 deletions tsc
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Options:
-s --silent Don't show any output
-h,-? --help Show this text
-v --version Show version
-C --color Use console colors in report
-c --luacov Output a coverage file using Luacov (http://luacov.luaforge.net/)
--load=<file> Load a Lua file before executing command
--name=<pattern> Only run tests whose name matches a Lua string pattern
Expand Down Expand Up @@ -272,10 +273,10 @@ end

local buffer = {}
local results = telescope.run(contexts, callbacks, test_pattern)
local summary, data = telescope.summary_report(contexts, results)
local summary, data = telescope.summary_report(contexts, results, opts.C or opts.color)

if opts.f or opts.full then
table.insert(buffer, telescope.test_report(contexts, results))
table.insert(buffer, telescope.test_report(contexts, results, opts.C or opts.color))
end

if not opts.s and not opts.silent then
Expand Down