172 lines
5.1 KiB
JavaScript
172 lines
5.1 KiB
JavaScript
getJasmineRequireObj().Spec = function(j$) {
|
|
function Spec(attrs) {
|
|
this.expectationFactory = attrs.expectationFactory;
|
|
this.resultCallback = attrs.resultCallback || function() {};
|
|
this.id = attrs.id;
|
|
this.description = attrs.description || '';
|
|
this.queueableFn = attrs.queueableFn;
|
|
this.beforeAndAfterFns = attrs.beforeAndAfterFns || function() { return {befores: [], afters: []}; };
|
|
this.userContext = attrs.userContext || function() { return {}; };
|
|
this.onStart = attrs.onStart || function() {};
|
|
this.getSpecName = attrs.getSpecName || function() { return ''; };
|
|
this.expectationResultFactory = attrs.expectationResultFactory || function() { };
|
|
this.queueRunnerFactory = attrs.queueRunnerFactory || function() {};
|
|
this.catchingExceptions = attrs.catchingExceptions || function() { return true; };
|
|
this.throwOnExpectationFailure = !!attrs.throwOnExpectationFailure;
|
|
|
|
if (!this.queueableFn.fn) {
|
|
this.pend();
|
|
}
|
|
|
|
/**
|
|
* @typedef SpecResult
|
|
* @property {Int} id - The unique id of this spec.
|
|
* @property {String} description - The description passed to the {@link it} that created this spec.
|
|
* @property {String} fullName - The full description including all ancestors of this spec.
|
|
* @property {Expectation[]} failedExpectations - The list of expectations that failed during execution of this spec.
|
|
* @property {Expectation[]} passedExpectations - The list of expectations that passed during execution of this spec.
|
|
* @property {String} pendingReason - If the spec is {@link pending}, this will be the reason.
|
|
* @property {String} status - Once the spec has completed, this string represents the pass/fail status of this spec.
|
|
*/
|
|
this.result = {
|
|
id: this.id,
|
|
description: this.description,
|
|
fullName: this.getFullName(),
|
|
failedExpectations: [],
|
|
passedExpectations: [],
|
|
pendingReason: ''
|
|
};
|
|
}
|
|
|
|
Spec.prototype.addExpectationResult = function(passed, data, isError) {
|
|
var expectationResult = this.expectationResultFactory(data);
|
|
if (passed) {
|
|
this.result.passedExpectations.push(expectationResult);
|
|
} else {
|
|
this.result.failedExpectations.push(expectationResult);
|
|
|
|
if (this.throwOnExpectationFailure && !isError) {
|
|
throw new j$.errors.ExpectationFailed();
|
|
}
|
|
}
|
|
};
|
|
|
|
Spec.prototype.expect = function(actual) {
|
|
return this.expectationFactory(actual, this);
|
|
};
|
|
|
|
Spec.prototype.execute = function(onComplete, enabled) {
|
|
var self = this;
|
|
|
|
this.onStart(this);
|
|
|
|
var fns = this.beforeAndAfterFns();
|
|
var regularFns = fns.befores.concat(this.queueableFn);
|
|
|
|
var runnerConfig = {
|
|
isLeaf: true,
|
|
queueableFns: regularFns,
|
|
cleanupFns: fns.afters,
|
|
onException: function() { self.onException.apply(self, arguments); },
|
|
onComplete: complete,
|
|
userContext: this.userContext()
|
|
};
|
|
|
|
if (!this.isExecutable() || this.markedPending || enabled === false) {
|
|
runnerConfig.queueableFns = [];
|
|
runnerConfig.cleanupFns = [];
|
|
runnerConfig.onComplete = function() { complete(enabled); };
|
|
}
|
|
|
|
this.queueRunnerFactory(runnerConfig);
|
|
|
|
function complete(enabledAgain) {
|
|
self.result.status = self.status(enabledAgain);
|
|
self.resultCallback(self.result);
|
|
|
|
if (onComplete) {
|
|
onComplete();
|
|
}
|
|
}
|
|
};
|
|
|
|
Spec.prototype.onException = function onException(e) {
|
|
if (Spec.isPendingSpecException(e)) {
|
|
this.pend(extractCustomPendingMessage(e));
|
|
return;
|
|
}
|
|
|
|
if (e instanceof j$.errors.ExpectationFailed) {
|
|
return;
|
|
}
|
|
|
|
this.addExpectationResult(false, {
|
|
matcherName: '',
|
|
passed: false,
|
|
expected: '',
|
|
actual: '',
|
|
error: e
|
|
}, true);
|
|
};
|
|
|
|
Spec.prototype.disable = function() {
|
|
this.disabled = true;
|
|
};
|
|
|
|
Spec.prototype.pend = function(message) {
|
|
this.markedPending = true;
|
|
if (message) {
|
|
this.result.pendingReason = message;
|
|
}
|
|
};
|
|
|
|
Spec.prototype.getResult = function() {
|
|
this.result.status = this.status();
|
|
return this.result;
|
|
};
|
|
|
|
Spec.prototype.status = function(enabled) {
|
|
if (this.disabled || enabled === false) {
|
|
return 'disabled';
|
|
}
|
|
|
|
if (this.markedPending) {
|
|
return 'pending';
|
|
}
|
|
|
|
if (this.result.failedExpectations.length > 0) {
|
|
return 'failed';
|
|
} else {
|
|
return 'passed';
|
|
}
|
|
};
|
|
|
|
Spec.prototype.isExecutable = function() {
|
|
return !this.disabled;
|
|
};
|
|
|
|
Spec.prototype.getFullName = function() {
|
|
return this.getSpecName(this);
|
|
};
|
|
|
|
var extractCustomPendingMessage = function(e) {
|
|
var fullMessage = e.toString(),
|
|
boilerplateStart = fullMessage.indexOf(Spec.pendingSpecExceptionMessage),
|
|
boilerplateEnd = boilerplateStart + Spec.pendingSpecExceptionMessage.length;
|
|
|
|
return fullMessage.substr(boilerplateEnd);
|
|
};
|
|
|
|
Spec.pendingSpecExceptionMessage = '=> marked Pending';
|
|
|
|
Spec.isPendingSpecException = function(e) {
|
|
return !!(e && e.toString && e.toString().indexOf(Spec.pendingSpecExceptionMessage) !== -1);
|
|
};
|
|
|
|
return Spec;
|
|
};
|
|
|
|
if (typeof window == void 0 && typeof exports == 'object') {
|
|
exports.Spec = jasmineRequire.Spec;
|
|
}
|