+function new_line_coverage_monitor(file)
+ local lines_hit, funcs_hit = {}, {};
+ local total_lines, covered_lines = 0, 0;
+
+ for line in io.lines(file) do
+ total_lines = total_lines + 1;
+ end
+
+ return function (event, line) -- Line hook
+ if not lines_hit[line] then
+ local info = debug.getinfo(2, "fSL")
+ if not info.source:find(file) then return; end
+ if not funcs_hit[info.func] and info.activelines then
+ funcs_hit[info.func] = true;
+ for line in pairs(info.activelines) do
+ lines_hit[line] = false; -- Marks it as hittable, but not hit yet
+ end
+ end
+ if lines_hit[line] == false then
+ --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
+ lines_hit[line] = true;
+ covered_lines = covered_lines + 1;
+ end
+ end
+ end,
+ function (test_name, success) -- Get info
+ local fn = file:gsub("^%W*", "");
+ local total_active_lines = 0;
+ local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
+ for line, active in pairs(lines_hit) do
+ if active ~= nil then total_active_lines = total_active_lines + 1; end
+ if coverage_file then
+ if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
+ else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
+ end
+ end
+ if coverage_file then coverage_file:close(); end
+ return covered_lines, total_active_lines, lines_hit;
+ end
+end
+
+run_all_tests()