Provide a way to add more matchers in a spec or suite without affecting later specs or suites.

This commit is contained in:
Christian Williams
2009-07-07 23:57:03 -07:00
parent 378d6bb4ff
commit ed00c13b15
3 changed files with 68 additions and 2 deletions

View File

@@ -21,6 +21,7 @@ jasmine.Spec = function(env, suite, description) {
this.results = new jasmine.NestedResults();
this.results.description = description;
this.runs = this.addToQueue;
this.matchersClass = null;
};
jasmine.Spec.prototype.getFullName = function() {
@@ -58,7 +59,7 @@ jasmine.Spec.prototype.expects_that = function(actual) {
* @private
*/
jasmine.Spec.prototype.expect = function(actual) {
return new jasmine.Matchers(this.env, actual, this.results);
return new (this.getMatchersClass_())(this.env, actual, this.results);
};
/**
@@ -80,6 +81,22 @@ jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, message) {
return this;
};
jasmine.Spec.prototype.getMatchersClass_ = function(matcherPrototype) {
return this.matchersClass || jasmine.Matchers;
};
jasmine.Spec.prototype.addMatchers = function(matchersPrototype) {
var parent = this.getMatchersClass_();
var newMatchersClass = function() {
parent.apply(this, arguments);
};
jasmine.util.inherit(newMatchersClass, parent);
for (var method in matchersPrototype) {
newMatchersClass.prototype[method] = matchersPrototype[method];
}
this.matchersClass = newMatchersClass;
};
jasmine.Spec.prototype.resetTimeout = function() {
this.currentTimeout = 0;
this.currentLatchFunction = undefined;