From 663fbd0cdb48b8bb69c51f5b68620c006290acfa Mon Sep 17 00:00:00 2001 From: Charles Hansen Date: Thu, 30 Oct 2014 11:57:24 -0700 Subject: [PATCH] toContain works with array-like objects (Arguments, HTMLCollections, etc) Fix #699 --- lib/jasmine-core/jasmine.js | 7 ++++++- spec/core/matchers/matchersUtilSpec.js | 9 +++++++++ src/core/matchers/matchersUtil.js | 7 ++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 2d4f32e8..07e0eabc 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -2195,7 +2195,12 @@ getJasmineRequireObj().matchersUtil = function(j$) { } return false; } - return !!haystack && haystack.indexOf(needle) >= 0; + if (!haystack) { + return false; + } else { + var indexOf = haystack.indexOf || Array.prototype.indexOf; + return indexOf.call(haystack, needle) >= 0; + } }, buildFailureMessage: function() { diff --git a/spec/core/matchers/matchersUtilSpec.js b/spec/core/matchers/matchersUtilSpec.js index 4a981a13..f5b7f20c 100644 --- a/spec/core/matchers/matchersUtilSpec.js +++ b/spec/core/matchers/matchersUtilSpec.js @@ -207,6 +207,15 @@ describe("matchersUtil", function() { it("fails when actual is null", function() { expect(j$.matchersUtil.contains(null, 'A')).toBe(false); }); + + it("passes with array-like objects", function() { + var capturedArgs = null; + function testFunction(){ + capturedArgs = arguments; + } + testFunction('foo', 'bar'); + expect(j$.matchersUtil.contains(capturedArgs, 'bar')).toBe(true); + }); }); describe("buildMessage", function() { diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index b6fb8383..488c337b 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -19,7 +19,12 @@ getJasmineRequireObj().matchersUtil = function(j$) { } return false; } - return !!haystack && haystack.indexOf(needle) >= 0; + if (!haystack) { + return false; + } else { + var indexOf = haystack.indexOf || Array.prototype.indexOf; + return indexOf.call(haystack, needle) >= 0; + } }, buildFailureMessage: function() {