New matcher "hashContaining" similar to rspec's hash_including
This commit is contained in:
@@ -369,3 +369,32 @@ jasmine.Matchers.Any.prototype.toString = function() {
|
||||
return '<jasmine.any(' + this.expectedClass + ')>';
|
||||
};
|
||||
|
||||
jasmine.Matchers.HashContaining = function (sample) {
|
||||
this.sample = sample;
|
||||
};
|
||||
|
||||
jasmine.Matchers.HashContaining.prototype.matches = function(other, mismatchKeys, mismatchValues) {
|
||||
mismatchKeys = mismatchKeys || [];
|
||||
mismatchValues = mismatchValues || [];
|
||||
|
||||
var env = jasmine.getEnv();
|
||||
|
||||
var hasKey = function(obj, keyName) {
|
||||
return obj != null && obj[keyName] !== jasmine.undefined;
|
||||
};
|
||||
|
||||
for (var property in this.sample) {
|
||||
if (!hasKey(other, property) && hasKey(this.sample, property)) {
|
||||
mismatchKeys.push("expected has key '" + property + "', but missing from actual.");
|
||||
}
|
||||
else if (!env.equals_(this.sample[property], other[property], mismatchKeys, mismatchValues)) {
|
||||
mismatchValues.push("'" + property + "' was '" + (other[property] ? jasmine.util.htmlEscape(other[property].toString()) : other[property]) + "' in expected, but was '" + (this.sample[property] ? jasmine.util.htmlEscape(this.sample[property].toString()) : this.sample[property]) + "' in actual.");
|
||||
}
|
||||
}
|
||||
|
||||
return (mismatchKeys.length === 0 && mismatchValues.length === 0);
|
||||
};
|
||||
|
||||
jasmine.Matchers.HashContaining.prototype.toString = function () {
|
||||
return "<jasmine.hashContaining(" + jasmine.pp(this.sample) + ")>";
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user