Bug 1337133 - Add number check to assert.js; r=ato+446296
authorMaja Frydrychowicz <mjzffr@gmail.com>
Mon, 20 Feb 2017 10:38:05 -0500
changeset 374048 7902afebdf2f2232f7c5ba8acdebd75ec752c3c4
parent 374047 313bfccbbd9f393617e423208d3777b6468f56b1
child 374049 397ef772d437a371837f66d43ba632846c2a05e3
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersato
bugs1337133, 446296
milestone54.0a1
Bug 1337133 - Add number check to assert.js; r=ato+446296 MozReview-Commit-ID: 9BVna5jO49b
testing/marionette/assert.js
testing/marionette/test_assert.js
--- a/testing/marionette/assert.js
+++ b/testing/marionette/assert.js
@@ -95,17 +95,17 @@ assert.b2g = function (msg = "") {
  *     |context| is returned unaltered.
  *
  * @throws {UnsupportedOperationError}
  *     If |context| is not content.
  */
 assert.content = function (context, msg = "") {
   msg = msg || "Only supported in content context";
   assert.that(c => c.toString() == "content", msg, UnsupportedOperationError)(context);
-}
+};
 
 /**
  * Asserts that the current browser is a mobile browser, that is either
  * B2G or Fennec.
  *
  * @param {string=} msg
  *     Custom error message.
  *
@@ -132,16 +132,35 @@ assert.mobile = function (msg = "") {
  *     If |obj| is not defined.
  */
 assert.defined = function (obj, msg = "") {
   msg = msg || error.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.
+ * @param {string=} msg
+ *     Custom error message.
+ *
+ * @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`;
+  return assert.that(Number.isFinite, msg)(obj);
+};
+
+/**
  * Asserts that |obj| is an integer.
  *
  * @param {?} obj
  *     Value to test.
  * @param {string=} msg
  *     Custom error message.
  *
  * @return {number}
--- a/testing/marionette/test_assert.js
+++ b/testing/marionette/test_assert.js
@@ -35,21 +35,33 @@ add_test(function test_platforms() {
 
 add_test(function test_defined() {
   assert.defined({});
   Assert.throws(() => assert.defined(undefined), InvalidArgumentError);
 
   run_next_test();
 });
 
+add_test(function test_number() {
+  assert.number(1);
+  assert.number(0);
+  assert.number(-1);
+  assert.number(1.2);
+  for (let i of ["foo", "1", {}, [], NaN, Infinity, undefined]) {
+    Assert.throws(() => assert.number(i), InvalidArgumentError);
+  }
+  run_next_test();
+});
+
 add_test(function test_integer() {
   assert.integer(1);
   assert.integer(0);
   assert.integer(-1);
   Assert.throws(() => assert.integer("foo"), InvalidArgumentError);
+  Assert.throws(() => assert.integer(1.2), InvalidArgumentError);
 
   run_next_test();
 });
 
 add_test(function test_positiveInteger() {
   assert.positiveInteger(1);
   assert.positiveInteger(0);
   Assert.throws(() => assert.positiveInteger(-1), InvalidArgumentError);