@ply-ct/ply
Version:
REST API Automated Testing
219 lines • 10.4 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CaseLoader = void 0;
const ts = __importStar(require("typescript"));
const suite_1 = require("./suite");
const case_1 = require("./case");
const retrieval_1 = require("./retrieval");
const location_1 = require("./location");
const runtime_1 = require("./runtime");
const result_1 = require("./result");
const log_1 = require("./log");
const logger_1 = require("./logger");
const skip_1 = require("./skip");
class CaseLoader {
constructor(sourceFiles, options, compileOptions, logger) {
this.options = options;
this.compileOptions = compileOptions;
this.logger = logger;
this.program = ts.createProgram(sourceFiles, compileOptions.compilerOptions);
this.checker = this.program.getTypeChecker();
if (options.skip) {
this.skip = new skip_1.Skip(options.skip);
}
}
async load() {
var _a;
const suites = [];
for (const sourceFile of this.program.getSourceFiles()) {
const suiteDecorations = this.findSuites(sourceFile);
if (suiteDecorations.length > 0) {
const retrieval = new retrieval_1.Retrieval(sourceFile.fileName);
const suitePath = retrieval.location.relativeTo(this.options.testsLocation);
for (const suiteDecoration of suiteDecorations) {
// every suite instance gets its own runtime
const results = await result_1.ResultPaths.create(this.options, retrieval, suiteDecoration.name);
const runtime = new runtime_1.Runtime(this.options, retrieval, results);
const logger = this.logger ||
new logger_1.Logger({
level: this.options.verbose
? log_1.LogLevel.debug
: this.options.quiet
? log_1.LogLevel.error
: log_1.LogLevel.info,
prettyIndent: this.options.prettyIndent
}, runtime.results.log);
let outFile = this.compileOptions.outFile;
if (!outFile) {
let suiteLoc = new location_1.Location(this.options.testsLocation + '/' + suitePath);
if (suiteLoc.isAbsolute) {
suiteLoc = new location_1.Location(suiteLoc.relativeTo('.'));
}
outFile = new location_1.Location(new location_1.Location(this.compileOptions.outDir).absolute +
'/' +
suiteLoc.parent +
'/' +
suiteLoc.base +
'.js').path;
}
const suite = new suite_1.Suite(suiteDecoration.name, 'case', suitePath, runtime, logger, sourceFile.getLineAndCharacterOfPosition(suiteDecoration.classDeclaration.getStart()).line, sourceFile.getLineAndCharacterOfPosition(suiteDecoration.classDeclaration.getEnd()).line, suiteDecoration.className, outFile);
for (const caseDecoration of this.findCases(suiteDecoration)) {
const c = new case_1.PlyCase(caseDecoration.name, caseDecoration.methodName, sourceFile.getLineAndCharacterOfPosition(caseDecoration.methodDeclaration.getStart()).line, sourceFile.getLineAndCharacterOfPosition(caseDecoration.methodDeclaration.getEnd()).line, logger);
suite.add(c);
}
// mark if skipped
if ((_a = this.skip) === null || _a === void 0 ? void 0 : _a.isSkipped(suite.path)) {
suite.skip = true;
}
suites.push(suite);
}
}
}
suites.sort((s1, s2) => {
if (s1.path === s2.path) {
// within a file suites are ordered as declared
return 0;
}
return s1.name.localeCompare(s2.name);
});
return suites;
}
findSuites(sourceFile) {
const suites = [];
if (!sourceFile.isDeclarationFile) {
ts.forEachChild(sourceFile, (node) => {
if (ts.isClassDeclaration(node) && node.name && this.isExported(node)) {
const suiteDecoration = this.findSuiteDecoration(node);
if (suiteDecoration) {
suites.push(suiteDecoration);
}
}
});
}
return suites;
}
findSuiteDecoration(classDeclaration) {
const classSymbol = this.checker.getSymbolAtLocation(classDeclaration.name);
if (classSymbol) {
let decorators;
if (ts.getDecorators) {
decorators = ts.getDecorators(classDeclaration);
}
else {
// old typescript compiler sdk incompatible
decorators = classDeclaration.decorators;
}
if (decorators) {
for (const decorator of decorators) {
if (decorator.expression) {
let decoratorSymbol;
const firstToken = decorator.expression.getFirstToken();
if (firstToken) {
decoratorSymbol = this.checker.getSymbolAtLocation(firstToken);
}
else {
decoratorSymbol = this.checker.getSymbolAtLocation(decorator.expression);
}
if (decoratorSymbol &&
(decoratorSymbol.name === 'suite' ||
this.checker.getAliasedSymbol(decoratorSymbol).name === 'suite')) {
let suiteName = classSymbol.name;
if (decorator.expression.getChildCount() >= 3) {
// suite name arg
const text = decorator.expression.getChildAt(2).getText();
suiteName = text.substring(1, text.length - 1);
}
return {
name: suiteName,
classDeclaration: classDeclaration,
className: classSymbol.name
};
}
}
}
}
}
}
findCases(suiteDecoration) {
const cases = [];
suiteDecoration.classDeclaration.forEachChild((node) => {
if (ts.isMethodDeclaration(node) && node.name && !ts.isPrivateIdentifier(node)) {
const caseDecoration = this.findCaseDecoration(node);
if (caseDecoration) {
cases.push(caseDecoration);
}
}
});
return cases;
}
findCaseDecoration(methodDeclaration) {
const methodSymbol = this.checker.getSymbolAtLocation(methodDeclaration.name);
if (methodSymbol) {
let decorators;
if (ts.getDecorators) {
decorators = ts.getDecorators(methodDeclaration);
}
else {
// old typescript compiler sdk incompatible
decorators = methodDeclaration.decorators;
}
if (decorators) {
for (const decorator of decorators) {
if (decorator.expression) {
let decoratorSymbol;
const firstToken = decorator.expression.getFirstToken();
if (firstToken) {
decoratorSymbol = this.checker.getSymbolAtLocation(firstToken);
}
else {
decoratorSymbol = this.checker.getSymbolAtLocation(decorator.expression);
}
if (decoratorSymbol &&
(decoratorSymbol.name === 'test' ||
this.checker.getAliasedSymbol(decoratorSymbol).name === 'test')) {
let testName = methodSymbol.name;
if (decorator.expression.getChildCount() >= 3) {
const text = decorator.expression.getChildAt(2).getText();
testName = text.substring(1, text.length - 1);
}
return {
name: testName,
methodDeclaration: methodDeclaration,
methodName: methodSymbol.name
};
}
}
}
}
}
}
isExported(node) {
return ((ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export) !== 0 ||
(!!node.parent && node.parent.kind === ts.SyntaxKind.SourceFile));
}
}
exports.CaseLoader = CaseLoader;
//# sourceMappingURL=cases.js.map