summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMark Benvenuto <mark.benvenuto@mongodb.com>2019-06-28 13:09:43 -0400
committerMark Benvenuto <mark.benvenuto@mongodb.com>2019-06-28 13:14:55 -0400
commita362a71c9df6ff4ae27615c3301c330b1b17b0d5 (patch)
tree97d93db2de6cc6fc3c2f03f76929c76e38b37db1 /src
parent51edf741a3db1c222e2980d21bca386ba82b1dd7 (diff)
downloadmongo-a362a71c9df6ff4ae27615c3301c330b1b17b0d5.tar.gz
SERVER-41897 Use assert.sameMembers to compare two sets in jstests/ssl/libs/ssl_x509_role_auth.js
Diffstat (limited to 'src')
-rw-r--r--src/mongo/shell/assert.js54
1 files changed, 49 insertions, 5 deletions
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 + "]");
};