Bug 1394849 - Export pprint separately. r?automatedtester
pprint is currently exposed twice: once on the error namespace and once
separately. We only want to expose it once, and since there are only a
handful "error.pprint" usages left, we can go ahead and make this change.
When we move transition to use "require" in the future, like devtools
does, it will be possible to use both "error.pprint" and "pprint" styles
without export duplication.
MozReview-Commit-ID: CAnPDWn9Vr7
--- a/testing/marionette/action.js
+++ b/testing/marionette/action.js
@@ -6,28 +6,26 @@
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("chrome://marionette/content/assert.js");
Cu.import("chrome://marionette/content/element.js");
const {
- error,
+ pprint,
InvalidArgumentError,
MoveTargetOutOfBoundsError,
UnsupportedOperationError,
} = Cu.import("chrome://marionette/content/error.js", {});
Cu.import("chrome://marionette/content/event.js");
Cu.import("chrome://marionette/content/interaction.js");
this.EXPORTED_SYMBOLS = ["action"];
-const {pprint} = error;
-
// TODO? With ES 2016 and Symbol you can make a safer approximation
// to an enum e.g. https://gist.github.com/xmlking/e86e4f15ec32b12c4689
/**
* Implements WebDriver Actions API: a low-level interface for providing
* virtualised device input to the web browser.
*
* @namespace
*/
--- a/testing/marionette/assert.js
+++ b/testing/marionette/assert.js
@@ -6,20 +6,20 @@
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/AppConstants.jsm");
Cu.import("resource://gre/modules/Preferences.jsm");
Cu.import("resource://gre/modules/Services.jsm");
const {
- error,
InvalidArgumentError,
InvalidSessionIDError,
NoSuchWindowError,
+ pprint,
UnexpectedAlertOpenError,
UnsupportedOperationError,
} = Cu.import("chrome://marionette/content/error.js", {});
this.EXPORTED_SYMBOLS = ["assert"];
const isFennec = () => AppConstants.platform == "android";
const isFirefox = () => Services.appinfo.name == "Firefox";
@@ -169,17 +169,17 @@ assert.noUserPrompt = function(dialog, m
*
* @return {?}
* |obj| is returned unaltered.
*
* @throws {InvalidArgumentError}
* If |obj| is not defined.
*/
assert.defined = function(obj, msg = "") {
- msg = msg || error.pprint`Expected ${obj} to be defined`;
+ msg = msg || pprint`Expected ${obj} to be defined`;
return assert.that(o => typeof o != "undefined", msg)(obj);
};
/**
* Asserts that |obj| is a finite number.
*
* @param {?} obj
* Value to test.
@@ -188,17 +188,17 @@ assert.defined = function(obj, msg = "")
*
* @return {number}
* |obj| is returned unaltered.
*
* @throws {InvalidArgumentError}
* If |obj| is not a number.
*/
assert.number = function(obj, msg = "") {
- msg = msg || error.pprint`Expected ${obj} to be finite number`;
+ msg = msg || pprint`Expected ${obj} to be finite number`;
return assert.that(Number.isFinite, msg)(obj);
};
/**
* Asserts that |obj| is callable.
*
* @param {?} obj
* Value to test.
@@ -207,17 +207,17 @@ assert.number = function(obj, msg = "")
*
* @return {Function}
* |obj| is returned unaltered.
*
* @throws {InvalidArgumentError}
* If |obj| is not callable.
*/
assert.callable = function(obj, msg = "") {
- msg = msg || error.pprint`${obj} is not callable`;
+ msg = msg || pprint`${obj} is not callable`;
return assert.that(o => typeof o == "function", msg)(obj);
};
/**
* Asserts that |obj| is an integer.
*
* @param {?} obj
* Value to test.
@@ -226,17 +226,17 @@ assert.callable = function(obj, msg = ""
*
* @return {number}
* |obj| is returned unaltered.
*
* @throws {InvalidArgumentError}
* If |obj| is not an integer.
*/
assert.integer = function(obj, msg = "") {
- msg = msg || error.pprint`Expected ${obj} to be an integer`;
+ msg = msg || pprint`Expected ${obj} to be an integer`;
return assert.that(Number.isInteger, msg)(obj);
};
/**
* Asserts that |obj| is a positive integer.
*
* @param {?} obj
* Value to test.
@@ -246,17 +246,17 @@ assert.integer = function(obj, msg = "")
* @return {number}
* |obj| is returned unaltered.
*
* @throws {InvalidArgumentError}
* If |obj| is not a positive integer.
*/
assert.positiveInteger = function(obj, msg = "") {
assert.integer(obj, msg);
- msg = msg || error.pprint`Expected ${obj} to be >= 0`;
+ msg = msg || pprint`Expected ${obj} to be >= 0`;
return assert.that(n => n >= 0, msg)(obj);
};
/**
* Asserts that |obj| is a boolean.
*
* @param {?} obj
* Value to test.
@@ -265,17 +265,17 @@ assert.positiveInteger = function(obj, m
*
* @return {boolean}
* |obj| is returned unaltered.
*
* @throws {InvalidArgumentError}
* If |obj| is not a boolean.
*/
assert.boolean = function(obj, msg = "") {
- msg = msg || error.pprint`Expected ${obj} to be boolean`;
+ msg = msg || pprint`Expected ${obj} to be boolean`;
return assert.that(b => typeof b == "boolean", msg)(obj);
};
/**
* Asserts that |obj| is a string.
*
* @param {?} obj
* Value to test.
@@ -284,17 +284,17 @@ assert.boolean = function(obj, msg = "")
*
* @return {string}
* |obj| is returned unaltered.
*
* @throws {InvalidArgumentError}
* If |obj| is not a string.
*/
assert.string = function(obj, msg = "") {
- msg = msg || error.pprint`Expected ${obj} to be a string`;
+ msg = msg || pprint`Expected ${obj} to be a string`;
return assert.that(s => typeof s == "string", msg)(obj);
};
/**
* Asserts that |obj| is an object.
*
* @param {?} obj
* Value to test.
@@ -303,17 +303,17 @@ assert.string = function(obj, msg = "")
*
* @return {Object}
* |obj| is returned unaltered.
*
* @throws {InvalidArgumentError}
* If |obj| is not an object.
*/
assert.object = function(obj, msg = "") {
- msg = msg || error.pprint`Expected ${obj} to be an object`;
+ msg = msg || pprint`Expected ${obj} to be an object`;
return assert.that(o => {
// unable to use instanceof because LHS and RHS may come from
// different globals
let s = Object.prototype.toString.call(o);
return s == "[object Object]" || s == "[object nsJSIID]";
}, msg)(obj);
};
@@ -330,17 +330,17 @@ assert.object = function(obj, msg = "")
* @return {?}
* Value of |obj|'s own property |prop|.
*
* @throws {InvalidArgumentError}
* If |prop| is not in |obj|, or |obj| is not an object.
*/
assert.in = function(prop, obj, msg = "") {
assert.object(obj, msg);
- msg = msg || error.pprint`Expected ${prop} in ${obj}`;
+ msg = msg || pprint`Expected ${prop} in ${obj}`;
assert.that(p => obj.hasOwnProperty(p), msg)(prop);
return obj[prop];
};
/**
* Asserts that |obj| is an Array.
*
* @param {?} obj
@@ -350,17 +350,17 @@ assert.in = function(prop, obj, msg = ""
*
* @return {Object}
* |obj| is returned unaltered.
*
* @throws {InvalidArgumentError}
* If |obj| is not an Array.
*/
assert.array = function(obj, msg = "") {
- msg = msg || error.pprint`Expected ${obj} to be an Array`;
+ msg = msg || pprint`Expected ${obj} to be an Array`;
return assert.that(Array.isArray, msg)(obj);
};
/**
* 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
--- a/testing/marionette/cookie.js
+++ b/testing/marionette/cookie.js
@@ -5,18 +5,18 @@
"use strict";
const {interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("chrome://marionette/content/assert.js");
const {
- error,
InvalidCookieDomainError,
+ pprint,
} = Cu.import("chrome://marionette/content/error.js", {});
this.EXPORTED_SYMBOLS = ["cookie"];
const IPV4_PORT_EXPR = /:\d+$/;
/** @namespace */
this.cookie = {
@@ -48,17 +48,17 @@ this.cookie = {
* Valid cookie object.
*
* @throws {InvalidArgumentError}
* If any of the properties are invalid.
*/
cookie.fromJSON = function(json) {
let newCookie = {};
- assert.object(json, error.pprint`Expected cookie object, got ${json}`);
+ assert.object(json, pprint`Expected cookie object, got ${json}`);
newCookie.name = assert.string(json.name, "Cookie name must be string");
newCookie.value = assert.string(json.value, "Cookie value must be string");
if (typeof json.path != "undefined") {
newCookie.path = assert.string(json.path, "Cookie path must be string");
}
if (typeof json.secure != "undefined") {
--- a/testing/marionette/element.js
+++ b/testing/marionette/element.js
@@ -7,20 +7,20 @@
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Log.jsm");
Cu.import("chrome://marionette/content/assert.js");
Cu.import("chrome://marionette/content/atom.js");
const {
- error,
InvalidSelectorError,
JavaScriptError,
NoSuchElementError,
+ pprint,
StaleElementReferenceError,
} = Cu.import("chrome://marionette/content/error.js", {});
Cu.import("chrome://marionette/content/wait.js");
const logger = Log.repository.getLogger("Marionette");
this.EXPORTED_SYMBOLS = ["element"];
@@ -175,17 +175,17 @@ element.Store = class {
el = el.get();
} catch (e) {
el = null;
delete this.els[uuid];
}
if (element.isStale(el)) {
throw new StaleElementReferenceError(
- error.pprint`The element reference of ${el} stale; ` +
+ pprint`The element reference of ${el} stale; ` +
"either the element is no longer attached to the DOM " +
"or the document has been refreshed");
}
return el;
}
};
--- a/testing/marionette/error.js
+++ b/testing/marionette/error.js
@@ -40,17 +40,20 @@ const BUILTIN_ERRORS = new Set([
"InternalError",
"RangeError",
"ReferenceError",
"SyntaxError",
"TypeError",
"URIError",
]);
-this.EXPORTED_SYMBOLS = ["error", "error.pprint"].concat(Array.from(ERRORS));
+this.EXPORTED_SYMBOLS = [
+ "error",
+ "pprint",
+].concat(Array.from(ERRORS));
/** @namespace */
this.error = {};
/**
* Check if |val| is an instance of the |Error| prototype.
*
* Because error objects may originate from different globals, comparing
@@ -153,17 +156,17 @@ error.stringify = function(err) {
* let bool = {value: true};
* pprint`Expected boolean, got ${bool}`;
* => 'Expected boolean, got [object Object] {"value": true}'
*
* let htmlElement = document.querySelector("input#foo");
* pprint`Expected element ${htmlElement}`;
* => 'Expected element <input id="foo" class="bar baz">'
*/
-error.pprint = function(ss, ...values) {
+this.pprint = function(ss, ...values) {
function prettyObject(obj) {
let proto = Object.prototype.toString.call(obj);
let s = "";
try {
s = JSON.stringify(obj);
} catch (e) {
if (e instanceof TypeError) {
s = `<${e.message}>`;
@@ -300,27 +303,27 @@ class ElementClickInterceptedError exten
constructor(obscuredEl = undefined, coords = undefined) {
let msg = "";
if (obscuredEl && coords) {
const doc = obscuredEl.ownerDocument;
const overlayingEl = doc.elementFromPoint(coords.x, coords.y);
switch (obscuredEl.style.pointerEvents) {
case "none":
- msg = error.pprint`Element ${obscuredEl} is not clickable ` +
+ msg = pprint`Element ${obscuredEl} is not clickable ` +
`at point (${coords.x},${coords.y}) ` +
`because it does not have pointer events enabled, ` +
- error.pprint`and element ${overlayingEl} ` +
+ pprint`and element ${overlayingEl} ` +
`would receive the click instead`;
break;
default:
- msg = error.pprint`Element ${obscuredEl} is not clickable ` +
+ msg = pprint`Element ${obscuredEl} is not clickable ` +
`at point (${coords.x},${coords.y}) ` +
- error.pprint`because another element ${overlayingEl} ` +
+ pprint`because another element ${overlayingEl} ` +
`obscures it`;
break;
}
}
super(msg);
this.status = "element click intercepted";
}
--- a/testing/marionette/interaction.js
+++ b/testing/marionette/interaction.js
@@ -6,17 +6,16 @@
const {utils: Cu} = Components;
Cu.import("chrome://marionette/content/accessibility.js");
Cu.import("chrome://marionette/content/atom.js");
const {
ElementClickInterceptedError,
ElementNotInteractableError,
- error,
InvalidArgument,
InvalidArgumentError,
InvalidElementStateError,
pprint,
} = Cu.import("chrome://marionette/content/error.js", {});
Cu.import("chrome://marionette/content/element.js");
Cu.import("chrome://marionette/content/event.js");
@@ -173,17 +172,17 @@ async function webdriverClickElement(el,
// step 5
// TODO(ato): wait for containerEl to be in view
// step 6
// if we cannot bring the container element into the viewport
// there is no point in checking if it is pointer-interactable
if (!element.isInView(containerEl)) {
throw new ElementNotInteractableError(
- error.pprint`Element ${el} could not be scrolled into view`);
+ pprint`Element ${el} could not be scrolled into view`);
}
// step 7
let rects = containerEl.getClientRects();
let clickPoint = element.getInViewCentrePoint(rects[0], win);
if (element.isObscured(containerEl)) {
throw new ElementClickInterceptedError(containerEl, clickPoint);
--- a/testing/marionette/test_error.js
+++ b/testing/marionette/test_error.js
@@ -1,15 +1,43 @@
/* 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/. */
const {utils: Cu} = Components;
-Cu.import("chrome://marionette/content/error.js");
+const {
+ ElementClickInterceptedError,
+ ElementNotAccessibleError,
+ ElementNotInteractableError,
+ error,
+ InsecureCertificateError,
+ InvalidArgumentError,
+ InvalidCookieDomainError,
+ InvalidElementStateError,
+ InvalidSelectorError,
+ InvalidSessionIDError,
+ JavaScriptError,
+ MoveTargetOutOfBoundsError,
+ NoAlertOpenError,
+ NoSuchElementError,
+ NoSuchFrameError,
+ NoSuchWindowError,
+ pprint,
+ ScriptTimeoutError,
+ SessionNotCreatedError,
+ StaleElementReferenceError,
+ TimeoutError,
+ UnableToSetCookieError,
+ UnexpectedAlertOpenError,
+ UnknownCommandError,
+ UnknownError,
+ UnsupportedOperationError,
+ WebDriverError,
+} = Cu.import("chrome://marionette/content/error.js", {});
function notok(condition) {
ok(!(condition));
}
add_test(function test_isError() {
notok(error.isError(null));
notok(error.isError([]));
@@ -85,38 +113,38 @@ add_test(function test_stringify() {
error.stringify(new WebDriverError("foo")).split("\n")[0]);
equal("InvalidArgumentError: foo",
error.stringify(new InvalidArgumentError("foo")).split("\n")[0]);
run_next_test();
});
add_test(function test_pprint() {
- equal('[object Object] {"foo":"bar"}', error.pprint`${{foo: "bar"}}`);
+ equal('[object Object] {"foo":"bar"}', pprint`${{foo: "bar"}}`);
- equal("[object Number] 42", error.pprint`${42}`);
- equal("[object Boolean] true", error.pprint`${true}`);
- equal("[object Undefined] undefined", error.pprint`${undefined}`);
- equal("[object Null] null", error.pprint`${null}`);
+ equal("[object Number] 42", pprint`${42}`);
+ equal("[object Boolean] true", pprint`${true}`);
+ equal("[object Undefined] undefined", pprint`${undefined}`);
+ equal("[object Null] null", pprint`${null}`);
let complexObj = {toJSON: () => "foo"};
- equal('[object Object] "foo"', error.pprint`${complexObj}`);
+ equal('[object Object] "foo"', pprint`${complexObj}`);
let cyclic = {};
cyclic.me = cyclic;
- equal("[object Object] <cyclic object value>", error.pprint`${cyclic}`);
+ equal("[object Object] <cyclic object value>", pprint`${cyclic}`);
let el = {
nodeType: 1,
localName: "input",
id: "foo",
classList: {length: 1},
className: "bar baz",
};
- equal('<input id="foo" class="bar baz">', error.pprint`${el}`);
+ equal('<input id="foo" class="bar baz">', pprint`${el}`);
run_next_test();
});
add_test(function test_toJSON() {
let e0 = new WebDriverError();
let e0s = e0.toJSON();
equal(e0s.error, "webdriver error");