Argument matcher for an array containing at least the specified entries
This commit is contained in:
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