JarrettBillingsley
Joined: 20 Jun 2006 Posts: 457 Location: Pennsylvania!
|
Posted: Fri Jul 10, 2009 1:32 pm Post subject: Code coverage using the debug lib |
|
|
Sorry, this was just too cool to not post (you'll have to have updated to the newest revision, 561, for it to work right, since I fixed a bug with ?=):
Code: | {
// {moduleName = {funcName = {line = int}}}
local coverTab = {}
function cover(type)
{
assert(type == "line")
local name = debug.sourceName(1)
local func = debug.getFunc(1)
local line = debug.currentLine(1)
coverTab[name] ?= {}
coverTab[name][func] ?= {[l] = 0 for l in debug.lineInfo(1)}
coverTab[name][func][line]++
}
function dumpCoverInfo()
{
foreach(sourceFile, funcs; coverTab)
{
if(io.exists(sourceFile))
{
writeln(sourceFile)
writeln("-".repeat(#sourceFile))
writeln()
local tmp = {}
foreach(func, lines; funcs)
{
foreach(line, count; lines)
{
tmp[line] ?= 0
tmp[line] += count
}
}
foreach(i, line; io.lines(sourceFile))
{
if(i in tmp)
{
local count = tmp[i]
if(count == 0)
writefln("000000 |{}", line)
else
writefln("{,-10}| {}", count, line)
}
else
writefln(" | {}", line)
}
}
else
writeln("Cannot find source for file ", sourceFile)
writeln();
}
}
}
debug.setHook$ cover, "l"
scope(exit)
{
debug.setHook$ null
dumpCoverInfo()
}
|
Bam, suddenly you have code coverage analysis that's dumped to the console when your program exits. The output is similar to DMD's -cov output. |
|