ObjectContaining no longer tries to track exact mismatches
- equals wasn't looking at it anyways, so just bail as soon as something is different.
This commit is contained in:
@@ -1545,26 +1545,21 @@ getJasmineRequireObj().ObjectContaining = function(j$) {
|
|||||||
this.sample = sample;
|
this.sample = sample;
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectContaining.prototype.jasmineMatches = function(other, mismatchKeys, mismatchValues) {
|
ObjectContaining.prototype.jasmineMatches = function(other) {
|
||||||
if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); }
|
if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); }
|
||||||
|
|
||||||
mismatchKeys = mismatchKeys || [];
|
|
||||||
mismatchValues = mismatchValues || [];
|
|
||||||
|
|
||||||
var hasKey = function(obj, keyName) {
|
var hasKey = function(obj, keyName) {
|
||||||
return obj !== null && !j$.util.isUndefined(obj[keyName]);
|
return obj !== null && !j$.util.isUndefined(obj[keyName]);
|
||||||
};
|
};
|
||||||
|
|
||||||
for (var property in this.sample) {
|
for (var property in this.sample) {
|
||||||
if (!hasKey(other, property) && hasKey(this.sample, property)) {
|
if (!hasKey(other, property) && hasKey(this.sample, property) ||
|
||||||
mismatchKeys.push('expected has key \'' + property + '\', but missing from actual.');
|
!j$.matchersUtil.equals(other[property], this.sample[property])) {
|
||||||
}
|
return false;
|
||||||
else if (!j$.matchersUtil.equals(other[property], this.sample[property])) {
|
|
||||||
mismatchValues.push('\'' + property + '\' was \'' + (other[property] ? j$.util.htmlEscape(other[property].toString()) : other[property]) + '\' in actual, but was \'' + (this.sample[property] ? j$.util.htmlEscape(this.sample[property].toString()) : this.sample[property]) + '\' in expected.');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (mismatchKeys.length === 0 && mismatchValues.length === 0);
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
ObjectContaining.prototype.jasmineToString = function() {
|
ObjectContaining.prototype.jasmineToString = function() {
|
||||||
|
|||||||
@@ -32,30 +32,6 @@ describe("ObjectContaining", function() {
|
|||||||
expect(containing.jasmineMatches({foo: "fooVal", bar: "barVal"})).toBe(false);
|
expect(containing.jasmineMatches({foo: "fooVal", bar: "barVal"})).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("mismatchValues parameter must return array with mismatched reason", function() {
|
|
||||||
var containing = new j$.ObjectContaining({foo: "other"});
|
|
||||||
|
|
||||||
var mismatchKeys = [];
|
|
||||||
var mismatchValues = [];
|
|
||||||
|
|
||||||
containing.jasmineMatches({foo: "fooVal", bar: "barVal"}, mismatchKeys, mismatchValues);
|
|
||||||
|
|
||||||
expect(mismatchValues.length).toBe(1);
|
|
||||||
expect(mismatchValues[0]).toEqual("'foo' was 'fooVal' in actual, but was 'other' in expected.");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("adds keys in expected but not actual to the mismatchKeys parameter", function() {
|
|
||||||
var containing = new j$.ObjectContaining({foo: "fooVal"});
|
|
||||||
|
|
||||||
var mismatchKeys = [];
|
|
||||||
var mismatchValues = [];
|
|
||||||
|
|
||||||
containing.jasmineMatches({bar: "barVal"}, mismatchKeys, mismatchValues);
|
|
||||||
|
|
||||||
expect(mismatchKeys.length).toBe(1);
|
|
||||||
expect(mismatchKeys[0]).toEqual("expected has key 'foo', but missing from actual.");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("jasmineToString's itself", function() {
|
it("jasmineToString's itself", function() {
|
||||||
var containing = new j$.ObjectContaining({});
|
var containing = new j$.ObjectContaining({});
|
||||||
|
|
||||||
|
|||||||
@@ -4,26 +4,21 @@ getJasmineRequireObj().ObjectContaining = function(j$) {
|
|||||||
this.sample = sample;
|
this.sample = sample;
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectContaining.prototype.jasmineMatches = function(other, mismatchKeys, mismatchValues) {
|
ObjectContaining.prototype.jasmineMatches = function(other) {
|
||||||
if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); }
|
if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); }
|
||||||
|
|
||||||
mismatchKeys = mismatchKeys || [];
|
|
||||||
mismatchValues = mismatchValues || [];
|
|
||||||
|
|
||||||
var hasKey = function(obj, keyName) {
|
var hasKey = function(obj, keyName) {
|
||||||
return obj !== null && !j$.util.isUndefined(obj[keyName]);
|
return obj !== null && !j$.util.isUndefined(obj[keyName]);
|
||||||
};
|
};
|
||||||
|
|
||||||
for (var property in this.sample) {
|
for (var property in this.sample) {
|
||||||
if (!hasKey(other, property) && hasKey(this.sample, property)) {
|
if (!hasKey(other, property) && hasKey(this.sample, property) ||
|
||||||
mismatchKeys.push('expected has key \'' + property + '\', but missing from actual.');
|
!j$.matchersUtil.equals(other[property], this.sample[property])) {
|
||||||
}
|
return false;
|
||||||
else if (!j$.matchersUtil.equals(other[property], this.sample[property])) {
|
|
||||||
mismatchValues.push('\'' + property + '\' was \'' + (other[property] ? j$.util.htmlEscape(other[property].toString()) : other[property]) + '\' in actual, but was \'' + (this.sample[property] ? j$.util.htmlEscape(this.sample[property].toString()) : this.sample[property]) + '\' in expected.');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (mismatchKeys.length === 0 && mismatchValues.length === 0);
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
ObjectContaining.prototype.jasmineToString = function() {
|
ObjectContaining.prototype.jasmineToString = function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user