-
-
Notifications
You must be signed in to change notification settings - Fork 240
/
pattern.test.ts
132 lines (120 loc) · 5.19 KB
/
pattern.test.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
import '@stoplight/spectral-test-utils/matchers';
import { RulesetValidationError } from '@stoplight/spectral-core';
import testFunction from './__helpers__/tester';
import pattern from '../pattern';
import AggregateError = require('es-aggregate-error');
const runPattern = testFunction.bind(null, pattern);
describe('Core Functions / Pattern', () => {
describe('given a string regex', () => {
it('given value matching the given match string regex without slashes, should return no error message', async () => {
expect(await runPattern('abc', { match: '[abc]+' })).toEqual([]);
});
it('given value matching the given match string regex with slashes, should return no error message', async () => {
expect(await runPattern('abc', { match: '/[abc]+/' })).toEqual([]);
});
it('should return empty array when given value matches the given match string regex with slashes and modifier', async () => {
expect(await runPattern('aBc', { match: '/[abc]+/im' })).toEqual([]);
});
it('should return same results when given a global (g) marker (pattern cache usecase)', async () => {
expect(await runPattern('abc', { match: '/[abc]+/gi' })).toEqual([]);
expect(await runPattern('abc', { match: '/[abc]+/gi' })).toEqual([]);
expect(await runPattern('abc', { match: '/[abc]+/gi' })).toEqual([]);
});
it('given string regex containing invalid flags, should throw an exception', async () => {
await expect(runPattern('aBc', { match: '/[abc]+/invalid' })).rejects.toThrow(
"Invalid flags supplied to RegExp constructor 'invalid'",
);
});
it('should return empty array when given value does not match the given notMatch string regex with slashes and modifier', async () => {
expect(await runPattern('def', { notMatch: '/[abc]+/i' })).toEqual([]);
});
it('should support unicode', async () => {
expect(await runPattern('DüsseldorfKölnМосква北京市إسرائيل', { match: '^\\p{Letter}+$' })).toEqual([]);
expect(await runPattern('DüsseldorfKölnМосква北京市إسرائيل', { match: '/^\\p{Letter}+$/u' })).toEqual([]);
});
});
describe('given match and notMatch regexes', () => {
it('should return empty array when given value match the given match and does not match the given notMatch regexes', async () => {
expect(await runPattern('def', { match: /[def]+/, notMatch: /[abc]+/ })).toEqual([]);
});
});
describe('validation', () => {
it.each([{ match: 'foo' }, { notMatch: 'foo' }, { match: 'foo', notMatch: 'bar' }])(
'given valid %p options, should not throw',
async opts => {
expect(await runPattern('def', opts)).toBeInstanceOf(Array);
},
);
it.each<[unknown, RulesetValidationError[]]>([
[
null,
[
new RulesetValidationError(
'invalid-function-options',
'"pattern" function has invalid options specified. Example valid options: { "match": "^Stoplight" }, { "notMatch": "Swagger" }, { "match": "Stoplight", "notMatch": "Swagger" }',
['rules', 'my-rule', 'then', 'functionOptions'],
),
],
],
[
{},
[
new RulesetValidationError(
'invalid-function-options',
`"pattern" function has invalid options specified. Example valid options: { "match": "^Stoplight" }, { "notMatch": "Swagger" }, { "match": "Stoplight", "notMatch": "Swagger" }`,
['rules', 'my-rule', 'then', 'functionOptions'],
),
],
],
[
{ foo: true },
[
new RulesetValidationError('invalid-function-options', '"pattern" function does not support "foo" option', [
'rules',
'my-rule',
'then',
'functionOptions',
'foo',
]),
],
],
[
{ match: 2 },
[
new RulesetValidationError(
'invalid-function-options',
'"pattern" function and its "match" option must be string or RegExp instance',
['rules', 'my-rule', 'then', 'functionOptions', 'match'],
),
],
],
[
{ notMatch: null },
[
new RulesetValidationError(
'invalid-function-options',
'"pattern" function and its "notMatch" option must be string or RegExp instance',
['rules', 'my-rule', 'then', 'functionOptions', 'notMatch'],
),
],
],
[
{ match: 4, notMatch: 10 },
[
new RulesetValidationError(
'invalid-function-options',
`"pattern" function and its "match" option must be string or RegExp instance`,
['rules', 'my-rule', 'then', 'functionOptions', 'match'],
),
new RulesetValidationError(
'invalid-function-options',
`"pattern" function and its "notMatch" option must be string or RegExp instance`,
['rules', 'my-rule', 'then', 'functionOptions', 'notMatch'],
),
],
],
])('given invalid %p options, should throw', async (opts, errors) => {
await expect(runPattern('abc', opts)).rejects.toThrowAggregateError(new AggregateError(errors));
});
});
});