-
Notifications
You must be signed in to change notification settings - Fork 3
/
decrator.ts
172 lines (150 loc) · 4.53 KB
/
decrator.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// https://github.com/i5ting/ts-junit/blob/7229bd060fa348705bcbf8af0b09fffa9605d085/src/decrator.ts
// Copyright i5ting. All rights reserved. MIT license.
// Copyright 2022 justjavac. All rights reserved. MIT license.
// deno-lint-ignore-file no-explicit-any
let cache: Record<string, any> = {};
/** @alpha */
export function emptydata() {
cache = {};
return cache;
}
/** @alpha */
export function data() {
return cache;
}
/** @alpha */
export function BeforeAll(
target: any,
propertyName: string,
_descriptor: TypedPropertyDescriptor<any>,
) {
const className = target.constructor.name;
if (!cache[className]) cache[className] = {};
if (!cache[className]["hook"]) cache[className]["hook"] = {};
cache[className]["hook"]["before"] = target[propertyName];
}
/** @alpha */
export function BeforeEach(
target: any,
propertyName: string,
_descriptor: TypedPropertyDescriptor<any>,
) {
const className = target.constructor.name;
if (!cache[className]) cache[className] = {};
if (!cache[className]["hook"]) cache[className]["hook"] = {};
cache[className]["hook"]["before.each"] = target[propertyName];
}
/** @alpha */
export function AfterEach(
target: any,
propertyName: string,
_descriptor: TypedPropertyDescriptor<any>,
) {
const className = target.constructor.name;
if (!cache[className]) cache[className] = {};
if (!cache[className]["hook"]) cache[className]["hook"] = {};
cache[className]["hook"]["after.each"] = target[propertyName];
}
/** @alpha */
export function AfterAll(
target: any,
propertyName: string,
_descriptor: TypedPropertyDescriptor<any>,
) {
const className = target.constructor.name;
if (!cache[className]) cache[className] = {};
if (!cache[className]["hook"]) cache[className]["hook"] = {};
cache[className]["hook"]["after"] = target[propertyName];
}
/** @alpha */
export function Test(
target: any,
propertyName: string,
_descriptor: TypedPropertyDescriptor<any>,
) {
const className = target.constructor.name;
if (!cache[className]) cache[className] = {};
if (!cache[className][propertyName]) cache[className][propertyName] = {};
if (cache[className][propertyName]["desc"] == null) {
cache[className][propertyName]["desc"] = propertyName;
}
cache[className][propertyName]["fn"] = target[propertyName];
}
/**
* Creates a test case DisplayName.
* Can be used on entity property or on entity.
* Can create suite name when used on entity.
*
* @alpha
*/
export function DisplayName(
message: string,
): ClassDecorator & PropertyDecorator {
return function (
clsOrObject: any,
propertyName?: string | symbol,
) {
const target = propertyName
? clsOrObject.constructor
: (clsOrObject as CallableFunction);
const className = target.name;
if (!cache[className]) cache[className] = {};
if (propertyName) {
if (!cache[className][propertyName]) {
cache[className][propertyName] = {
desc: message,
};
}
} else {
console.debug(`when @DisplayName with class: ${message}`);
}
};
}
/**
* Creates a test case DisplayName.
* Can be used on entity property or on entity.
* Can create suite name when used on entity.
*
* @alpha
*/
export function Disabled(message: string): ClassDecorator & PropertyDecorator {
return function (
clsOrObject: any,
propertyName?: string | symbol,
) {
const target = propertyName
? clsOrObject.constructor
: (clsOrObject as CallableFunction);
const className = target.name;
if (!cache[className]) cache[className] = {};
if (propertyName) {
if (!cache[className][propertyName]) {
cache[className][propertyName] = {};
}
cache[className][propertyName]["skip"] = true;
cache[className][propertyName]["skipReason"] = message;
} else {
cache[className]["skipClaas"] = true;
cache[className]["skipClaasReason"] = message;
}
};
}
/**
* Parameterized tests make it possible to run a test multiple times with different arguments.
*
* @alpha
*/
export function ParameterizedTest(params: Array<any>): MethodDecorator {
return function (
target: any,
propertyName: string | symbol,
_descriptor: TypedPropertyDescriptor<any>,
) {
const className = target.constructor.name;
if (!cache[className]) cache[className] = {};
if (!cache[className][propertyName]) cache[className][propertyName] = {};
cache[className][propertyName]["desc"] = "no display name";
cache[className][propertyName]["fn"] = target[propertyName];
cache[className][propertyName]["params"] = params;
};
}