mapContaining and setContaining asymmetric matchers
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
getJasmineRequireObj().MapContaining = function(j$) {
|
||||
function MapContaining(sample) {
|
||||
if (!j$.isMap(sample)) {
|
||||
throw new Error('You must provide a map to `mapContaining`, not ' + j$.pp(sample));
|
||||
}
|
||||
|
||||
this.sample = sample;
|
||||
}
|
||||
|
||||
MapContaining.prototype.asymmetricMatch = function(other, customTesters) {
|
||||
if (!j$.isMap(other)) return false;
|
||||
|
||||
var hasAllMatches = true;
|
||||
j$.util.forEachBreakable(this.sample, function(breakLoop, value, key) {
|
||||
// for each key/value pair in `sample`
|
||||
// there should be at least one pair in `other` whose key and value both match
|
||||
var hasMatch = false;
|
||||
j$.util.forEachBreakable(other, function(oBreakLoop, oValue, oKey) {
|
||||
if (
|
||||
j$.matchersUtil.equals(oKey, key, customTesters)
|
||||
&& j$.matchersUtil.equals(oValue, value, customTesters)
|
||||
) {
|
||||
hasMatch = true;
|
||||
oBreakLoop();
|
||||
}
|
||||
});
|
||||
if (!hasMatch) {
|
||||
hasAllMatches = false;
|
||||
breakLoop();
|
||||
}
|
||||
});
|
||||
|
||||
return hasAllMatches;
|
||||
};
|
||||
|
||||
MapContaining.prototype.jasmineToString = function() {
|
||||
return '<jasmine.mapContaining(' + j$.pp(this.sample) + ')>';
|
||||
};
|
||||
|
||||
return MapContaining;
|
||||
};
|
||||
@@ -0,0 +1,39 @@
|
||||
getJasmineRequireObj().SetContaining = function(j$) {
|
||||
function SetContaining(sample) {
|
||||
if (!j$.isSet(sample)) {
|
||||
throw new Error('You must provide a set to `setContaining`, not ' + j$.pp(sample));
|
||||
}
|
||||
|
||||
this.sample = sample;
|
||||
}
|
||||
|
||||
SetContaining.prototype.asymmetricMatch = function(other, customTesters) {
|
||||
if (!j$.isSet(other)) return false;
|
||||
|
||||
var hasAllMatches = true;
|
||||
j$.util.forEachBreakable(this.sample, function(breakLoop, item) {
|
||||
// for each item in `sample` there should be at least one matching item in `other`
|
||||
// (not using `j$.matchersUtil.contains` because it compares set members by reference,
|
||||
// not by deep value equality)
|
||||
var hasMatch = false;
|
||||
j$.util.forEachBreakable(other, function(oBreakLoop, oItem) {
|
||||
if (j$.matchersUtil.equals(oItem, item, customTesters)) {
|
||||
hasMatch = true;
|
||||
oBreakLoop();
|
||||
}
|
||||
});
|
||||
if (!hasMatch) {
|
||||
hasAllMatches = false;
|
||||
breakLoop();
|
||||
}
|
||||
});
|
||||
|
||||
return hasAllMatches;
|
||||
};
|
||||
|
||||
SetContaining.prototype.jasmineToString = function() {
|
||||
return '<jasmine.setContaining(' + j$.pp(this.sample) + ')>';
|
||||
};
|
||||
|
||||
return SetContaining;
|
||||
};
|
||||
Reference in New Issue
Block a user