/
matchers.ts
485 lines (446 loc) · 13.3 KB
/
matchers.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import { isNil, pickBy, times } from 'ramda';
import { AnyJson } from '../common/jsonTypes';
import PactNative from '../../native/index.node';
/**
* Pact Matcher
*/
export interface Matcher<T> {
'pact:matcher:type': string;
'pact:generator:type'?: string;
value?: T;
}
export type AnyTemplate =
| AnyJson
| TemplateMap
| TemplateArray
| Matcher<unknown>;
interface TemplateMap {
[key: string]: AnyJson | AnyTemplate;
}
type TemplateArray = Array<AnyTemplate>;
/**
* Value must match the given template
* @param template Template to base the comparison on
*/
export const like = <T extends AnyTemplate>(template: T): Matcher<T> => ({
'pact:matcher:type': 'type',
value: template,
});
/**
* Object where the key itself is ignored, but the value template must match.
*
* @param keyTemplate Example key to use
* @param template Example value template to base the comparison on
*/
export const eachKeyLike = <T extends AnyTemplate>(
keyTemplate: string,
template: T
): Matcher<AnyTemplate> => ({
'pact:matcher:type': 'values',
value: {
[keyTemplate]: template,
},
});
/**
* Array where each element must match the given template
* @param template Template to base the comparison on
*/
export const eachLike = <T extends AnyTemplate>(template: T): Matcher<T[]> => ({
'pact:matcher:type': 'type',
value: [template],
});
/**
* Like Matcher with a minimum number of required values
*/
export interface MinLikeMatcher<T> extends Matcher<T> {
min: number;
}
/**
* An array that has to have at least one element and each element must match the given template
* @param template Template to base the comparison on
* @param count Number of examples to generate, defaults to one
*/
export const atLeastOneLike = <T extends AnyTemplate>(
template: T,
count = 1
): MinLikeMatcher<T[]> => ({
min: 1,
'pact:matcher:type': 'type',
value: times(() => template, count),
});
/**
* An array that has to have at least the required number of elements and each element must match the given template
* @param template Template to base the comparison on
* @param min Minimum number of elements required in the array
* @param count Number of examples to generate, defaults to min
*/
export const atLeastLike = <T extends AnyTemplate>(
template: T,
min: number,
count?: number
): MinLikeMatcher<T[]> => {
const elements = count || min;
if (count && count < min) {
throw new Error(
`atLeastLike has a minimum of ${min} but ${count} elements were requested.` +
` Make sure the count is greater than or equal to the min.`
);
}
return {
min,
'pact:matcher:type': 'type',
value: times(() => template, elements),
};
};
/**
* Like Matcher with a maximum number of required values
*/
export interface MaxLikeMatcher<T> extends Matcher<T> {
max: number;
}
/**
* An array that has to have at most the required number of elements and each element must match the given template
* @param template Template to base the comparison on
* @param max Maximum number of elements required in the array
* @param count Number of examples to generate, defaults to one
*/
export const atMostLike = <T extends AnyTemplate>(
template: T,
max: number,
count?: number
): MaxLikeMatcher<T[]> => {
const elements = count || 1;
if (count && count > max) {
throw new Error(
`atMostLike has a maximum of ${max} but ${count} elements where requested.` +
` Make sure the count is less than or equal to the max.`
);
}
return {
max,
'pact:matcher:type': 'type',
value: times(() => template, elements),
};
};
/**
* An array whose size is constrained to the minimum and maximum number of elements and each element must match the given template
* @param template Template to base the comparison on
* @param min Minimum number of elements required in the array
* @param max Maximum number of elements required in the array
* @param count Number of examples to generate, defaults to one
*/
export const constrainedArrayLike = <T extends AnyTemplate>(
template: T,
min: number,
max: number,
count?: number
): MinLikeMatcher<T[]> & MaxLikeMatcher<T[]> => {
const elements = count || min;
if (count) {
if (count < min) {
throw new Error(
`constrainedArrayLike has a minimum of ${min} but ${count} elements where requested.` +
` Make sure the count is greater than or equal to the min.`
);
} else if (count > max) {
throw new Error(
`constrainedArrayLike has a maximum of ${max} but ${count} elements where requested.` +
` Make sure the count is less than or equal to the max.`
);
}
}
return {
min,
max,
'pact:matcher:type': 'type',
value: times(() => template, elements),
};
};
/**
* Value must be a boolean
* @param b Boolean example value. Defaults to true if unsupplied
*/
export const boolean = (b = true): Matcher<boolean> => ({
'pact:matcher:type': 'type',
value: b,
});
/**
* Value must be an integer (must be a number and have no decimal places)
* @param int Example value. If omitted a random value will be generated.
*/
export const integer = (int?: number): Matcher<number> => {
if (int) {
return {
'pact:matcher:type': 'integer',
value: int,
};
}
return {
'pact:generator:type': 'RandomInt',
'pact:matcher:type': 'integer',
value: 101,
};
};
/**
* Value must be a decimal number (must be a number and have decimal places)
* @param num Example value. If omitted a random value will be generated.
*/
export const decimal = (num?: number): Matcher<number> => {
if (num) {
return {
'pact:matcher:type': 'decimal',
value: num,
};
}
return {
'pact:generator:type': 'RandomDecimal',
'pact:matcher:type': 'decimal',
value: 12.34,
};
};
/**
* Value must be a number
* @param num Example value. If omitted a random integer value will be generated.
*/
export function number(num?: number): Matcher<number> {
if (num) {
return {
'pact:matcher:type': 'number',
value: num,
};
}
return {
'pact:generator:type': 'RandomInt',
'pact:matcher:type': 'number',
value: 1234,
};
}
/**
* Value must be a string
* @param str Example value
*/
export function string(str: string): Matcher<string> {
return {
'pact:matcher:type': 'type',
value: str,
};
}
export interface RegexMatcher extends Matcher<string> {
regex: string;
example?: string;
}
/**
* Value that must match the given regular expression
* @param pattern Regular Expression to match
* @param str Example value
*/
export function regex(pattern: RegExp | string, str: string): RegexMatcher {
if (pattern instanceof RegExp) {
return {
'pact:matcher:type': 'regex',
regex: pattern.source,
value: str,
};
}
return {
'pact:matcher:type': 'regex',
regex: pattern,
value: str,
};
}
/**
* Value that must be equal to the example. This is mainly used to reset the matching rules which cascade.
* @param value Example value
*/
export const equal = <T extends AnyTemplate>(value: T): Matcher<T> => ({
'pact:matcher:type': 'equality',
value,
});
export interface DateTimeMatcher extends Matcher<string> {
format: string;
}
/**
* String value that must match the provided datetime format string.
* @param format Datetime format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)
* @param example Example value to use. If omitted a value using the current system date and time will be generated.
*/
export function datetime(format: string, example?: string): DateTimeMatcher {
return pickBy((v) => !isNil(v), {
'pact:generator:type': example ? undefined : 'DateTime',
'pact:matcher:type': 'timestamp',
format,
value: example || PactNative.generate_datetime_string(format),
});
}
/**
* String value that must match the provided datetime format string.
* @param format Datetime format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)
* @param example Example value to use. If omitted a value using the current system date and time will be generated.
*/
export function timestamp(format: string, example?: string): DateTimeMatcher {
return datetime(format, example);
}
/**
* String value that must match the provided time format string.
* @param format Time format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)
* @param example Example value to use. If omitted a value using the current system time will be generated.
*/
export function time(format: string, example?: string): DateTimeMatcher {
return {
'pact:generator:type': 'Time',
'pact:matcher:type': 'time',
format,
value: example || PactNative.generate_datetime_string(format),
};
}
/**
* String value that must match the provided date format string.
* @param format Date format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html)
* @param example Example value to use. If omitted a value using the current system date will be generated.
*/
export function date(format: string, example?: string): DateTimeMatcher {
return {
format,
'pact:generator:type': 'Date',
'pact:matcher:type': 'date',
value: example || PactNative.generate_datetime_string(format),
};
}
/**
* Value that must include the example value as a substring.
* @param value String value to include
*/
export function includes(value: string): Matcher<string> {
return {
'pact:matcher:type': 'include',
value,
};
}
/**
* Value that must be null. This will only match the JSON Null value. For other content types, it will
* match if the attribute is missing.
*/
export function nullValue(): Matcher<null> {
return {
'pact:matcher:type': 'null',
};
}
/**
* Matches a URL composed of a base path and a list of path fragments
* @param basePath Base path of the URL. If null, will use the base URL from the mock server.
* @param pathFragments list of path fragments, can be regular expressions
*/
export function url2(
basePath: string | null,
pathFragments: Array<string | RegexMatcher | RegExp>
): RegexMatcher {
const regexpr = [
'.*(',
...pathFragments.map((p) => {
if (p instanceof RegExp) {
return `\\/${p.source}`;
}
if (p instanceof Object && p['pact:matcher:type'] === 'regex') {
return `\\/${p.regex}`;
}
return `\\/${p.toString()}`;
}),
].join('');
const example = [
basePath || 'http://localhost:8080',
...pathFragments.map((p) => {
if (p instanceof RegExp) {
return `/${PactNative.generate_regex_string(p.source)}`;
}
if (p instanceof Object && p['pact:matcher:type'] === 'regex') {
return `/${p.value}`;
}
return `/${p.toString()}`;
}),
].join('');
// Temporary fix for inconsistancies between matchers and generators. Matchers use "value" attribute for
// example values, while generators use "example"
if (basePath == null) {
return {
'pact:matcher:type': 'regex',
'pact:generator:type': 'MockServerURL',
regex: `${regexpr})$`,
value: example,
example,
};
}
return {
'pact:matcher:type': 'regex',
regex: `${regexpr})$`,
value: example,
};
}
/**
* Matches a URL composed of a list of path fragments. The base URL from the mock server will be used.
* @param pathFragments list of path fragments, can be regular expressions
*/
export function url(
pathFragments: Array<string | RegexMatcher | RegExp>
): RegexMatcher {
return url2(null, pathFragments);
}
export interface ArrayContainsMatcher extends Matcher<AnyTemplate[]> {
variants: Array<AnyTemplate>;
}
/**
* Matches the items in an array against a number of variants. Matching is successful if each variant
* occurs once in the array. Variants may be objects containing matching rules.
*/
export function arrayContaining(
...variants: AnyTemplate[]
): ArrayContainsMatcher {
return {
'pact:matcher:type': 'arrayContains',
variants,
};
}
export interface ProviderStateInjectedValue extends Matcher<string> {
expression: string;
}
/**
* Marks a item as to be injected from the provider state
* @param expression Expression to lookup in the provider state context
* @param exampleValue Example value to use in the consumer test
*/
export function fromProviderState(
expression: string,
exampleValue: string
): ProviderStateInjectedValue {
return {
'pact:matcher:type': 'type',
'pact:generator:type': 'ProviderState',
expression,
value: exampleValue,
};
}
/**
* Match a universally unique identifier (UUID). Random values will be used for examples if no example is given.
*/
export function uuid(example?: string): RegexMatcher {
const regexStr =
'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
if (example) {
const regexpr = new RegExp(`^${regexStr}$`);
if (!example.match(regexpr)) {
throw new Error(
`regex: Example value '${example}' does not match the UUID regular expression '${regexStr}'`
);
}
return {
'pact:matcher:type': 'regex',
regex: regexStr,
value: example,
};
}
return {
'pact:matcher:type': 'regex',
regex: regexStr,
'pact:generator:type': 'Uuid',
value: 'e2490de5-5bd3-43d5-b7c4-526e33f71304',
};
}