diff options
-rw-r--r-- | jstests/ssl/libs/ssl_x509_role_auth.js | 2 | ||||
-rw-r--r-- | src/mongo/shell/assert.js | 54 |
2 files changed, 50 insertions, 6 deletions
diff --git a/jstests/ssl/libs/ssl_x509_role_auth.js b/jstests/ssl/libs/ssl_x509_role_auth.js index 66e8c648bed..1536ac02069 100644 --- a/jstests/ssl/libs/ssl_x509_role_auth.js +++ b/jstests/ssl/libs/ssl_x509_role_auth.js @@ -16,5 +16,5 @@ let expectedRoles = [{"role": "backup", "db": "admin"}, {"role": "readAnyDatabase", "db": "admin"}]; - assert.docEq(connStatus.authInfo.authenticatedUserRoles, expectedRoles); + assert.sameMembers(connStatus.authInfo.authenticatedUserRoles, expectedRoles); }()); diff --git a/src/mongo/shell/assert.js b/src/mongo/shell/assert.js index 24677f96349..295af1a59c3 100644 --- a/src/mongo/shell/assert.js +++ b/src/mongo/shell/assert.js @@ -181,22 +181,30 @@ assert = (function() { msg, "[" + tojson(a) + "] != [" + tojson(b) + "] are not equal")); }; - assert.docEq = function(a, b, msg) { - _validateAssertionMessage(msg); - + function _isDocEq(a, b) { if (a == b) { - return; + return true; } var aSorted = sortDoc(a); var bSorted = sortDoc(b); if ((aSorted != null && bSorted != null) && friendlyEqual(aSorted, bSorted)) { + return true; + } + + return false; + } + + assert.docEq = function(a, b, msg) { + _validateAssertionMessage(msg); + + if (_isDocEq(a, b)) { return; } doassert(_buildAssertionMessage( - msg, "[" + tojson(aSorted) + "] != [" + tojson(bSorted) + "] are not equal")); + msg, "[" + tojson(a) + "] != [" + tojson(b) + "] are not equal")); }; assert.setEq = function(aSet, bSet, msg) { @@ -213,6 +221,42 @@ assert = (function() { } }; + /** + * Throws if the two arrays do not have the same members, in any order. By default, nested + * arrays must have the same order to be considered equal. + * + * Optionally accepts a compareFn to compare values instead of using docEq. + */ + assert.sameMembers = function(aArr, bArr, msg, compareFn = _isDocEq) { + _validateAssertionMessage(msg); + + const failAssertion = function() { + doassert(_buildAssertionMessage(msg, tojson(aArr) + " != " + tojson(bArr))); + }; + + if (aArr.length !== bArr.length) { + failAssertion(); + } + + // Keep a set of which indices we've already used to avoid double counting values. + const matchedIndicesInRight = new Set(); + for (let a of aArr) { + let foundMatch = false; + for (let i = 0; i < bArr.length; ++i) { + // Sort both inputs in case either is a document. Note: by default this does not + // sort any nested arrays. + if (!matchedIndicesInRight.has(i) && compareFn(a, bArr[i])) { + matchedIndicesInRight.add(i); + foundMatch = true; + break; + } + } + if (!foundMatch) { + failAssertion(); + } + } + }; + assert.eq.automsg = function(a, b) { assert.eq(eval(a), eval(b), "[" + a + "] != [" + b + "]"); }; |