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
This commit is contained in:
Andreas Tolfsen 2016-12-30 11:39:22 +00:00
Родитель 4a5f5aabaf
Коммит 52426eb1ab
2 изменённых файлов: 31 добавлений и 0 удалений

Просмотреть файл

@ -212,6 +212,28 @@ assert.object = function (obj, msg = "") {
return assert.that(o => return assert.that(o =>
Object.prototype.toString.call(o) == "[object Object]", msg)(obj); 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];
}; };
/** /**

Просмотреть файл

@ -76,6 +76,15 @@ add_test(function test_object() {
run_next_test(); 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() { add_test(function test_array() {
assert.array([]); assert.array([]);
assert.array(new Array()); assert.array(new Array());