-
Notifications
You must be signed in to change notification settings - Fork 649
/
Copy pathassertion_state.ts
215 lines (202 loc) · 5.69 KB
/
assertion_state.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
/**
* Check the test suite internal state
*
* @example Usage
* ```ts ignore
* import { AssertionState } from "@std/internal";
*
* const assertionState = new AssertionState();
* ```
*/
export class AssertionState {
#state: {
assertionCount: number | undefined;
assertionCheck: boolean;
assertionTriggered: boolean;
assertionTriggeredCount: number;
};
constructor() {
this.#state = {
assertionCount: undefined,
assertionCheck: false,
assertionTriggered: false,
assertionTriggeredCount: 0,
};
}
/**
* Get the number that through `expect.assertions` api set.
*
* @returns the number that through `expect.assertions` api set.
*
* @example Usage
* ```ts ignore
* import { AssertionState } from "@std/internal";
*
* const assertionState = new AssertionState();
* assertionState.assertionCount;
* ```
*/
get assertionCount(): number | undefined {
return this.#state.assertionCount;
}
/**
* Get a certain number that assertions were called before.
*
* @returns return a certain number that assertions were called before.
*
* @example Usage
* ```ts ignore
* import { AssertionState } from "@std/internal";
*
* const assertionState = new AssertionState();
* assertionState.assertionTriggeredCount;
* ```
*/
get assertionTriggeredCount(): number {
return this.#state.assertionTriggeredCount;
}
/**
* If `expect.hasAssertions` called, then through this method to update #state.assertionCheck value.
*
* @param val Set #state.assertionCheck's value
*
* @example Usage
* ```ts ignore
* import { AssertionState } from "@std/internal";
*
* const assertionState = new AssertionState();
* assertionState.setAssertionCheck(true);
* ```
*/
setAssertionCheck(val: boolean) {
this.#state.assertionCheck = val;
}
/**
* If any matchers was called, `#state.assertionTriggered` will be set through this method.
*
* @param val Set #state.assertionTriggered's value
*
* @example Usage
* ```ts ignore
* import { AssertionState } from "@std/internal";
*
* const assertionState = new AssertionState();
* assertionState.setAssertionTriggered(true);
* ```
*/
setAssertionTriggered(val: boolean) {
this.#state.assertionTriggered = val;
}
/**
* If `expect.assertions` called, then through this method to update #state.assertionCheck value.
*
* @param num Set #state.assertionCount's value, for example if the value is set 2, that means
* you must have two assertion matchers call in your test suite.
*
* @example Usage
* ```ts ignore
* import { AssertionState } from "@std/internal";
*
* const assertionState = new AssertionState();
* assertionState.setAssertionCount(2);
* ```
*/
setAssertionCount(num: number) {
this.#state.assertionCount = num;
}
/**
* If any matchers was called, `#state.assertionTriggeredCount` value will plus one internally.
*
* @example Usage
* ```ts ignore
* import { AssertionState } from "@std/internal";
*
* const assertionState = new AssertionState();
* assertionState.updateAssertionTriggerCount();
* ```
*/
updateAssertionTriggerCount() {
if (this.#state.assertionCount !== undefined) {
this.#state.assertionTriggeredCount += 1;
}
}
/**
* Check Assertion internal state, if `#state.assertionCheck` is set true, but
* `#state.assertionTriggered` is still false, then should throw an Assertion Error.
*
* @returns a boolean value, that the test suite is satisfied with the check. If not,
* it should throw an AssertionError.
*
* @example Usage
* ```ts ignore
* import { AssertionState } from "@std/internal";
*
* const assertionState = new AssertionState();
* if (assertionState.checkAssertionErrorState()) {
* // throw AssertionError("");
* }
* ```
*/
checkAssertionErrorState(): boolean {
return this.#state.assertionCheck && !this.#state.assertionTriggered;
}
/**
* Reset all assertion state when every test suite function ran completely.
*
* @example Usage
* ```ts ignore
* import { AssertionState } from "@std/internal";
*
* const assertionState = new AssertionState();
* assertionState.resetAssertionState();
* ```
*/
resetAssertionState(): void {
this.#state = {
assertionCount: undefined,
assertionCheck: false,
assertionTriggered: false,
assertionTriggeredCount: 0,
};
}
/**
* Check Assertion called state, if `#state.assertionCount` is set to a number value, but
* `#state.assertionTriggeredCount` is less then it, then should throw an assertion error.
*
* @returns a boolean value, that the test suite is satisfied with the check. If not,
* it should throw an AssertionError.
*
* @example Usage
* ```ts ignore
* import { AssertionState } from "@std/internal";
*
* const assertionState = new AssertionState();
* if (assertionState.checkAssertionCountSatisfied()) {
* // throw AssertionError("");
* }
* ```
*/
checkAssertionCountSatisfied(): boolean {
return this.#state.assertionCount !== undefined &&
this.#state.assertionCount !== this.#state.assertionTriggeredCount;
}
}
const assertionState = new AssertionState();
/**
* return an instance of AssertionState
*
* @returns AssertionState
*
* @example Usage
* ```ts ignore
* import { getAssertionState } from "@std/internal";
*
* const assertionState = getAssertionState();
* assertionState.setAssertionTriggered(true);
* ```
*/
export function getAssertionState(): AssertionState {
return assertionState;
}