2016-11-01 01:00:21 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
|
|
|
|
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
2016-11-01 01:00:21 +03:00
|
|
|
|
2017-06-28 21:01:49 +03:00
|
|
|
const {
|
|
|
|
InvalidArgumentError,
|
|
|
|
InvalidSessionIDError,
|
2017-11-24 21:21:03 +03:00
|
|
|
JavaScriptError,
|
2017-06-28 21:01:49 +03:00
|
|
|
NoSuchWindowError,
|
|
|
|
UnexpectedAlertOpenError,
|
|
|
|
UnsupportedOperationError,
|
2019-01-17 21:18:31 +03:00
|
|
|
} = ChromeUtils.import("chrome://marionette/content/error.js");
|
|
|
|
const {pprint} = ChromeUtils.import("chrome://marionette/content/format.js");
|
2016-11-01 01:00:21 +03:00
|
|
|
|
2018-07-06 20:35:21 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
|
|
|
evaluate: "chrome://marionette/content/evaluate.js",
|
|
|
|
browser: "chrome://marionette/content/browser.js",
|
2018-01-12 17:25:30 +03:00
|
|
|
});
|
|
|
|
|
2016-11-01 01:00:21 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ["assert"];
|
|
|
|
|
|
|
|
const isFennec = () => AppConstants.platform == "android";
|
2017-10-12 14:21:17 +03:00
|
|
|
const isFirefox = () =>
|
|
|
|
Services.appinfo.ID == "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}";
|
2018-11-23 00:38:19 +03:00
|
|
|
const isThunderbird = () =>
|
|
|
|
Services.appinfo.ID == "{3550f703-e582-4d05-9a08-453d09bdfdc6}";
|
2016-11-01 01:00:21 +03:00
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/**
|
|
|
|
* Shorthands for common assertions made in Marionette.
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2016-11-01 01:00:21 +03:00
|
|
|
this.assert = {};
|
|
|
|
|
2017-11-24 21:21:03 +03:00
|
|
|
/**
|
2018-07-06 20:35:21 +03:00
|
|
|
* Asserts that an arbitrary object is not acyclic.
|
2017-11-24 21:21:03 +03:00
|
|
|
*
|
|
|
|
* @param {*} obj
|
2018-07-06 20:35:21 +03:00
|
|
|
* Object to test. This assertion is only meaningful if passed
|
2017-11-24 21:21:03 +03:00
|
|
|
* an actual object or array.
|
|
|
|
* @param {Error=} [error=JavaScriptError] error
|
|
|
|
* Error to throw if assertion fails.
|
|
|
|
* @param {string=} message
|
2018-07-06 20:35:21 +03:00
|
|
|
* Custom message to use for `error` if assertion fails.
|
2017-11-24 21:21:03 +03:00
|
|
|
*
|
|
|
|
* @throws {JavaScriptError}
|
2018-07-06 20:35:21 +03:00
|
|
|
* If the object is cyclic.
|
2017-11-24 21:21:03 +03:00
|
|
|
*/
|
|
|
|
assert.acyclic = function(obj, msg = "", error = JavaScriptError) {
|
2018-07-06 20:35:21 +03:00
|
|
|
if (evaluate.isCyclic(obj)) {
|
|
|
|
throw new error(msg || "Cyclic object value");
|
2017-11-24 21:21:03 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-02 19:09:14 +03:00
|
|
|
/**
|
|
|
|
* Asserts that Marionette has a session.
|
|
|
|
*
|
|
|
|
* @param {GeckoDriver} driver
|
|
|
|
* Marionette driver instance.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @return {string}
|
2017-08-04 22:32:19 +03:00
|
|
|
* Current session's ID.
|
2017-02-02 19:09:14 +03:00
|
|
|
*
|
2017-02-10 21:36:52 +03:00
|
|
|
* @throws {InvalidSessionIDError}
|
2017-08-04 22:32:19 +03:00
|
|
|
* If <var>driver</var> does not have a session ID.
|
2017-02-02 19:09:14 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.session = function(driver, msg = "") {
|
2017-02-02 19:09:14 +03:00
|
|
|
assert.that(sessionID => sessionID,
|
2017-08-04 22:32:19 +03:00
|
|
|
msg, InvalidSessionIDError)(driver.sessionID);
|
|
|
|
return driver.sessionID;
|
2017-02-02 19:09:14 +03:00
|
|
|
};
|
|
|
|
|
2016-11-01 01:00:21 +03:00
|
|
|
/**
|
|
|
|
* Asserts that the current browser is Firefox Desktop.
|
|
|
|
*
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @throws {UnsupportedOperationError}
|
|
|
|
* If current browser is not Firefox.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.firefox = function(msg = "") {
|
2016-12-29 16:04:16 +03:00
|
|
|
msg = msg || "Only supported in Firefox";
|
2016-11-01 01:00:21 +03:00
|
|
|
assert.that(isFirefox, msg, UnsupportedOperationError)();
|
|
|
|
};
|
|
|
|
|
2018-11-23 00:38:19 +03:00
|
|
|
/**
|
|
|
|
* Asserts that the current browser is Firefox Desktop or Thunderbird.
|
|
|
|
*
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @throws {UnsupportedOperationError}
|
|
|
|
* If current browser is not Firefox or Thunderbird.
|
|
|
|
*/
|
|
|
|
assert.desktop = function(msg = "") {
|
|
|
|
msg = msg || "Only supported in desktop applications";
|
|
|
|
assert.that(obj => isFirefox(obj) || isThunderbird(obj),
|
|
|
|
msg, UnsupportedOperationError)();
|
|
|
|
};
|
|
|
|
|
2016-11-01 01:00:21 +03:00
|
|
|
/**
|
|
|
|
* Asserts that the current browser is Fennec, or Firefox for Android.
|
|
|
|
*
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @throws {UnsupportedOperationError}
|
|
|
|
* If current browser is not Fennec.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.fennec = function(msg = "") {
|
2016-12-29 16:04:16 +03:00
|
|
|
msg = msg || "Only supported in Fennec";
|
2016-11-01 01:00:21 +03:00
|
|
|
assert.that(isFennec, msg, UnsupportedOperationError)();
|
|
|
|
};
|
|
|
|
|
2016-12-29 16:04:16 +03:00
|
|
|
/**
|
2017-10-24 19:32:26 +03:00
|
|
|
* Asserts that the current <var>context</var> is content.
|
2016-12-29 16:04:16 +03:00
|
|
|
*
|
|
|
|
* @param {string} context
|
|
|
|
* Context to test.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @return {string}
|
2017-10-24 19:32:26 +03:00
|
|
|
* <var>context</var> is returned unaltered.
|
2016-12-29 16:04:16 +03:00
|
|
|
*
|
|
|
|
* @throws {UnsupportedOperationError}
|
2017-10-24 19:32:26 +03:00
|
|
|
* If <var>context</var> is not content.
|
2016-12-29 16:04:16 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.content = function(context, msg = "") {
|
2016-12-29 16:04:16 +03:00
|
|
|
msg = msg || "Only supported in content context";
|
|
|
|
assert.that(c => c.toString() == "content", msg, UnsupportedOperationError)(context);
|
2017-02-20 18:38:05 +03:00
|
|
|
};
|
2016-12-29 16:04:16 +03:00
|
|
|
|
2017-03-20 17:13:32 +03:00
|
|
|
/**
|
2018-01-12 17:25:30 +03:00
|
|
|
* Asserts that the {@link ChromeWindow} is open or that the {@link
|
|
|
|
* browser.Context} has a content browser attached.
|
2017-03-20 17:13:32 +03:00
|
|
|
*
|
2018-01-12 17:25:30 +03:00
|
|
|
* When passed in a {@link ChromeContext} this is equivalent to
|
|
|
|
* testing that the associated <code>window</code> global is open,
|
|
|
|
* and when given {@link browser.Context} it will test that the content
|
|
|
|
* frame, represented by <code><xul:browser></code>, is
|
|
|
|
* connected.
|
2017-05-31 17:44:49 +03:00
|
|
|
*
|
2018-01-12 17:25:30 +03:00
|
|
|
* @param {(ChromeWindow|browser.Context)} context
|
2017-05-31 17:44:49 +03:00
|
|
|
* Browsing context to test.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
2018-01-12 17:25:30 +03:00
|
|
|
* @return {(ChromeWindow|browser.Context)}
|
|
|
|
* <var>context</var> is returned unaltered.
|
|
|
|
*
|
2017-05-31 17:44:49 +03:00
|
|
|
* @throws {NoSuchWindowError}
|
2018-01-12 17:25:30 +03:00
|
|
|
* If <var>context</var>'s <code>window</code> has been closed.
|
2017-05-31 17:44:49 +03:00
|
|
|
*/
|
2018-01-12 17:25:30 +03:00
|
|
|
assert.open = function(context, msg = "") {
|
2017-06-13 19:08:22 +03:00
|
|
|
// TODO: The contentBrowser uses a cached tab, which is only updated when
|
|
|
|
// switchToTab is called. Because of that an additional check is needed to
|
|
|
|
// make sure that the chrome window has not already been closed.
|
2018-01-12 17:25:30 +03:00
|
|
|
if (context instanceof browser.Context) {
|
|
|
|
assert.open(context.window);
|
|
|
|
}
|
2017-06-13 19:08:22 +03:00
|
|
|
|
2018-01-12 17:25:30 +03:00
|
|
|
msg = msg || "Browsing context has been discarded";
|
|
|
|
return assert.that(ctx => ctx && !ctx.closed,
|
2017-05-31 17:44:49 +03:00
|
|
|
msg,
|
|
|
|
NoSuchWindowError)(context);
|
|
|
|
};
|
|
|
|
|
2017-04-20 20:00:46 +03:00
|
|
|
/**
|
|
|
|
* Asserts that there is no current user prompt.
|
|
|
|
*
|
|
|
|
* @param {modal.Dialog} dialog
|
|
|
|
* Reference to current dialogue.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @throws {UnexpectedAlertOpenError}
|
|
|
|
* If there is a user prompt.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.noUserPrompt = function(dialog, msg = "") {
|
2017-04-20 20:00:46 +03:00
|
|
|
assert.that(d => d === null || typeof d == "undefined",
|
|
|
|
msg,
|
|
|
|
UnexpectedAlertOpenError)(dialog);
|
|
|
|
};
|
2017-03-20 17:13:32 +03:00
|
|
|
|
2016-11-01 01:00:21 +03:00
|
|
|
/**
|
2017-10-24 19:32:26 +03:00
|
|
|
* Asserts that <var>obj</var> is defined.
|
2016-11-01 01:00:21 +03:00
|
|
|
*
|
|
|
|
* @param {?} obj
|
|
|
|
* Value to test.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @return {?}
|
2017-10-24 19:32:26 +03:00
|
|
|
* <var>obj</var> is returned unaltered.
|
2016-11-01 01:00:21 +03:00
|
|
|
*
|
|
|
|
* @throws {InvalidArgumentError}
|
2017-10-24 19:32:26 +03:00
|
|
|
* If <var>obj</var> is not defined.
|
2016-11-01 01:00:21 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.defined = function(obj, msg = "") {
|
2017-08-29 19:33:38 +03:00
|
|
|
msg = msg || pprint`Expected ${obj} to be defined`;
|
2016-11-01 01:00:21 +03:00
|
|
|
return assert.that(o => typeof o != "undefined", msg)(obj);
|
|
|
|
};
|
|
|
|
|
2017-02-20 18:38:05 +03:00
|
|
|
/**
|
2017-10-24 19:32:26 +03:00
|
|
|
* Asserts that <var>obj</var> is a finite number.
|
2017-02-20 18:38:05 +03:00
|
|
|
*
|
|
|
|
* @param {?} obj
|
|
|
|
* Value to test.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @return {number}
|
2017-10-24 19:32:26 +03:00
|
|
|
* <var>obj</var> is returned unaltered.
|
2017-02-20 18:38:05 +03:00
|
|
|
*
|
|
|
|
* @throws {InvalidArgumentError}
|
2017-10-24 19:32:26 +03:00
|
|
|
* If <var>obj</var> is not a number.
|
2017-02-20 18:38:05 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.number = function(obj, msg = "") {
|
2017-08-29 19:33:38 +03:00
|
|
|
msg = msg || pprint`Expected ${obj} to be finite number`;
|
2017-02-20 18:38:05 +03:00
|
|
|
return assert.that(Number.isFinite, msg)(obj);
|
2017-02-10 22:28:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-10-24 19:32:26 +03:00
|
|
|
* Asserts that <var>obj</var> is callable.
|
2017-02-10 22:28:51 +03:00
|
|
|
*
|
|
|
|
* @param {?} obj
|
|
|
|
* Value to test.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @return {Function}
|
2017-10-24 19:32:26 +03:00
|
|
|
* <var>obj</var> is returned unaltered.
|
2017-02-10 22:28:51 +03:00
|
|
|
*
|
|
|
|
* @throws {InvalidArgumentError}
|
2017-10-24 19:32:26 +03:00
|
|
|
* If <var>obj</var> is not callable.
|
2017-02-10 22:28:51 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.callable = function(obj, msg = "") {
|
2017-08-29 19:33:38 +03:00
|
|
|
msg = msg || pprint`${obj} is not callable`;
|
2017-02-10 22:28:51 +03:00
|
|
|
return assert.that(o => typeof o == "function", msg)(obj);
|
2017-02-20 18:38:05 +03:00
|
|
|
};
|
|
|
|
|
2016-11-01 01:00:21 +03:00
|
|
|
/**
|
2017-10-24 19:32:26 +03:00
|
|
|
* Asserts that <var>obj</var> is an integer.
|
2016-11-01 01:00:21 +03:00
|
|
|
*
|
|
|
|
* @param {?} obj
|
|
|
|
* Value to test.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @return {number}
|
2017-10-24 19:32:26 +03:00
|
|
|
* <var>obj</var> is returned unaltered.
|
2016-11-01 01:00:21 +03:00
|
|
|
*
|
|
|
|
* @throws {InvalidArgumentError}
|
2017-10-24 19:32:26 +03:00
|
|
|
* If <var>obj</var> is not an integer.
|
2016-11-01 01:00:21 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.integer = function(obj, msg = "") {
|
2017-08-29 19:33:38 +03:00
|
|
|
msg = msg || pprint`Expected ${obj} to be an integer`;
|
2017-10-28 13:37:40 +03:00
|
|
|
return assert.that(Number.isSafeInteger, msg)(obj);
|
2016-11-01 01:00:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-10-24 19:32:26 +03:00
|
|
|
* Asserts that <var>obj</var> is a positive integer.
|
2016-11-01 01:00:21 +03:00
|
|
|
*
|
|
|
|
* @param {?} obj
|
|
|
|
* Value to test.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @return {number}
|
2017-10-24 19:32:26 +03:00
|
|
|
* <var>obj</var> is returned unaltered.
|
2016-11-01 01:00:21 +03:00
|
|
|
*
|
|
|
|
* @throws {InvalidArgumentError}
|
2017-10-24 19:32:26 +03:00
|
|
|
* If <var>obj</var> is not a positive integer.
|
2016-11-01 01:00:21 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.positiveInteger = function(obj, msg = "") {
|
2016-11-01 01:00:21 +03:00
|
|
|
assert.integer(obj, msg);
|
2017-08-29 19:33:38 +03:00
|
|
|
msg = msg || pprint`Expected ${obj} to be >= 0`;
|
2016-11-01 01:00:21 +03:00
|
|
|
return assert.that(n => n >= 0, msg)(obj);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-10-24 19:32:26 +03:00
|
|
|
* Asserts that <var>obj</var> is a boolean.
|
2016-11-01 01:00:21 +03:00
|
|
|
*
|
|
|
|
* @param {?} obj
|
|
|
|
* Value to test.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
2017-10-24 19:32:26 +03:00
|
|
|
* <var>obj</var> is returned unaltered.
|
2016-11-01 01:00:21 +03:00
|
|
|
*
|
|
|
|
* @throws {InvalidArgumentError}
|
2017-10-24 19:32:26 +03:00
|
|
|
* If <var>obj</var> is not a boolean.
|
2016-11-01 01:00:21 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.boolean = function(obj, msg = "") {
|
2017-08-29 19:33:38 +03:00
|
|
|
msg = msg || pprint`Expected ${obj} to be boolean`;
|
2016-11-01 01:00:21 +03:00
|
|
|
return assert.that(b => typeof b == "boolean", msg)(obj);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-10-24 19:32:26 +03:00
|
|
|
* Asserts that <var>obj</var> is a string.
|
2016-11-01 01:00:21 +03:00
|
|
|
*
|
|
|
|
* @param {?} obj
|
|
|
|
* Value to test.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @return {string}
|
2017-10-24 19:32:26 +03:00
|
|
|
* <var>obj</var> is returned unaltered.
|
2016-11-01 01:00:21 +03:00
|
|
|
*
|
|
|
|
* @throws {InvalidArgumentError}
|
2017-10-24 19:32:26 +03:00
|
|
|
* If <var>obj</var> is not a string.
|
2016-11-01 01:00:21 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.string = function(obj, msg = "") {
|
2017-08-29 19:33:38 +03:00
|
|
|
msg = msg || pprint`Expected ${obj} to be a string`;
|
2016-11-01 01:00:21 +03:00
|
|
|
return assert.that(s => typeof s == "string", msg)(obj);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-10-24 19:32:26 +03:00
|
|
|
* Asserts that <var>obj</var> is an object.
|
2016-11-01 01:00:21 +03:00
|
|
|
*
|
|
|
|
* @param {?} obj
|
|
|
|
* Value to test.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @return {Object}
|
2017-10-24 19:32:26 +03:00
|
|
|
* obj| is returned unaltered.
|
2016-11-01 01:00:21 +03:00
|
|
|
*
|
|
|
|
* @throws {InvalidArgumentError}
|
2017-10-24 19:32:26 +03:00
|
|
|
* If <var>obj</var> is not an object.
|
2016-11-01 01:00:21 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.object = function(obj, msg = "") {
|
2017-08-29 19:33:38 +03:00
|
|
|
msg = msg || pprint`Expected ${obj} to be an object`;
|
2017-02-09 19:35:07 +03:00
|
|
|
return assert.that(o => {
|
2017-02-14 19:48:14 +03:00
|
|
|
// unable to use instanceof because LHS and RHS may come from
|
|
|
|
// different globals
|
2017-02-09 19:35:07 +03:00
|
|
|
let s = Object.prototype.toString.call(o);
|
|
|
|
return s == "[object Object]" || s == "[object nsJSIID]";
|
2017-06-12 20:02:39 +03:00
|
|
|
}, msg)(obj);
|
2016-12-30 14:21:27 +03:00
|
|
|
};
|
2016-12-30 14:39:22 +03:00
|
|
|
|
|
|
|
/**
|
2017-10-24 19:32:26 +03:00
|
|
|
* Asserts that <var>prop</var> is in <var>obj</var>.
|
2016-12-30 14:39:22 +03:00
|
|
|
*
|
|
|
|
* @param {?} prop
|
2017-10-24 19:32:26 +03:00
|
|
|
* Own property to test if is in <var>obj</var>.
|
2016-12-30 14:39:22 +03:00
|
|
|
* @param {?} obj
|
|
|
|
* Object.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @return {?}
|
2017-10-24 19:32:26 +03:00
|
|
|
* Value of <var>obj</var>'s own property <var>prop</var>.
|
2016-12-30 14:39:22 +03:00
|
|
|
*
|
|
|
|
* @throws {InvalidArgumentError}
|
2017-10-24 19:32:26 +03:00
|
|
|
* If <var>prop</var> is not in <var>obj</var>, or <var>obj</var>
|
|
|
|
* is not an object.
|
2016-12-30 14:39:22 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.in = function(prop, obj, msg = "") {
|
2016-12-30 14:39:22 +03:00
|
|
|
assert.object(obj, msg);
|
2017-08-29 19:33:38 +03:00
|
|
|
msg = msg || pprint`Expected ${prop} in ${obj}`;
|
2016-12-30 14:39:22 +03:00
|
|
|
assert.that(p => obj.hasOwnProperty(p), msg)(prop);
|
|
|
|
return obj[prop];
|
2016-11-01 01:00:21 +03:00
|
|
|
};
|
|
|
|
|
2016-12-14 02:29:48 +03:00
|
|
|
/**
|
2017-10-24 19:32:26 +03:00
|
|
|
* Asserts that <var>obj</var> is an Array.
|
2016-12-14 02:29:48 +03:00
|
|
|
*
|
|
|
|
* @param {?} obj
|
|
|
|
* Value to test.
|
|
|
|
* @param {string=} msg
|
|
|
|
* Custom error message.
|
|
|
|
*
|
|
|
|
* @return {Object}
|
2017-10-24 19:32:26 +03:00
|
|
|
* <var>obj</var> is returned unaltered.
|
2016-12-14 02:29:48 +03:00
|
|
|
*
|
|
|
|
* @throws {InvalidArgumentError}
|
2017-10-24 19:32:26 +03:00
|
|
|
* If <var>obj</var> is not an Array.
|
2016-12-14 02:29:48 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.array = function(obj, msg = "") {
|
2017-08-29 19:33:38 +03:00
|
|
|
msg = msg || pprint`Expected ${obj} to be an Array`;
|
2017-01-05 01:48:11 +03:00
|
|
|
return assert.that(Array.isArray, msg)(obj);
|
2016-12-14 02:29:48 +03:00
|
|
|
};
|
|
|
|
|
2016-11-01 01:00:21 +03:00
|
|
|
/**
|
|
|
|
* Returns a function that is used to assert the |predicate|.
|
|
|
|
*
|
|
|
|
* @param {function(?): boolean} predicate
|
|
|
|
* Evaluated on calling the return value of this function. If its
|
2017-10-24 19:32:26 +03:00
|
|
|
* return value of the inner function is false, <var>error</var>
|
|
|
|
* is thrown with <var>message</var>.
|
2016-11-01 01:00:21 +03:00
|
|
|
* @param {string=} message
|
|
|
|
* Custom error message.
|
|
|
|
* @param {Error=} error
|
|
|
|
* Custom error type by its class.
|
|
|
|
*
|
|
|
|
* @return {function(?): ?}
|
2017-10-24 19:32:26 +03:00
|
|
|
* Function that takes and returns the passed in value unaltered,
|
|
|
|
* and which may throw <var>error</var> with <var>message</var>
|
|
|
|
* if <var>predicate</var> evaluates to false.
|
2016-11-01 01:00:21 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
assert.that = function(
|
2016-11-01 01:00:21 +03:00
|
|
|
predicate, message = "", error = InvalidArgumentError) {
|
|
|
|
return obj => {
|
|
|
|
if (!predicate(obj)) {
|
|
|
|
throw new error(message);
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
};
|