From 2ab22951a13267dd078d4181ca2cdc21ff510b22 Mon Sep 17 00:00:00 2001 From: slackersoft Date: Tue, 16 Dec 2014 17:29:18 -0800 Subject: [PATCH] Just check if either side implements `asymmetricMatch` - Don't explicitly look for `Any` or `ObjectContaining` [#82295210] --- lib/jasmine-core/jasmine.js | 16 ++++++---------- spec/core/matchers/matchersUtilSpec.js | 14 ++++++++++++++ src/core/matchers/matchersUtil.js | 16 ++++++---------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 8658bb3b..52bbd86c 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -2235,6 +2235,10 @@ getJasmineRequireObj().matchersUtil = function(j$) { } }; + function isAsymmetric(obj) { + return obj && j$.isA_('Function', obj.asymmetricMatch); + } + // Equality function lovingly adapted from isEqual in // [Underscore](http://underscorejs.org) function eq(a, b, aStack, bStack, customTesters) { @@ -2247,19 +2251,11 @@ getJasmineRequireObj().matchersUtil = function(j$) { } } - if (a instanceof j$.Any) { + if (isAsymmetric(a)) { return a.asymmetricMatch(b); } - if (b instanceof j$.Any) { - return b.asymmetricMatch(a); - } - - if (a instanceof j$.ObjectContaining) { - return a.asymmetricMatch(b); - } - - if (b instanceof j$.ObjectContaining) { + if (isAsymmetric(b)) { return b.asymmetricMatch(a); } diff --git a/spec/core/matchers/matchersUtilSpec.js b/spec/core/matchers/matchersUtilSpec.js index cfc0e1cb..d3f99833 100644 --- a/spec/core/matchers/matchersUtilSpec.js +++ b/spec/core/matchers/matchersUtilSpec.js @@ -198,6 +198,20 @@ describe("matchersUtil", function() { expect(j$.matchersUtil.equals(containing, obj)).toBe(true); }); + it("passes when an asymmetric equality tester returns true", function() { + var tester = { asymmetricMatch: function(other) { return true; } }; + + expect(j$.matchersUtil.equals(false, tester)).toBe(true); + expect(j$.matchersUtil.equals(tester, false)).toBe(true); + }); + + it("fails when an asymmetric equality tester returns false", function() { + var tester = { asymmetricMatch: function(other) { return false; } }; + + expect(j$.matchersUtil.equals(true, tester)).toBe(false); + expect(j$.matchersUtil.equals(tester, true)).toBe(false); + }); + it("passes when a custom equality matcher returns true", function() { var tester = function(a, b) { return true; }; diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index a1ca9741..015813f1 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -51,6 +51,10 @@ getJasmineRequireObj().matchersUtil = function(j$) { } }; + function isAsymmetric(obj) { + return obj && j$.isA_('Function', obj.asymmetricMatch); + } + // Equality function lovingly adapted from isEqual in // [Underscore](http://underscorejs.org) function eq(a, b, aStack, bStack, customTesters) { @@ -63,19 +67,11 @@ getJasmineRequireObj().matchersUtil = function(j$) { } } - if (a instanceof j$.Any) { + if (isAsymmetric(a)) { return a.asymmetricMatch(b); } - if (b instanceof j$.Any) { - return b.asymmetricMatch(a); - } - - if (a instanceof j$.ObjectContaining) { - return a.asymmetricMatch(b); - } - - if (b instanceof j$.ObjectContaining) { + if (isAsymmetric(b)) { return b.asymmetricMatch(a); }