Argument matcher for an array containing at least the specified entries

This commit is contained in:
slackersoft
2013-10-03 18:21:47 -07:00
parent 6354ee17e5
commit b5b77b318a
7 changed files with 106 additions and 2 deletions

View 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;
};

View File

@@ -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({

View File

@@ -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';
}
}
};
};

View File

@@ -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();