Allow asymmetric equality testers to preempt their symmetric brethren
- Also allow them to be compared to each other properly. Fixes #540
This commit is contained in:
@@ -2239,11 +2239,33 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
|||||||
return obj && j$.isA_('Function', obj.asymmetricMatch);
|
return obj && j$.isA_('Function', obj.asymmetricMatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function asymmetricMatch(a, b) {
|
||||||
|
var asymmetricA = isAsymmetric(a),
|
||||||
|
asymmetricB = isAsymmetric(b);
|
||||||
|
|
||||||
|
if (asymmetricA && asymmetricB) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (asymmetricA) {
|
||||||
|
return a.asymmetricMatch(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (asymmetricB) {
|
||||||
|
return b.asymmetricMatch(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Equality function lovingly adapted from isEqual in
|
// Equality function lovingly adapted from isEqual in
|
||||||
// [Underscore](http://underscorejs.org)
|
// [Underscore](http://underscorejs.org)
|
||||||
function eq(a, b, aStack, bStack, customTesters) {
|
function eq(a, b, aStack, bStack, customTesters) {
|
||||||
var result = true;
|
var result = true;
|
||||||
|
|
||||||
|
var asymmetricResult = asymmetricMatch(a, b);
|
||||||
|
if (!j$.util.isUndefined(asymmetricResult)) {
|
||||||
|
return asymmetricResult;
|
||||||
|
}
|
||||||
|
|
||||||
for (var i = 0; i < customTesters.length; i++) {
|
for (var i = 0; i < customTesters.length; i++) {
|
||||||
var customTesterResult = customTesters[i](a, b);
|
var customTesterResult = customTesters[i](a, b);
|
||||||
if (!j$.util.isUndefined(customTesterResult)) {
|
if (!j$.util.isUndefined(customTesterResult)) {
|
||||||
@@ -2251,14 +2273,6 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isAsymmetric(a)) {
|
|
||||||
return a.asymmetricMatch(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isAsymmetric(b)) {
|
|
||||||
return b.asymmetricMatch(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (a instanceof Error && b instanceof Error) {
|
if (a instanceof Error && b instanceof Error) {
|
||||||
return a.message == b.message;
|
return a.message == b.message;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -235,6 +235,21 @@ describe("matchersUtil", function() {
|
|||||||
|
|
||||||
expect(j$.matchersUtil.equals(1, 1, [tester])).toBe(false);
|
expect(j$.matchersUtil.equals(1, 1, [tester])).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("passes for an asymmetric equality tester that returns true when a custom equality tester return false", function() {
|
||||||
|
var asymmetricTester = { asymmetricMatch: function(other) { return true; } },
|
||||||
|
symmetricTester = function(a, b) { return false; };
|
||||||
|
|
||||||
|
expect(j$.matchersUtil.equals(asymmetricTester, true, [symmetricTester])).toBe(true);
|
||||||
|
expect(j$.matchersUtil.equals(true, asymmetricTester, [symmetricTester])).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("passes when an Any is compared to an Any that checks for the same type", function() {
|
||||||
|
var any1 = new j$.Any(Function),
|
||||||
|
any2 = new j$.Any(Function);
|
||||||
|
|
||||||
|
expect(j$.matchersUtil.equals(any1, any2)).toBe(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("contains", function() {
|
describe("contains", function() {
|
||||||
|
|||||||
@@ -55,11 +55,33 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
|||||||
return obj && j$.isA_('Function', obj.asymmetricMatch);
|
return obj && j$.isA_('Function', obj.asymmetricMatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function asymmetricMatch(a, b) {
|
||||||
|
var asymmetricA = isAsymmetric(a),
|
||||||
|
asymmetricB = isAsymmetric(b);
|
||||||
|
|
||||||
|
if (asymmetricA && asymmetricB) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (asymmetricA) {
|
||||||
|
return a.asymmetricMatch(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (asymmetricB) {
|
||||||
|
return b.asymmetricMatch(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Equality function lovingly adapted from isEqual in
|
// Equality function lovingly adapted from isEqual in
|
||||||
// [Underscore](http://underscorejs.org)
|
// [Underscore](http://underscorejs.org)
|
||||||
function eq(a, b, aStack, bStack, customTesters) {
|
function eq(a, b, aStack, bStack, customTesters) {
|
||||||
var result = true;
|
var result = true;
|
||||||
|
|
||||||
|
var asymmetricResult = asymmetricMatch(a, b);
|
||||||
|
if (!j$.util.isUndefined(asymmetricResult)) {
|
||||||
|
return asymmetricResult;
|
||||||
|
}
|
||||||
|
|
||||||
for (var i = 0; i < customTesters.length; i++) {
|
for (var i = 0; i < customTesters.length; i++) {
|
||||||
var customTesterResult = customTesters[i](a, b);
|
var customTesterResult = customTesters[i](a, b);
|
||||||
if (!j$.util.isUndefined(customTesterResult)) {
|
if (!j$.util.isUndefined(customTesterResult)) {
|
||||||
@@ -67,14 +89,6 @@ getJasmineRequireObj().matchersUtil = function(j$) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isAsymmetric(a)) {
|
|
||||||
return a.asymmetricMatch(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isAsymmetric(b)) {
|
|
||||||
return b.asymmetricMatch(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (a instanceof Error && b instanceof Error) {
|
if (a instanceof Error && b instanceof Error) {
|
||||||
return a.message == b.message;
|
return a.message == b.message;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user