rooibos-roku
Version:
simple, flexible, fun brightscript test framework for roku scenegraph apps - roku brighterscript plugin
221 lines • 10.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAnnotationType = exports.RooibosAnnotation = exports.AnnotationParams = exports.AnnotationType = void 0;
const Diagnostics_1 = require("../utils/Diagnostics");
var AnnotationType;
(function (AnnotationType) {
AnnotationType["None"] = "none";
AnnotationType["TestSuite"] = "suite";
AnnotationType["Describe"] = "describe";
AnnotationType["It"] = "it";
AnnotationType["Ignore"] = "ignore";
AnnotationType["Solo"] = "only";
AnnotationType["NodeTest"] = "sgnode";
AnnotationType["Slow"] = "slow";
AnnotationType["Setup"] = "setup";
AnnotationType["TearDown"] = "teardown";
AnnotationType["BeforeEach"] = "beforeeach";
AnnotationType["AfterEach"] = "aftereach";
AnnotationType["Params"] = "params";
AnnotationType["IgnoreParams"] = "ignoreparams";
AnnotationType["SoloParams"] = "onlyparams";
AnnotationType["Async"] = "async";
AnnotationType["Tags"] = "tags";
AnnotationType["NoCatch"] = "nocatch";
AnnotationType["NoEarlyExit"] = "noearlyexit";
})(AnnotationType = exports.AnnotationType || (exports.AnnotationType = {}));
let annotationLookup = {
suite: AnnotationType.TestSuite,
describe: AnnotationType.Describe,
it: AnnotationType.It,
ignore: AnnotationType.Ignore,
only: AnnotationType.Solo,
sgnode: AnnotationType.NodeTest,
setup: AnnotationType.Setup,
slow: AnnotationType.Slow,
teardown: AnnotationType.TearDown,
beforeeach: AnnotationType.BeforeEach,
aftereach: AnnotationType.AfterEach,
params: AnnotationType.Params,
ignoreparams: AnnotationType.IgnoreParams,
onlyparams: AnnotationType.SoloParams,
async: AnnotationType.Async,
tags: AnnotationType.Tags,
nocatch: AnnotationType.NoCatch,
noearlyexit: AnnotationType.NoEarlyExit
};
const defaultSlowDuration = 75;
class AnnotationParams {
constructor(annotation, text, lineNumber, params, isIgnore = false, isSolo = false, noCatch = false, noEarlyexit = false) {
this.annotation = annotation;
this.text = text;
this.lineNumber = lineNumber;
this.params = params;
this.isIgnore = isIgnore;
this.isSolo = isSolo;
this.noCatch = noCatch;
this.noEarlyexit = noEarlyexit;
}
}
exports.AnnotationParams = AnnotationParams;
class RooibosAnnotation {
/**
* Represents a group of comments which contain tags such as @only, @suite, @describe, @it etc
*/
constructor(file, annotation, annotationType, text, name, isIgnore = false, isSolo = false, params = [], nodeName, rawTags = [], noCatch = false, noEarlyExit = false, slow = defaultSlowDuration) {
this.file = file;
this.annotation = annotation;
this.annotationType = annotationType;
this.text = text;
this.name = name;
this.isIgnore = isIgnore;
this.isSolo = isSolo;
this.params = params;
this.nodeName = nodeName;
this.noCatch = noCatch;
this.noEarlyExit = noEarlyExit;
this.slow = slow;
this.hasSoloParams = false;
this.tags = new Set(rawTags);
}
static getAnnotation(file, statement) {
var _a;
//split annotations in case they include an it group..
let blockAnnotation;
let testAnnotation;
let isSolo = false;
let async = false;
let isIgnore = false;
let noCatch = false;
let noEarlyExit = false;
let nodeName;
let asyncTimeout = -1;
let tags = [];
if ((_a = statement.annotations) === null || _a === void 0 ? void 0 : _a.length) {
let describeAnnotations = statement.annotations.filter((a) => getAnnotationType(a.name) === AnnotationType.Describe);
if (describeAnnotations.length > 1) {
for (let a of describeAnnotations) {
(0, Diagnostics_1.diagnosticMultipleDescribeAnnotations)(file, a);
}
}
// Break up the annotations grouped by Describes
let blocks = [];
let currentBlock = [];
for (const annotation of statement.annotations) {
currentBlock.push(annotation);
if (getAnnotationType(annotation.name) === AnnotationType.Describe) {
blocks.push(currentBlock);
currentBlock = [];
}
}
// Make sure to push the last block as the last annotation likely wasn't a Describe
if (blocks[blocks.length - 1] !== currentBlock) {
blocks.push(currentBlock);
}
for (const annotationBlock of blocks) {
const getAnnotationsOfType = (...names) => {
return annotationBlock.filter(a => names.includes(a.name.toLowerCase()));
};
noEarlyExit = getAnnotationsOfType(AnnotationType.NoEarlyExit).length > 0;
noCatch = getAnnotationsOfType(AnnotationType.NoCatch).length > 0;
isSolo = getAnnotationsOfType(AnnotationType.Solo).length > 0;
isIgnore = getAnnotationsOfType(AnnotationType.Ignore).length > 0;
for (const annotation of getAnnotationsOfType(AnnotationType.NodeTest)) {
nodeName = annotation.getArguments()[0];
}
for (const annotation of getAnnotationsOfType(AnnotationType.Async)) {
async = true;
asyncTimeout = annotation.getArguments().length === 1 ? parseInt(annotation.getArguments()[0]) : -1;
// If the test is async, it must be a node test
// Default to `Node` if no node is specified
nodeName !== null && nodeName !== void 0 ? nodeName : (nodeName = 'Node');
}
for (const annotation of getAnnotationsOfType(AnnotationType.Tags)) {
tags = annotation.getArguments().map((a) => a === null || a === void 0 ? void 0 : a.toString());
}
for (const annotation of getAnnotationsOfType(AnnotationType.BeforeEach, AnnotationType.AfterEach, AnnotationType.Setup, AnnotationType.TearDown)) {
testAnnotation = new RooibosAnnotation(file, annotation, getAnnotationType(annotation.name), annotation.name, annotation.name);
}
for (const annotation of getAnnotationsOfType(AnnotationType.Describe, AnnotationType.TestSuite)) {
const groupName = annotation.getArguments()[0];
blockAnnotation = new RooibosAnnotation(file, annotation, getAnnotationType(annotation.name), annotation.name, groupName, isIgnore, isSolo, undefined, nodeName, tags, noCatch, noEarlyExit);
blockAnnotation.isAsync = async;
blockAnnotation.asyncTimeout = asyncTimeout === -1 ? 60000 : asyncTimeout;
nodeName = null;
isSolo = false;
isIgnore = false;
async = false;
asyncTimeout = -1;
}
for (const annotation of getAnnotationsOfType(AnnotationType.It)) {
const testName = annotation.getArguments()[0];
if (!testName || testName.trim() === '') {
(0, Diagnostics_1.diagnosticNoTestNameDefined)(file, annotation);
}
let newAnnotation = new RooibosAnnotation(file, annotation, getAnnotationType(annotation.name), annotation.name, testName, isIgnore, isSolo, undefined, undefined, tags, noCatch);
newAnnotation.isAsync = async;
newAnnotation.asyncTimeout = asyncTimeout === -1 ? 2000 : asyncTimeout;
if (testAnnotation) {
(0, Diagnostics_1.diagnosticMultipleTestOnFunctionDefined)(file, newAnnotation.annotation);
}
else {
testAnnotation = newAnnotation;
}
isSolo = false;
isIgnore = false;
async = false;
asyncTimeout = -1;
}
for (const annotation of getAnnotationsOfType(AnnotationType.Params, AnnotationType.SoloParams, AnnotationType.IgnoreParams)) {
if (testAnnotation) {
testAnnotation.parseParams(file, annotation, getAnnotationType(annotation.name), noCatch);
}
else {
//error
}
}
for (const annotation of getAnnotationsOfType(AnnotationType.Slow)) {
if (testAnnotation) {
let slow = annotation.getArguments().length === 1 ? parseInt(annotation.getArguments()[0]) : defaultSlowDuration;
if (isNaN(slow)) {
(0, Diagnostics_1.diagnosticSlowAnnotationRequiresNumber)(file, annotation);
testAnnotation.slow = defaultSlowDuration;
}
else {
testAnnotation.slow = slow;
}
}
else {
//error
}
}
}
}
return { blockAnnotation: blockAnnotation, testAnnotation: testAnnotation };
}
parseParams(file, annotation, annotationType, noCatch) {
let rawParams = JSON.stringify(annotation.getArguments());
let isSolo = annotationType === AnnotationType.SoloParams;
let isIgnore = annotationType === AnnotationType.IgnoreParams;
if (isSolo) {
this.hasSoloParams = true;
}
try {
if (rawParams) {
this.params.push(new AnnotationParams(annotation, rawParams, annotation.range.start.line, annotation.getArguments(), isIgnore, isSolo, noCatch));
}
else {
(0, Diagnostics_1.diagnosticIllegalParams)(file, annotation);
}
}
catch (e) {
(0, Diagnostics_1.diagnosticIllegalParams)(file, annotation);
}
}
}
exports.RooibosAnnotation = RooibosAnnotation;
function getAnnotationType(text) {
return annotationLookup[text.toLowerCase()] || AnnotationType.None;
}
exports.getAnnotationType = getAnnotationType;
//# sourceMappingURL=Annotation.js.map