/
decorators.ts
125 lines (116 loc) · 4.01 KB
/
decorators.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 { TEST, BEFORE, AFTER, SUITE } from './names';
type ClassTarget = {
new (...args: any[]): any;
name: string;
};
export interface TestSuite {
name: string;
className: string;
}
/**
* If name arg is not specified, class name is suite name.
*/
export function suite(target: ClassTarget): void;
export function suite(name: string): (target: ClassTarget) => void;
export function suite(nameOrTarget: string | ClassTarget) {
if (typeof nameOrTarget === 'string') {
// with decorator args: function params are dec args, return callback taking Target
return function (target: ClassTarget) {
// class name is target.name
if ((target as any)[SUITE]) {
throw new Error(
`@suite ${target.name} must not extend another @suite-decorated class.`
);
}
(target as any)[SUITE] = { name: nameOrTarget };
};
} else {
// no decorator args: first function param is ClassTarget
if ((nameOrTarget as any)[SUITE]) {
throw new Error(
`@suite ${nameOrTarget.name} must not extend another @suite-decorated class.`
);
}
(nameOrTarget as any)[SUITE] = { name: nameOrTarget.name };
}
}
export interface TestCase {
name: string;
method: Function;
}
/**
* If name arg is not specified, function name is test name.
*/
export function test(target: any, propertyKey: string, descriptor: PropertyDescriptor): void;
export function test(
name: string
): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
export function test(
nameOrTarget: string | any,
propertyKey?: string,
_descriptor?: PropertyDescriptor
) {
if (typeof nameOrTarget === 'string') {
return function (target: any, propertyKey: string, _descriptor: PropertyDescriptor) {
// with decorator args: function params are dec args, return callback taking Target, etc
target[propertyKey][TEST] = { name: nameOrTarget };
};
} else {
// no decorator args: first function param is target, second is propKey, etc
nameOrTarget[propertyKey!][TEST] = { name: propertyKey };
}
}
export interface Before {
name: string;
tests?: string;
method: Function;
hasRun: boolean;
}
/**
* Invoked before tests.
* @param tests glob pattern for test names before which function will be called (* = each, omitted = once before all)
*/
export function before(target: any, propertyKey: string, descriptor: PropertyDescriptor): void;
export function before(
tests: string
): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
export function before(
testsOrTarget: string | any,
propertyKey?: string,
_descriptor?: PropertyDescriptor
) {
if (typeof testsOrTarget === 'string') {
return function (target: any, propertyKey: string, _descriptor: PropertyDescriptor) {
target[propertyKey][BEFORE] = { name: propertyKey, tests: testsOrTarget };
};
} else {
testsOrTarget[propertyKey!][BEFORE] = { name: propertyKey };
}
}
export interface After {
name: string;
tests?: string;
method: Function;
hasRun: boolean;
}
/**
* Invoked after tests.
* @param tests glob pattern for test names after which function will be called (* = each, omitted = once after all)
*/
export function after(target: any, propertyKey: string, descriptor: PropertyDescriptor): void;
export function after(
tests: string
): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
export function after(
testsOrTarget: string | any,
propertyKey?: string,
_descriptor?: PropertyDescriptor
) {
if (typeof testsOrTarget === 'string') {
return function (target: any, propertyKey: string, _descriptor: PropertyDescriptor) {
target[propertyKey][AFTER] = { name: propertyKey, tests: testsOrTarget };
};
} else {
testsOrTarget[propertyKey!][AFTER] = { name: propertyKey };
}
}