From 02161b7d48b92402c662a7197c441594fc61c823 Mon Sep 17 00:00:00 2001 From: slackersoft Date: Fri, 5 Dec 2014 08:06:20 -0800 Subject: [PATCH] ObjectContaining no longer tries to track exact mismatches - equals wasn't looking at it anyways, so just bail as soon as something is different. --- lib/jasmine-core/jasmine.js | 17 ++++++----------- spec/core/ObjectContainingSpec.js | 24 ------------------------ src/core/Any.js | 2 +- src/core/ObjectContaining.js | 15 +++++---------- 4 files changed, 12 insertions(+), 46 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index b225eca1..5c543254 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -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() { diff --git a/spec/core/ObjectContainingSpec.js b/spec/core/ObjectContainingSpec.js index 27bd71ef..8102bdfb 100644 --- a/spec/core/ObjectContainingSpec.js +++ b/spec/core/ObjectContainingSpec.js @@ -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({}); diff --git a/src/core/Any.js b/src/core/Any.js index 66213371..62992ecb 100644 --- a/src/core/Any.js +++ b/src/core/Any.js @@ -20,7 +20,7 @@ getJasmineRequireObj().Any = function() { if (this.expectedObject == Object) { return typeof other == 'object'; } - + if (this.expectedObject == Boolean) { return typeof other == 'boolean'; } diff --git a/src/core/ObjectContaining.js b/src/core/ObjectContaining.js index a9d05185..5fcb77ca 100644 --- a/src/core/ObjectContaining.js +++ b/src/core/ObjectContaining.js @@ -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() {