Argument matcher for an array containing at least the specified entries
This commit is contained in:
51
spec/core/ArrayContainingSpec.js
Normal file
51
spec/core/ArrayContainingSpec.js
Normal file
@@ -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("<jasmine.arrayContaining");
|
||||
});
|
||||
});
|
||||
@@ -61,4 +61,4 @@ describe("ObjectContaining", function() {
|
||||
|
||||
expect(containing.jasmineToString()).toMatch("<jasmine.objectContaining");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -147,6 +147,12 @@ describe("matchersUtil", function() {
|
||||
expect(j$.matchersUtil.equals(obj, new j$.ObjectContaining({foo: 3}))).toBe(true);
|
||||
});
|
||||
|
||||
it("passes when ArrayContaining is used", function() {
|
||||
var arr = ["foo", "bar"];
|
||||
|
||||
expect(j$.matchersUtil.equals(arr, new j$.ArrayContaining(["bar"]))).toBe(true);
|
||||
});
|
||||
|
||||
it("passes when a custom equality matcher returns true", function() {
|
||||
var tester = function(a, b) { return true; };
|
||||
|
||||
|
||||
35
src/core/ArrayContaining.js
Normal file
35
src/core/ArrayContaining.js
Normal file
@@ -0,0 +1,35 @@
|
||||
getJasmineRequireObj().ArrayContaining = function(j$) {
|
||||
function ArrayContaining(sample) {
|
||||
this.sample = sample;
|
||||
}
|
||||
|
||||
ArrayContaining.prototype.jasmineMatches = function(other, mismatchKeys, mismatchValues) {
|
||||
var className = Object.prototype.toString.call(this.sample);
|
||||
if (className !== "[object Array]") { throw new Error("You must provide an array to arrayContaining, not '"+this.sample+"'."); }
|
||||
|
||||
mismatchKeys = mismatchKeys || [];
|
||||
mismatchValues = mismatchValues || [];
|
||||
var missingItems = [];
|
||||
for (var i = 0; i < this.sample.length; i++) {
|
||||
var item = this.sample[i];
|
||||
if (!j$.matchersUtil.contains(other, item)) {
|
||||
missingItems.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
if (missingItems.length > 0) {
|
||||
mismatchValues.push("expected to have values ['" + missingItems.join("','") + "']");
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
ArrayContaining.prototype.jasmineToString = function () {
|
||||
return "<jasmine.arrayContaining(" + jasmine.pp(this.sample) +")>";
|
||||
};
|
||||
|
||||
return ArrayContaining;
|
||||
};
|
||||
@@ -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({
|
||||
|
||||
@@ -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';
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user