Bug 1326534 - Exclude array and null from being counted as objects; r?automatedtester draft
authorAndreas Tolfsen <ato@mozilla.com>
Fri, 30 Dec 2016 11:21:27 +0000
changeset 457103 7936520f03f7d8c69c352ae40ab04c54001a2957
parent 457102 4797e1c50cb55e5ba790d5f5f2c27f850f39920d
child 457104 a2cb1121cf3dbc430d1bc9c83856cc5ac090958a
push id40670
push userbmo:ato@mozilla.com
push dateFri, 06 Jan 2017 18:52:13 +0000
reviewersautomatedtester
bugs1326534
milestone53.0a1
Bug 1326534 - Exclude array and null from being counted as objects; r?automatedtester Calling `typeof` on arrays or null in JavaScript returns `"object"`, and a safer check is to rely on `Object.prototype.toString`. MozReview-Commit-ID: 5VxXf2ByoEx
testing/marionette/assert.js
testing/marionette/test_assert.js
--- a/testing/marionette/assert.js
+++ b/testing/marionette/assert.js
@@ -204,17 +204,19 @@ 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`;
-  return assert.that(o => typeof o == "object", msg)(obj);
+  return assert.that(o =>
+      Object.prototype.toString.call(o) == "[object Object]", msg)(obj);
+};
 };
 
 /**
  * Asserts that |obj| is an Array.
  *
  * @param {?} obj
  *     Value to test.
  * @param {string=} msg
--- a/testing/marionette/test_assert.js
+++ b/testing/marionette/test_assert.js
@@ -64,17 +64,19 @@ add_test(function test_string() {
   Assert.throws(() => assert.string(42), InvalidArgumentError);
 
   run_next_test();
 });
 
 add_test(function test_object() {
   assert.object({});
   assert.object(new Object());
-  Assert.throws(() => assert.object(42), InvalidArgumentError);
+  for (let typ of [42, "foo", true, null, undefined]) {
+    Assert.throws(() => assert.object(typ), InvalidArgumentError);
+  }
 
   run_next_test();
 });
 
 add_test(function test_that() {
   equal(1, assert.that(n => n + 1)(1));
   Assert.throws(() => assert.that(() => false)());
   Assert.throws(() => assert.that(val => val)(false));