-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Requirements.mts
313 lines (297 loc) · 8.87 KB
/
Requirements.mts
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import type {
ObjectVerifier,
SetVerifier,
ArrayVerifier,
ClassVerifier,
ArrayValidator,
SetValidator,
ClassValidator,
ObjectValidator,
MapVerifier,
StringVerifier,
NumberVerifier,
StringValidator,
NumberValidator,
MapValidator,
BooleanVerifier,
BooleanValidator,
ClassConstructor,
AnythingButClassConstructor
} from "./internal/internal.mjs";
import {
Configuration,
MainGlobalConfiguration,
Objects,
ObjectValidatorImpl,
ObjectVerifierImpl,
SetValidatorImpl,
SetVerifierImpl,
ArrayVerifierImpl,
ArrayValidatorImpl,
Pluralizer,
ClassVerifierImpl,
ClassValidatorImpl,
MapVerifierImpl,
MapValidatorImpl,
BooleanVerifierImpl,
BooleanValidatorImpl,
StringVerifierImpl,
StringValidatorImpl,
NumberVerifierImpl,
NumberValidatorImpl
} from "./internal/internal.mjs";
/**
* Verifies the requirements of types from the Javascript core API.
*/
class Requirements
{
private readonly config: Configuration;
/**
* Verifies a value.
* <p>
* Unlike {@link Requirements}, instances of this class can be configured prior to initiating verification.
* Doing so causes the same configuration to get reused across runs.
*
* @param configuration - the instance configuration
*/
constructor(configuration?: Configuration)
{
if (typeof (configuration) === "undefined")
configuration = new Configuration(MainGlobalConfiguration.INSTANCE);
this.config = configuration;
}
/**
* Verifies the requirements of an object.
*
* @typeParam T - the type the actual value
* @typeParam E - the type elements in the array or set
* @param actual - the actual value
* @param name - the name of the value
* @returns a verifier
* @throws TypeError if <code>name</code> is null
* @throws RangeError if <code>name</code> is empty
*/
requireThat(actual: boolean, name: string): BooleanVerifier;
requireThat(actual: string, name: string): StringVerifier;
requireThat(actual: number, name: string): NumberVerifier;
requireThat<E>(actual: Array<E>, name: string): ArrayVerifier<E>;
requireThat<E>(actual: Set<E>, name: string): SetVerifier<E>;
requireThat<K, V>(actual: Map<K, V>, name: string): MapVerifier<K, V>;
requireThat<T>(actual: ClassConstructor<T>, name: string): ClassVerifier<T>;
requireThat<T>(actual: T, name: string): ObjectVerifier<T>;
requireThat<T, E, K, V>
(actual: unknown, name: string): BooleanVerifier | StringVerifier | NumberVerifier | ArrayVerifier<E> |
SetVerifier<E> | MapVerifier<K, V> | ClassVerifier<T> | ObjectVerifier<T>
{
Objects.verifyName(name, "name");
const typeOfActual = Objects.getTypeInfo(actual);
switch (typeOfActual.type)
{
case "boolean":
return new BooleanVerifierImpl(new BooleanValidatorImpl(this.config, actual as boolean, name, []));
case "string":
return new StringVerifierImpl(new StringValidatorImpl(this.config, actual as string, name, []));
case "number":
return new NumberVerifierImpl(new NumberValidatorImpl(this.config, actual as number, name, []));
case "array":
{
return new ArrayVerifierImpl<E>(new ArrayValidatorImpl<E>(this.config, actual as E[], name,
Pluralizer.ELEMENT, []));
}
case "object":
{
switch (typeOfActual.name)
{
case "Set":
return new SetVerifierImpl<E>(new SetValidatorImpl<E>(this.config, actual as Set<E>, name, []));
case "Map":
{
return new MapVerifierImpl<K, V>(new MapValidatorImpl<K, V>(this.config, actual as Map<K, V>,
name, []));
}
}
break;
}
case "class":
{
return new ClassVerifierImpl<T>(new ClassValidatorImpl(this.config,
actual as ClassConstructor<T> | undefined, name, []));
}
}
return new ObjectVerifierImpl<ObjectValidator<T>, T>(new ObjectValidatorImpl<T>(this.config,
actual as T | undefined, name, []));
}
/**
* Validates the requirements of an object.
*
* @typeParam T - the type the actual value
* @typeParam E - the type elements in the array or set
* @param actual - the actual value
* @param name - the name of the value
* @returns validator for the value
* @throws TypeError if <code>name</code> is null
* @throws RangeError if <code>name</code> is empty
*/
validateThat(actual: boolean, name: string): BooleanValidator;
validateThat(actual: string, name: string): StringValidator;
validateThat(actual: number, name: string): NumberValidator;
validateThat<E>(actual: Array<E>, name: string): ArrayValidator<E>;
validateThat<E>(actual: Set<E>, name: string): SetValidator<E>;
validateThat<K, V>(actual: Map<K, V>, name: string): MapValidator<K, V>;
validateThat<T>(actual: AnythingButClassConstructor<T>, name: string): ObjectValidator<T>;
validateThat<T>(actual: ClassConstructor<T>, name: string): ClassValidator<T>;
validateThat(actual: unknown, name: string): ObjectValidator<unknown>;
validateThat<E, K, V, T>
(actual: unknown, name: string): BooleanValidator | StringValidator | NumberValidator |
ArrayValidator<E> | SetValidator<E> | MapValidator<K, V> | ObjectValidator<T> | ClassValidator<T> |
ObjectValidator<unknown>
{
Objects.verifyName(name, "name");
const typeOfActual = Objects.getTypeInfo(actual);
switch (typeOfActual.type)
{
case "boolean":
return new BooleanValidatorImpl(this.config, actual as boolean, name, []);
case "string":
return new StringValidatorImpl(this.config, actual as string, name, []);
case "number":
return new NumberValidatorImpl(this.config, actual as number, name, []);
case "class":
return new ClassValidatorImpl<T>(this.config, actual as ClassConstructor<T> | undefined, name, []);
case "array":
return new ArrayValidatorImpl<E>(this.config, actual as E[], name, Pluralizer.ELEMENT, []);
case "object":
{
switch (typeOfActual.name)
{
case "Set":
return new SetValidatorImpl<E>(this.config, actual as Set<E>, name, []);
case "Map":
return new MapValidatorImpl<K, V>(this.config, actual as Map<K, V>, name, []);
}
}
}
return new ObjectValidatorImpl(this.config, actual, name, []);
}
/**
* Verifies requirements only if assertions are enabled.
*
* @param requirements - the requirements to verify
* @throws TypeError if <code>name</code> is null
* @throws RangeError if <code>name</code> is empty
*/
assertThat(requirements: (requirements: Requirements) => void)
{
Objects.requireThatValueIsDefinedAndNotNull(requirements, "requirements");
if (this.config.assertionsAreEnabled())
requirements(this.copy());
}
/**
* Verifies requirements only if assertions are enabled.
*
* @param requirements - the requirements to verify
* @returns the value returned by the operation, or <code>undefined</code> if assertions are disabled
* @throws TypeError if <code>name</code> is null
* @throws RangeError if <code>name</code> is empty
* @see #assertThat
*/
assertThatAndReturn<V>(requirements: (requirements: Requirements) => V)
{
Objects.requireThatValueIsDefinedAndNotNull(requirements, "requirements");
if (this.config.assertionsAreEnabled())
return requirements(this.copy());
return undefined;
}
/**
* Returns a copy of this configuration.
*
* @returns a copy of this configuration
*/
copy()
{
return new Requirements(this.config.copy());
}
/**
* Indicates whether <code>assertThat()</code> should invoke <code>requireThat()</code>.
*
* @returns true if <code>assertThat()</code> should delegate to <code>requireThat()</code>; false if it
* shouldn't do anything
*/
assertionsAreEnabled()
{
return this.config.assertionsAreEnabled();
}
/**
* Indicates that <code>assertThat()</code> should invoke <code>requireThat()</code>.
*
* @returns this
*/
withAssertionsEnabled()
{
this.config.withAssertionsEnabled();
return this;
}
/**
* Indicates that <code>assertThat()</code> shouldn't do anything.
*
* @returns this
*/
withAssertionsDisabled()
{
this.config.withAssertionsDisabled();
return this;
}
/**
* Indicates if exceptions should show the difference between the actual and expected values.
*
* @returns true by default
*/
isDiffEnabled()
{
return this.config.isDiffEnabled();
}
/**
* Indicates that exceptions should show the difference between the actual and expected values.
*
* @returns this
*/
withDiff()
{
this.config.withDiff();
return this;
}
/**
* Indicates that exceptions should not show the difference between the actual and expected
* values.
*
* @returns this
*/
withoutDiff()
{
this.config.withoutDiff();
return this;
}
/**
* @returns a map of key-value pairs to append to the exception message
* @see #putContext
*/
getContext()
{
return this.config.getContext();
}
/**
* Appends contextual information to the exception message.
*
* @param key - a key
* @param value - a value
* @returns this
* @throws TypeError if <code>key</code> is not a string
* @see #getContext
*/
putContext(key: string, value: unknown)
{
this.config.putContext(key, value);
return this;
}
}
export {Requirements};