FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Code coverage using the debug lib

 
Post new topic   Reply to topic     Forum Index -> MiniD
View previous topic :: View next topic  
Author Message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Fri Jul 10, 2009 1:32 pm    Post subject: Code coverage using the debug lib Reply with quote

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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> MiniD All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group