Bug 1147751 - Implement Assert.greater, Assert.greaterOrEqual, Assert.less and Assert.lessOrEqual. r=mikedeboer
authorHiroyuki Ikezoe <hiikezoe@mozilla-japan.org>
Fri, 24 Apr 2015 01:12:00 -0400
changeset 241014 bb087ed9f8cdebd50b1d51594532596ca5657669
parent 241013 e9ef9a74e0dc7d1d69938455eb75823a6ffd8ed9
child 241015 be163dc787d17652ee242b4d4e5839274b221bf8
push id28652
push usercbook@mozilla.com
push dateMon, 27 Apr 2015 10:00:58 +0000
treeherdermozilla-central@8aff0d2a7bc7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmikedeboer
bugs1147751
milestone40.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1147751 - Implement Assert.greater, Assert.greaterOrEqual, Assert.less and Assert.lessOrEqual. r=mikedeboer
testing/modules/Assert.jsm
testing/modules/tests/xpcshell/test_assert.js
--- a/testing/modules/Assert.jsm
+++ b/testing/modules/Assert.jsm
@@ -387,8 +387,87 @@ proto.rejects = function(promise, expect
           return;
         }
         this.report(false, err, expected, message);
         resolve();
       }
     ).then(null, reject);
   });
 };
+
+function compareNumbers(expression, lhs, rhs, message, operator) {
+  let lhsIsNumber = typeof lhs == "number";
+  let rhsIsNumber = typeof rhs == "number";
+
+  if (lhsIsNumber && rhsIsNumber) {
+    this.report(expression, lhs, rhs, message, operator);
+    return;
+  }
+
+  let errorMessage;
+  if (!lhsIsNumber && !rhsIsNumber) {
+    errorMessage = "Neither '" + lhs + "' nor '" + rhs + "' are numbers";
+  } else {
+    errorMessage = "'" + (lhsIsNumber ? rhs : lhs) + "' is not a number";
+  }
+  this.report(true, lhs, rhs, errorMessage);
+}
+
+/**
+ * The lhs must be greater than the rhs.
+ * assert.greater(lhs, rhs, message_opt);
+ *
+ * @param lhs
+ *        (number) The left-hand side value
+ * @param rhs
+ *        (number) The right-hand side value
+ * @param message (optional)
+ *        (string) Short explanation of the comparison result
+ */
+proto.greater = function greater(lhs, rhs, message) {
+  compareNumbers.call(this, lhs <= rhs, lhs, rhs, message, ">");
+};
+
+/**
+ * The lhs must be greater than or equal to the rhs.
+ * assert.greaterOrEqual(lhs, rhs, message_opt);
+ *
+ * @param lhs
+ *        (number) The left-hand side value
+ * @param rhs
+ *        (number) The right-hand side value
+ * @param message (optional)
+ *        (string) Short explanation of the comparison result
+ */
+proto.greaterOrEqual = function greaterOrEqual(lhs, rhs, message) {
+  compareNumbers.call(this, lhs < rhs, lhs, rhs, message, ">=");
+};
+
+/**
+ * The lhs must be less than the rhs.
+ * assert.less(lhs, rhs, message_opt);
+ *
+ * @param lhs
+ *        (number) The left-hand side value
+ * @param rhs
+ *        (number) The right-hand side value
+ * @param message (optional)
+ *        (string) Short explanation of the comparison result
+ */
+proto.less = function less(lhs, rhs, message) {
+  compareNumbers.call(this, lhs >= rhs, lhs, rhs, message, "<");
+};
+
+/**
+ * The lhs must be less than or equal to the rhs.
+ * assert.lessOrEqual(lhs, rhs, message_opt);
+ *
+ * @param lhs
+ *        (number) The left-hand side value
+ * @param rhs
+ *        (number) The right-hand side value
+ * @param message (optional)
+ *        (string) Short explanation of the comparison result
+ */
+proto.lessOrEqual = function lessOrEqual(lhs, rhs, message) {
+  compareNumbers.call(this, lhs > rhs, lhs, rhs, message, "<=");
+};
+
--- a/testing/modules/tests/xpcshell/test_assert.js
+++ b/testing/modules/tests/xpcshell/test_assert.js
@@ -275,16 +275,49 @@ function run_test() {
       toJSON: function() {
         throw "bam!";
       }
     },
     expected: "foo",
     operator: "="
   }).message, "[object Object] = \"foo\"");
 
+  let message;
+  assert.greater(3, 2);
+  try {
+    assert.greater(2, 2);
+  } catch(e) {
+    message = e.toString().split("\n")[0];
+  }
+  assert.equal(message, "AssertionError: 2 > 2");
+
+  assert.greaterOrEqual(2, 2);
+  try {
+    assert.greaterOrEqual(1, 2);
+  } catch(e) {
+    message = e.toString().split("\n")[0];
+  }
+  assert.equal(message, "AssertionError: 1 >= 2");
+
+  assert.less(1, 2);
+  try {
+    assert.less(2, 2);
+  } catch(e) {
+    message = e.toString().split("\n")[0];
+  }
+  assert.equal(message, "AssertionError: 2 < 2");
+
+  assert.lessOrEqual(2, 2);
+  try {
+    assert.lessOrEqual(2, 1);
+  } catch(e) {
+    message = e.toString().split("\n")[0];
+  }
+  assert.equal(message, "AssertionError: 2 <= 1");
+
   run_next_test();
 }
 
 add_task(function* test_rejects() {
   let ns = {};
   Components.utils.import("resource://testing-common/Assert.jsm", ns);
   let assert = new ns.Assert();