Rewrite ES6 Set and Map comparison to ignore insertion order

This commit is contained in:
Sébastien Cevey
2017-08-02 14:59:29 +01:00
parent b256741bec
commit fc6ee243e9
3 changed files with 154 additions and 36 deletions

View File

@@ -239,21 +239,66 @@ getJasmineRequireObj().matchersUtil = function(j$) {
if (!result) {
return false;
}
} else if (className == '[object Set]' || className == '[object Map]') {
} else if (className == '[object Map]') {
if (a.size != b.size) {
diffBuilder.record(a, b);
return false;
}
var iterA = a.entries(), iterB = b.entries();
var valA, valB;
do {
valA = iterA.next();
valB = iterB.next();
if (!eq(valA.value, valB.value, aStack, bStack, customTesters, j$.NullDiffBuilder())) {
diffBuilder.record(a, b);
return false;
// For both sets of keys, check they map to equal values in both maps
var mapKeys = [a.keys(), b.keys()];
var mapIter, mapKeyIt, mapKey;
for (i = 0; result && i < mapKeys.length; i++) {
mapIter = mapKeys[i];
mapKeyIt = mapIter.next();
while (result && !mapKeyIt.done) {
mapKey = mapKeyIt.value;
result = eq(a.get(mapKey), b.get(mapKey), aStack, bStack, customTesters, j$.NullDiffBuilder());
mapKeyIt = mapIter.next();
}
} while (!valA.done && !valB.done);
}
if (!result) {
diffBuilder.record(a, b);
return false;
}
} else if (className == '[object Set]') {
if (a.size != b.size) {
diffBuilder.record(a, b);
return false;
}
// For both sets, check they are all contained in the other set
var setPairs = [[a, b], [b, a]];
var baseIter, baseValueIt, baseValue;
var otherSet, otherIter, otherValueIt, otherValue, found;
for (i = 0; result && i < setPairs.length; i++) {
baseIter = setPairs[i][0].values();
otherSet = setPairs[i][1];
// For each value in the base set...
baseValueIt = baseIter.next();
while (result && !baseValueIt.done) {
baseValue = baseValueIt.value;
// ... test that it is present in the other set
otherIter = otherSet.values();
otherValueIt = otherIter.next();
// Optimisation: start looking for value by object identity
found = otherSet.has(baseValue);
// If not found, compare by value equality
while (!found && !otherValueIt.done) {
otherValue = otherValueIt.value;
found = eq(baseValue, otherValue, aStack, bStack, customTesters, j$.NullDiffBuilder());
otherValueIt = otherIter.next();
}
result = result && found;
baseValueIt = baseIter.next();
}
}
if (!result) {
diffBuilder.record(a, b);
return false;
}
} else {
// Objects with different constructors are not equivalent, but `Object`s