Folded util.objectDifference into MatchersUtil

This was always an implementation detail of objectKeysAreDifferentFormatter,
and didn't really do what its name suggested.

* #1966
This commit is contained in:
Steve Gravrock
2022-05-07 14:03:26 -07:00
parent 468e9577cd
commit 9a27407d35
4 changed files with 20 additions and 76 deletions

View File

@@ -662,12 +662,6 @@ getJasmineRequireObj().util = function(j$) {
return descriptor;
};
util.objectDifference = function(obj, toRemove) {
return j$.MatchersUtil.keys(obj)
.filter(key => !util.has(toRemove, key))
.map(key => [key, obj[key]]);
};
util.has = function(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
};
@@ -5609,9 +5603,17 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
return typeof obj === 'function';
}
// Returns an array of [k, v] pairs for eacch property that's in objA
// and not in objB.
function extraKeysAndValues(objA, objB) {
return MatchersUtil.keys(objA)
.filter(key => !j$.util.has(objB, key))
.map(key => [key, objA[key]]);
}
function objectKeysAreDifferentFormatter(pp, actual, expected, path) {
var missingProperties = j$.util.objectDifference(expected, actual),
extraProperties = j$.util.objectDifference(actual, expected),
var missingProperties = extraKeysAndValues(expected, actual),
extraProperties = extraKeysAndValues(actual, expected),
missingPropertiesMessage = formatKeyValuePairs(pp, missingProperties),
extraPropertiesMessage = formatKeyValuePairs(pp, extraProperties),
messages = [];

View File

@@ -183,66 +183,6 @@ describe('util', function() {
});
});
describe('objectDifference', function() {
it('given two objects A and B, returns the properties in A not present in B', function() {
const a = {
foo: 3,
bar: 4,
baz: 5
};
const b = {
bar: 6,
quux: 7
};
expect(jasmineUnderTest.util.objectDifference(a, b)).toEqual([
['foo', 3],
['baz', 5]
]);
});
it('includes Symbol keys', function() {
const missing = Symbol('missing');
const both = Symbol('both');
const symbolDuplicated1 = Symbol('symbolDuplicated');
const symbolDuplicated2 = Symbol('symbolDuplicated');
const added = Symbol('added');
const a = {
[missing]: 1,
[both]: 2,
[symbolDuplicated1]: 3
};
const b = {
[both]: 'anything',
[symbolDuplicated2]: 4,
[added]: 5
};
expect(jasmineUnderTest.util.objectDifference(a, b)).toEqual([
[missing, 1],
[symbolDuplicated1, 3]
]);
});
it('only looks at own properties of both objects', function() {
function Foo() {}
Foo.prototype.x = 1;
Foo.prototype.y = 2;
const a = new Foo();
a.x = 1;
const b = new Foo();
b.y = 2;
expect(jasmineUnderTest.util.objectDifference(a, b)).toEqual([['x', 1]]);
expect(jasmineUnderTest.util.objectDifference(b, a)).toEqual([['y', 2]]);
});
});
describe('jasmineFile', function() {
it('returns the file containing jasmine.util', function() {
// Chrome sometimes reports foo.js as foo.js/, so tolerate

View File

@@ -558,9 +558,17 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
return typeof obj === 'function';
}
// Returns an array of [k, v] pairs for eacch property that's in objA
// and not in objB.
function extraKeysAndValues(objA, objB) {
return MatchersUtil.keys(objA)
.filter(key => !j$.util.has(objB, key))
.map(key => [key, objA[key]]);
}
function objectKeysAreDifferentFormatter(pp, actual, expected, path) {
var missingProperties = j$.util.objectDifference(expected, actual),
extraProperties = j$.util.objectDifference(actual, expected),
var missingProperties = extraKeysAndValues(expected, actual),
extraProperties = extraKeysAndValues(actual, expected),
missingPropertiesMessage = formatKeyValuePairs(pp, missingProperties),
extraPropertiesMessage = formatKeyValuePairs(pp, extraProperties),
messages = [];

View File

@@ -75,12 +75,6 @@ getJasmineRequireObj().util = function(j$) {
return descriptor;
};
util.objectDifference = function(obj, toRemove) {
return j$.MatchersUtil.keys(obj)
.filter(key => !util.has(toRemove, key))
.map(key => [key, obj[key]]);
};
util.has = function(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
};