-
Notifications
You must be signed in to change notification settings - Fork 160
/
MatcherTest.java
510 lines (459 loc) · 16.4 KB
/
MatcherTest.java
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* Copyright (c) 2020 The Go Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package com.google.re2j;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.common.truth.Truth;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Testing the RE2Matcher class.
*
* @author afrozm@google.com (Afroz Mohiuddin)
*/
@RunWith(JUnit4.class)
public class MatcherTest {
@Test
public void testLookingAt() {
ApiTestUtils.verifyLookingAt("abcdef", "abc", true);
ApiTestUtils.verifyLookingAt("ab", "abc", false);
}
@Test
public void testMatches() {
ApiTestUtils.testMatcherMatches("ab+c", "abbbc", "cbbba");
ApiTestUtils.testMatcherMatches("ab.*c", "abxyzc", "ab\nxyzc");
ApiTestUtils.testMatcherMatches("^ab.*c$", "abc", "xyz\nabc\ndef");
ApiTestUtils.testMatcherMatches("ab+c", "abbbc", "abbcabc");
}
@Test
public void testReplaceAll() {
ApiTestUtils.testReplaceAll(
"What the Frog's Eye Tells the Frog's Brain",
"Frog",
"Lizard",
"What the Lizard's Eye Tells the Lizard's Brain");
ApiTestUtils.testReplaceAll(
"What the Frog's Eye Tells the Frog's Brain",
"F(rog)",
"\\$Liza\\rd$1",
"What the $Lizardrog's Eye Tells the $Lizardrog's Brain");
ApiTestUtils.testReplaceAll(
"abcdefghijklmnopqrstuvwxyz123",
"(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)",
"$10$20",
"jb0wo0123");
ApiTestUtils.testReplaceAll(
"\u00e1\u0062\u00e7\u2655", "(.)", "<$1>", "<\u00e1><\u0062><\u00e7><\u2655>");
ApiTestUtils.testReplaceAll(
"\u00e1\u0062\u00e7\u2655", "[\u00e0-\u00e9]", "<$0>", "<\u00e1>\u0062<\u00e7>\u2655");
ApiTestUtils.testReplaceAll("hello world", "z*", "x", "xhxexlxlxox xwxoxrxlxdx");
// test replaceAll with alternation
ApiTestUtils.testReplaceAll("123:foo", "(?:\\w+|\\d+:foo)", "x", "x:x");
ApiTestUtils.testReplaceAll("123:foo", "(?:\\d+:foo|\\w+)", "x", "x");
ApiTestUtils.testReplaceAll("aab", "a*", "<$0>", "<aa><>b<>");
ApiTestUtils.testReplaceAll("aab", "a*?", "<$0>", "<>a<>a<>b<>");
}
@Test
public void testReplaceFirst() {
ApiTestUtils.testReplaceFirst(
"What the Frog's Eye Tells the Frog's Brain",
"Frog",
"Lizard",
"What the Lizard's Eye Tells the Frog's Brain");
ApiTestUtils.testReplaceFirst(
"What the Frog's Eye Tells the Frog's Brain",
"F(rog)",
"\\$Liza\\rd$1",
"What the $Lizardrog's Eye Tells the Frog's Brain");
ApiTestUtils.testReplaceFirst(
"abcdefghijklmnopqrstuvwxyz123",
"(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)",
"$10$20",
"jb0nopqrstuvwxyz123");
ApiTestUtils.testReplaceFirst(
"\u00e1\u0062\u00e7\u2655", "(.)", "<$1>", "<\u00e1>\u0062\u00e7\u2655");
ApiTestUtils.testReplaceFirst(
"\u00e1\u0062\u00e7\u2655", "[\u00e0-\u00e9]", "<$0>", "<\u00e1>\u0062\u00e7\u2655");
ApiTestUtils.testReplaceFirst("hello world", "z*", "x", "xhello world");
ApiTestUtils.testReplaceFirst("aab", "a*", "<$0>", "<aa>b");
ApiTestUtils.testReplaceFirst("aab", "a*?", "<$0>", "<>aab");
}
@Test
public void testGroupCount() {
ApiTestUtils.testGroupCount("(a)(b(c))d?(e)", 4);
}
@Test
public void testGroup() {
// ApiTestUtils.testGroup("xabdez", "(a)(b(c)?)d?(e)", new String[] {"abde", "a", "b", null, "e"});
// ApiTestUtils.testGroup("abc", "(a)(b$)?(b)?", new String[] {"ab", "a", null, "b"});
// ApiTestUtils.testGroup("abc", "(^b)?(b)?c", new String[] {"bc", null, "b"});
// ApiTestUtils.testGroup(" a b", "\\b(.).\\b", new String[] {"a ", "a"});
// Not allowed to use UTF-8 except in comments, per Java style guide.
// ("αβξδεφγ", "(.)(..)(...)", new String[] {"αβξδεφ", "α", "βξ", "δεφ"});
ApiTestUtils.testGroup(
"\u03b1\u03b2\u03be\u03b4\u03b5\u03c6\u03b3",
"(.)(..)(...)",
new String[] {
"\u03b1\u03b2\u03be\u03b4\u03b5\u03c6", "\u03b1", "\u03b2\u03be", "\u03b4\u03b5\u03c6"
});
}
@Test
public void testFind() {
ApiTestUtils.testFind("abcdefgh", ".*[aeiou]", 0, "abcde");
ApiTestUtils.testFind("abcdefgh", ".*[aeiou]", 1, "bcde");
ApiTestUtils.testFind("abcdefgh", ".*[aeiou]", 2, "cde");
ApiTestUtils.testFind("abcdefgh", ".*[aeiou]", 3, "de");
ApiTestUtils.testFind("abcdefgh", ".*[aeiou]", 4, "e");
ApiTestUtils.testFindNoMatch("abcdefgh", ".*[aeiou]", 5);
ApiTestUtils.testFindNoMatch("abcdefgh", ".*[aeiou]", 6);
ApiTestUtils.testFindNoMatch("abcdefgh", ".*[aeiou]", 7);
}
@Test
public void testInvalidFind() {
try {
ApiTestUtils.testFind("abcdef", ".*", 10, "xxx");
fail();
} catch (IndexOutOfBoundsException e) {
/* ok */
}
}
@Test
public void testInvalidReplacement() {
try {
ApiTestUtils.testReplaceFirst("abc", "abc", "$4", "xxx");
fail();
} catch (IndexOutOfBoundsException e) {
/* ok */
assertTrue(true);
}
}
@Test
public void testInvalidGroupNoMatch() {
try {
ApiTestUtils.testInvalidGroup("abc", "xxx", 0);
fail();
} catch (IllegalStateException e) {
// Linter complains on empty catch block.
assertTrue(true);
}
}
@Test
public void testInvalidGroupOutOfRange() {
try {
ApiTestUtils.testInvalidGroup("abc", "abc", 1);
fail();
} catch (IndexOutOfBoundsException e) {
// Linter complains on empty catch block.
assertTrue(true);
}
}
/**
* Test the NullPointerException is thrown on null input.
*/
@Test
public void testThrowsOnNullInputReset() {
// null in constructor.
try {
new Matcher(Pattern.compile("pattern"), (String) null);
fail();
} catch (NullPointerException n) {
// Linter complains on empty catch block.
assertTrue(true);
}
}
@Test
public void testThrowsOnNullInputCtor() {
// null in constructor.
try {
new Matcher(null, "input");
fail();
} catch (NullPointerException n) {
// Linter complains on empty catch block.
assertTrue(true);
}
}
/**
* Test that IllegalStateException is thrown if start/end are called before calling find
*/
@Test
public void testStartEndBeforeFind() {
try {
Matcher m = Pattern.compile("a").matcher("abaca");
m.start();
fail();
} catch (IllegalStateException ise) {
assertTrue(true);
}
}
/**
* Test for b/6891357. Basically matches should behave like find when it comes to updating the
* information of the match.
*/
@Test
public void testMatchesUpdatesMatchInformation() {
Matcher m = Pattern.compile("a+").matcher("aaa");
if (m.matches()) {
assertEquals("aaa", m.group(0));
}
}
/**
* Test for b/6891133. Test matches in case of alternation.
*/
@Test
public void testAlternationMatches() {
String s = "123:foo";
assertTrue(Pattern.compile("(?:\\w+|\\d+:foo)").matcher(s).matches());
assertTrue(Pattern.compile("(?:\\d+:foo|\\w+)").matcher(s).matches());
}
void helperTestMatchEndUTF16(String string, int num, final int end) {
String pattern = "[" + string + "]";
RE2 re =
new RE2(pattern) {
@Override
public boolean match(
CharSequence input, int start, int e, int anchor, int[] group, int ngroup) {
assertEquals(end, e);
return super.match(input, start, e, anchor, group, ngroup);
}
};
Pattern pat = new Pattern(pattern, 0, re);
Matcher m = pat.matcher(string);
int found = 0;
while (m.find()) {
found++;
}
assertEquals(
"Matches Expected " + num + " but found " + found + ", for input " + string, num, found);
}
/**
* Test for variable length encoding, test whether RE2's match function gets the required
* parameter based on UTF16 codes and not chars and Runes.
*/
@Test
public void testMatchEndUTF16() {
// Latin alphabetic chars such as these 5 lower-case, acute vowels have multi-byte UTF-8
// encodings but fit in a single UTF-16 code, so the final match is at UTF16 offset 5.
String vowels = "\225\233\237\243\250";
helperTestMatchEndUTF16(vowels, 5, 5);
// But surrogates are encoded as two UTF16 codes, so we should expect match
// to get 6 rather than 3.
String utf16 =
new StringBuilder()
.appendCodePoint(0x10000)
.appendCodePoint(0x10001)
.appendCodePoint(0x10002)
.toString();
assertEquals(utf16, "\uD800\uDC00\uD800\uDC01\uD800\uDC02");
helperTestMatchEndUTF16(utf16, 3, 6);
}
@Test
public void testAppendTail_StringBuffer() {
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
m.appendTail(sb);
assertEquals("one dog two dogs in the yards in the yard", sb.toString());
}
@Test
public void testAppendTail_StringBuilder() {
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuilder sb = new StringBuilder();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
m.appendTail(sb);
assertEquals("one dog two dogs in the yards in the yard", sb.toString());
}
@Test
public void testResetOnFindInt_StringBuffer() {
StringBuffer buffer;
Matcher matcher = Pattern.compile("a").matcher("zza");
assertTrue(matcher.find());
buffer = new StringBuffer();
matcher.appendReplacement(buffer, "foo");
assertEquals("1st time", "zzfoo", buffer.toString());
assertTrue(matcher.find(0));
buffer = new StringBuffer();
matcher.appendReplacement(buffer, "foo");
assertEquals("2nd time", "zzfoo", buffer.toString());
}
@Test
public void testResetOnFindInt_StringBuilder() {
StringBuilder buffer;
Matcher matcher = Pattern.compile("a").matcher("zza");
assertTrue(matcher.find());
buffer = new StringBuilder();
matcher.appendReplacement(buffer, "foo");
assertEquals("1st time", "zzfoo", buffer.toString());
assertTrue(matcher.find(0));
buffer = new StringBuilder();
matcher.appendReplacement(buffer, "foo");
assertEquals("2nd time", "zzfoo", buffer.toString());
}
@Test
public void testEmptyReplacementGroups_StringBuffer() {
StringBuffer buffer = new StringBuffer();
Matcher matcher = Pattern.compile("(a)(b$)?(b)?").matcher("abc");
assertTrue(matcher.find());
matcher.appendReplacement(buffer, "$1-$2-$3");
assertEquals("a--b", buffer.toString());
matcher.appendTail(buffer);
assertEquals("a--bc", buffer.toString());
buffer = new StringBuffer();
matcher = Pattern.compile("(a)(b$)?(b)?").matcher("ab");
assertTrue(matcher.find());
matcher.appendReplacement(buffer, "$1-$2-$3");
matcher.appendTail(buffer);
assertEquals("a-b-", buffer.toString());
buffer = new StringBuffer();
matcher = Pattern.compile("(^b)?(b)?c").matcher("abc");
assertTrue(matcher.find());
matcher.appendReplacement(buffer, "$1-$2");
matcher.appendTail(buffer);
assertEquals("a-b", buffer.toString());
buffer = new StringBuffer();
matcher = Pattern.compile("^(.)[^-]+(-.)?(.*)").matcher("Name");
assertTrue(matcher.find());
matcher.appendReplacement(buffer, "$1$2");
matcher.appendTail(buffer);
assertEquals("N", buffer.toString());
}
@Test
public void testEmptyReplacementGroups_StringBuilder() {
StringBuilder buffer = new StringBuilder();
Matcher matcher = Pattern.compile("(a)(b$)?(b)?").matcher("abc");
assertTrue(matcher.find());
matcher.appendReplacement(buffer, "$1-$2-$3");
assertEquals("a--b", buffer.toString());
matcher.appendTail(buffer);
assertEquals("a--bc", buffer.toString());
buffer = new StringBuilder();
matcher = Pattern.compile("(a)(b$)?(b)?").matcher("ab");
assertTrue(matcher.find());
matcher.appendReplacement(buffer, "$1-$2-$3");
matcher.appendTail(buffer);
assertEquals("a-b-", buffer.toString());
buffer = new StringBuilder();
matcher = Pattern.compile("(^b)?(b)?c").matcher("abc");
assertTrue(matcher.find());
matcher.appendReplacement(buffer, "$1-$2");
matcher.appendTail(buffer);
assertEquals("a-b", buffer.toString());
buffer = new StringBuilder();
matcher = Pattern.compile("^(.)[^-]+(-.)?(.*)").matcher("Name");
assertTrue(matcher.find());
matcher.appendReplacement(buffer, "$1$2");
matcher.appendTail(buffer);
assertEquals("N", buffer.toString());
}
// This example is documented in the com.google.re2j package.html.
@Test
public void testDocumentedExample() {
Pattern p = Pattern.compile("b(an)*(.)");
Matcher m = p.matcher("by, band, banana");
assertTrue(m.lookingAt());
m.reset();
assertTrue(m.find());
assertEquals("by", m.group(0));
assertNull(m.group(1));
assertEquals("y", m.group(2));
assertTrue(m.find());
assertEquals("band", m.group(0));
assertEquals("an", m.group(1));
assertEquals("d", m.group(2));
assertTrue(m.find());
assertEquals("banana", m.group(0));
assertEquals("an", m.group(1));
assertEquals("a", m.group(2));
assertFalse(m.find());
}
@Test
public void testMutableCharSequence() {
Pattern p = Pattern.compile("b(an)*(.)");
StringBuilder b = new StringBuilder("by, band, banana");
Matcher m = p.matcher(b);
assertTrue(m.find(0));
int start = b.indexOf("ban");
b.replace(b.indexOf("ban"), start + 3, "b");
assertTrue(m.find(b.indexOf("ban")));
}
@Test
public void testNamedGroups() {
Pattern p =
Pattern.compile(
"(?P<baz>f(?P<foo>b*a(?P<another>r+)){0,10})" + "(?P<bag>bag)?(?P<nomatch>zzz)?");
Matcher m = p.matcher("fbbarrrrrbag");
assertTrue(m.matches());
assertEquals("fbbarrrrr", m.group("baz"));
assertEquals("bbarrrrr", m.group("foo"));
assertEquals("rrrrr", m.group("another"));
assertEquals(0, m.start("baz"));
assertEquals(1, m.start("foo"));
assertEquals(4, m.start("another"));
assertEquals(9, m.end("baz"));
assertEquals(9, m.end("foo"));
assertEquals("bag", m.group("bag"));
assertEquals(9, m.start("bag"));
assertEquals(12, m.end("bag"));
assertNull(m.group("nomatch"));
assertEquals(-1, m.start("nomatch"));
assertEquals(-1, m.end("nomatch"));
assertEquals("whatbbarrrrreverbag", appendReplacement(m, "what$2ever${bag}"));
try {
m.group("nonexistent");
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
// Expected
}
}
private String appendReplacement(Matcher m, String replacement) {
StringBuilder b = new StringBuilder();
m.appendReplacement(b, replacement);
return b.toString();
}
// See https://github.com/google/re2j/issues/96.
// Ensures that RE2J generates the correct zero-width assertions (e.g. END_LINE, END_TEXT) when matching on
// a substring of a larger input. For example:
//
// pattern: (\d{2} ?(\d|[a-z])?)($|[^a-zA-Z])
// input: "22 bored"
//
// pattern.find(input) is true matcher.group(0) will contain "22 b". When retrieving group(1) from this matcher,
// RE2J re-matches the group, but only considers "22 b" as the input. If it incorrectly treats 'b' as END_OF_LINE
// and END_OF_TEXT, then group(1) will contain "22 b" when it should actually contain "22".
@Test
public void testGroupZeroWidthAssertions() {
Matcher m = Pattern.compile("(\\d{2} ?(\\d|[a-z])?)($|[^a-zA-Z])").matcher("22 bored");
Truth.assertThat(m.find()).isTrue();
Truth.assertThat(m.group(1)).isEqualTo("22");
}
@Test
public void testPatternLongestMatch() {
final String pattern = "(?:a+)|(?:a+ b+)";
final String text = "xxx aaa bbb yyy";
{
final Matcher matcher = Pattern.compile(pattern).matcher(text);
assertTrue(matcher.find());
assertEquals("aaa", text.substring(matcher.start(), matcher.end()));
}
{
final Matcher matcher = Pattern.compile(pattern, Pattern.LONGEST_MATCH).matcher(text);
assertTrue(matcher.find());
assertEquals("aaa bbb", text.substring(matcher.start(), matcher.end()));
}
}
}