forked from MixinNetwork/flutter-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex_test.dart
159 lines (138 loc) · 4.99 KB
/
regex_test.dart
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
import 'package:flutter/foundation.dart';
import 'package:flutter_app/utils/reg_exp_utils.dart';
import 'package:flutter_test/flutter_test.dart';
void match(RegExp regExp, String text, String uri) {
final matches = regExp.allMatches(text).toList();
expect(matches, isNotEmpty, reason: text);
expect(matches.length, 1, reason: text);
expect(matches.first[0], uri);
}
void main() {
final bigText = List.generate(1024 * 64, (index) => 'a').join();
group('uri', () {
test('speed', () {
final timer = Stopwatch()..start();
final matches = uriRegExp.allMatches(bigText).toList();
timer.stop();
expect(timer.elapsedMilliseconds, lessThan(30));
expect(matches, <RegExpMatch>[]);
if (kDebugMode) {
print('uri match large text: ${timer.elapsedMilliseconds}ms');
}
});
const uris = [
'https://www.mixin.one',
'https://www.mixin.one/',
'https://www.mixin.one/foo',
'https://www.mixin.one/foo/',
'https://www.mixin.one/foo/bar',
'https://www.mixin.one/foo/bar.html',
'https://www.mixin.one/foo/bar.php',
'http://www.mixin.one/path/to/page.html?key=value',
'ftp://ftp.example.com/files/file.txt',
'https://en.wikipedia.org/wiki/Regularexpression',
'http://localhost:8080/index.html',
'https://www.google.com/search?q=test&rlz=1C1GCEAenUS832US832&oq=test&aqs=chrome..69i57j0l7.2117j1j7&sourceid=chrome&ie=UTF-8',
'smb://server/share/file.txt',
'git+ssh://github.com/user/repo.git',
'ftps://ftp.example.com/files/file.txt',
'rsync://example.com/module/file.txt',
'ldap://ldap.example.com/dc=example,dc=com',
'svn+ssh://svn.example.com/project/trunk',
'ssh://[email protected]/path/to/file.txt',
'telnet://telnet.example.com:23/',
'sftp://example.com/user/file.txt',
'news:alt.test',
'gopher://gopher.example.com/00/',
'bitcoin:HASHVALUE',
];
test('syntax', () {
for (final uri in uris) {
match(uriRegExp, uri, uri);
}
});
test('accuracy', () {
for (final uri in uris) {
match(uriRegExp, '($uri)', uri);
match(uriRegExp, '[$uri]', uri);
match(uriRegExp, 'url: $uri', uri);
match(uriRegExp, '链接:$uri', uri);
match(uriRegExp, '$uri,接下来', uri);
match(uriRegExp, '$uri。', uri);
match(uriRegExp, '你看看这个$uri可以吗', uri);
}
});
}, skip: true);
group('bot number', () {
test('speed', () {
final timer = Stopwatch()..start();
final matches = botNumberRegExp.allMatches(bigText).toList();
timer.stop();
expect(timer.elapsedMilliseconds, lessThan(10));
expect(matches, <RegExpMatch>[]);
if (kDebugMode) {
print('bot number match large text: ${timer.elapsedMilliseconds}ms');
}
});
const botNumber = 7000123456;
test('syntax', () {
match(botNumberRegExp, '$botNumber', '$botNumber');
});
test('accuracy', () {
match(botNumberRegExp, '($botNumber)', '$botNumber');
match(botNumberRegExp, 'foo${botNumber}bar', '$botNumber');
match(botNumberRegExp, '福$botNumber报', '$botNumber');
match(botNumberRegExp, ':$botNumber,', '$botNumber');
match(botNumberRegExp, ':$botNumber。', '$botNumber');
});
}, skip: true);
group('mail', () {
test('speed', () {
final timer = Stopwatch()..start();
final matches = mailRegExp.allMatches(bigText).toList();
timer.stop();
expect(timer.elapsedMilliseconds, lessThan(20));
expect(matches, <RegExpMatch>[]);
if (kDebugMode) {
print('mail match large text: ${timer.elapsedMilliseconds}ms');
}
});
final mails = [
'jane.doe@my_domain.org',
'[email protected]', // IDN domain
'[email protected]', // new TLD
'[email protected]', // museum TLD
'[email protected]', // foundation TLD
'[email protected]', // not a real TLD, but still valid email format
];
test('syntax', () {
for (final mail in mails) {
match(mailRegExp, mail, mail);
}
});
test('accuracy', () {
for (final mail in mails) {
match(mailRegExp, '($mail)', mail);
match(mailRegExp, '[$mail]', mail);
match(mailRegExp, 'url: $mail', mail);
match(mailRegExp, '链接:$mail', mail);
match(mailRegExp, '$mail,接下来', mail);
match(mailRegExp, '$mail。', mail);
match(mailRegExp, '你看看这个$mail可以吗', mail);
}
});
}, skip: true);
}