-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathRegularExpressionTest.ts
144 lines (123 loc) · 4.59 KB
/
RegularExpressionTest.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
import assert from 'assert'
import fs from 'fs'
import glob from 'glob'
import yaml from 'js-yaml'
import ParameterTypeRegistry from '../src/ParameterTypeRegistry.js'
import RegularExpression from '../src/RegularExpression.js'
import { testDataDir } from './testDataDir.js'
interface Expectation {
expression: string
text: string
expected_args: string
}
describe('RegularExpression', () => {
<<<<<<< HEAD
it('documents match arguments', () => {
const parameterRegistry = new ParameterTypeRegistry()
const expr = /I have (\d+) cukes? in my (\w+) now/
const expression = new RegularExpression(expr, parameterRegistry)
const args = expression.match('I have 7 cukes in my belly now')!
assert.strictEqual(7, args[0].getValue(null))
assert.strictEqual('belly', args[1].getValue(null))
})
=======
for (const path of glob.sync(`${testDataDir}/regular-expression/matching/*.yaml`)) {
const expectation = yaml.load(fs.readFileSync(path, 'utf-8')) as Expectation
it(`matches ${path}`, () => {
const parameterTypeRegistry = new ParameterTypeRegistry()
const expression = new RegularExpression(
new RegExp(expectation.expression),
parameterTypeRegistry
)
const matches = expression.match(expectation.text)
assert.deepStrictEqual(
JSON.parse(JSON.stringify(matches ? matches.map((value) => value.getValue(null)) : null)), // Removes type information.
expectation.expected_args
)
})
}
>>>>>>> main
it('does no transform by default', () => {
assert.deepStrictEqual(match(/(\d\d)/, '22'), ['22'])
})
it('does not transform anonymous', () => {
assert.deepStrictEqual(match(/(.*)/, '22'), ['22'])
})
it('transforms negative int', () => {
assert.deepStrictEqual(match(/(-?\d+)/, '-22'), [-22])
})
it('transforms positive int', () => {
assert.deepStrictEqual(match(/(\d+)/, '22'), [22])
})
it('transforms float without integer part', () => {
assert.deepStrictEqual(
match(new RegExp(`(${ParameterTypeRegistry.FLOAT_REGEXP.source})`), '.22'),
[0.22]
)
})
it('transforms float with sign', () => {
assert.deepStrictEqual(
match(new RegExp(`(${ParameterTypeRegistry.FLOAT_REGEXP.source})`), '-1.22'),
[-1.22]
)
})
it('returns null when there is no match', () => {
assert.strictEqual(match(/hello/, 'world'), null)
})
it('matches empty string', () => {
assert.deepStrictEqual(match(/^The value equals "([^"]*)"$/, 'The value equals ""'), [''])
})
it('matches nested capture group without match', () => {
assert.deepStrictEqual(match(/^a user( named "([^"]*)")?$/, 'a user'), [null])
})
it('matches nested capture group with match', () => {
assert.deepStrictEqual(match(/^a user( named "([^"]*)")?$/, 'a user named "Charlie"'), [
'Charlie',
])
})
it('matches capture group nested in optional one', () => {
const regexp =
/^a (pre-commercial transaction |pre buyer fee model )?purchase(?: for \$(\d+))?$/
assert.deepStrictEqual(match(regexp, 'a purchase'), [null, null])
assert.deepStrictEqual(match(regexp, 'a purchase for $33'), [null, 33])
assert.deepStrictEqual(match(regexp, 'a pre buyer fee model purchase'), [
'pre buyer fee model ',
null,
])
})
it('ignores non capturing groups', () => {
assert.deepStrictEqual(
match(
/(\S+) ?(can|cannot)? (?:delete|cancel) the (\d+)(?:st|nd|rd|th) (attachment|slide) ?(?:upload)?/,
'I can cancel the 1st slide upload'
),
['I', 'can', 1, 'slide']
)
})
it('works with escaped parenthesis', () => {
assert.deepStrictEqual(match(/Across the line\(s\)/, 'Across the line(s)'), [])
})
it('exposes regexp and source', () => {
const regexp = /I have (\d+) cukes? in my (.+) now/
const expression = new RegularExpression(regexp, new ParameterTypeRegistry())
assert.deepStrictEqual(expression.regexp, regexp)
assert.deepStrictEqual(expression.source, regexp.source)
})
it('does not take consider parenthesis in character class as group', function () {
const expression = new RegularExpression(
/^drawings: ([A-Z_, ()]+)$/,
new ParameterTypeRegistry()
)
const args = expression.match('drawings: ONE, TWO(ABC)')!
assert.strictEqual(args[0].getValue(this), 'ONE, TWO(ABC)')
})
})
const match = (regexp: RegExp, text: string) => {
const parameterRegistry = new ParameterTypeRegistry()
const regularExpression = new RegularExpression(regexp, parameterRegistry)
const args = regularExpression.match(text)
if (!args) {
return null
}
return args.map((arg) => arg.getValue(null))
}