Merge remote-tracking branch 'upstream/main' into array_buffer

# Conflicts:
#	spec/core/matchers/matchersUtilSpec.js
This commit is contained in:
Surgie Finesse
2021-03-22 18:31:35 +10:00
246 changed files with 21415 additions and 8225 deletions

View File

@@ -1,99 +1,201 @@
getJasmineRequireObj().matchersUtil = function(j$) {
// TODO: what to do about jasmine.pp not being inject? move to JSON.stringify? gut PrettyPrinter?
getJasmineRequireObj().MatchersUtil = function(j$) {
// TODO: convert all uses of j$.pp to use the injected pp
return {
equals: equals,
/**
* _Note:_ Do not construct this directly. Jasmine will construct one and
* pass it to matchers and asymmetric equality testers.
* @name MatchersUtil
* @classdesc Utilities for use in implementing matchers
* @constructor
*/
function MatchersUtil(options) {
options = options || {};
this.customTesters_ = options.customTesters || [];
/**
* Formats a value for use in matcher failure messages and similar contexts,
* taking into account the current set of custom value formatters.
* @function
* @name MatchersUtil#pp
* @since 3.6.0
* @param {*} value The value to pretty-print
* @return {string} The pretty-printed value
*/
this.pp = options.pp || function() {};
}
contains: function(haystack, needle, customTesters) {
customTesters = customTesters || [];
/**
* Determines whether `haystack` contains `needle`, using the same comparison
* logic as {@link MatchersUtil#equals}.
* @function
* @name MatchersUtil#contains
* @since 2.0.0
* @param {*} haystack The collection to search
* @param {*} needle The value to search for
* @param [customTesters] An array of custom equality testers
* @returns {boolean} True if `needle` was found in `haystack`
*/
MatchersUtil.prototype.contains = function(haystack, needle, customTesters) {
if (j$.isSet(haystack)) {
return haystack.has(needle);
}
if ((Object.prototype.toString.apply(haystack) === '[object Set]')) {
return haystack.has(needle);
}
if ((Object.prototype.toString.apply(haystack) === '[object Array]') ||
(!!haystack && !haystack.indexOf))
{
for (var i = 0; i < haystack.length; i++) {
if (equals(haystack[i], needle, customTesters)) {
return true;
}
}
return false;
}
return !!haystack && haystack.indexOf(needle) >= 0;
},
buildFailureMessage: function() {
var args = Array.prototype.slice.call(arguments, 0),
matcherName = args[0],
isNot = args[1],
actual = args[2],
expected = args.slice(3),
englishyPredicate = matcherName.replace(/[A-Z]/g, function(s) { return ' ' + s.toLowerCase(); });
var message = 'Expected ' +
j$.pp(actual) +
(isNot ? ' not ' : ' ') +
englishyPredicate;
if (expected.length > 0) {
for (var i = 0; i < expected.length; i++) {
if (i > 0) {
message += ',';
}
message += ' ' + j$.pp(expected[i]);
if (
Object.prototype.toString.apply(haystack) === '[object Array]' ||
(!!haystack && !haystack.indexOf)
) {
for (var i = 0; i < haystack.length; i++) {
if (this.equals(haystack[i], needle, customTesters)) {
return true;
}
}
return false;
}
return message + '.';
return !!haystack && haystack.indexOf(needle) >= 0;
};
MatchersUtil.prototype.buildFailureMessage = function() {
var self = this;
var args = Array.prototype.slice.call(arguments, 0),
matcherName = args[0],
isNot = args[1],
actual = args[2],
expected = args.slice(3),
englishyPredicate = matcherName.replace(/[A-Z]/g, function(s) {
return ' ' + s.toLowerCase();
});
var message =
'Expected ' +
self.pp(actual) +
(isNot ? ' not ' : ' ') +
englishyPredicate;
if (expected.length > 0) {
for (var i = 0; i < expected.length; i++) {
if (i > 0) {
message += ',';
}
message += ' ' + self.pp(expected[i]);
}
}
return message + '.';
};
MatchersUtil.prototype.asymmetricDiff_ = function(
a,
b,
aStack,
bStack,
customTesters,
diffBuilder
) {
if (j$.isFunction_(b.valuesForDiff_)) {
var values = b.valuesForDiff_(a, this.pp);
this.eq_(
values.other,
values.self,
aStack,
bStack,
customTesters,
diffBuilder
);
} else {
diffBuilder.recordMismatch();
}
};
function isAsymmetric(obj) {
return obj && j$.isA_('Function', obj.asymmetricMatch);
}
MatchersUtil.prototype.asymmetricMatch_ = function(
a,
b,
aStack,
bStack,
customTesters,
diffBuilder
) {
var asymmetricA = j$.isAsymmetricEqualityTester_(a),
asymmetricB = j$.isAsymmetricEqualityTester_(b),
shim,
result;
function asymmetricMatch(a, b, customTesters, diffBuilder) {
var asymmetricA = isAsymmetric(a),
asymmetricB = isAsymmetric(b),
result;
if (asymmetricA && asymmetricB) {
if (asymmetricA === asymmetricB) {
return undefined;
}
shim = j$.asymmetricEqualityTesterArgCompatShim(this, customTesters);
if (asymmetricA) {
result = a.asymmetricMatch(b, customTesters);
result = a.asymmetricMatch(b, shim);
if (!result) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
}
return result;
}
if (asymmetricB) {
result = b.asymmetricMatch(a, customTesters);
result = b.asymmetricMatch(a, shim);
if (!result) {
diffBuilder.record(a, b);
this.asymmetricDiff_(a, b, aStack, bStack, customTesters, diffBuilder);
}
return result;
}
}
};
function equals(a, b, customTesters, diffBuilder) {
customTesters = customTesters || [];
/**
* Determines whether two values are deeply equal to each other.
* @function
* @name MatchersUtil#equals
* @since 2.0.0
* @param {*} a The first value to compare
* @param {*} b The second value to compare
* @param [customTesters] An array of custom equality testers
* @returns {boolean} True if the values are equal
*/
MatchersUtil.prototype.equals = function(
a,
b,
customTestersOrDiffBuilder,
diffBuilderOrNothing
) {
var customTesters, diffBuilder;
if (isDiffBuilder(customTestersOrDiffBuilder)) {
diffBuilder = customTestersOrDiffBuilder;
} else {
customTesters = customTestersOrDiffBuilder;
diffBuilder = diffBuilderOrNothing;
}
customTesters = customTesters || this.customTesters_;
diffBuilder = diffBuilder || j$.NullDiffBuilder();
diffBuilder.setRoots(a, b);
return eq(a, b, [], [], customTesters, diffBuilder);
}
return this.eq_(a, b, [], [], customTesters, diffBuilder);
};
// Equality function lovingly adapted from isEqual in
// [Underscore](http://underscorejs.org)
function eq(a, b, aStack, bStack, customTesters, diffBuilder) {
var result = true, i;
MatchersUtil.prototype.eq_ = function(
a,
b,
aStack,
bStack,
customTesters,
diffBuilder
) {
var result = true,
self = this,
i;
var asymmetricResult = asymmetricMatch(a, b, customTesters, diffBuilder);
var asymmetricResult = this.asymmetricMatch_(
a,
b,
aStack,
bStack,
customTesters,
diffBuilder
);
if (!j$.util.isUndefined(asymmetricResult)) {
return asymmetricResult;
}
@@ -102,7 +204,7 @@ getJasmineRequireObj().matchersUtil = function(j$) {
var customTesterResult = customTesters[i](a, b);
if (!j$.util.isUndefined(customTesterResult)) {
if (!customTesterResult) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
}
return customTesterResult;
}
@@ -111,18 +213,17 @@ getJasmineRequireObj().matchersUtil = function(j$) {
if (a instanceof Error && b instanceof Error) {
result = a.message == b.message;
if (!result) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
}
return result;
}
// Identical objects are equal. `0 === -0`, but they aren't identical.
// See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
if (a === b) {
result = a !== 0 || 1 / a == 1 / b;
if (!result) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
}
return result;
}
@@ -130,13 +231,13 @@ getJasmineRequireObj().matchersUtil = function(j$) {
if (a === null || b === null) {
result = a === b;
if (!result) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
}
return result;
}
var className = Object.prototype.toString.call(a);
if (className != Object.prototype.toString.call(b)) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
return false;
}
switch (className) {
@@ -146,15 +247,16 @@ getJasmineRequireObj().matchersUtil = function(j$) {
// equivalent to `new String("5")`.
result = a == String(b);
if (!result) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
}
return result;
case '[object Number]':
// `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for
// other numeric values.
result = a != +a ? b != +b : (a === 0 ? 1 / a == 1 / b : a == +b);
result =
a != +a ? b != +b : a === 0 && b === 0 ? 1 / a == 1 / b : a == +b;
if (!result) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
}
return result;
case '[object Date]':
@@ -164,7 +266,7 @@ getJasmineRequireObj().matchersUtil = function(j$) {
// of `NaN` are not equivalent.
result = +a == +b;
if (!result) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
}
return result;
// RegExps are compared by their source patterns and flags.
@@ -199,13 +301,15 @@ getJasmineRequireObj().matchersUtil = function(j$) {
}
return result;
case '[object RegExp]':
return a.source == b.source &&
return (
a.source == b.source &&
a.global == b.global &&
a.multiline == b.multiline &&
a.ignoreCase == b.ignoreCase;
a.ignoreCase == b.ignoreCase
);
}
if (typeof a != 'object' || typeof b != 'object') {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
return false;
}
@@ -215,12 +319,12 @@ getJasmineRequireObj().matchersUtil = function(j$) {
// At first try to use DOM3 method isEqualNode
result = a.isEqualNode(b);
if (!result) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
}
return result;
}
if (aIsDomNode || bIsDomNode) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
return false;
}
@@ -236,7 +340,9 @@ getJasmineRequireObj().matchersUtil = function(j$) {
while (length--) {
// Linear search. Performance is inversely proportional to the number of
// unique nested structures.
if (aStack[length] == a) { return bStack[length] == b; }
if (aStack[length] == a) {
return bStack[length] == b;
}
}
// Add the first object to the stack of traversed objects.
aStack.push(a);
@@ -250,19 +356,28 @@ getJasmineRequireObj().matchersUtil = function(j$) {
diffBuilder.withPath('length', function() {
if (aLength !== bLength) {
diffBuilder.record(aLength, bLength);
diffBuilder.recordMismatch();
result = false;
}
});
for (i = 0; i < aLength || i < bLength; i++) {
var formatter = false;
diffBuilder.withPath(i, function() {
if (i >= bLength) {
diffBuilder.record(a[i], void 0, actualArrayIsLongerFormatter);
diffBuilder.recordMismatch(
actualArrayIsLongerFormatter.bind(null, self.pp)
);
result = false;
} else {
result = eq(i < aLength ? a[i] : void 0, i < bLength ? b[i] : void 0, aStack, bStack, customTesters, diffBuilder) && result;
result =
self.eq_(
i < aLength ? a[i] : void 0,
i < bLength ? b[i] : void 0,
aStack,
bStack,
customTesters,
diffBuilder
) && result;
}
});
}
@@ -271,17 +386,17 @@ getJasmineRequireObj().matchersUtil = function(j$) {
}
} else if (j$.isMap(a) && j$.isMap(b)) {
if (a.size != b.size) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
return false;
}
var keysA = [];
var keysB = [];
a.forEach( function( valueA, keyA ) {
keysA.push( keyA );
a.forEach(function(valueA, keyA) {
keysA.push(keyA);
});
b.forEach( function( valueB, keyB ) {
keysB.push( keyB );
b.forEach(function(valueB, keyB) {
keysB.push(keyB);
});
// For both sets of keys, check they map to equal values in both maps.
@@ -302,33 +417,50 @@ getJasmineRequireObj().matchersUtil = function(j$) {
// Only use the cmpKey when one of the keys is asymmetric and the corresponding key matches,
// otherwise explicitly look up the mapKey in the other Map since we want keys with unique
// obj identity (that are otherwise equal) to not match.
if (isAsymmetric(mapKey) || isAsymmetric(cmpKey) &&
eq(mapKey, cmpKey, aStack, bStack, customTesters, j$.NullDiffBuilder())) {
if (
j$.isAsymmetricEqualityTester_(mapKey) ||
(j$.isAsymmetricEqualityTester_(cmpKey) &&
this.eq_(
mapKey,
cmpKey,
aStack,
bStack,
customTesters,
j$.NullDiffBuilder()
))
) {
mapValueB = b.get(cmpKey);
} else {
mapValueB = b.get(mapKey);
}
result = eq(mapValueA, mapValueB, aStack, bStack, customTesters, j$.NullDiffBuilder());
result = this.eq_(
mapValueA,
mapValueB,
aStack,
bStack,
customTesters,
j$.NullDiffBuilder()
);
}
}
if (!result) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
return false;
}
} else if (j$.isSet(a) && j$.isSet(b)) {
if (a.size != b.size) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
return false;
}
var valuesA = [];
a.forEach( function( valueA ) {
valuesA.push( valueA );
a.forEach(function(valueA) {
valuesA.push(valueA);
});
var valuesB = [];
b.forEach( function( valueB ) {
valuesB.push( valueB );
b.forEach(function(valueB) {
valuesB.push(valueB);
});
// For both sets, check they are all contained in the other set
@@ -352,7 +484,14 @@ getJasmineRequireObj().matchersUtil = function(j$) {
otherValue = otherValues[l];
prevStackSize = baseStack.length;
// compare by value equality
found = eq(baseValue, otherValue, baseStack, otherStack, customTesters, j$.NullDiffBuilder());
found = this.eq_(
baseValue,
otherValue,
baseStack,
otherStack,
customTesters,
j$.NullDiffBuilder()
);
if (!found && prevStackSize !== baseStack.length) {
baseStack.splice(prevStackSize);
otherStack.splice(prevStackSize);
@@ -363,31 +502,43 @@ getJasmineRequireObj().matchersUtil = function(j$) {
}
if (!result) {
diffBuilder.record(a, b);
diffBuilder.recordMismatch();
return false;
}
} else if (j$.isURL(a) && j$.isURL(b)) {
// URLs have no enumrable properties, so the default object comparison
// would consider any two URLs to be equal.
return a.toString() === b.toString();
} else {
// Objects with different constructors are not equivalent, but `Object`s
// or `Array`s from different frames are.
var aCtor = a.constructor, bCtor = b.constructor;
if (aCtor !== bCtor &&
isFunction(aCtor) && isFunction(bCtor) &&
a instanceof aCtor && b instanceof bCtor &&
!(aCtor instanceof aCtor && bCtor instanceof bCtor)) {
diffBuilder.record(a, b, constructorsAreDifferentFormatter);
var aCtor = a.constructor,
bCtor = b.constructor;
if (
aCtor !== bCtor &&
isFunction(aCtor) &&
isFunction(bCtor) &&
a instanceof aCtor &&
b instanceof bCtor &&
!(aCtor instanceof aCtor && bCtor instanceof bCtor)
) {
diffBuilder.recordMismatch(
constructorsAreDifferentFormatter.bind(null, this.pp)
);
return false;
}
}
// Deep compare objects.
var aKeys = keys(a, className == '[object Array]'), key;
var aKeys = keys(a, className == '[object Array]'),
key;
size = aKeys.length;
// Ensure that both objects contain the same number of properties before comparing deep equality.
if (keys(b, className == '[object Array]').length !== size) {
diffBuilder.record(a, b, objectKeysAreDifferentFormatter);
diffBuilder.recordMismatch(
objectKeysAreDifferentFormatter.bind(null, this.pp)
);
return false;
}
@@ -395,13 +546,17 @@ getJasmineRequireObj().matchersUtil = function(j$) {
key = aKeys[i];
// Deep compare each member
if (!j$.util.has(b, key)) {
diffBuilder.record(a, b, objectKeysAreDifferentFormatter);
diffBuilder.recordMismatch(
objectKeysAreDifferentFormatter.bind(null, this.pp)
);
result = false;
continue;
}
diffBuilder.withPath(key, function() {
if(!eq(a[key], b[key], aStack, bStack, customTesters, diffBuilder)) {
if (
!self.eq_(a[key], b[key], aStack, bStack, customTesters, diffBuilder)
) {
result = false;
}
});
@@ -416,26 +571,27 @@ getJasmineRequireObj().matchersUtil = function(j$) {
bStack.pop();
return result;
}
};
function keys(obj, isArray) {
var allKeys = Object.keys ? Object.keys(obj) :
(function(o) {
var allKeys = Object.keys
? Object.keys(obj)
: (function(o) {
var keys = [];
for (var key in o) {
if (j$.util.has(o, key)) {
keys.push(key);
}
if (j$.util.has(o, key)) {
keys.push(key);
}
}
return keys;
})(obj);
})(obj);
if (!isArray) {
return allKeys;
}
if (allKeys.length === 0) {
return allKeys;
return allKeys;
}
var extraKeys = [];
@@ -448,59 +604,73 @@ getJasmineRequireObj().matchersUtil = function(j$) {
return extraKeys;
}
function has(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}
function isFunction(obj) {
return typeof obj === 'function';
}
function objectKeysAreDifferentFormatter(actual, expected, path) {
function objectKeysAreDifferentFormatter(pp, actual, expected, path) {
var missingProperties = j$.util.objectDifference(expected, actual),
extraProperties = j$.util.objectDifference(actual, expected),
missingPropertiesMessage = formatKeyValuePairs(missingProperties),
extraPropertiesMessage = formatKeyValuePairs(extraProperties),
messages = [];
extraProperties = j$.util.objectDifference(actual, expected),
missingPropertiesMessage = formatKeyValuePairs(pp, missingProperties),
extraPropertiesMessage = formatKeyValuePairs(pp, extraProperties),
messages = [];
if (!path.depth()) {
path = 'object';
}
if (missingPropertiesMessage.length) {
messages.push('Expected ' + path + ' to have properties' + missingPropertiesMessage);
messages.push(
'Expected ' + path + ' to have properties' + missingPropertiesMessage
);
}
if (extraPropertiesMessage.length) {
messages.push('Expected ' + path + ' not to have properties' + extraPropertiesMessage);
messages.push(
'Expected ' + path + ' not to have properties' + extraPropertiesMessage
);
}
return messages.join('\n');
}
function constructorsAreDifferentFormatter(actual, expected, path) {
function constructorsAreDifferentFormatter(pp, actual, expected, path) {
if (!path.depth()) {
path = 'object';
}
return 'Expected ' +
path + ' to be a kind of ' +
return (
'Expected ' +
path +
' to be a kind of ' +
j$.fnNameFor(expected.constructor) +
', but was ' + j$.pp(actual) + '.';
', but was ' +
pp(actual) +
'.'
);
}
function actualArrayIsLongerFormatter(actual, expected, path) {
return 'Unexpected ' +
path + (path.depth() ? ' = ' : '') +
j$.pp(actual) +
' in array.';
function actualArrayIsLongerFormatter(pp, actual, expected, path) {
return (
'Unexpected ' +
path +
(path.depth() ? ' = ' : '') +
pp(actual) +
' in array.'
);
}
function formatKeyValuePairs(obj) {
function formatKeyValuePairs(pp, obj) {
var formatted = '';
for (var key in obj) {
formatted += '\n ' + key + ': ' + j$.pp(obj[key]);
formatted += '\n ' + key + ': ' + pp(obj[key]);
}
return formatted;
}
function isDiffBuilder(obj) {
return obj && typeof obj.recordMismatch === 'function';
}
return MatchersUtil;
};