cirandas.net

ref: master

public/javascripts/tinymce/tests/coverage/js/JSCovReporter.js


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
JSCovFileReporter = Backbone.View.extend({
    initialize: function () {
        _.bindAll(this);
        this.open  = '<tr class="{class}"><td class="line">{line_number}</td><td class="hits">{count}</td><td class="source">';
        this.close = '</td></tr>';

        this.coverObject = this.options.coverObject;

        this.error = 0;
        this.pass = 0;
        this.total = 0;
    },

    // substitute credits: MooTools
    substitute: function(string, object){
        return string.replace(/\\?\{([^{}]+)\}/g, function(match, name){
            if (match.charAt(0) == '\\') return match.slice(1);
            return (object[name] !== null) ? object[name] : '';
        });
    },

    generateClose: function(count){
        return this.substitute(this.close, {
            count: count
        });
    },

    generateOpen: function(hit_count, line_number){
        return this.substitute(this.open, {
            'count': hit_count,
            'line_number': line_number,
            'class': hit_count ? 'hit' : 'miss'
        });
    },

    report: function () {
        var thisview = this;
        var i, l, k;

        var code = this.coverObject.__code;

        // generate array of all tokens
        var codez = [];
        for (i = 0, l = code.length; i < l; i++){
            codez.push({
                pos: i,
                value: code.slice(i, i + 1)
            });
        }

        // CoverObject has keys like "12:200" which means from char 12 to 200
        // This orders all first gaps in a list of dictionaries to ease drawing table lines
        var gaps = Object.keys(this.coverObject);
        gaps = _.without(gaps, '__code');
        var first_gaps = _.map(gaps, function ( gap ) {
            return {
                gap: parseInt(gap.split(':')[0], 10),
                hit_count: thisview.coverObject[gap]
            };
        }).sort(function (a, b) {
            if (a['gap'] > b['gap']) return 1;
            if (b['gap'] > a['gap']) return -1;
            return 0;
        });

        var second_gaps = _.map(gaps, function ( gap ) {
            return {
                gap: parseInt(gap.split(':')[1], 10),
                hit_count: thisview.coverObject[gap]
            };
        }).sort(function (a, b) {
            if (a['gap'] > b['gap']) return 1;
            if (b['gap'] > a['gap']) return -1;
            return 0;
        });


        // If it doesn't start from 0 it's because there are comments in the beginning
        // We add a initial gap with one hit
        if (first_gaps[0] !== 0) {
            first_gaps.splice(0, 0, {gap: 0, hit_count: 1});
        }

        var result = '';
        var number_trailing_whitespaces = 0;
        var trailing_whitespaces = '';


        // We will go from one gap to the next wrapping them in table lines
        for (i=0, l = first_gaps.length; i < l; i++){

            var hit_count = first_gaps[i]['hit_count'];

            this.total++;
            if (hit_count) this.pass++;
            else this.error++;

            var limit = null;
            if (i+1 >= l) {
                limit = codez.length;
            }
            else {
                limit = first_gaps[i+1]['gap'];
            }

            // Table line opening
            result += this.generateOpen(hit_count, this.total);

            // Add trailing white space if it existed from previous line without carriage returns
            if (number_trailing_whitespaces > 0 ) {
                result += trailing_whitespaces.replace(/(\r\n|\n|\r)/gm,"");
            }

            // Add lines of code without initial white spaces, and replacing conflictive chars
            result += _.map(codez.slice(first_gaps[i]['gap'], limit), function (loc) {
                return loc['value'];
            }).join('').trimLeft().replace(/</g, '&lt;').replace(/>/g, '&gt;');

            // Count trailing white spaces for future line, then remove them
            var matches = result.match(/(\s+)$/);
            result = result.trimRight();

            if (matches !== null) {
                number_trailing_whitespaces = matches[0].length;
                trailing_whitespaces = matches[0];
            }
            else {
                number_trailing_whitespaces = 0;
            }

            // Generate table line closing
            result += this.generateClose(hit_count);
        }

        return result;
    }
});


JSCovReporter = Backbone.View.extend({
    initialize: function () {
        this.coverObject = this.options.coverObject;

        // Generate the report
        this.report();

        // Activate reporter.js scrolling UX
        onload();
    },

    report: function () {
        var result = '';
        var index = '';

        for (var file in this.coverObject) {
            var fileReporter = new JSCovFileReporter({ coverObject: this.coverObject[file] });

            var fileReport = fileReporter.report();
            var percentage = Math.round(fileReporter.pass / fileReporter.total * 100);

            this.error += fileReporter.error;
            this.pass  += fileReporter.pass;
            this.total += fileReporter.total;

            var type_coverage = "high";
            if (percentage < 75 && percentage >= 50) {
                type_coverage = 'medium';
            }
            else if (percentage < 50 && percentage >= 25) {
                type_coverage = 'low';
            }
            else if (percentage < 25) {
                type_coverage = 'terrible';
            }

            // Title
            result += '<h2 id="' + file + '" class="file-title">' + file + '</h2>';
            // Stats
            result += '<div class="stats ' + type_coverage + '"><div class="percentage">'+ percentage + '%</div>';
            result += '<div class="sloc">' + fileReporter.total + '</div><div class="hits">' + fileReporter.pass + '</div>';
            result += '<div class="misses">' + fileReporter.error + '</div></div>';
            // Report
            result += '<div class="file-report">';
            result += '<table id="source"><tbody>' + fileReport + '</tbody></table>';
            result += '</div>';

            // Menu index
            index += '<li><span class="cov ' + type_coverage + '">' + percentage + '</span><a href="#' + file+ '">' + file + '</a></li>';
        }

        $('#coverage').html(result);
        $('#menu').html('<ul id="toc">' + index + '</ul>');
    }
});