summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormpeterv <mpeterval@gmail.com>2016-04-17 17:58:54 +0300
committermpeterv <mpeterval@gmail.com>2016-04-17 17:58:54 +0300
commit7806e8bd91654b210ba7aa8f5125da3a3c5cab9a (patch)
tree9f1ccc76be4d9b21cd34ae596b02dbf059247316
parenta5ff5b514ec0a0878b24d51a9db3db385e789d71 (diff)
downloadluacov-7806e8bd91654b210ba7aa8f5125da3a3c5cab9a.tar.gz
Change default reporter output format
* Don't use tabs. * Avoid trailing whitespace. * Format summary using a nice table.
-rw-r--r--src/luacov/reporter.lua102
-rw-r--r--tests/cli.lua6
-rw-r--r--tests/coroutines/expected.out43
-rw-r--r--tests/filefilter/expected.out17
-rw-r--r--tests/filefilter/expected2.out11
-rw-r--r--tests/hook/expected.out27
-rw-r--r--tests/nested/expected.out41
-rw-r--r--tests/simple/expected.out17
8 files changed, 165 insertions, 99 deletions
diff --git a/src/luacov/reporter.lua b/src/luacov/reporter.lua
index 5236d4f..c62f8f6 100644
--- a/src/luacov/reporter.lua
+++ b/src/luacov/reporter.lua
@@ -520,56 +520,116 @@ function DefaultReporter:on_start()
self._empty_format = (" "):rep(most_hits_length + 1)
self._zero_format = ("*"):rep(most_hits_length).."0"
self._count_format = ("%% %dd"):format(most_hits_length+1)
+ self._printed_first_header = false
end
function DefaultReporter:on_new_file(filename)
- self:write("\n")
- self:write("==============================================================================\n")
+ if self._printed_first_header then
+ self:write("\n")
+ else
+ self._printed_first_header = true
+ end
+
+ self:write(("="):rep(78), "\n")
self:write(filename, "\n")
- self:write("==============================================================================\n")
+ self:write(("="):rep(78), "\n")
end
function DefaultReporter:on_empty_line(_, _, line)
- self:write(self._empty_format, "\t", line, "\n")
+ if line == "" then
+ self:write("\n")
+ else
+ self:write(self._empty_format, " ", line, "\n")
+ end
end
function DefaultReporter:on_mis_line(_, _, line)
- self:write(self._zero_format, "\t", line, "\n")
+ self:write(self._zero_format, " ", line, "\n")
end
function DefaultReporter:on_hit_line(_, _, line, hits)
- self:write(self._count_format:format(hits), "\t", line, "\n")
+ self:write(self._count_format:format(hits), " ", line, "\n")
end
function DefaultReporter:on_end_file(filename, hits, miss)
self._summary[filename] = { hits = hits, miss = miss }
end
+local function coverage_to_string(hits, missed)
+ local total = hits + missed
+
+ if total == 0 then
+ total = 1
+ end
+
+ return ("%.2f%%"):format(hits/total*100.0)
+end
+
function DefaultReporter:on_end()
self:write("\n")
- self:write("==============================================================================\n")
+ self:write(("="):rep(78), "\n")
self:write("Summary\n")
- self:write("==============================================================================\n")
+ self:write(("="):rep(78), "\n")
self:write("\n")
- local function write_total(hits, miss, filename)
- local total = hits + miss
- if total == 0 then total = 1 end
+ local lines = {{"File", "Hits", "Missed", "Coverage"}}
+ local total_hits, total_missed = 0, 0
+
+ for _, filename in ipairs(self:files()) do
+ local summary = self._summary[filename]
+
+ if summary then
+ local hits, missed = summary.hits, summary.miss
- self:write(hits, "\t", miss, "\t", ("%.2f%%"):format(hits/(total)*100.0), "\t", filename, "\n")
+ table.insert(lines, {
+ filename,
+ tostring(summary.hits),
+ tostring(summary.miss),
+ coverage_to_string(hits, missed)
+ })
+
+ total_hits = total_hits + hits
+ total_missed = total_missed + missed
+ end
end
- local total_hits, total_miss = 0, 0
- for _, filename in ipairs(self:files()) do
- local s = self._summary[filename]
- if s then
- write_total(s.hits, s.miss, filename)
- total_hits = total_hits + s.hits
- total_miss = total_miss + s.miss
+ table.insert(lines, {
+ "Total",
+ tostring(total_hits),
+ tostring(total_missed),
+ coverage_to_string(total_hits, total_missed)
+ })
+
+ local max_column_lengths = {}
+
+ for _, line in ipairs(lines) do
+ for column_nr, column in ipairs(line) do
+ max_column_lengths[column_nr] = math.max(max_column_lengths[column_nr] or -1, #column)
+ end
+ end
+
+ local table_width = #max_column_lengths - 1
+
+ for _, column_length in ipairs(max_column_lengths) do
+ table_width = table_width + column_length
+ end
+
+
+ for line_nr, line in ipairs(lines) do
+ if line_nr == #lines or line_nr == 2 then
+ self:write(("-"):rep(table_width), "\n")
+ end
+
+ for column_nr, column in ipairs(line) do
+ self:write(column)
+
+ if column_nr == #line then
+ self:write("\n")
+ else
+ self:write((" "):rep(max_column_lengths[column_nr] - #column + 1))
+ end
end
end
- self:write("------------------------\n")
- write_total(total_hits, total_miss, "")
end
end
diff --git a/tests/cli.lua b/tests/cli.lua
index 528aa02..4f7370d 100644
--- a/tests/cli.lua
+++ b/tests/cli.lua
@@ -22,7 +22,7 @@ local function read(file)
end
-- dir must be a subdir of tests/ containing expected.out or expected_file.
--- The file can contain 'H' to match any number of hits.
+-- The file can contain 'X' to match any number of hits.
-- flags will be passed to luacov.
local function test(dir, expected_file, flags)
ntests = ntests + 1
@@ -45,8 +45,8 @@ local function test(dir, expected_file, flags)
local ok
- if expected:find("H") then
- local expected_pattern = expected:gsub("%p", "%%%0"):gsub("H", "%%d+")
+ if expected:find("X") then
+ local expected_pattern = expected:gsub("%p", "%%%0"):gsub("X", "%%d+")
ok = actual:match("^" .. expected_pattern .. "$")
else
ok = actual == expected
diff --git a/tests/coroutines/expected.out b/tests/coroutines/expected.out
index f4c60c7..8a9468f 100644
--- a/tests/coroutines/expected.out
+++ b/tests/coroutines/expected.out
@@ -1,29 +1,30 @@
-
==============================================================================
test.lua
==============================================================================
- 1 local runner = require "luacov.runner"
-
- local function f(x)
- H return coroutine.yield(x + 1) + 2
- end
-
- local function g(x)
- H return coroutine.yield(x + 3) + 4
- end
-
- H local wf = coroutine.wrap(f)
- H local wg = corowrap(runner.with_luacov(g))
-
- 1 assert(wf(3) == 4)
- 1 assert(wf(5) == 7)
- 1 assert(wg(8) == 11)
- 1 assert(wg(10) == 14)
+ 1 local runner = require "luacov.runner"
+
+ local function f(x)
+ X return coroutine.yield(x + 1) + 2
+ end
+
+ local function g(x)
+ X return coroutine.yield(x + 3) + 4
+ end
+
+ X local wf = coroutine.wrap(f)
+ X local wg = corowrap(runner.with_luacov(g))
+
+ 1 assert(wf(3) == 4)
+ 1 assert(wf(5) == 7)
+ 1 assert(wg(8) == 11)
+ 1 assert(wg(10) == 14)
==============================================================================
Summary
==============================================================================
-9 0 100.00% test.lua
-------------------------
-9 0 100.00%
+File Hits Missed Coverage
+-----------------------------
+test.lua 9 0 100.00%
+-----------------------------
+Total 9 0 100.00%
diff --git a/tests/filefilter/expected.out b/tests/filefilter/expected.out
index 5b92f85..a22d8ee 100644
--- a/tests/filefilter/expected.out
+++ b/tests/filefilter/expected.out
@@ -1,20 +1,21 @@
-
==============================================================================
test.lua
==============================================================================
- 1 require "test2"
- 1 require "test3"
+ 1 require "test2"
+ 1 require "test3"
==============================================================================
test2.lua
==============================================================================
- 1 local a = 1
+ 1 local a = 1
==============================================================================
Summary
==============================================================================
-2 0 100.00% test.lua
-1 0 100.00% test2.lua
-------------------------
-3 0 100.00%
+File Hits Missed Coverage
+------------------------------
+test.lua 2 0 100.00%
+test2.lua 1 0 100.00%
+------------------------------
+Total 3 0 100.00%
diff --git a/tests/filefilter/expected2.out b/tests/filefilter/expected2.out
index 9e11fc1..3fca0c6 100644
--- a/tests/filefilter/expected2.out
+++ b/tests/filefilter/expected2.out
@@ -1,13 +1,14 @@
-
==============================================================================
test2.lua
==============================================================================
- 1 local a = 1
+ 1 local a = 1
==============================================================================
Summary
==============================================================================
-1 0 100.00% test2.lua
-------------------------
-1 0 100.00%
+File Hits Missed Coverage
+------------------------------
+test2.lua 1 0 100.00%
+------------------------------
+Total 1 0 100.00%
diff --git a/tests/hook/expected.out b/tests/hook/expected.out
index fe99697..b925ece 100644
--- a/tests/hook/expected.out
+++ b/tests/hook/expected.out
@@ -1,25 +1,26 @@
-
==============================================================================
my_hook.lua
==============================================================================
- 1 local runner = require "luacov.runner"
- 1 return function(_, line) runner.debug_hook(_, line, 3) end
+ 1 local runner = require "luacov.runner"
+ 1 return function(_, line) runner.debug_hook(_, line, 3) end
==============================================================================
test.lua
==============================================================================
- 1 local runner = require "luacov.runner"
- 1 local my_hook = require "my_hook"
- 1 debug.sethook(my_hook, "line")
- 1 local a = 2
- 1 debug.sethook(runner.debug_hook, "line")
- 1 local b = 3
+ 1 local runner = require "luacov.runner"
+ 1 local my_hook = require "my_hook"
+ 1 debug.sethook(my_hook, "line")
+ 1 local a = 2
+ 1 debug.sethook(runner.debug_hook, "line")
+ 1 local b = 3
==============================================================================
Summary
==============================================================================
-2 0 100.00% my_hook.lua
-6 0 100.00% test.lua
-------------------------
-8 0 100.00%
+File Hits Missed Coverage
+--------------------------------
+my_hook.lua 2 0 100.00%
+test.lua 6 0 100.00%
+--------------------------------
+Total 8 0 100.00%
diff --git a/tests/nested/expected.out b/tests/nested/expected.out
index 28784b2..fb5891e 100644
--- a/tests/nested/expected.out
+++ b/tests/nested/expected.out
@@ -1,28 +1,29 @@
-
==============================================================================
testlib.lua
==============================================================================
- 2 local a = 1
- 2 local b = 2
-
- 2 local lib = {}
-
- 2 function lib.f1()
- 2 local c = 3
- 2 return 4
- end
-
- 2 function lib.f2()
- 3 local d = 5
- 3 return 6
- end
-
- 2 return lib
+ 2 local a = 1
+ 2 local b = 2
+
+ 2 local lib = {}
+
+ 2 function lib.f1()
+ 2 local c = 3
+ 2 return 4
+ end
+
+ 2 function lib.f2()
+ 3 local d = 5
+ 3 return 6
+ end
+
+ 2 return lib
==============================================================================
Summary
==============================================================================
-10 0 100.00% testlib.lua
-------------------------
-10 0 100.00%
+File Hits Missed Coverage
+--------------------------------
+testlib.lua 10 0 100.00%
+--------------------------------
+Total 10 0 100.00%
diff --git a/tests/simple/expected.out b/tests/simple/expected.out
index ebb3690..a1c1d6b 100644
--- a/tests/simple/expected.out
+++ b/tests/simple/expected.out
@@ -1,16 +1,17 @@
-
==============================================================================
test.lua
==============================================================================
- 1 local a = 1
- 1 local b = 2
-
- 1 local c = 3
+ 1 local a = 1
+ 1 local b = 2
+
+ 1 local c = 3
==============================================================================
Summary
==============================================================================
-3 0 100.00% test.lua
-------------------------
-3 0 100.00%
+File Hits Missed Coverage
+-----------------------------
+test.lua 3 0 100.00%
+-----------------------------
+Total 3 0 100.00%