UNPKG

@ply-ct/ply

Version:

REST API Automated Testing

163 lines 6.61 kB
"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.DecoratedSuite = exports.Runtime = void 0; const path = __importStar(require("path")); const minimatch_1 = require("minimatch"); const yaml = __importStar(require("./yaml")); const util = __importStar(require("./util")); const location_1 = require("./location"); const names_1 = require("./names"); /** * Runtime information for a test suite. */ class Runtime { constructor(options, retrieval, results) { this.options = options; this.retrieval = retrieval; this.results = results; if (path.isAbsolute(this.options.testsLocation)) { this.testsLocation = new location_1.Location(this.options.testsLocation); } else { this.testsLocation = new location_1.Location(path.resolve(process.cwd() + '/' + this.options.testsLocation)); } } get suitePath() { return this.retrieval.location.relativeTo(this.options.testsLocation); } appendResult(line, options) { line = line.padStart(line.length + (options.level || 0) * (this.options.prettyIndent || 0)); this.results.actual.append(`${line}${options.comment ? ' # ' + options.comment : ''}\n`); if (options.withExpected) { this.results.expected.append(`${line}\n`); } } updateResult(name, line, options) { if (options.subflow) { // parallel not supported in subflow this.appendResult(line, options); return; } line = line.padStart(line.length + (options.level || 0) * (this.options.prettyIndent || 0)); const actual = yaml.load(this.results.actual.location.path, this.results.actual.read(), true); const loc = actual[name].__end; this.results.actual.insert(`${line}${options.comment ? ' # ' + options.comment : ''}`, loc + 1); if (options.withExpected) { this.results.expected.insert(`${line}`, loc + 1); } } /** * Take into account comments in expected yaml. */ async padActualStart(name, instNum) { const expectedYaml = await this.results.getExpectedYaml(name, instNum); if (expectedYaml.start > 0) { const actualYaml = this.results.getActualYaml(name, instNum); if (expectedYaml.start > actualYaml.start) { const extraExpectedLines = util .lines(expectedYaml.text) .slice(actualYaml.start, expectedYaml.start); const extraWhite = extraExpectedLines.filter((line) => { const trimmed = line.trim(); return !trimmed.length || trimmed.startsWith('#'); }).length; if (extraWhite) { this.results.actual.padLines(actualYaml.start, extraWhite); } } } } } exports.Runtime = Runtime; /** * Applicable for Cases */ class DecoratedSuite { /** * @param instance runtime instance of a suite */ constructor(instance) { this.instance = instance; this.testCases = []; this.befores = []; this.afters = []; this.testSuite = instance.constructor[names_1.SUITE]; if (this.testSuite) { this.testSuite = { ...this.testSuite, className: this.testSuite.name }; } Object.getOwnPropertyNames(instance.constructor.prototype).forEach((propName) => { try { if (typeof instance.constructor.prototype[propName] === 'function') { const method = instance.constructor.prototype[propName]; if (method[names_1.TEST]) { const testCase = method[names_1.TEST]; if (!this.testCases.find((tc) => tc.name === testCase.name)) { this.testCases.push({ ...testCase, method }); } } if (method[names_1.BEFORE]) { const before = method[names_1.BEFORE]; if (!this.befores.find((b) => b.name === before.name)) { this.befores.push({ ...before, method }); } } if (method[names_1.AFTER]) { const after = method[names_1.AFTER]; if (!this.afters.find((a) => a.name === after.name)) { this.afters.push({ ...after, method }); } } } } catch (_ignored) { // getter or setter before constructor? } }); } async runIfMatch(beforeOrAfter, test, values) { if (this.isMatch(beforeOrAfter, test)) { if (beforeOrAfter.tests || !beforeOrAfter.hasRun) { await beforeOrAfter.method.call(this.instance, values); beforeOrAfter.hasRun = true; } } } isMatch(beforeOrAfter, test) { return !beforeOrAfter.tests || (0, minimatch_1.minimatch)(test, beforeOrAfter.tests); } async runBefores(test, values) { for (const before of this.befores) { await this.runIfMatch(before, test, values); } } async runAfters(test, values) { for (const after of this.afters) { await this.runIfMatch(after, test, values); } } } exports.DecoratedSuite = DecoratedSuite; //# sourceMappingURL=runtime.js.map