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) {
|
|
|
|
this.id = env.nextSpecId_++;
|
|
|
|
this.env = env;
|
|
|
|
this.suite = suite;
|
|
|
|
this.description = description;
|
|
|
|
this.queue = [];
|
|
|
|
this.finished = false;
|
|
|
|
this.afterCallbacks = [];
|
|
|
|
this.spies_ = [];
|
|
|
|
|
|
|
|
this.results = new jasmine.NestedResults();
|
|
|
|
this.results.description = description;
|
|
|
|
this.runs = this.addToQueue;
|
2009-07-08 06:57:03 +00:00
|
|
|
this.matchersClass = null;
|
2009-05-29 03:02:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.getFullName = function() {
|
|
|
|
return this.suite.getFullName() + ' ' + this.description + '.';
|
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.getResults = function() {
|
|
|
|
return this.results;
|
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.addToQueue = function(func) {
|
2009-07-30 05:27:11 +00:00
|
|
|
var block = new jasmine.Block(this.env, func, this);
|
2009-05-29 03:02:15 +00:00
|
|
|
|
2009-07-30 05:27:11 +00:00
|
|
|
this.setNextOnLastInQueue(block);
|
|
|
|
|
|
|
|
this.queue.push(block);
|
2009-05-29 03:02:15 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2009-06-28 20:36:51 +00:00
|
|
|
/**
|
2009-07-30 05:27:11 +00:00
|
|
|
* @private
|
2009-06-28 20:36:51 +00:00
|
|
|
*/
|
2009-05-29 03:02:15 +00:00
|
|
|
jasmine.Spec.prototype.expect = function(actual) {
|
2009-07-08 06:57:03 +00:00
|
|
|
return new (this.getMatchersClass_())(this.env, actual, this.results);
|
2009-05-29 03:02:15 +00:00
|
|
|
};
|
|
|
|
|
2009-07-30 05:27:11 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
jasmine.Spec.prototype.setNextOnLastInQueue = function (block) {
|
|
|
|
if (this.queue.length > 0) {
|
|
|
|
var previousBlock = this.queue[this.queue.length - 1];
|
|
|
|
previousBlock.next = function() {
|
|
|
|
block.execute();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-28 20:36:51 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
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);
|
|
|
|
this.setNextOnLastInQueue(waitsFunc);
|
|
|
|
this.queue.push(waitsFunc);
|
2009-05-29 03:02:15 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2009-06-28 20:36:51 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
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);
|
|
|
|
this.setNextOnLastInQueue(waitsForFunc);
|
|
|
|
this.queue.push(waitsForFunc);
|
2009-05-29 03:02:15 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2009-07-30 05:27:11 +00:00
|
|
|
jasmine.Spec.prototype.failWithException = function (e) {
|
|
|
|
this.results.addResult(new jasmine.ExpectationResult(false, jasmine.util.formatException(e), null));
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.finish = function() {
|
|
|
|
this.safeExecuteAfters();
|
|
|
|
|
|
|
|
this.removeAllSpies();
|
|
|
|
this.finishCallback();
|
|
|
|
this.finished = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.after = function(doAfter) {
|
|
|
|
this.afterCallbacks.unshift(doAfter);
|
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.execute = function() {
|
|
|
|
if (!this.env.specFilter(this)) {
|
|
|
|
this.results.skipped = true;
|
|
|
|
this.finishCallback();
|
|
|
|
this.finished = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.env.currentSpec = this;
|
|
|
|
this.env.currentlyRunningTests = true;
|
|
|
|
|
|
|
|
this.safeExecuteBefores();
|
|
|
|
|
|
|
|
if (this.queue[0]) {
|
|
|
|
this.queue[0].execute();
|
|
|
|
} else {
|
|
|
|
this.finish();
|
|
|
|
}
|
|
|
|
this.env.currentlyRunningTests = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.safeExecuteBefores = function() {
|
|
|
|
var befores = [];
|
|
|
|
for (var suite = this.suite; suite; suite = suite.parentSuite) {
|
|
|
|
if (suite.beforeEachFunction) befores.push(suite.beforeEachFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (befores.length) {
|
|
|
|
this.safeExecuteBeforeOrAfter(befores.pop());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.safeExecuteAfters = function() {
|
|
|
|
for (var suite = this.suite; suite; suite = suite.parentSuite) {
|
|
|
|
if (suite.afterEachFunction) this.safeExecuteBeforeOrAfter(suite.afterEachFunction);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
jasmine.Spec.prototype.safeExecuteBeforeOrAfter = function(func) {
|
|
|
|
try {
|
|
|
|
func.apply(this);
|
|
|
|
} catch (e) {
|
|
|
|
this.results.addResult(new jasmine.ExpectationResult(false, func.typeName + '() fail: ' + jasmine.util.formatException(e), null));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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_ = [];
|
|
|
|
};
|
|
|
|
|