2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('test-console', function (Y, NAME) {
11 Provides a specialized log console widget that's pre-configured to display YUI
12 Test output with no extra configuration.
16 <div id="log" class="yui3-skin-sam"></div>
19 YUI().use('test-console', function (Y) {
20 // ... set up your test cases here ...
22 // Render the console inside the #log div, then run the tests.
23 new Y.Test.Console().render('#log');
34 @param {Object} [config] Config attributes.
35 @param {Object} [config.filters] Category filter configuration.
40 function TestConsole() {
41 TestConsole.superclass.constructor.apply(this, arguments);
44 Y.namespace('Test').Console = Y.extend(TestConsole, Y.Console, {
45 initializer: function (config) {
46 this.on('entry', this._onEntry);
48 this.plug(Y.Plugin.ConsoleFilters, {
54 }, (config && config.filters) || {}),
56 defaultVisibility: false,
63 Y.Test.Runner.on('complete', Y.bind(this._parseCoverage, this));
66 // -- Protected Coverage Parser ---------------------------------------------
68 * Scans the coverage data to determine if it's an Istanbul coverage object.
70 * @param {Object} json The coverage data to scan
71 * @return {Boolean} True if this is Istanbul Coverage
73 _isIstanbul: function(json) {
74 var first = Y.Object.keys(json)[0],
77 if (json[first].s !== undefined && json[first].fnMap !== undefined) {
81 if (json.s !== undefined && json.fnMap !== undefined) {
87 * Parses and logs a summary of YUITest coverage data.
88 * @method parseYUITest
89 * @param {Object} coverage The YUITest Coverage JSON data
91 parseYUITestCoverage: function (coverage) {
107 Y.Object.each(coverage, function(info) {
108 cov.lines.total += info.coveredLines;
109 cov.lines.hit += info.calledLines;
110 cov.lines.miss += (info.coveredLines - info.calledLines);
111 cov.lines.percent = Math.floor((cov.lines.hit / cov.lines.total) * 100);
113 cov.functions.total += info.coveredFunctions;
114 cov.functions.hit += info.calledFunctions;
115 cov.functions.miss += (info.coveredFunctions - info.calledFunctions);
116 cov.functions.percent = Math.floor((cov.functions.hit / cov.functions.total) * 100);
120 coverageLog = 'Lines: Hit:' + cov.lines.hit + ' Missed:' + cov.lines.miss + ' Total:' + cov.lines.total + ' Percent:' + cov.lines.percent + '%\n';
121 coverageLog += 'Functions: Hit:' + cov.functions.hit + ' Missed:' + cov.functions.miss + ' Total:' + cov.functions.total + ' Percent:' + cov.functions.percent + '%';
123 this.log('Coverage: ' + coverageLog, 'info', 'TestRunner');
126 * Generates a generic summary object used for Istanbul conversions.
127 * @method _blankSummary
128 * @return {Object} Generic summary object
130 _blankSummary: function () {
155 * Calculates line numbers from statement coverage
156 * @method _addDerivedInfoForFile
158 * @param {Object} fileCoverage JSON coverage data
160 _addDerivedInfoForFile: function (fileCoverage) {
161 var statementMap = fileCoverage.statementMap,
162 statements = fileCoverage.s,
165 if (!fileCoverage.l) {
166 fileCoverage.l = lineMap = {};
167 Y.Object.each(statements, function (value, st) {
168 var line = statementMap[st].start.line,
169 count = statements[st],
170 prevVal = lineMap[line];
171 if (typeof prevVal === 'undefined' || prevVal < count) {
172 lineMap[line] = count;
178 * Generic percent calculator
180 * @param {Number} covered The covered amount
181 * @param {Number} total The total
184 _percent: function (covered, total) {
185 var tmp, pct = 100.00;
187 tmp = 1000 * 100 * covered / total + 5;
188 pct = Math.floor(tmp / 10) / 100;
193 * Summarize simple properties in the coverage data
194 * @method _computSimpleTotals
196 * @param {Object} fileCoverage JSON coverage data
197 * @param {String} property The property to summarize
199 _computeSimpleTotals: function (fileCoverage, property) {
200 var stats = fileCoverage[property],
201 ret = { total: 0, covered: 0 };
203 Y.Object.each(stats, function(val) {
209 ret.pct = this._percent(ret.covered, ret.total);
213 * Noramlizes branch data from Istanbul
214 * @method _computeBranchTotals
216 * @param {Object} fileCoverage JSON coverage data
218 _computeBranchTotals: function (fileCoverage) {
219 var stats = fileCoverage.b,
220 ret = { total: 0, covered: 0 };
222 Y.Object.each(stats, function (branches) {
223 var covered = Y.Array.filter(branches, function (num) { return num > 0; });
224 ret.total += branches.length;
225 ret.covered += covered.length;
227 ret.pct = this._percent(ret.covered, ret.total);
231 * Takes an Istanbul coverage object, normalizes it and prints a log with a summary
232 * @method parseInstanbul
233 * @param {Object} coverage The coverage object to normalize and log
235 parseIstanbul: function (coverage) {
237 str = 'Coverage Report:\n';
239 Y.Object.each(coverage, function(fileCoverage, file) {
240 var ret = self._blankSummary();
242 self._addDerivedInfoForFile(fileCoverage);
243 ret.lines = self._computeSimpleTotals(fileCoverage, 'l');
244 ret.functions = self._computeSimpleTotals(fileCoverage, 'f');
245 ret.statements = self._computeSimpleTotals(fileCoverage, 's');
246 ret.branches = self._computeBranchTotals(fileCoverage);
248 Y.Array.each(['lines','functions','statements','branches'], function(key) {
249 str += ' ' + key +': ' + ret[key].covered + '/' + ret[key].total + ' : ' + ret[key].pct + '%\n';
253 this.log(str, 'info', 'TestRunner');
257 * Parses YUITest or Istanbul coverage results if they are available and logs them.
258 * @method _parseCoverage
261 _parseCoverage: function() {
262 var coverage = Y.Test.Runner.getCoverage();
266 if (this._isIstanbul(coverage)) {
267 this.parseIstanbul(coverage);
269 this.parseYUITestCoverage(coverage);
273 // -- Protected Event Handlers ---------------------------------------------
274 _onEntry: function (e) {
277 if (msg.category === 'info'
278 && /\s(?:case|suite)\s|yuitests\d+|began/.test(msg.message)) {
279 msg.category = 'status';
280 } else if (msg.category === 'fail') {
290 '<div class="{entry_class} {cat_class} {src_class}">' +
291 '<div class="{entry_content_class}">{message}</div>' +
308 value: Y.UA.ie && Y.UA.ie < 9 ? '100%' : 'inherit'
314 }, '3.13.0', {"requires": ["console-filters", "test", "array-extras"], "skinnable": true});