2009-05-29 03:02:15 +00:00
|
|
|
/**
|
2009-06-28 20:36:51 +00:00
|
|
|
* Internal representation of a Jasmine specification, or test.
|
2009-07-09 00:55:25 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2009-05-29 03:02:15 +00:00
|
|
|
* @param {jasmine.Env} env
|
2009-06-28 20:36:51 +00:00
|
|
|
* @param {jasmine.Suite} suite
|
2009-05-29 03:02:15 +00:00
|
|
|
* @param {String} description
|
|
|
|
*/
|
|
|
|
jasmine.Spec = function(env, suite, description) {
|
2009-09-28 18:13:44 +00:00
|
|
|
if (!env) {
|
|
|
|
throw new Error('jasmine.Env() required');
|
2009-09-28 23:23:21 +00:00
|
|
|
}
|
|
|
|
;
|
2009-09-28 18:13:44 +00:00
|
|
|
if (!suite) {
|
|
|
|
throw new Error('jasmine.Suite() required');
|
2009-09-28 23:23:21 +00:00
|
|
|
}
|
|
|
|
;
|
2009-08-01 22:28:39 +00:00
|
|
|
var spec = this;
|
2009-09-28 18:13:44 +00:00
|
|
|
spec.id = env.nextSpecId ? env.nextSpecId() : null;
|
2009-08-01 22:28:39 +00:00
|
|
|
spec.env = env;
|
|
|
|
spec.suite = suite;
|
|
|
|
spec.description = description;
|
2009-08-13 14:52:44 +00:00
|
|
|
spec.queue = new jasmine.Queue(env);
|
2009-08-01 22:28:39 +00:00
|
|
|
|
|
|
|
spec.afterCallbacks = [];
|
|
|
|
spec.spies_ = [];
|
2009-05-29 03:02:15 +00:00
|
|
|
|
2009-09-28 18:13:44 +00:00
|
|
|
spec.results_ = new jasmine.NestedResults();
|
|
|
|
spec.results_.description = description;
|
2009-08-01 22:28:39 +00:00
|
|
|
spec.matchersClass = null;
|
2009-05-29 03:02:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.getFullName = function() {
|
|
|
|
return this.suite.getFullName() + ' ' + this.description + '.';
|
|
|
|
};
|
|
|
|
|
2009-09-28 18:13:44 +00:00
|
|
|
|
|
|
|
jasmine.Spec.prototype.results = function() {
|
|
|
|
return this.results_;
|
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.log = function(message) {
|
|
|
|
return this.results_.log(message);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @deprecated */
|
2009-05-29 03:02:15 +00:00
|
|
|
jasmine.Spec.prototype.getResults = function() {
|
2009-09-28 18:13:44 +00:00
|
|
|
return this.results_;
|
2009-05-29 03:02:15 +00:00
|
|
|
};
|
|
|
|
|
2009-07-31 05:31:57 +00:00
|
|
|
jasmine.Spec.prototype.runs = function (func) {
|
2009-07-30 05:27:11 +00:00
|
|
|
var block = new jasmine.Block(this.env, func, this);
|
2009-08-04 06:09:50 +00:00
|
|
|
this.addToQueue(block);
|
2009-07-31 05:31:57 +00:00
|
|
|
return this;
|
|
|
|
};
|
2009-05-29 03:02:15 +00:00
|
|
|
|
2009-08-04 06:09:50 +00:00
|
|
|
jasmine.Spec.prototype.addToQueue = function (block) {
|
|
|
|
if (this.queue.isRunning()) {
|
|
|
|
this.queue.insertNext(block);
|
|
|
|
} else {
|
|
|
|
this.queue.add(block);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-28 20:36:51 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2009-05-29 03:02:15 +00:00
|
|
|
jasmine.Spec.prototype.expects_that = function(actual) {
|
|
|
|
return this.expect(actual);
|
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.expect = function(actual) {
|
2009-09-28 18:13:44 +00:00
|
|
|
return new (this.getMatchersClass_())(this.env, actual, this.results_);
|
2009-05-29 03:02:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.waits = function(timeout) {
|
2009-07-30 05:27:11 +00:00
|
|
|
var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this);
|
2009-08-04 06:09:50 +00:00
|
|
|
this.addToQueue(waitsFunc);
|
2009-05-29 03:02:15 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2009-07-30 05:27:11 +00:00
|
|
|
jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) {
|
|
|
|
var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this);
|
2009-08-04 06:09:50 +00:00
|
|
|
this.addToQueue(waitsForFunc);
|
2009-05-29 03:02:15 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2009-09-28 18:13:44 +00:00
|
|
|
jasmine.Spec.prototype.fail = function (e) {
|
|
|
|
this.results_.addResult(new jasmine.ExpectationResult(false, e ? jasmine.util.formatException(e) : null, null));
|
2009-07-30 05:27:11 +00:00
|
|
|
};
|
|
|
|
|
2009-07-08 06:59:38 +00:00
|
|
|
jasmine.Spec.prototype.getMatchersClass_ = function() {
|
2009-07-08 06:57:03 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2009-05-29 03:02:15 +00:00
|
|
|
jasmine.Spec.prototype.finishCallback = function() {
|
2009-07-09 01:01:05 +00:00
|
|
|
this.env.reporter.reportSpecResults(this);
|
2009-05-29 03:02:15 +00:00
|
|
|
};
|
|
|
|
|
2009-08-04 05:22:13 +00:00
|
|
|
jasmine.Spec.prototype.finish = function(onComplete) {
|
2009-05-29 03:02:15 +00:00
|
|
|
this.removeAllSpies();
|
|
|
|
this.finishCallback();
|
2009-08-04 05:22:13 +00:00
|
|
|
if (onComplete) {
|
|
|
|
onComplete();
|
2009-08-01 22:28:39 +00:00
|
|
|
}
|
2009-05-29 03:02:15 +00:00
|
|
|
};
|
|
|
|
|
2009-08-05 02:24:00 +00:00
|
|
|
jasmine.Spec.prototype.after = function(doAfter, test) {
|
|
|
|
|
2009-09-28 23:23:21 +00:00
|
|
|
if (this.queue.isRunning()) {
|
2009-08-05 02:24:00 +00:00
|
|
|
this.queue.add(new jasmine.Block(this.env, doAfter, this));
|
|
|
|
} else {
|
2009-09-28 23:23:21 +00:00
|
|
|
this.afterCallbacks.unshift(doAfter);
|
2009-08-05 02:24:00 +00:00
|
|
|
}
|
2009-05-29 03:02:15 +00:00
|
|
|
};
|
|
|
|
|
2009-08-04 05:22:13 +00:00
|
|
|
jasmine.Spec.prototype.execute = function(onComplete) {
|
2009-08-01 21:56:29 +00:00
|
|
|
var spec = this;
|
|
|
|
if (!spec.env.specFilter(spec)) {
|
2009-09-28 18:13:44 +00:00
|
|
|
spec.results_.skipped = true;
|
2009-08-19 14:55:51 +00:00
|
|
|
spec.finish(onComplete);
|
2009-05-29 03:02:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-08-21 02:21:07 +00:00
|
|
|
this.env.reporter.log('>> Jasmine Running ' + this.suite.description + ' ' + this.description + '...');
|
2009-05-29 03:02:15 +00:00
|
|
|
|
2009-08-01 21:56:29 +00:00
|
|
|
spec.env.currentSpec = spec;
|
|
|
|
spec.env.currentlyRunningTests = true;
|
2009-05-29 03:02:15 +00:00
|
|
|
|
2009-08-04 14:12:39 +00:00
|
|
|
spec.addBeforesAndAftersToQueue();
|
2009-05-29 03:02:15 +00:00
|
|
|
|
2009-08-04 06:09:50 +00:00
|
|
|
spec.queue.start(function () {
|
|
|
|
spec.finish(onComplete);
|
|
|
|
});
|
2009-08-01 21:56:29 +00:00
|
|
|
spec.env.currentlyRunningTests = false;
|
2009-05-29 03:02:15 +00:00
|
|
|
};
|
|
|
|
|
2009-08-04 14:12:39 +00:00
|
|
|
jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() {
|
2009-09-28 23:23:21 +00:00
|
|
|
var runner = this.env.currentRunner();
|
2009-05-29 03:02:15 +00:00
|
|
|
for (var suite = this.suite; suite; suite = suite.parentSuite) {
|
2009-09-28 23:23:21 +00:00
|
|
|
for (var i = 0; i < suite.before_.length; i++) {
|
|
|
|
this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this));
|
2009-08-01 17:43:03 +00:00
|
|
|
}
|
2009-08-05 02:24:00 +00:00
|
|
|
}
|
2009-09-28 23:23:21 +00:00
|
|
|
for (var i = 0; i < runner.before_.length; i++) {
|
|
|
|
this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this));
|
|
|
|
}
|
2009-08-05 02:24:00 +00:00
|
|
|
for (i = 0; i < this.afterCallbacks.length; i++) {
|
|
|
|
this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this));
|
|
|
|
}
|
|
|
|
for (suite = this.suite; suite; suite = suite.parentSuite) {
|
2009-09-28 23:23:21 +00:00
|
|
|
for (var i = 0; i < suite.after_.length; i++) {
|
|
|
|
this.queue.add(new jasmine.Block(this.env, suite.after_[i], this));
|
2009-08-01 17:43:03 +00:00
|
|
|
}
|
2009-09-28 23:23:21 +00:00
|
|
|
}
|
|
|
|
for (var i = 0; i < runner.after_.length; i++) {
|
|
|
|
this.queue.add(new jasmine.Block(this.env, runner.after_[i], this));
|
2009-05-29 03:02:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.explodes = function() {
|
|
|
|
throw 'explodes function should not have been called';
|
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.spyOn = function(obj, methodName, ignoreMethodDoesntExist) {
|
|
|
|
if (obj == undefined) {
|
|
|
|
throw "spyOn could not find an object to spy upon for " + methodName + "()";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ignoreMethodDoesntExist && obj[methodName] === undefined) {
|
|
|
|
throw methodName + '() method does not exist';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ignoreMethodDoesntExist && obj[methodName] && obj[methodName].isSpy) {
|
|
|
|
throw new Error(methodName + ' has already been spied upon');
|
|
|
|
}
|
|
|
|
|
|
|
|
var spyObj = jasmine.createSpy(methodName);
|
|
|
|
|
|
|
|
this.spies_.push(spyObj);
|
|
|
|
spyObj.baseObj = obj;
|
|
|
|
spyObj.methodName = methodName;
|
|
|
|
spyObj.originalValue = obj[methodName];
|
|
|
|
|
|
|
|
obj[methodName] = spyObj;
|
|
|
|
|
|
|
|
return spyObj;
|
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.removeAllSpies = function() {
|
|
|
|
for (var i = 0; i < this.spies_.length; i++) {
|
|
|
|
var spy = this.spies_[i];
|
|
|
|
spy.baseObj[spy.methodName] = spy.originalValue;
|
|
|
|
}
|
|
|
|
this.spies_ = [];
|
|
|
|
};
|
|
|
|
|