From b5b77b318a4919b10826f66d9aaabaf2c7e94cfa Mon Sep 17 00:00:00 2001 From: slackersoft Date: Thu, 3 Oct 2013 18:21:47 -0700 Subject: [PATCH] Argument matcher for an array containing at least the specified entries --- spec/core/ArrayContainingSpec.js | 51 ++++++++++++++++++++++++++ spec/core/ObjectContainingSpec.js | 2 +- spec/core/matchers/matchersUtilSpec.js | 6 +++ src/core/ArrayContaining.js | 35 ++++++++++++++++++ src/core/base.js | 4 ++ src/core/matchers/matchersUtil.js | 9 ++++- src/core/requireCore.js | 1 + 7 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 spec/core/ArrayContainingSpec.js create mode 100644 src/core/ArrayContaining.js diff --git a/spec/core/ArrayContainingSpec.js b/spec/core/ArrayContainingSpec.js new file mode 100644 index 00000000..1ab94b7f --- /dev/null +++ b/spec/core/ArrayContainingSpec.js @@ -0,0 +1,51 @@ +describe("ArrayContaining", function() { + it("matches any actual to an empty array", function() { + var containing = new j$.ArrayContaining([]); + + expect(containing.jasmineMatches("foo")).toBe(true); + }); + + it("does not work when not passed an array", function() { + var containing = new j$.ArrayContaining("foo"); + + expect(function() { + containing.jasmineMatches([]); + }).toThrowError(/not 'foo'/); + }); + + it("matches when the item is in the actual", function() { + var containing = new j$.ArrayContaining(["foo"]); + + expect(containing.jasmineMatches(["foo"])).toBe(true); + }); + + it("matches when additional items are in the actual", function() { + var containing = new j$.ArrayContaining(["foo"]); + + expect(containing.jasmineMatches(["foo", "bar"])).toBe(true); + }); + + it("does not match when the item is not in the actual", function() { + var containing = new j$.ArrayContaining(["foo"]); + + expect(containing.jasmineMatches(["bar"])).toBe(false); + }); + + it("mismatchValues parameter returns array with mismatched reason", function() { + var containing = new j$.ArrayContaining(["foo", "bar"]); + + var mismatchKeys = []; + var mismatchValues = []; + + containing.jasmineMatches([], mismatchKeys, mismatchValues); + + expect(mismatchValues.length).toBe(1); + expect(mismatchValues[0]).toEqual("expected to have values ['foo','bar']") + }); + + it("jasmineToStrings itself", function() { + var containing = new j$.ArrayContaining([]); + + expect(containing.jasmineToString()).toMatch(" 0) { + mismatchValues.push("expected to have values ['" + missingItems.join("','") + "']"); + return false; + } else { + return true; + } + + return true; + }; + + ArrayContaining.prototype.jasmineToString = function () { + return ""; + }; + + return ArrayContaining; +}; diff --git a/src/core/base.js b/src/core/base.js index d36326b3..da8de9d1 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -48,6 +48,10 @@ getJasmineRequireObj().base = function(j$) { return new j$.ObjectContaining(sample); }; + j$.arrayContaining = function(sample) { + return new j$.ArrayContaining(sample); + }; + j$.createSpy = function(name, originalFn) { var spyStrategy = new j$.SpyStrategy({ diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index da16d739..2b8011ce 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -79,6 +79,13 @@ getJasmineRequireObj().matchersUtil = function(j$) { } } + if (b instanceof j$.ArrayContaining) { + result = b.jasmineMatches(a); + if (result) { + return true; + } + } + if (a instanceof Error && b instanceof Error) { return a.message == b.message; } @@ -176,4 +183,4 @@ getJasmineRequireObj().matchersUtil = function(j$) { return typeof obj === 'function'; } } -}; \ No newline at end of file +}; diff --git a/src/core/requireCore.js b/src/core/requireCore.js index 15b949ea..33bdbd16 100644 --- a/src/core/requireCore.js +++ b/src/core/requireCore.js @@ -23,6 +23,7 @@ getJasmineRequireObj().core = function(jRequire) { j$.JsApiReporter = jRequire.JsApiReporter(); j$.matchersUtil = jRequire.matchersUtil(j$); j$.ObjectContaining = jRequire.ObjectContaining(j$); + j$.ArrayContaining = jRequire.ArrayContaining(j$); j$.pp = jRequire.pp(j$); j$.QueueRunner = jRequire.QueueRunner(); j$.ReportDispatcher = jRequire.ReportDispatcher();