michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, you can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var EXPORTED_SYMBOLS = ['Assert', 'Expect']; michael@0: michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: var broker = {}; Cu.import('resource://mozmill/driver/msgbroker.js', broker); michael@0: var errors = {}; Cu.import('resource://mozmill/modules/errors.js', errors); michael@0: var stack = {}; Cu.import('resource://mozmill/modules/stack.js', stack); michael@0: michael@0: /** michael@0: * @name assertions michael@0: * @namespace Defines expect and assert methods to be used for assertions. michael@0: */ michael@0: michael@0: /** michael@0: * The Assert class implements fatal assertions, and can be used in cases michael@0: * when a failing test has to directly abort the current test function. All michael@0: * remaining tasks will not be performed. michael@0: * michael@0: */ michael@0: var Assert = function () {} michael@0: michael@0: Assert.prototype = { michael@0: michael@0: // The following deepEquals implementation is from Narwhal under this license: michael@0: michael@0: // http://wiki.commonjs.org/wiki/Unit_Testing/1.0 michael@0: // michael@0: // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8! michael@0: // michael@0: // Originally from narwhal.js (http://narwhaljs.org) michael@0: // Copyright (c) 2009 Thomas Robinson <280north.com> michael@0: // michael@0: // Permission is hereby granted, free of charge, to any person obtaining a copy michael@0: // of this software and associated documentation files (the 'Software'), to michael@0: // deal in the Software without restriction, including without limitation the michael@0: // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or michael@0: // sell copies of the Software, and to permit persons to whom the Software is michael@0: // furnished to do so, subject to the following conditions: michael@0: // michael@0: // The above copyright notice and this permission notice shall be included in michael@0: // all copies or substantial portions of the Software. michael@0: // michael@0: // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR michael@0: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, michael@0: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE michael@0: // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN michael@0: // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION michael@0: // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. michael@0: michael@0: _deepEqual: function (actual, expected) { michael@0: // 7.1. All identical values are equivalent, as determined by ===. michael@0: if (actual === expected) { michael@0: return true; michael@0: michael@0: // 7.2. If the expected value is a Date object, the actual value is michael@0: // equivalent if it is also a Date object that refers to the same time. michael@0: } else if (actual instanceof Date && expected instanceof Date) { michael@0: return actual.getTime() === expected.getTime(); michael@0: michael@0: // 7.3. Other pairs that do not both pass typeof value == 'object', michael@0: // equivalence is determined by ==. michael@0: } else if (typeof actual != 'object' && typeof expected != 'object') { michael@0: return actual == expected; michael@0: michael@0: // 7.4. For all other Object pairs, including Array objects, equivalence is michael@0: // determined by having the same number of owned properties (as verified michael@0: // with Object.prototype.hasOwnProperty.call), the same set of keys michael@0: // (although not necessarily the same order), equivalent values for every michael@0: // corresponding key, and an identical 'prototype' property. Note: this michael@0: // accounts for both named and indexed properties on Arrays. michael@0: } else { michael@0: return this._objEquiv(actual, expected); michael@0: } michael@0: }, michael@0: michael@0: _objEquiv: function (a, b) { michael@0: if (a == null || a == undefined || b == null || b == undefined) michael@0: return false; michael@0: // an identical 'prototype' property. michael@0: if (a.prototype !== b.prototype) return false; michael@0: michael@0: function isArguments(object) { michael@0: return Object.prototype.toString.call(object) == '[object Arguments]'; michael@0: } michael@0: michael@0: //~~~I've managed to break Object.keys through screwy arguments passing. michael@0: // Converting to array solves the problem. michael@0: if (isArguments(a)) { michael@0: if (!isArguments(b)) { michael@0: return false; michael@0: } michael@0: a = pSlice.call(a); michael@0: b = pSlice.call(b); michael@0: return _deepEqual(a, b); michael@0: } michael@0: try { michael@0: var ka = Object.keys(a), michael@0: kb = Object.keys(b), michael@0: key, i; michael@0: } catch (e) {//happens when one is a string literal and the other isn't michael@0: return false; michael@0: } michael@0: // having the same number of owned properties (keys incorporates michael@0: // hasOwnProperty) michael@0: if (ka.length != kb.length) michael@0: return false; michael@0: //the same set of keys (although not necessarily the same order), michael@0: ka.sort(); michael@0: kb.sort(); michael@0: //~~~cheap key test michael@0: for (i = ka.length - 1; i >= 0; i--) { michael@0: if (ka[i] != kb[i]) michael@0: return false; michael@0: } michael@0: //equivalent values for every corresponding key, and michael@0: //~~~possibly expensive deep test michael@0: for (i = ka.length - 1; i >= 0; i--) { michael@0: key = ka[i]; michael@0: if (!this._deepEqual(a[key], b[key])) return false; michael@0: } michael@0: return true; michael@0: }, michael@0: michael@0: _expectedException : function Assert__expectedException(actual, expected) { michael@0: if (!actual || !expected) { michael@0: return false; michael@0: } michael@0: michael@0: if (expected instanceof RegExp) { michael@0: return expected.test(actual); michael@0: } else if (actual instanceof expected) { michael@0: return true; michael@0: } else if (expected.call({}, actual) === true) { michael@0: return true; michael@0: } else if (actual.name === expected.name) { michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: }, michael@0: michael@0: /** michael@0: * Log a test as failing by throwing an AssertionException. michael@0: * michael@0: * @param {object} aResult michael@0: * Test result details used for reporting. michael@0: *
michael@0: *
fileName
michael@0: *
Name of the file in which the assertion failed.
michael@0: *
functionName
michael@0: *
Function in which the assertion failed.
michael@0: *
lineNumber
michael@0: *
Line number of the file in which the assertion failed.
michael@0: *
message
michael@0: *
Message why the assertion failed.
michael@0: *
michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: */ michael@0: _logFail: function Assert__logFail(aResult) { michael@0: throw new errors.AssertionError(aResult.message, michael@0: aResult.fileName, michael@0: aResult.lineNumber, michael@0: aResult.functionName, michael@0: aResult.name); michael@0: }, michael@0: michael@0: /** michael@0: * Log a test as passing by adding a pass frame. michael@0: * michael@0: * @param {object} aResult michael@0: * Test result details used for reporting. michael@0: *
michael@0: *
fileName
michael@0: *
Name of the file in which the assertion failed.
michael@0: *
functionName
michael@0: *
Function in which the assertion failed.
michael@0: *
lineNumber
michael@0: *
Line number of the file in which the assertion failed.
michael@0: *
message
michael@0: *
Message why the assertion failed.
michael@0: *
michael@0: */ michael@0: _logPass: function Assert__logPass(aResult) { michael@0: broker.pass({pass: aResult}); michael@0: }, michael@0: michael@0: /** michael@0: * Test the condition and mark test as passed or failed michael@0: * michael@0: * @param {boolean} aCondition michael@0: * Condition to test. michael@0: * @param {string} aMessage michael@0: * Message to show for the test result michael@0: * @param {string} aDiagnosis michael@0: * Diagnose message to show for the test result michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {boolean} Result of the test. michael@0: */ michael@0: _test: function Assert__test(aCondition, aMessage, aDiagnosis) { michael@0: let diagnosis = aDiagnosis || ""; michael@0: let message = aMessage || ""; michael@0: michael@0: if (diagnosis) michael@0: message = aMessage ? message + " - " + diagnosis : diagnosis; michael@0: michael@0: // Build result data michael@0: let frame = stack.findCallerFrame(Components.stack); michael@0: michael@0: let result = { michael@0: 'fileName' : frame.filename.replace(/(.*)-> /, ""), michael@0: 'functionName' : frame.name, michael@0: 'lineNumber' : frame.lineNumber, michael@0: 'message' : message michael@0: }; michael@0: michael@0: // Log test result michael@0: if (aCondition) { michael@0: this._logPass(result); michael@0: } michael@0: else { michael@0: result.stack = Components.stack; michael@0: this._logFail(result); michael@0: } michael@0: michael@0: return aCondition; michael@0: }, michael@0: michael@0: /** michael@0: * Perform an always passing test michael@0: * michael@0: * @param {string} aMessage michael@0: * Message to show for the test result. michael@0: * @returns {boolean} Always returns true. michael@0: */ michael@0: pass: function Assert_pass(aMessage) { michael@0: return this._test(true, aMessage, undefined); michael@0: }, michael@0: michael@0: /** michael@0: * Perform an always failing test michael@0: * michael@0: * @param {string} aMessage michael@0: * Message to show for the test result. michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {boolean} Always returns false. michael@0: */ michael@0: fail: function Assert_fail(aMessage) { michael@0: return this._test(false, aMessage, undefined); michael@0: }, michael@0: michael@0: /** michael@0: * Test if the value pass michael@0: * michael@0: * @param {boolean|string|number|object} aValue michael@0: * Value to test. michael@0: * @param {string} aMessage michael@0: * Message to show for the test result. michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {boolean} Result of the test. michael@0: */ michael@0: ok: function Assert_ok(aValue, aMessage) { michael@0: let condition = !!aValue; michael@0: let diagnosis = "got '" + aValue + "'"; michael@0: michael@0: return this._test(condition, aMessage, diagnosis); michael@0: }, michael@0: michael@0: /** michael@0: * Test if both specified values are identical. michael@0: * michael@0: * @param {boolean|string|number|object} aValue michael@0: * Value to test. michael@0: * @param {boolean|string|number|object} aExpected michael@0: * Value to strictly compare with. michael@0: * @param {string} aMessage michael@0: * Message to show for the test result michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {boolean} Result of the test. michael@0: */ michael@0: equal: function Assert_equal(aValue, aExpected, aMessage) { michael@0: let condition = (aValue === aExpected); michael@0: let diagnosis = "'" + aValue + "' should equal '" + aExpected + "'"; michael@0: michael@0: return this._test(condition, aMessage, diagnosis); michael@0: }, michael@0: michael@0: /** michael@0: * Test if both specified values are not identical. michael@0: * michael@0: * @param {boolean|string|number|object} aValue michael@0: * Value to test. michael@0: * @param {boolean|string|number|object} aExpected michael@0: * Value to strictly compare with. michael@0: * @param {string} aMessage michael@0: * Message to show for the test result michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {boolean} Result of the test. michael@0: */ michael@0: notEqual: function Assert_notEqual(aValue, aExpected, aMessage) { michael@0: let condition = (aValue !== aExpected); michael@0: let diagnosis = "'" + aValue + "' should not equal '" + aExpected + "'"; michael@0: michael@0: return this._test(condition, aMessage, diagnosis); michael@0: }, michael@0: michael@0: /** michael@0: * Test if an object equals another object michael@0: * michael@0: * @param {object} aValue michael@0: * The object to test. michael@0: * @param {object} aExpected michael@0: * The object to strictly compare with. michael@0: * @param {string} aMessage michael@0: * Message to show for the test result michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {boolean} Result of the test. michael@0: */ michael@0: deepEqual: function equal(aValue, aExpected, aMessage) { michael@0: let condition = this._deepEqual(aValue, aExpected); michael@0: try { michael@0: var aValueString = JSON.stringify(aValue); michael@0: } catch (e) { michael@0: var aValueString = String(aValue); michael@0: } michael@0: try { michael@0: var aExpectedString = JSON.stringify(aExpected); michael@0: } catch (e) { michael@0: var aExpectedString = String(aExpected); michael@0: } michael@0: michael@0: let diagnosis = "'" + aValueString + "' should equal '" + michael@0: aExpectedString + "'"; michael@0: michael@0: return this._test(condition, aMessage, diagnosis); michael@0: }, michael@0: michael@0: /** michael@0: * Test if an object does not equal another object michael@0: * michael@0: * @param {object} aValue michael@0: * The object to test. michael@0: * @param {object} aExpected michael@0: * The object to strictly compare with. michael@0: * @param {string} aMessage michael@0: * Message to show for the test result michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {boolean} Result of the test. michael@0: */ michael@0: notDeepEqual: function notEqual(aValue, aExpected, aMessage) { michael@0: let condition = !this._deepEqual(aValue, aExpected); michael@0: try { michael@0: var aValueString = JSON.stringify(aValue); michael@0: } catch (e) { michael@0: var aValueString = String(aValue); michael@0: } michael@0: try { michael@0: var aExpectedString = JSON.stringify(aExpected); michael@0: } catch (e) { michael@0: var aExpectedString = String(aExpected); michael@0: } michael@0: michael@0: let diagnosis = "'" + aValueString + "' should not equal '" + michael@0: aExpectedString + "'"; michael@0: michael@0: return this._test(condition, aMessage, diagnosis); michael@0: }, michael@0: michael@0: /** michael@0: * Test if the regular expression matches the string. michael@0: * michael@0: * @param {string} aString michael@0: * String to test. michael@0: * @param {RegEx} aRegex michael@0: * Regular expression to use for testing that a match exists. michael@0: * @param {string} aMessage michael@0: * Message to show for the test result michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {boolean} Result of the test. michael@0: */ michael@0: match: function Assert_match(aString, aRegex, aMessage) { michael@0: // XXX Bug 634948 michael@0: // Regex objects are transformed to strings when evaluated in a sandbox michael@0: // For now lets re-create the regex from its string representation michael@0: let pattern = flags = ""; michael@0: try { michael@0: let matches = aRegex.toString().match(/\/(.*)\/(.*)/); michael@0: michael@0: pattern = matches[1]; michael@0: flags = matches[2]; michael@0: } catch (e) { michael@0: } michael@0: michael@0: let regex = new RegExp(pattern, flags); michael@0: let condition = (aString.match(regex) !== null); michael@0: let diagnosis = "'" + regex + "' matches for '" + aString + "'"; michael@0: michael@0: return this._test(condition, aMessage, diagnosis); michael@0: }, michael@0: michael@0: /** michael@0: * Test if the regular expression does not match the string. michael@0: * michael@0: * @param {string} aString michael@0: * String to test. michael@0: * @param {RegEx} aRegex michael@0: * Regular expression to use for testing that a match does not exist. michael@0: * @param {string} aMessage michael@0: * Message to show for the test result michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {boolean} Result of the test. michael@0: */ michael@0: notMatch: function Assert_notMatch(aString, aRegex, aMessage) { michael@0: // XXX Bug 634948 michael@0: // Regex objects are transformed to strings when evaluated in a sandbox michael@0: // For now lets re-create the regex from its string representation michael@0: let pattern = flags = ""; michael@0: try { michael@0: let matches = aRegex.toString().match(/\/(.*)\/(.*)/); michael@0: michael@0: pattern = matches[1]; michael@0: flags = matches[2]; michael@0: } catch (e) { michael@0: } michael@0: michael@0: let regex = new RegExp(pattern, flags); michael@0: let condition = (aString.match(regex) === null); michael@0: let diagnosis = "'" + regex + "' doesn't match for '" + aString + "'"; michael@0: michael@0: return this._test(condition, aMessage, diagnosis); michael@0: }, michael@0: michael@0: michael@0: /** michael@0: * Test if a code block throws an exception. michael@0: * michael@0: * @param {string} block michael@0: * function to call to test for exception michael@0: * @param {RegEx} error michael@0: * the expected error class michael@0: * @param {string} message michael@0: * message to present if assertion fails michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {boolean} Result of the test. michael@0: */ michael@0: throws : function Assert_throws(block, /*optional*/error, /*optional*/message) { michael@0: return this._throws.apply(this, [true].concat(Array.prototype.slice.call(arguments))); michael@0: }, michael@0: michael@0: /** michael@0: * Test if a code block doesn't throw an exception. michael@0: * michael@0: * @param {string} block michael@0: * function to call to test for exception michael@0: * @param {RegEx} error michael@0: * the expected error class michael@0: * @param {string} message michael@0: * message to present if assertion fails michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {boolean} Result of the test. michael@0: */ michael@0: doesNotThrow : function Assert_doesNotThrow(block, /*optional*/error, /*optional*/message) { michael@0: return this._throws.apply(this, [false].concat(Array.prototype.slice.call(arguments))); michael@0: }, michael@0: michael@0: /* Tests whether a code block throws the expected exception michael@0: class. helper for throws() and doesNotThrow() michael@0: michael@0: adapted from node.js's assert._throws() michael@0: https://github.com/joyent/node/blob/master/lib/assert.js michael@0: */ michael@0: _throws : function Assert__throws(shouldThrow, block, expected, message) { michael@0: var actual; michael@0: michael@0: if (typeof expected === 'string') { michael@0: message = expected; michael@0: expected = null; michael@0: } michael@0: michael@0: try { michael@0: block(); michael@0: } catch (e) { michael@0: actual = e; michael@0: } michael@0: michael@0: message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + michael@0: (message ? ' ' + message : '.'); michael@0: michael@0: if (shouldThrow && !actual) { michael@0: return this._test(false, message, 'Missing expected exception'); michael@0: } michael@0: michael@0: if (!shouldThrow && this._expectedException(actual, expected)) { michael@0: return this._test(false, message, 'Got unwanted exception'); michael@0: } michael@0: michael@0: if ((shouldThrow && actual && expected && michael@0: !this._expectedException(actual, expected)) || (!shouldThrow && actual)) { michael@0: throw actual; michael@0: } michael@0: michael@0: return this._test(true, message); michael@0: }, michael@0: michael@0: /** michael@0: * Test if the string contains the pattern. michael@0: * michael@0: * @param {String} aString String to test. michael@0: * @param {String} aPattern Pattern to look for in the string michael@0: * @param {String} aMessage Message to show for the test result michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {Boolean} Result of the test. michael@0: */ michael@0: contain: function Assert_contain(aString, aPattern, aMessage) { michael@0: let condition = (aString.indexOf(aPattern) !== -1); michael@0: let diagnosis = "'" + aString + "' should contain '" + aPattern + "'"; michael@0: michael@0: return this._test(condition, aMessage, diagnosis); michael@0: }, michael@0: michael@0: /** michael@0: * Test if the string does not contain the pattern. michael@0: * michael@0: * @param {String} aString String to test. michael@0: * @param {String} aPattern Pattern to look for in the string michael@0: * @param {String} aMessage Message to show for the test result michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {Boolean} Result of the test. michael@0: */ michael@0: notContain: function Assert_notContain(aString, aPattern, aMessage) { michael@0: let condition = (aString.indexOf(aPattern) === -1); michael@0: let diagnosis = "'" + aString + "' should not contain '" + aPattern + "'"; michael@0: michael@0: return this._test(condition, aMessage, diagnosis); michael@0: }, michael@0: michael@0: /** michael@0: * Waits for the callback evaluates to true michael@0: * michael@0: * @param {Function} aCallback michael@0: * Callback for evaluation michael@0: * @param {String} aMessage michael@0: * Message to show for result michael@0: * @param {Number} aTimeout michael@0: * Timeout in waiting for evaluation michael@0: * @param {Number} aInterval michael@0: * Interval between evaluation attempts michael@0: * @param {Object} aThisObject michael@0: * this object michael@0: * @throws {errors.AssertionError} michael@0: * michael@0: * @returns {Boolean} Result of the test. michael@0: */ michael@0: waitFor: function Assert_waitFor(aCallback, aMessage, aTimeout, aInterval, aThisObject) { michael@0: var timeout = aTimeout || 5000; michael@0: var interval = aInterval || 100; michael@0: michael@0: var self = { michael@0: timeIsUp: false, michael@0: result: aCallback.call(aThisObject) michael@0: }; michael@0: var deadline = Date.now() + timeout; michael@0: michael@0: function wait() { michael@0: if (self.result !== true) { michael@0: self.result = aCallback.call(aThisObject); michael@0: self.timeIsUp = Date.now() > deadline; michael@0: } michael@0: } michael@0: michael@0: var hwindow = Services.appShell.hiddenDOMWindow; michael@0: var timeoutInterval = hwindow.setInterval(wait, interval); michael@0: var thread = Services.tm.currentThread; michael@0: michael@0: while (self.result !== true && !self.timeIsUp) { michael@0: thread.processNextEvent(true); michael@0: michael@0: let type = typeof(self.result); michael@0: if (type !== 'boolean') michael@0: throw TypeError("waitFor() callback has to return a boolean" + michael@0: " instead of '" + type + "'"); michael@0: } michael@0: michael@0: hwindow.clearInterval(timeoutInterval); michael@0: michael@0: if (self.result !== true && self.timeIsUp) { michael@0: aMessage = aMessage || arguments.callee.name + ": Timeout exceeded for '" + aCallback + "'"; michael@0: throw new errors.TimeoutError(aMessage); michael@0: } michael@0: michael@0: broker.pass({'function':'assert.waitFor()'}); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: /* non-fatal assertions */ michael@0: var Expect = function () {} michael@0: michael@0: Expect.prototype = new Assert(); michael@0: michael@0: /** michael@0: * Log a test as failing by adding a fail frame. michael@0: * michael@0: * @param {object} aResult michael@0: * Test result details used for reporting. michael@0: *
michael@0: *
fileName
michael@0: *
Name of the file in which the assertion failed.
michael@0: *
functionName
michael@0: *
Function in which the assertion failed.
michael@0: *
lineNumber
michael@0: *
Line number of the file in which the assertion failed.
michael@0: *
message
michael@0: *
Message why the assertion failed.
michael@0: *
michael@0: */ michael@0: Expect.prototype._logFail = function Expect__logFail(aResult) { michael@0: broker.fail({fail: aResult}); michael@0: } michael@0: michael@0: /** michael@0: * Waits for the callback evaluates to true michael@0: * michael@0: * @param {Function} aCallback michael@0: * Callback for evaluation michael@0: * @param {String} aMessage michael@0: * Message to show for result michael@0: * @param {Number} aTimeout michael@0: * Timeout in waiting for evaluation michael@0: * @param {Number} aInterval michael@0: * Interval between evaluation attempts michael@0: * @param {Object} aThisObject michael@0: * this object michael@0: */ michael@0: Expect.prototype.waitFor = function Expect_waitFor(aCallback, aMessage, aTimeout, aInterval, aThisObject) { michael@0: let condition = true; michael@0: let message = aMessage; michael@0: michael@0: try { michael@0: Assert.prototype.waitFor.apply(this, arguments); michael@0: } michael@0: catch (ex if ex instanceof errors.AssertionError) { michael@0: message = ex.message; michael@0: condition = false; michael@0: } michael@0: michael@0: return this._test(condition, message); michael@0: }