| 1 |
efrain |
1 |
YUI.add('test-console', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
Provides a specialized log console widget that's pre-configured to display YUI
|
|
|
5 |
Test output with no extra configuration.
|
|
|
6 |
|
|
|
7 |
@example
|
|
|
8 |
|
|
|
9 |
<div id="log" class="yui3-skin-sam"></div>
|
|
|
10 |
|
|
|
11 |
<script>
|
|
|
12 |
YUI().use('test-console', function (Y) {
|
|
|
13 |
// ... set up your test cases here ...
|
|
|
14 |
|
|
|
15 |
// Render the console inside the #log div, then run the tests.
|
|
|
16 |
new Y.Test.Console().render('#log');
|
|
|
17 |
Y.Test.Runner.run();
|
|
|
18 |
});
|
|
|
19 |
</script>
|
|
|
20 |
|
|
|
21 |
@module test-console
|
|
|
22 |
@namespace Test
|
|
|
23 |
@class Console
|
|
|
24 |
@extends Console
|
|
|
25 |
@constructor
|
|
|
26 |
|
|
|
27 |
@param {Object} [config] Config attributes.
|
|
|
28 |
@param {Object} [config.filters] Category filter configuration.
|
|
|
29 |
|
|
|
30 |
@since 3.5.0
|
|
|
31 |
**/
|
|
|
32 |
|
|
|
33 |
function TestConsole() {
|
|
|
34 |
TestConsole.superclass.constructor.apply(this, arguments);
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
Y.namespace('Test').Console = Y.extend(TestConsole, Y.Console, {
|
|
|
38 |
initializer: function (config) {
|
|
|
39 |
this.on('entry', this._onEntry);
|
|
|
40 |
|
|
|
41 |
this.plug(Y.Plugin.ConsoleFilters, {
|
|
|
42 |
category: Y.merge({
|
|
|
43 |
info : true,
|
|
|
44 |
pass : false,
|
|
|
45 |
fail : true,
|
|
|
46 |
status: false
|
|
|
47 |
}, (config && config.filters) || {}),
|
|
|
48 |
|
|
|
49 |
defaultVisibility: false,
|
|
|
50 |
|
|
|
51 |
source: {
|
|
|
52 |
TestRunner: true
|
|
|
53 |
}
|
|
|
54 |
});
|
|
|
55 |
|
|
|
56 |
Y.Test.Runner.on('complete', Y.bind(this._parseCoverage, this));
|
|
|
57 |
},
|
|
|
58 |
|
|
|
59 |
// -- Protected Coverage Parser ---------------------------------------------
|
|
|
60 |
/**
|
|
|
61 |
* Scans the coverage data to determine if it's an Istanbul coverage object.
|
|
|
62 |
* @method _isIstanbul
|
|
|
63 |
* @param {Object} json The coverage data to scan
|
|
|
64 |
* @return {Boolean} True if this is Istanbul Coverage
|
|
|
65 |
*/
|
|
|
66 |
_isIstanbul: function(json) {
|
|
|
67 |
var first = Y.Object.keys(json)[0],
|
|
|
68 |
ret = false;
|
|
|
69 |
|
|
|
70 |
if (json[first].s !== undefined && json[first].fnMap !== undefined) {
|
|
|
71 |
ret = true;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
if (json.s !== undefined && json.fnMap !== undefined) {
|
|
|
75 |
ret = true;
|
|
|
76 |
}
|
|
|
77 |
return ret;
|
|
|
78 |
},
|
|
|
79 |
/**
|
|
|
80 |
* Parses and logs a summary of YUITest coverage data.
|
|
|
81 |
* @method parseYUITest
|
|
|
82 |
* @param {Object} coverage The YUITest Coverage JSON data
|
|
|
83 |
*/
|
|
|
84 |
parseYUITestCoverage: function (coverage) {
|
|
|
85 |
var cov = {
|
|
|
86 |
lines: {
|
|
|
87 |
hit: 0,
|
|
|
88 |
miss: 0,
|
|
|
89 |
total: 0,
|
|
|
90 |
percent: 0
|
|
|
91 |
},
|
|
|
92 |
functions: {
|
|
|
93 |
hit: 0,
|
|
|
94 |
miss: 0,
|
|
|
95 |
total: 0,
|
|
|
96 |
percent: 0
|
|
|
97 |
}
|
|
|
98 |
}, coverageLog;
|
|
|
99 |
|
|
|
100 |
Y.Object.each(coverage, function(info) {
|
|
|
101 |
cov.lines.total += info.coveredLines;
|
|
|
102 |
cov.lines.hit += info.calledLines;
|
|
|
103 |
cov.lines.miss += (info.coveredLines - info.calledLines);
|
|
|
104 |
cov.lines.percent = Math.floor((cov.lines.hit / cov.lines.total) * 100);
|
|
|
105 |
|
|
|
106 |
cov.functions.total += info.coveredFunctions;
|
|
|
107 |
cov.functions.hit += info.calledFunctions;
|
|
|
108 |
cov.functions.miss += (info.coveredFunctions - info.calledFunctions);
|
|
|
109 |
cov.functions.percent = Math.floor((cov.functions.hit / cov.functions.total) * 100);
|
|
|
110 |
});
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
coverageLog = 'Lines: Hit:' + cov.lines.hit + ' Missed:' + cov.lines.miss + ' Total:' + cov.lines.total + ' Percent:' + cov.lines.percent + '%\n';
|
|
|
114 |
coverageLog += 'Functions: Hit:' + cov.functions.hit + ' Missed:' + cov.functions.miss + ' Total:' + cov.functions.total + ' Percent:' + cov.functions.percent + '%';
|
|
|
115 |
|
|
|
116 |
this.log('Coverage: ' + coverageLog, 'info', 'TestRunner');
|
|
|
117 |
},
|
|
|
118 |
/**
|
|
|
119 |
* Generates a generic summary object used for Istanbul conversions.
|
|
|
120 |
* @method _blankSummary
|
|
|
121 |
* @return {Object} Generic summary object
|
|
|
122 |
*/
|
|
|
123 |
_blankSummary: function () {
|
|
|
124 |
return {
|
|
|
125 |
lines: {
|
|
|
126 |
total: 0,
|
|
|
127 |
covered: 0,
|
|
|
128 |
pct: 'Unknown'
|
|
|
129 |
},
|
|
|
130 |
statements: {
|
|
|
131 |
total: 0,
|
|
|
132 |
covered: 0,
|
|
|
133 |
pct: 'Unknown'
|
|
|
134 |
},
|
|
|
135 |
functions: {
|
|
|
136 |
total: 0,
|
|
|
137 |
covered: 0,
|
|
|
138 |
pct: 'Unknown'
|
|
|
139 |
},
|
|
|
140 |
branches: {
|
|
|
141 |
total: 0,
|
|
|
142 |
covered: 0,
|
|
|
143 |
pct: 'Unknown'
|
|
|
144 |
}
|
|
|
145 |
};
|
|
|
146 |
},
|
|
|
147 |
/**
|
|
|
148 |
* Calculates line numbers from statement coverage
|
|
|
149 |
* @method _addDerivedInfoForFile
|
|
|
150 |
* @private
|
|
|
151 |
* @param {Object} fileCoverage JSON coverage data
|
|
|
152 |
*/
|
|
|
153 |
_addDerivedInfoForFile: function (fileCoverage) {
|
|
|
154 |
var statementMap = fileCoverage.statementMap,
|
|
|
155 |
statements = fileCoverage.s,
|
|
|
156 |
lineMap;
|
|
|
157 |
|
|
|
158 |
if (!fileCoverage.l) {
|
|
|
159 |
fileCoverage.l = lineMap = {};
|
|
|
160 |
Y.Object.each(statements, function (value, st) {
|
|
|
161 |
var line = statementMap[st].start.line,
|
|
|
162 |
count = statements[st],
|
|
|
163 |
prevVal = lineMap[line];
|
|
|
164 |
if (typeof prevVal === 'undefined' || prevVal < count) {
|
|
|
165 |
lineMap[line] = count;
|
|
|
166 |
}
|
|
|
167 |
});
|
|
|
168 |
}
|
|
|
169 |
},
|
|
|
170 |
/**
|
|
|
171 |
* Generic percent calculator
|
|
|
172 |
* @method _percent
|
|
|
173 |
* @param {Number} covered The covered amount
|
|
|
174 |
* @param {Number} total The total
|
|
|
175 |
* @private
|
|
|
176 |
*/
|
|
|
177 |
_percent: function (covered, total) {
|
|
|
178 |
var tmp, pct = 100.00;
|
|
|
179 |
if (total > 0) {
|
|
|
180 |
tmp = 1000 * 100 * covered / total + 5;
|
|
|
181 |
pct = Math.floor(tmp / 10) / 100;
|
|
|
182 |
}
|
|
|
183 |
return pct;
|
|
|
184 |
},
|
|
|
185 |
/**
|
|
|
186 |
* Summarize simple properties in the coverage data
|
|
|
187 |
* @method _computSimpleTotals
|
|
|
188 |
* @private
|
|
|
189 |
* @param {Object} fileCoverage JSON coverage data
|
|
|
190 |
* @param {String} property The property to summarize
|
|
|
191 |
*/
|
|
|
192 |
_computeSimpleTotals: function (fileCoverage, property) {
|
|
|
193 |
var stats = fileCoverage[property],
|
|
|
194 |
ret = { total: 0, covered: 0 };
|
|
|
195 |
|
|
|
196 |
Y.Object.each(stats, function(val) {
|
|
|
197 |
ret.total += 1;
|
|
|
198 |
if (val) {
|
|
|
199 |
ret.covered += 1;
|
|
|
200 |
}
|
|
|
201 |
});
|
|
|
202 |
ret.pct = this._percent(ret.covered, ret.total);
|
|
|
203 |
return ret;
|
|
|
204 |
},
|
|
|
205 |
/**
|
|
|
206 |
* Noramlizes branch data from Istanbul
|
|
|
207 |
* @method _computeBranchTotals
|
|
|
208 |
* @private
|
|
|
209 |
* @param {Object} fileCoverage JSON coverage data
|
|
|
210 |
*/
|
|
|
211 |
_computeBranchTotals: function (fileCoverage) {
|
|
|
212 |
var stats = fileCoverage.b,
|
|
|
213 |
ret = { total: 0, covered: 0 };
|
|
|
214 |
|
|
|
215 |
Y.Object.each(stats, function (branches) {
|
|
|
216 |
var covered = Y.Array.filter(branches, function (num) { return num > 0; });
|
|
|
217 |
ret.total += branches.length;
|
|
|
218 |
ret.covered += covered.length;
|
|
|
219 |
});
|
|
|
220 |
ret.pct = this._percent(ret.covered, ret.total);
|
|
|
221 |
return ret;
|
|
|
222 |
},
|
|
|
223 |
/**
|
|
|
224 |
* Takes an Istanbul coverage object, normalizes it and prints a log with a summary
|
|
|
225 |
* @method parseInstanbul
|
|
|
226 |
* @param {Object} coverage The coverage object to normalize and log
|
|
|
227 |
*/
|
|
|
228 |
parseIstanbul: function (coverage) {
|
|
|
229 |
var self = this,
|
|
|
230 |
str = 'Coverage Report:\n';
|
|
|
231 |
|
|
|
232 |
Y.Object.each(coverage, function(fileCoverage, file) {
|
|
|
233 |
var ret = self._blankSummary();
|
|
|
234 |
|
|
|
235 |
self._addDerivedInfoForFile(fileCoverage);
|
|
|
236 |
ret.lines = self._computeSimpleTotals(fileCoverage, 'l');
|
|
|
237 |
ret.functions = self._computeSimpleTotals(fileCoverage, 'f');
|
|
|
238 |
ret.statements = self._computeSimpleTotals(fileCoverage, 's');
|
|
|
239 |
ret.branches = self._computeBranchTotals(fileCoverage);
|
|
|
240 |
str += file + ':\n';
|
|
|
241 |
Y.Array.each(['lines','functions','statements','branches'], function(key) {
|
|
|
242 |
str += ' ' + key +': ' + ret[key].covered + '/' + ret[key].total + ' : ' + ret[key].pct + '%\n';
|
|
|
243 |
});
|
|
|
244 |
|
|
|
245 |
});
|
|
|
246 |
this.log(str, 'info', 'TestRunner');
|
|
|
247 |
|
|
|
248 |
},
|
|
|
249 |
/**
|
|
|
250 |
* Parses YUITest or Istanbul coverage results if they are available and logs them.
|
|
|
251 |
* @method _parseCoverage
|
|
|
252 |
* @private
|
|
|
253 |
*/
|
|
|
254 |
_parseCoverage: function() {
|
|
|
255 |
var coverage = Y.Test.Runner.getCoverage();
|
|
|
256 |
if (!coverage) {
|
|
|
257 |
return;
|
|
|
258 |
}
|
|
|
259 |
if (this._isIstanbul(coverage)) {
|
|
|
260 |
this.parseIstanbul(coverage);
|
|
|
261 |
} else {
|
|
|
262 |
this.parseYUITestCoverage(coverage);
|
|
|
263 |
}
|
|
|
264 |
},
|
|
|
265 |
|
|
|
266 |
// -- Protected Event Handlers ---------------------------------------------
|
|
|
267 |
_onEntry: function (e) {
|
|
|
268 |
var msg = e.message;
|
|
|
269 |
|
|
|
270 |
if (msg.category === 'info'
|
|
|
271 |
&& /\s(?:case|suite)\s|yuitests\d+|began/.test(msg.message)) {
|
|
|
272 |
msg.category = 'status';
|
|
|
273 |
} else if (msg.category === 'fail') {
|
|
|
274 |
this.printBuffer();
|
|
|
275 |
}
|
|
|
276 |
}
|
|
|
277 |
}, {
|
|
|
278 |
NAME: 'testConsole',
|
|
|
279 |
|
|
|
280 |
ATTRS: {
|
|
|
281 |
entryTemplate: {
|
|
|
282 |
value:
|
|
|
283 |
'<div class="{entry_class} {cat_class} {src_class}">' +
|
|
|
284 |
'<div class="{entry_content_class}">{message}</div>' +
|
|
|
285 |
'</div>'
|
|
|
286 |
},
|
|
|
287 |
|
|
|
288 |
height: {
|
|
|
289 |
value: '350px'
|
|
|
290 |
},
|
|
|
291 |
|
|
|
292 |
newestOnTop: {
|
|
|
293 |
value: false
|
|
|
294 |
},
|
|
|
295 |
|
|
|
296 |
style: {
|
|
|
297 |
value: 'block'
|
|
|
298 |
},
|
|
|
299 |
|
|
|
300 |
width: {
|
|
|
301 |
value: Y.UA.ie && Y.UA.ie < 9 ? '100%' : 'inherit'
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
});
|
|
|
305 |
|
|
|
306 |
|
|
|
307 |
}, '3.18.1', {"requires": ["console-filters", "test", "array-extras"], "skinnable": true});
|