From cf7ed5b289e428f1e5e9f451ff1e4f1b47622707 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Thu, 29 Dec 2016 01:06:40 +0100 Subject: [PATCH] That sure is a nice status bar. --- test/TestSuite.lua | 80 +++++++++++--------------------------------- test/test_pretty.lua | 6 ---- 2 files changed, 19 insertions(+), 67 deletions(-) diff --git a/test/TestSuite.lua b/test/TestSuite.lua index 9ca7c8d..3cec5a9 100644 --- a/test/TestSuite.lua +++ b/test/TestSuite.lua @@ -198,76 +198,34 @@ function TestSuite:runTests (parent_prefix, parent_indent) end if not parent_prefix then - io.write(TERM_COLOR_CODE_WHITE) - io.write('\n## All tests run! ##\n\n') - - local width_of_bar = 70 - local size_of_red_bar = math.ceil( width_of_bar * nr_errors/nr_tests) - local size_of_green_bar = width_of_bar-size_of_red_bar - - io.write('Status: ') - io.write(nr_errors == 0 and TERM_COLOR_CODE_GREEN or TERM_COLOR_CODE_RED) - io.write(string.rep('#', width_of_bar)) - io.write(TERM_COLOR_CODE_WHITE) - - -- Get - io.write('\n\nSummary: '..TERM_COLOR_CODE_GREEN..string.rep('#', size_of_green_bar)..TERM_COLOR_CODE_RED..string.rep('#', size_of_red_bar)..'\n\n') - io.write(TERM_COLOR_CODE_WHITE) + self:writeStatus(nr_errors, nr_tests) end return nr_errors, nr_tests end --------------------------------------------------------------------------------- +function TestSuite:writeStatus ( nr_errors, nr_tests ) + io.write(TERM_COLOR_CODE_WHITE) + io.write('\n## All tests run! ##\n\n') -local function get_end_of_function (text, start_i) - local indent, i, end_i = 0, start_i, text:len() - while i <= end_i do - if text:sub(i,i) == '"' then -- "style" strings - i = text:find('"', i + 1) + 1 - elseif text:sub(i,i) == "'" then -- 'style' strings - i = text:find("'", i + 1) + 1 - elseif text:sub(i,i+1) == '[[' then -- [[style]] strings - i = text:find(']]', i) + 2 - elseif text:sub(i,i+3) == '--[[' then -- multi line comments - i = text:find(']]', i) + 2 - elseif text:sub(i,i+1) == '--' then -- single line comments - i = text:find('\n', i) + 1 - elseif text:sub(i-1, i+8):find('%sfunction%s') or i == start_i and text:sub(i, i+7) == 'function' then - indent, i = indent + 1, i + 8 - elseif text:sub(i-1, i+5):find('%swhile%s') or i == start_i and text:sub(i, i+4) == 'while' then - indent, i = indent + 1, i + 4 - elseif text:sub(i-1, i+3):find('%sfor%s') or i == start_i and text:sub(i, i+2) == 'for' then - indent, i = indent + 1, i + 2 - elseif text:sub(i-1, i+2):find('%sif%s') or i == start_i and text:sub(i, i+1) == 'if' then - indent, i = indent + 1, i + 2 - elseif text:sub(i-1, i+3):find('%send%s') or (i == end_i - 2) and text:sub(i, i+2) == 'end' then - indent, i = indent - 1, i + 2 - if indent == 0 then - return i - end - else - i = i + 1 - end - end -end + local WIDTH_OR_BAR = 70 + local BAR_CHAR = '#' + local nr_successes = nr_tests - nr_errors + local size_of_green_bar = math.floor( WIDTH_OR_BAR * nr_successes/nr_tests) -function TestSuite.getLocalFunction (module_name, func_name) - local file = io.open(module_name .. '.lua') - local file_text = file:read('*a') - file:close() + io.write('Status: ') + io.write(nr_errors == 0 and TERM_COLOR_CODE_GREEN or TERM_COLOR_CODE_RED) + io.write(BAR_CHAR:rep(WIDTH_OR_BAR)) + io.write(TERM_COLOR_CODE_WHITE) - local function_start_index = file_text:find('function '..func_name) - local function_end_index = get_end_of_function(file_text, function_start_index) + local numbers_str = ' ' .. nr_successes .. ' / ' .. nr_tests .. ' ' + local half_bar = (WIDTH_OR_BAR-#numbers_str)/2 + local green_bar = BAR_CHAR:rep(math.floor(half_bar)) .. numbers_str .. BAR_CHAR:rep(math.ceil(half_bar)) + green_bar = TERM_COLOR_CODE_GREEN .. green_bar:sub(1, size_of_green_bar) .. TERM_COLOR_CODE_RED .. green_bar:sub(size_of_green_bar + 1) - local new_text = file_text:sub(1, function_end_index):gsub('local function '..func_name, 'return function') - assert(new_text ~= file_text, ('No function in module "%s" with name "%s" '):format(module_name, func_name)) - local chunk, error_msg = load(new_text) - if not chunk then - error(('While loading function in module "%s" with name "%s":\n\n\t%s\n'):format(module_name, func_name, error_msg)) - end - assert(chunk, error) - return chunk() + -- Get + io.write('\n\nSummary: '..green_bar..'\n\n') + io.write(TERM_COLOR_CODE_WHITE) end -------------------------------------------------------------------------------- diff --git a/test/test_pretty.lua b/test/test_pretty.lua index 957cb78..e64f652 100644 --- a/test/test_pretty.lua +++ b/test/test_pretty.lua @@ -266,12 +266,6 @@ format_test { expect = '{\n\t[{...}] = \'Hello World\'\n}', } - -format_test { - input = false, - expect = 'true', -} - -------------------------------------------------------------------------------- return SUITE