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:
slackersoft
2014-12-05 08:06:20 -08:00
parent 4d5f27d359
commit 02161b7d48
4 changed files with 12 additions and 46 deletions

View File

@@ -948,7 +948,7 @@ getJasmineRequireObj().Any = function() {
if (this.expectedObject == Object) {
return typeof other == 'object';
}
if (this.expectedObject == Boolean) {
return typeof other == 'boolean';
}
@@ -1545,26 +1545,21 @@ getJasmineRequireObj().ObjectContaining = function(j$) {
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+'\'.'); }
mismatchKeys = mismatchKeys || [];
mismatchValues = mismatchValues || [];
var hasKey = function(obj, keyName) {
return obj !== null && !j$.util.isUndefined(obj[keyName]);
};
for (var property in this.sample) {
if (!hasKey(other, property) && hasKey(this.sample, property)) {
mismatchKeys.push('expected has key \'' + property + '\', but missing from actual.');
}
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.');
if (!hasKey(other, property) && hasKey(this.sample, property) ||
!j$.matchersUtil.equals(other[property], this.sample[property])) {
return false;
}
}
return (mismatchKeys.length === 0 && mismatchValues.length === 0);
return true;
};
ObjectContaining.prototype.jasmineToString = function() {

View File

@@ -31,30 +31,6 @@ describe("ObjectContaining", function() {
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() {
var containing = new j$.ObjectContaining({});

View File

@@ -20,7 +20,7 @@ getJasmineRequireObj().Any = function() {
if (this.expectedObject == Object) {
return typeof other == 'object';
}
if (this.expectedObject == Boolean) {
return typeof other == 'boolean';
}

View File

@@ -4,26 +4,21 @@ getJasmineRequireObj().ObjectContaining = function(j$) {
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+'\'.'); }
mismatchKeys = mismatchKeys || [];
mismatchValues = mismatchValues || [];
var hasKey = function(obj, keyName) {
return obj !== null && !j$.util.isUndefined(obj[keyName]);
};
for (var property in this.sample) {
if (!hasKey(other, property) && hasKey(this.sample, property)) {
mismatchKeys.push('expected has key \'' + property + '\', but missing from actual.');
}
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.');
if (!hasKey(other, property) && hasKey(this.sample, property) ||
!j$.matchersUtil.equals(other[property], this.sample[property])) {
return false;
}
}
return (mismatchKeys.length === 0 && mismatchValues.length === 0);
return true;
};
ObjectContaining.prototype.jasmineToString = function() {