tests/test.lua
author Matthew Wild <mwild1@gmail.com>
Sun, 30 Nov 2008 18:57:23 +0000
changeset 509 32899c8a6fe5
parent 470 2f9d42fdeffa
child 519 cccd610a0ef9
permissions -rw-r--r--
Add test for latin1toutf8 (which passes)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
     1
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
     2
function run_all_tests()
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
     3
	dotest "util.jid"
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
     4
	dotest "core.stanza_router"
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
     5
	dotest "core.s2smanager"
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
     6
	dotest "core.configmanager"
509
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
     7
	
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
     8
	dosingletest("test_sasl.lua", "latin1toutf8");
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
     9
end
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local verbosity = tonumber(arg[1]) or 2;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
    13
package.path = package.path..";../?.lua";
470
2f9d42fdeffa Also look for binary modules in the parent directory when running tests
Matthew Wild <mwild1@gmail.com>
parents: 370
diff changeset
    14
package.cpath = package.cpath..";../?.so";
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
    15
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
    16
require "util.import"
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
    17
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
    18
local env_mt = { __index = function (t,k) return rawget(_G, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
    19
function testlib_new_env(t)
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
    20
	return setmetatable(t or {}, env_mt);
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
    21
end
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
    22
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
    23
function assert_equal(a, b, message)
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
	if not (a == b) then
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
    25
		error("\n   assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n   Message: "..message) or ""), 2);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
	elseif verbosity >= 4 then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
		print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
	end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
509
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    31
function dosingletest(testname, fname)
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    32
	local tests = setmetatable({}, { __index = _G });
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    33
	tests.__unit = testname;
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    34
	tests.__test = fname;
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    35
	local chunk, err = loadfile(testname);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    36
	if not chunk then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    37
		print("WARNING: ", "Failed to load tests for "..testname, err);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    38
		return;
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    39
	end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    40
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    41
	setfenv(chunk, tests);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    42
	local success, err = pcall(chunk);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    43
	if not success then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    44
		print("WARNING: ", "Failed to initialise tests for "..testname, err);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    45
		return;
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    46
	end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    47
	
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    48
	if type(tests[fname]) ~= "function" then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    49
		error(testname.." has no test '"..fname.."'", 0);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    50
	end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    51
	
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    52
	
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    53
	local line_hook, line_info = new_line_coverage_monitor(testname);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    54
	debug.sethook(line_hook, "l")
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    55
	local success, ret = pcall(tests[fname]);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    56
	debug.sethook();
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    57
	if not success then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    58
		print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]");
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    59
		print("   Location: "..ret:gsub(":%s*\n", "\n"));
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    60
		line_info(fname, false, report_file);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    61
	elseif verbosity >= 2 then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    62
		print("TEST SUCCEEDED: ", testname, fname);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    63
		print(string.format("TEST COVERED %d/%d lines", line_info(fname, true, report_file)));
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    64
	else
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    65
		line_info(name, success, report_file);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    66
	end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    67
end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
    68
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
function dotest(unitname)
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
	local tests = setmetatable({}, { __index = _G });
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
	tests.__unit = unitname;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
	local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
	if not chunk then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
		print("WARNING: ", "Failed to load tests for "..unitname, err);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
		return;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
	end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
	setfenv(chunk, tests);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
	local success, err = pcall(chunk);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
	if not success then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
		print("WARNING: ", "Failed to initialise tests for "..unitname, err);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
		return;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
	end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
	
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
	local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) });
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
    87
	local fn = "../"..unitname:gsub("%.", "/")..".lua";
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
    88
	local chunk, err = loadfile(fn);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
	if not chunk then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
		print("WARNING: ", "Failed to load module: "..unitname, err);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
		return;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
	end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
	setfenv(chunk, unit);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
	local success, err = pcall(chunk);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
	if not success then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
		print("WARNING: ", "Failed to initialise module: "..unitname, err);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
		return;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
	end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
	
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
	for name, f in pairs(unit) do
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   102
		local test = rawget(tests, name);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
		if type(f) ~= "function" then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
			if verbosity >= 3 then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
				print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
			end
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   107
		elseif type(test) ~= "function" then
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
			if verbosity >= 1 then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
				print("WARNING: ", unitname.."."..name.." has no test!");
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
			end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
		else
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   112
			local line_hook, line_info = new_line_coverage_monitor(fn);
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   113
			debug.sethook(line_hook, "l")
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   114
			local success, ret = pcall(test, f, unit);
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   115
			debug.sethook();
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
			if not success then
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
   117
				print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
   118
				print("   Location: "..ret:gsub(":%s*\n", "\n"));
370
9ade55e059ea Update test.lua with a work-in-progress
Matthew Wild <mwild1@gmail.com>
parents: 361
diff changeset
   119
				line_info(name, false, report_file);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
			elseif verbosity >= 2 then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
				print("TEST SUCCEEDED: ", unitname, name);
370
9ade55e059ea Update test.lua with a work-in-progress
Matthew Wild <mwild1@gmail.com>
parents: 361
diff changeset
   122
				print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   123
			else
370
9ade55e059ea Update test.lua with a work-in-progress
Matthew Wild <mwild1@gmail.com>
parents: 361
diff changeset
   124
				line_info(name, success, report_file);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
			end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
		end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
	end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
   130
function runtest(f, msg)
301
fcb7e63630ae Warn when subtest function does not exist
Matthew Wild <mwild1@gmail.com>
parents: 271
diff changeset
   131
	if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
   132
	local success, ret = pcall(f);
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
   133
	if success and verbosity >= 2 then
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
   134
		print("SUBTEST PASSED: "..(msg or "(no description)"));
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
   135
	elseif (not success) and verbosity >= 1 then
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
   136
		print("SUBTEST FAILED: "..(msg or "(no description)"));
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
   137
		error(ret, 0);
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
   138
	end
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
   139
end
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
   140
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   141
function new_line_coverage_monitor(file)
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   142
	local lines_hit, funcs_hit = {}, {};
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   143
	local total_lines, covered_lines = 0, 0;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   144
	
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   145
	for line in io.lines(file) do
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   146
		total_lines = total_lines + 1;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   147
	end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   148
	
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   149
	return function (event, line) -- Line hook
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   150
			if not lines_hit[line] then
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   151
				local info = debug.getinfo(2, "fSL")
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   152
				if not info.source:find(file) then return; end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   153
				if not funcs_hit[info.func] and info.activelines then
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   154
					funcs_hit[info.func] = true;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   155
					for line in pairs(info.activelines) do
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   156
						lines_hit[line] = false; -- Marks it as hittable, but not hit yet
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   157
					end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   158
				end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   159
				if lines_hit[line] == false then
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   160
					--print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   161
					lines_hit[line] = true;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   162
					covered_lines = covered_lines + 1;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   163
				end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   164
			end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   165
		end,
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   166
		function (test_name, success) -- Get info
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   167
			local fn = file:gsub("^%W*", "");
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   168
			local total_active_lines = 0;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   169
			local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   170
			for line, active in pairs(lines_hit) do
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   171
				if active ~= nil then total_active_lines = total_active_lines + 1; end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   172
				if coverage_file then
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   173
					if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n"); 
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   174
					else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   175
				end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   176
			end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   177
			if coverage_file then coverage_file:close(); end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   178
			return covered_lines, total_active_lines, lines_hit;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   179
		end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   180
end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   181
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
   182
run_all_tests()