From 52426eb1abca96223c5dff25cc80fdc21457331a Mon Sep 17 00:00:00 2001 From: Andreas Tolfsen Date: Fri, 30 Dec 2016 11:39:22 +0000 Subject: [PATCH] Bug 1326534 - Add assert.in for own properties; r=automatedtester The implementation of the new library function `assert.in` is analogous to Python's `unittest.TestCase.assertIn`. MozReview-Commit-ID: 1pjS9ttPXgo --HG-- extra : rebase_source : 3f4121226d65f127fdf0c990915483659f3a7f93 --- testing/marionette/assert.js | 22 ++++++++++++++++++++++ testing/marionette/test_assert.js | 9 +++++++++ 2 files changed, 31 insertions(+) diff --git a/testing/marionette/assert.js b/testing/marionette/assert.js index 8d791cfa179b..c9d6906c6e8e 100644 --- a/testing/marionette/assert.js +++ b/testing/marionette/assert.js @@ -212,6 +212,28 @@ assert.object = function (obj, msg = "") { return assert.that(o => Object.prototype.toString.call(o) == "[object Object]", msg)(obj); }; + +/** + * Asserts that |prop| is in |obj|. + * + * @param {?} prop + * Own property to test if is in |obj|. + * @param {?} obj + * Object. + * @param {string=} msg + * Custom error message. + * + * @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}`; + assert.that(p => obj.hasOwnProperty(p), msg)(prop); + return obj[prop]; }; /** diff --git a/testing/marionette/test_assert.js b/testing/marionette/test_assert.js index b67a6f1fffdd..029f36c2f335 100644 --- a/testing/marionette/test_assert.js +++ b/testing/marionette/test_assert.js @@ -76,6 +76,15 @@ add_test(function test_object() { run_next_test(); }); +add_test(function test_in() { + assert.in("foo", {foo: 42}); + for (let typ of [{}, 42, true, null, undefined]) { + Assert.throws(() => assert.in("foo", typ), InvalidArgumentError); + } + + run_next_test(); +}); + add_test(function test_array() { assert.array([]); assert.array(new Array());