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

Statements: 96.67% (58 / 60)      Branches: 88.46% (23 / 26)      Functions: 96.88% (31 / 32)      Lines: 96.67% (58 / 60)      Ignored: none     

All files » preceptor-reporter/lib/ » abstractReporter.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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412      1 1 1 1   1                 1                   41   41 41   41                   41                         1391                   363                   2                   580                   23                   126                   375                   375                         352     352   352 218 218   134 20   114       352 229                     244                                                         23   23   15 15 15       23                                                                                                                                                                                                                                                                                 11             11 11 55 209 209 44   209     11 11 11   11 451   451 451 451 144         11                                       23 23 4                           1  
// Copyright 2014, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
 
var Base = require('preceptor-core').Base;
var utils = require('preceptor-core').utils;
var fs = require('fs');
var _ = require('underscore');
 
var defaultsReporter = require('./defaults/reporter');
 
/**
 * @class AbstractReporter
 * @extends Base
 *
 * @property {ReportContainer} _container
 * @property {object} _options
 */
var AbstractReporter = Base.extend(
 
	/**
	 * Abstract reporter constructor
	 *
	 * @param {ReportContainer} container
	 * @param {object} options
	 * @constructor
	 */
	function (container, options) {
		this.__super();
 
		this._container = container;
		this._options = utils.deepExtend({}, [defaultsReporter, options || {}]);
 
		this.initialize();
	},
 
	{
		/**
		 * Initializes the instance
		 *
		 * @method initialize
		 */
		initialize: function () {
			Iif (this.getOptions().color === undefined) {
				this.getOptions().color = true;
			}
		},
 
 
		/**
		 * Gets the options
		 *
		 * @method getOptions
		 * @return {object}
		 */
		getOptions: function () {
			return this._options;
		},
 
		/**
		 * Gets the type of reporter
		 *
		 * @method getType
		 * @return {string}
		 */
		getType: function () {
			return this.getOptions().type;
		},
 
		/**
		 * Gets the configuration supplied
		 *
		 * @method getConfiguration
		 * @return {object}
		 */
		getConfiguration: function () {
			return this.getOptions().configuration;
		},
 
		/**
		 * Gets the container
		 *
		 * @method getContainer
		 * @return {ReportContainer}
		 */
		getContainer: function () {
			return this._container;
		},
 
		/**
		 * Output path
		 *
		 * @method getPath
		 * @return {string}
		 */
		getPath: function () {
			return this.getOptions().path;
		},
 
		/**
		 * Should reporter use color?
		 *
		 * @method useColor
		 * @return {boolean}
		 */
		useColor: function () {
			return !!this.getOptions().color;
		},
 
		/**
		 * Should the reporter output the data?
		 *
		 * @method shouldOutput
		 * @return {boolean}
		 */
		shouldOutput: function () {
			return !!this.getOptions().output;
		},
 
		/**
		 * Should the reporter show progress?
		 *
		 * @method shouldShowProgress
		 * @return {boolean}
		 */
		shouldShowProgress: function () {
			return !!this.getOptions().progress;
		},
 
 
		/**
		 * Sends data to stdout
		 *
		 * @method console
		 * @param {string} id
		 * @param {string} msgType
		 * @param {string} msg
		 */
		console: function (id, msgType, msg) {
			var type = this.getType(),
				action;
 
			action = this.getContainer().getAction(id);
 
			if (!action.output[msgType]) {
				action.output[msgType] = {};
				action.output[msgType][type] = [msg];
			} else {
				if (!action.output[msgType][type]) {
					action.output[msgType][type] = [msg];
				} else {
					action.output[msgType][type].push(msg);
				}
			}
 
			if (this.shouldOutput() && this.shouldShowProgress()) {
				this.output(msg);
			}
		},
 
		/**
		 * Outputs data to stdout
		 *
		 * @method output
		 * @param {string} data
		 */
		output: function (data) {
			process.stdout.write(data);
		},
 
 
		/**
		 * Called when reporting starts
		 *
		 * @method start
		 */
		start: function () {
			// Do nothing
		},
 
		/**
		 * Called when reporting stops
		 *
		 * @method stop
		 */
		stop: function () {
			// Do nothing
		},
 
 
		/**
		 * Reporting is completed
		 *
		 * @method complete
		 */
		complete: function () {
			var output;
 
			if (this.shouldOutput() && !this.shouldShowProgress()) {
 
				output = this.getOutput();
				Eif (output.length > 0) {
					this.output(output);
				}
			}
 
			this.write();
		},
 
 
		/**
		 * Called when suite starts
		 *
		 * @method suiteStart
		 * @param {string} id
		 * @param {string} parentId
		 * @param {string} suiteName
		 */
		suiteStart: function (id, parentId, suiteName) {
			// Do nothing
		},
 
		/**
		 * Called when suite ends
		 *
		 * @method suiteEnd
		 * @param {string} id
		 */
		suiteEnd: function (id) {
			// Do nothing
		},
 
 
		/**
		 * Called when any item has custom data
		 *
		 * @method itemData
		 * @param {string} id
		 * @param {string} json JSON-data
		 */
		itemData: function (id, json) {
			// Do nothing
		},
 
		/**
		 * Called when any item has a custom message
		 *
		 * @method itemMessage
		 * @param {string} id
		 * @param {string} message
		 */
		itemMessage: function (id, message) {
			// Do nothing
		},
 
 
		/**
		 * Called when test starts
		 *
		 * @method testStart
		 * @param {string} id
		 * @param {string} parentId
		 * @param {string} testName
		 */
		testStart: function (id, parentId, testName) {
			// Do nothing
		},
 
 
		/**
		 * Called when test fails
		 *
		 * @method testFailed
		 * @param {string} id
		 * @param {string} [message]
		 * @param {string} [reason]
		 */
		testFailed: function (id, message, reason) {
			// Do nothing
		},
 
		/**
		 * Called when test has an error
		 *
		 * @method testError
		 * @param {string} id
		 * @param {string} [message]
		 * @param {string} [reason]
		 */
		testError: function (id, message, reason) {
			// Do nothing
		},
 
		/**
		 * Called when test has passed
		 *
		 * @method testPassed
		 * @param {string} id
		 */
		testPassed: function (id) {
			// Do nothing
		},
 
		/**
		 * Called when test is undefined
		 *
		 * @method testUndefined
		 * @param {string} id
		 */
		testUndefined: function (id) {
			// Do nothing
		},
 
		/**
		 * Called when test is skipped
		 *
		 * @method testSkipped
		 * @param {string} id
		 * @param {string} [reason]
		 */
		testSkipped: function (id, reason) {
			// Do nothing
		},
 
		/**
		 * Called when test is incomplete
		 *
		 * @method testIncomplete
		 * @param {string} id
		 */
		testIncomplete: function (id) {
			// Do nothing
		},
 
 
		/**
		 * Gets the collected output
		 *
		 * @method getOutput
		 * @return {string}
		 */
		getOutput: function () {
 
			var container = this.getContainer(),
				type = this.getType(),
				actionIds = [],
				output = [],
				fn;
 
			// Gather all node-ids in tree in-order
			actionIds.push([undefined, "start"]);
			fn = function (children) {
				_.each(children, function (child) {
					actionIds.push([child.id, 'start']);
					if (child.children) {
						fn(child.children);
					}
					actionIds.push([child.id, 'end']);
				});
			};
			fn(container.getTree().children);
			actionIds.push([undefined, "stop"]);
			actionIds.push([undefined, "complete"]);
 
			_.each(actionIds, function (id) {
				var action, types;
 
				action = container.getAction(id[0]);
				types = action.output[id[1]];
				if (types) {
					output = output.concat(types[type] || []);
				}
 
			}, this);
 
			return output.join('');
		},
 
 
		/**
		 * Exports data to a string
		 *
		 * @method toString
		 * @return {string}
		 */
		toString: function () {
			return this.getOutput();
		},
 
		/**
		 * Writes the data into an output file
		 *
		 * @method write
		 */
		write: function () {
			var path = this.getPath();
			if (path) {
				fs.writeFileSync(path, this.getOutput());
			}
		}
	},
 
	{
		/**
		 * @property TYPE
		 * @type {string}
		 * @static
		 */
		TYPE: 'AbstractReporter'
	});
 
module.exports = AbstractReporter;