Code coverage report for preceptor-reporter/lib/reporter/tap.js

Statements: 95.74% (45 / 47)      Branches: 63.33% (19 / 30)      Functions: 100% (6 / 6)      Lines: 95.74% (45 / 47)      Ignored: none     

All files » preceptor-reporter/lib/reporter/ » tap.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      1 1             1                 1   1 1   1                         2       2   2     2     2 2   2 2   2 2   2 2   2 2   2 2   2 2     2                         36                                 30   30 12   18     30   30   30 6 24 4     30                             40   40 30       10   10 38           1  
// Copyright 2014, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
 
var AbstractReporter = require('../abstractReporter');
var _ = require('underscore');
 
/**
 * @class TapReporter
 * @extends AbstractReporter
 * @constructor
 */
var TapReporter = AbstractReporter.extend(
 
	{
		/**
		 * Initializes the instance
		 *
		 * @method initialize
		 */
		initialize: function () {
			this.__super();
 
			Eif (this.getOptions().progress === undefined) {
				this.getOptions().progress = false;
			}
			Iif (this.getOptions().output === undefined) {
				this.getOptions().output = false;
			}
		},
 
 
		/**
		 * Gets the collected output
		 *
		 * @method getOutput
		 * @return {string}
		 */
		getOutput: function () {
			var tests = [],
				tree = this.getContainer().getTree(),
				numbers = this.getContainer().gatherTestOutcomes(tree);
 
			this._buildTestResults(tests, '', tree);
 
			Iif (tests.length === 0) {
				tests.unshift('1..0 # SKIP Nothing here!')
			} else {
				tests.unshift('1..' + tests.length);
			}
 
			Eif (numbers.tests) {
				tests.push('# tests ' + numbers.tests);
			}
			Eif (numbers.failed) {
				tests.push('# fail ' + numbers.failed);
			}
			Eif (numbers.error) {
				tests.push('# error ' + numbers.error);
			}
			Eif (numbers.undef) {
				tests.push('# undefined ' + numbers.undef);
			}
			Eif (numbers.skipped) {
				tests.push('# skipped ' + numbers.skipped);
			}
			Eif (numbers.incomplete) {
				tests.push('# incomplete ' + numbers.incomplete);
			}
			Eif (numbers.passed) {
				tests.push('# passed ' + numbers.passed);
			}
 
			return tests.join("\n") + "\n";
		},
 
 
		/**
		 * Escape message
		 *
		 * @method _escape
		 * @param {string} msg
		 * @return {string}
		 * @private
		 */
		_escape: function (msg) {
			return (msg + '').
				replace(/#/g, "").
				replace(/\n/g, "\\n").
				replace(/\r/g, "\\r");
		},
 
 
		/**
		 * Adds a test result to the list
		 *
		 * @method _addTestResult
		 * @param {string[]} testResults
		 * @param {string} parentTitle
		 * @param {object} test
		 * @private
		 */
		_addTestResult: function (testResults, parentTitle, test) {
			var line = [];
 
			if (['passed', 'skipped', 'incomplete'].indexOf(test.outcome) === -1) {
				line.push('not ok');
			} else {
				line.push('ok');
			}
 
			line.push(testResults.length + 1);
 
			line.push(this._escape(parentTitle + ' ' + test.name));
 
			if (test.outcome === 'skipped') {
				line.push('# SKIP ' + this._escape(test.reason));
			} else if (test.outcome === 'incomplete') {
				line.push('# TODO -');
			}
 
			testResults.push(line.join(' '));
		},
 
 
		/**
		 * Build all test results from the node downwards
		 *
		 * @method _buildTestResults
		 * @param {string[]} testResults
		 * @param {string} parentTitle
		 * @param {object} treeNode
		 * @private
		 */
		_buildTestResults: function (testResults, parentTitle, treeNode) {
 
			var title;
 
			if (treeNode.type === 'test') {
				this._addTestResult(testResults, parentTitle, treeNode);
 
			} else {
 
				title = parentTitle + ' ' + (treeNode.name || 'Tests');
 
				_.each(treeNode.children, function (child) {
					this._buildTestResults(testResults, title, child);
				}, this);
			}
		}
	});
 
module.exports = TapReporter;