Skip to content

Instantly share code, notes, and snippets.

@neonux
Created September 14, 2011 16:46
Show Gist options
  • Save neonux/1217064 to your computer and use it in GitHub Desktop.
Save neonux/1217064 to your computer and use it in GitHub Desktop.
function coverage(aDump)
{
return [];
}
function aggregate(aOriginalStats, aNewStats, aUpdate)
{
// aUpdate(aScript, aLine) is called for every line that changed since aOriginalStats
}
@davidflanagan
Copy link

[
{
"filename": "/tmp/test.js",
"covered": 6,
"partial": 1,
"uncovered": 1,
"dead": 1,
"lines": [
{
"linenum": 1,
"coverage": "full",
"counts": [
1
]
},
{
"linenum": 2,
"coverage": "full",
"counts": [
1000
]
},
{
"linenum": 3,
"coverage": "dead",
"counts": [
-1
]
},
{
"linenum": 6,
"coverage": "full",
"counts": [
1
]
},
{
"linenum": 7,
"coverage": "full",
"counts": [
1,
1000,
1001
]
},
{
"linenum": 8,
"coverage": "full",
"counts": [
1000
]
},
{
"linenum": 10,
"coverage": "full",
"counts": [
1
]
},
{
"linenum": 11,
"coverage": "none",
"counts": [
0
]
},
{
"linenum": 12,
"coverage": "some",
"counts": [
0,
1
]
}
]
}
]

@davidflanagan
Copy link

// This class represents parsed code coverage data.
// Pass data (as a string in -D format) to the parseData() method.
// Access the parsed data through the data property.
// You can call parseData() multiple times, and the data will be
// updated with the most recent data.
//
// Or, pass a newFile callback that will be invoked for each new script/file
// in the coverage data. And pass a lineUpdate callback; it will be invoked
// each time an line in an existing file gets a new count. (I don't think
// that new lines will ever be added to an existing file, so lines should
// only ever have count updates)

function Coverage(newFileCallback, lineUpdateCallback) {
this.newFileCallback = newFileCallback;
this.lineUpdateCallbac = lineUpdateCallback;
this.data = [];
}

Coverage.prototype.parseData = function(rawdata) {
}

@davidflanagan
Copy link

var coverage = new Coverage(newFileCallback,
                            fileUpdateCallback,
                            lineUpdateCallback);


// Repeat these two lines multiple times
var data = getDisassmeblyDump();
coverage.parseData(data);  

function newFileCallback(filename) {
    var file = coverage.getFileData(filename);
    var covered = file.covered;  // # of covered lines
    var partial = file.partial;  // # of partially covered lines
    var uncoveredd = file.uncovered;  // # of uncovered lines
    var dead = file.dead;  // # of dead lines
    file.lines.forEach(function(line) {
        var linenum = line.linenum;  // line number in filename
        var coverage = line.coverage; // One of the strings
                                      // "full", "some", "none", "dead"
        var counts = line.counts;     // An array of numbers
    });
}

function fileUpdateCallback(filename) {
    // Coverage statistics for this file have changed
    var file = coverage.getFileData(filename);
    var coveragePercent = Math.round(100 * file.covered/
                                     (file.covered + file.uncovered +
                                      file.partial + file.dead)); 
}

function lineUpdateCallback(filename, linenum, coverage, counts) {
    // This callback is only called for lines where the coverage and
    // counts have changed.
}

@neonux
Copy link
Author

neonux commented Sep 14, 2011 via email

@neonux
Copy link
Author

neonux commented Sep 14, 2011 via email

@davidflanagan
Copy link

load("Coverage.js")

var coverage = new Coverage();
coverage.addListener({
    onNewScript: function(filename, filedata) {
        print("New file: " + filename);
        print(JSON.stringify(filedata));
    },
    onScriptUpdate: function(filename, filedata) {
        print("File updated: " + filename);
        print("covered: " + filedata.covered);
        print("partial: " + filedata.partial);
        print("uncovered: " + filedata.uncovered);
        print("dead: " + filedata.dead);
    },
    onLineUpdate: function(filename, linedata) {
        print("Line updated: " + filename + ":" + linedata.linenum);
        print(linedata.counts);
    }
});

coverage.parseData(snarf("test.dis1"));
coverage.parseData(snarf("test.dis2"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment