/
runtime.ts
125 lines (113 loc) · 4.16 KB
/
runtime.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import * as path from 'path';
import * as minimatch from 'minimatch';
import { Location } from './location';
import { Retrieval } from './retrieval';
import { PlyOptions } from './options';
import { ResultPaths } from './result';
import { TEST, BEFORE, AFTER, SUITE } from './names';
import { TestSuite, TestCase, Before, After } from './decorators';
/**
* Runtime information for a test suite.
*/
export class Runtime {
testsLocation: Location;
decoratedSuite?: DecoratedSuite;
/**
* Verified response header names.
*/
responseHeaders: string[] | undefined;
constructor(
readonly options: PlyOptions,
readonly retrieval: Retrieval,
public results: ResultPaths
) {
if (path.isAbsolute(this.options.testsLocation)) {
this.testsLocation = new Location(this.options.testsLocation);
} else {
this.testsLocation = new Location(
path.resolve(process.cwd() + '/' + this.options.testsLocation)
);
}
}
get suitePath(): string {
return this.retrieval.location.relativeTo(this.options.testsLocation);
}
appendResult(line: string, level = 0, withExpected = false, comment?: string) {
line = line.padStart(line.length + level * (this.options.prettyIndent || 0));
this.results.actual.append(`${line}${comment ? ' # ' + comment : ''}\n`);
if (withExpected) {
this.results.expected.append(`${line}\n`);
}
}
}
export type CallingCaseInfo = {
results: ResultPaths;
suiteName: string;
caseName: string;
};
/**
* Applicable for Cases
*/
export class DecoratedSuite {
readonly testSuite: TestSuite;
readonly testCases: TestCase[] = [];
readonly befores: Before[] = [];
readonly afters: After[] = [];
/**
* @param instance runtime instance of a suite
*/
constructor(readonly instance: any) {
this.testSuite = instance.constructor[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[TEST]) {
const testCase = method[TEST];
if (!this.testCases.find((tc) => tc.name === testCase.name)) {
this.testCases.push({ ...testCase, method });
}
}
if (method[BEFORE]) {
const before = method[BEFORE];
if (!this.befores.find((b) => b.name === before.name)) {
this.befores.push({ ...before, method });
}
}
if (method[AFTER]) {
const after = method[AFTER];
if (!this.afters.find((a) => a.name === after.name)) {
this.afters.push({ ...after, method });
}
}
}
} catch (_ignored) {
// getter or setter before constructor?
}
});
}
private async runIfMatch(beforeOrAfter: Before | After, test: string, values: object) {
if (this.isMatch(beforeOrAfter, test)) {
if (beforeOrAfter.tests || !beforeOrAfter.hasRun) {
await beforeOrAfter.method.call(this.instance, values);
beforeOrAfter.hasRun = true;
}
}
}
private isMatch(beforeOrAfter: Before | After, test: string) {
return !beforeOrAfter.tests || minimatch(test, beforeOrAfter.tests);
}
async runBefores(test: string, values: object) {
for (const before of this.befores) {
await this.runIfMatch(before, test, values);
}
}
async runAfters(test: string, values: object) {
for (const after of this.afters) {
await this.runIfMatch(after, test, values);
}
}
}