soda-test
Version:
Package for Unit and API tests
219 lines • 8.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getInfo = exports.DescribeInfo = exports.ContextInfo = exports.CaseInfo = exports.ItInfo = exports.commentInfo = exports.SetStubType = exports.SinonKind = void 0;
var SinonKind;
(function (SinonKind) {
SinonKind[SinonKind["Spy"] = 0] = "Spy";
SinonKind[SinonKind["Stub"] = 1] = "Stub";
SinonKind[SinonKind["Rewire"] = 2] = "Rewire";
SinonKind[SinonKind["RewireReload"] = 3] = "RewireReload";
SinonKind[SinonKind["Import"] = 4] = "Import";
SinonKind[SinonKind["Timers"] = 5] = "Timers";
SinonKind[SinonKind["Fixture"] = 6] = "Fixture";
SinonKind[SinonKind["Component"] = 7] = "Component";
})(SinonKind = exports.SinonKind || (exports.SinonKind = {}));
var SetStubType;
(function (SetStubType) {
SetStubType[SetStubType["None"] = 0] = "None";
SetStubType[SetStubType["Fake"] = 1] = "Fake";
SetStubType[SetStubType["Return"] = 2] = "Return";
SetStubType[SetStubType["Resolve"] = 3] = "Resolve";
SetStubType[SetStubType["Reject"] = 4] = "Reject";
SetStubType[SetStubType["Access"] = 5] = "Access";
SetStubType[SetStubType["Construct"] = 6] = "Construct";
})(SetStubType = exports.SetStubType || (exports.SetStubType = {}));
class commentInfo {
}
exports.commentInfo = commentInfo;
class ItInfo {
constructor() {
this.sinons = {};
}
}
exports.ItInfo = ItInfo;
class CaseInfo {
constructor(text, stepsFactory) {
this.stepsFactory = stepsFactory;
this.its = [];
this.instances = [];
this.caseText = text;
}
}
exports.CaseInfo = CaseInfo;
class ContextInfo {
constructor(text) {
this.contextControlMethods = {};
this.itsAndCases = {};
this.contextText = text;
}
}
exports.ContextInfo = ContextInfo;
class DescribeInfo {
constructor() {
this.contexts = {};
this.methodsContexts = {};
this.uncontext = new ContextInfo('');
this.sandboxes = [];
this.sinons = {};
this.lastSinon = null;
this.lastControlMethod = null;
}
getContext(contextName) {
if (contextName === '')
return this.uncontext;
let context = this.contexts[contextName];
if (!context) {
context = new ContextInfo(contextName);
this.contexts[contextName] = context;
}
return context;
}
setMethodContext(methodName, contextName = '', contextExtraData) {
if (this.lastControlMethod && this.lastControlMethod.name === methodName) {
const prevContextMethods = this.getContext(this.lastControlMethod.context).contextControlMethods;
const func = prevContextMethods[this.lastControlMethod.controlName];
prevContextMethods[this.lastControlMethod.controlName] = prevContextMethods[this.lastControlMethod.controlName + '.bak'];
delete prevContextMethods[this.lastControlMethod.controlName + '.bak'];
const context = this.getContext(contextName);
context.contextControlMethods[this.lastControlMethod.controlName] = func;
if (contextExtraData)
context.extraData = contextExtraData;
this.methodsContexts[methodName] = contextName;
this.currentContext = contextName;
return;
}
const prevContext = this.methodsContexts[methodName];
let it;
if (prevContext !== undefined) {
if (prevContext === contextName)
return;
const context = this.getContext(prevContext);
it = context.itsAndCases[methodName];
if (!it)
return;
delete context.itsAndCases[methodName];
}
else {
it = new ItInfo();
}
this.currentContext = contextName;
this.methodsContexts[methodName] = contextName;
const context = this.getContext(contextName);
context.itsAndCases[methodName] = it;
}
setMemberContext(memberName, contextName = '', extraData) {
if (this.lastSinon && this.lastSinon.name === memberName) {
this.lastSinon.sinon.context = contextName;
}
this.currentContext = contextName;
if (extraData) {
const context = this.getContext(contextName);
context.extraData = extraData;
}
}
getIt(methodName) {
let contextName = this.methodsContexts[methodName];
if (contextName === undefined) {
contextName = '';
this.methodsContexts[methodName] = contextName;
}
const context = this.getContext(contextName);
let it = context.itsAndCases[methodName];
if (!it) {
it = new ItInfo();
context.itsAndCases[methodName] = it;
}
return it;
}
getCase(methodName, text, stepsFactory) {
let contextName = this.methodsContexts[methodName];
if (contextName === undefined) {
contextName = '';
this.methodsContexts[methodName] = contextName;
}
const context = this.getContext(contextName);
let tcase = context.itsAndCases[methodName];
if (!tcase) {
tcase = new CaseInfo(text, stepsFactory);
context.itsAndCases[methodName] = tcase;
}
return tcase;
}
setIt(methodName, itText, itMethod, extraData) {
const it = this.getIt(methodName);
it.itText = itText;
it.extraData = extraData;
it.method = itMethod;
if (this.currentContext) {
this.setMethodContext(methodName, this.currentContext);
}
}
setComment(methodName, commentText, extraData) {
const it = this.getIt(methodName);
if (it.comments === undefined)
it.comments = [];
const comment = new commentInfo();
comment.commentText = commentText;
comment.extraData = extraData;
it.comments.splice(0, 0, comment);
}
setCase(methodName, caseText, instanceConstructor) {
const tcase = this.getCase(methodName, caseText, instanceConstructor);
if (this.currentContext) {
this.setMethodContext(methodName, this.currentContext);
}
return tcase;
}
setPending(methodName) {
const it = this.getIt(methodName);
it.pending = true;
}
// add a new sinon as a member property or an argument of it
// member:
// - propertyKey: name of the member
// - parameterIndex: undefined
// argument:
// - propertyKey: name of the it function
// - parameterIndex: index of the argument in the function
// both:
// - sinon: information of the sinon data
addSinon(propertyKey, parameterIndex, sinon) {
if (parameterIndex === undefined) {
// member decorator
sinon.context = this.currentContext;
this.sinons[propertyKey] = sinon;
this.lastSinon = { name: propertyKey, sinon };
}
else {
// parameter decorator
this.getIt(propertyKey).sinons[parameterIndex] = sinon;
}
}
getSinon(propertyKey, parameterIndex) {
if (parameterIndex === undefined) {
// member decorator
return this.sinons[propertyKey];
}
else {
// parameter decorator
const itInfo = this.getIt(propertyKey);
if (!itInfo)
return null;
return itInfo.sinons[parameterIndex];
}
}
}
exports.DescribeInfo = DescribeInfo;
function getInfo(target) {
let info;
if (!Reflect.hasMetadata('soda-test', target)) {
info = new DescribeInfo();
Reflect.defineMetadata('soda-test', info, target);
}
else {
info = Reflect.getMetadata('soda-test', target);
}
return info;
}
exports.getInfo = getInfo;
//# sourceMappingURL=testInfo.js.map