From da6813ef0d8c37053161445412b8ea4e013838b2 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) Fixes #699 Don't rely on Array.prototype.indexOf for testing containment (not in IE8) --- lib/jasmine-core/jasmine.js | 5 ++++- spec/core/matchers/matchersUtilSpec.js | 11 ++++++++++- src/core/matchers/matchersUtil.js | 5 ++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 2d4f32e8..c7dd1db5 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -2187,7 +2187,9 @@ getJasmineRequireObj().matchersUtil = function(j$) { contains: function(haystack, needle, customTesters) { customTesters = customTesters || []; - if (Object.prototype.toString.apply(haystack) === '[object Array]') { + if ((Object.prototype.toString.apply(haystack) === '[object Array]') || + (!!haystack && !haystack.indexOf)) + { for (var i = 0; i < haystack.length; i++) { if (eq(haystack[i], needle, [], [], customTesters)) { return true; @@ -2195,6 +2197,7 @@ getJasmineRequireObj().matchersUtil = function(j$) { } return false; } + return !!haystack && haystack.indexOf(needle) >= 0; }, diff --git a/spec/core/matchers/matchersUtilSpec.js b/spec/core/matchers/matchersUtilSpec.js index 4a981a13..f778394e 100644 --- a/spec/core/matchers/matchersUtilSpec.js +++ b/spec/core/matchers/matchersUtilSpec.js @@ -174,7 +174,7 @@ describe("matchersUtil", function() { describe("contains", function() { it("passes when expected is a substring of actual", function() { - expect(j$.matchersUtil.contains("ABC", "B")).toBe(true); + expect(j$.matchersUtil.contains("ABC", "BC")).toBe(true); }); it("fails when expected is a not substring of actual", function() { @@ -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..e34e5220 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -11,7 +11,9 @@ getJasmineRequireObj().matchersUtil = function(j$) { contains: function(haystack, needle, customTesters) { customTesters = customTesters || []; - if (Object.prototype.toString.apply(haystack) === '[object Array]') { + if ((Object.prototype.toString.apply(haystack) === '[object Array]') || + (!!haystack && !haystack.indexOf)) + { for (var i = 0; i < haystack.length; i++) { if (eq(haystack[i], needle, [], [], customTesters)) { return true; @@ -19,6 +21,7 @@ getJasmineRequireObj().matchersUtil = function(j$) { } return false; } + return !!haystack && haystack.indexOf(needle) >= 0; },