diff options
author | Ian Boros <ian.boros@10gen.com> | 2018-08-22 12:49:05 -0400 |
---|---|---|
committer | Daniel Gottlieb <daniel.gottlieb@mongodb.com> | 2019-01-14 11:40:00 -0500 |
commit | 8fa57b8981903e54d6bee624abd10aa1311493c0 (patch) | |
tree | 581a290c61c1d2c765573bef2e1914367270a3f1 | |
parent | 43001e962d974cb685a8d55aa7517833804e4040 (diff) | |
download | mongo-8fa57b8981903e54d6bee624abd10aa1311493c0.tar.gz |
SERVER-36774 Allow shell assertion functions to take an object for the 'msg'
(cherry picked from commit 2bed54b084995f2c2dd048b6a70b6fd678e1ac30)
-rw-r--r-- | jstests/noPassthrough/shell_assertions.js | 19 | ||||
-rw-r--r-- | src/mongo/shell/assert.js | 6 |
2 files changed, 23 insertions, 2 deletions
diff --git a/jstests/noPassthrough/shell_assertions.js b/jstests/noPassthrough/shell_assertions.js index 8915d087a3e..88fdcaa79ab 100644 --- a/jstests/noPassthrough/shell_assertions.js +++ b/jstests/noPassthrough/shell_assertions.js @@ -133,6 +133,25 @@ assert.eq(true, called, 'called should not have been udpated'); }); + tests.push(function assertShouldAcceptObjectAsMsg() { + const objMsg = {someMessage: 1}; + const err = assert.throws(() => { + assert(false, objMsg); + }); + + assert.neq(-1, + err.message.indexOf(tojson(objMsg)), + 'Error message should have included ' + tojson(objMsg)); + }); + + tests.push(function assertShouldNotAcceptNonObjStringFunctionAsMsg() { + const err = assert.throws(() => { + assert(true, 1234); + }); + + assert.neq(-1, err.message.indexOf("msg parameter must be a ")); + }); + /* assert.automsg tests */ tests.push(function automsgShouldPassToAssert() { diff --git a/src/mongo/shell/assert.js b/src/mongo/shell/assert.js index fa5e18dfe72..24677f96349 100644 --- a/src/mongo/shell/assert.js +++ b/src/mongo/shell/assert.js @@ -97,6 +97,8 @@ assert = (function() { function _processMsg(msg) { if (typeof msg === "function") { return msg(); + } else if (typeof msg === "object") { + return tojson(msg); } return msg; @@ -108,8 +110,8 @@ assert = (function() { if (msg.length !== 0) { doassert("msg function cannot expect any parameters."); } - } else if (typeof msg !== "string") { - doassert("msg parameter must be a string or a function."); + } else if (typeof msg !== "string" && typeof msg !== "object") { + doassert("msg parameter must be a string, function or object."); } if (msg && assert._debug) { |