<commit_before>/// <reference path="../../../dojo" />
/// <reference path="../../intern" />

import Application = require('../../../Application');
import arrayUtil = require('dojo/_base/array');
import assert = require('intern/chai!assert');
import has = require('../../../has');
import LogLevel = require('../../../LogLevel');
import registerSuite = require('intern!object');

var app:Application;

registerSuite({
	name: 'mayhem/Application',

	afterEach() {
		app && app.destroy();
	},

	'#run': {
		simple() {
			app = new Application();
			var runPromise = app.run();

			return runPromise.then(function (result) {
				assert.strictEqual(result, app, 'app.run should resolve to the application instance');
				assert.strictEqual(app.run(), runPromise, 'app.run should return the same promise each call');
			}, function (error) {
				throw error;
			});
		},

		'with bad component module id'() {
			if (has('host-node')) {
				this.skip('require does not emit an error when loading a bad mid in Node.js');
			}

			// This test sometimes times out, so give it a long timeout value
			var dfd = this.async(10000);

			app = new Application({
				components: {
					test: {
						// TODO: require is broken and only throws an error on the first request for a mid that returns
						// a 404
						// For now, ensure that you don't reuse invalid mids in tests
						constructor: 'App1/bad/module/id'
					}
				}
			});

			app.run().then(function () {
				dfd.reject(new Error('app.run should reject when passed a bad module id'));
			}, function () {
				dfd.resolve();
			});
		}
	},

	'#handleError': {
		'with errorHandler'() {
			var receivedError:Error;

			app = new Application({
				errorHandler: {
					handleError(error:Error) {
						receivedError = error;
					}
				},
				components: {
					errorHandler: null
				}
			});

			return app.run().then(function () {
				var testError = new Error('test');

				app.handleError(testError);
				assert.strictEqual(receivedError, testError, 'error handler should receive passed Error object');
			});
		},

		'without errorHandler'() {
			var loggedLevel:number;

			app = new Application({
				logger: {
					log(message:string, level:number, category:string):void {
						loggedLevel = level;
					}
				},
				components: {
					errorHandler: null,
					logger: null
				}
			});

			return app.run().then(function () {
				app.handleError(new Error('test error'));
				assert.strictEqual(loggedLevel, LogLevel.ERROR, 'logger.log should be called with correct LogLevel');
			});
		}
	},

	'#log': {
		'with logger'() {
			var receivedArguments:any[];

			app = new Application({
				logger: {
					log(message:string, level:number, category:string):void {
						receivedArguments = Array.prototype.slice.call(arguments);
					}
				},
				components: {
					errorHandler: null,
					logger: null
				}
			});

			return app.run().then(function () {
				var logArguments = [ 'test message', LogLevel.LOG, 'test category'];

				app.log.apply(app, logArguments);
				assert.deepEqual(receivedArguments, logArguments, 'logger.log should be called with correct arguments');
			});
		},

		'without logger'() {
			app = new Application({
				components: {
					logger: null
				}
			});

			return app.run().then(function () {
				var testMessage = 'test message';
				var logMessage:any;
				var handle:IHandle;

				arrayUtil.forEach([ 'ERROR', 'WARN', 'LOG', 'INFO', 'DEBUG' ], function (logLevel) {
					var methodName = logLevel.toLowerCase();
					var originalMethod = console[methodName];

					// dojo/aspect does not work on console methods in IE8
					console[methodName] = function (message:any) {
						logMessage = message;
					};

					try {
						// TS7017
						app.log(testMessage, (<any> LogLevel)[logLevel]);

						assert.include(logMessage, testMessage,
							'console.' + methodName + ' should be called with correct arguments');
					}
					finally {
						console[methodName] = originalMethod;<commit_msg>Application: fix unit test type annotations and skip in IE8<commit_after>/// <reference path="../../../dojo" />
/// <reference path="../../intern" />

import Application = require('../../../Application');
import arrayUtil = require('dojo/_base/array');
import assert = require('intern/chai!assert');
import has = require('../../../has');
import LogLevel = require('../../../LogLevel');
import registerSuite = require('intern!object');

var app:Application;

registerSuite({
	name: 'mayhem/Application',

	afterEach() {
		app && app.destroy();
	},

	'#run': {
		simple() {
			app = new Application();
			var runPromise = app.run();

			return runPromise.then(function (result) {
				assert.strictEqual(result, app, 'app.run should resolve to the application instance');
				assert.strictEqual(app.run(), runPromise, 'app.run should return the same promise each call');
			}, function (error) {
				throw error;
			});
		},

		'with bad component module id'() {
			if (has('host-node')) {
				this.skip('require does not emit an error when loading a bad mid in Node.js');
			}

			// This test sometimes times out, so give it a long timeout value
			var dfd = this.async(10000);

			app = new Application({
				components: {
					test: {
						// TODO: require is broken and only throws an error on the first request for a mid that returns
						// a 404
						// For now, ensure that you don't reuse invalid mids in tests
						constructor: 'App1/bad/module/id'
					}
				}
			});

			app.run().then(function () {
				dfd.reject(new Error('app.run should reject when passed a bad module id'));
			}, function () {
				dfd.resolve();
			});
		}
	},

	'#handleError': {
		'with errorHandler'() {
			var receivedError:Error;

			app = new Application({
				errorHandler: {
					handleError(error:Error) {
						receivedError = error;
					}
				},
				components: {
					errorHandler: null
				}
			});

			return app.run().then(function () {
				var testError = new Error('test');

				app.handleError(testError);
				assert.strictEqual(receivedError, testError, 'error handler should receive passed Error object');
			});
		},

		'without errorHandler'() {
			var loggedLevel:number;

			app = new Application({
				logger: {
					log(message:string, level:number, category:string):void {
						loggedLevel = level;
					}
				},
				components: {
					errorHandler: null,
					logger: null
				}
			});

			return app.run().then(function () {
				app.handleError(new Error('test error'));
				assert.strictEqual(loggedLevel, LogLevel.ERROR, 'logger.log should be called with correct LogLevel');
			});
		}
	},

	'#log': {
		'with logger'() {
			var receivedArguments:any[];

			app = new Application({
				logger: {
					log(message:string, level:number, category:string):void {
						receivedArguments = Array.prototype.slice.call(arguments);
					}
				},
				components: {
					errorHandler: null,
					logger: null
				}
			});

			return app.run().then(function () {
				var logArguments = [ 'test message', LogLevel.LOG, 'test category'];

				app.log.apply(app, logArguments);
				assert.deepEqual(receivedArguments, logArguments, 'logger.log should be called with correct arguments');
			});
		},

		'without logger'() {
			if (typeof console === 'undefined') {
				this.skip('Test requires console object');
			}

			app = new Application({
				components: {
					logger: null
				}
			});

			return app.run().then(function () {
				var testMessage = 'test message';
				var logMessage:any;

				arrayUtil.forEach([ 'ERROR', 'WARN', 'LOG', 'INFO', 'DEBUG' ], function (logLevel) {
					var methodName = logLevel.toLowerCase();
					var originalMethod = (<any> console)[methodName];

					// dojo/aspect does not work on console methods in IE8
					(<any> console)[methodName] = function (message:any) {
						logMessage = message;
					};

					try {
						// TS7017
						app.log(testMessage, (<any> LogLevel)[logLevel]);

						assert.include(logMessage, testMessage,
							'console.' + methodName + ' should be called with correct arguments');
					}
					finally {
						(<any> console)[methodName] = originalMethod;
{'fraction_non_alphanumeric': 0.1489780842157104, 'fraction_numerical': 0.003570549125831076, 'mean_word_length': 2.9015369836695486, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3079064', 'n_tokens_mistral': 3103, 'n_tokens_neox': 2352, 'n_words': 651}