-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathBasicFunctionalityUnitTest.java
679 lines (559 loc) · 23.6 KB
/
BasicFunctionalityUnitTest.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
package ru.lanwen.verbalregex;
import org.junit.Test;
import java.util.List;
import java.util.regex.MatchResult;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static ru.lanwen.verbalregex.VerbalExpression.regex;
import static ru.lanwen.verbalregex.matchers.EqualToRegexMatcher.equalToRegex;
import static ru.lanwen.verbalregex.matchers.TestMatchMatcher.matchesTo;
import static ru.lanwen.verbalregex.matchers.TestsExactMatcher.matchesExactly;
public class BasicFunctionalityUnitTest {
@Test
public void testSomething() {
VerbalExpression testRegex = new VerbalExpression.Builder().something().build();
assertThat("Null object doesn't have something", testRegex, not(matchesTo(null)));
assertThat("empty string doesn't have something", testRegex, not(matchesTo("")));
assertThat("a", testRegex, matchesTo("a"));
}
@Test
public void testAnything() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.anything()
.build();
assertThat(testRegex, matchesTo("what"));
assertThat(testRegex, not(matchesTo("")));
assertThat(testRegex, matchesTo(" "));
}
@Test
public void testAnythingBut() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.anythingBut("w")
.build();
assertFalse("starts with w", testRegex.testExact("what"));
assertTrue("Not contain w", testRegex.testExact("that"));
assertTrue("Not contain w", testRegex.testExact(" "));
assertFalse("Null object", testRegex.testExact(null));
}
@Test
public void testSomethingBut() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.somethingButNot("a")
.build();
assertFalse("Null string", testRegex.testExact(null));
assertFalse("empty string doesn't have something", testRegex.testExact(""));
assertTrue("doesn't contain a", testRegex.testExact("b"));
assertFalse("Contain a", testRegex.testExact("a"));
}
@Test
public void testStartOfLine() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.then("a")
.build();
assertFalse("Null string", testRegex.testExact(null));
assertFalse("empty string doesn't have something", testRegex.testExact(""));
assertThat("Starts with a", testRegex, matchesTo("a"));
assertThat("Starts with a", testRegex, matchesTo("ab"));
assertThat("Doesn't start with a", testRegex, not(matchesTo("ba")));
}
@Test
public void testStartOfLineFalse() {
VerbalExpression testRegex = regex()
.startOfLine(false)
.then("a")
.build();
assertThat(testRegex, matchesTo("ba"));
assertThat(testRegex, matchesTo("ab"));
}
@Test
public void testRangeWithMultiplyRanges() throws Exception {
VerbalExpression regex = regex().range("a", "z", "A", "Z").build();
assertThat("Regex with multi-range differs from expected", regex.toString(), equalTo("[a-zA-Z]"));
assertThat("Regex don't matches letter", regex, matchesTo("b"));
assertThat("Regex matches digit, but should match only letter", regex, not(matchesTo("1")));
}
@Test
public void testEndOfLine() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.find("a")
.endOfLine()
.build();
assertThat("Ends with a", testRegex, matchesTo("bba"));
assertThat("Ends with a", testRegex, matchesTo("a"));
assertThat("Ends with a", testRegex, not(matchesTo(null)));
assertThat("Doesn't end with a", testRegex, not(matchesTo("ab")));
}
@Test
public void testEndOfLineIsFalse() {
VerbalExpression testRegex = regex()
.find("a")
.endOfLine(false)
.build();
assertThat(testRegex, matchesTo("ba"));
assertThat(testRegex, matchesTo("ab"));
}
@Test
public void testMaybe() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.then("a")
.maybe("b")
.build();
assertThat("Regex isn't correct", testRegex.toString(), equalTo("^(?:a)(?:b)?"));
assertThat("Maybe has a 'b' after an 'a'", testRegex, matchesTo("acb"));
assertThat("Maybe has a 'b' after an 'a'", testRegex, matchesTo("abc"));
assertThat("Maybe has a 'b' after an 'a'", testRegex, not(matchesTo("cab")));
}
@Test
public void testAnyOf() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.then("a")
.anyOf("xyz")
.build();
assertThat("Has an x, y, or z after a", testRegex, matchesTo("ay"));
assertThat("Doesn't have an x, y, or z after a", testRegex, not(matchesTo("abc")));
}
@Test
public void testAnySameAsAnyOf() {
VerbalExpression any = regex().any("abc").build();
VerbalExpression anyOf = regex().anyOf("abc").build();
assertThat("any differs from anyOf", any.toString(), equalTo(anyOf.toString()));
}
@Test
public void testOr() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.then("abc")
.or("def")
.build();
assertThat("Starts with abc or def", testRegex, matchesTo("defzzz"));
assertThat("Doesn't start with abc or def", testRegex, not(matchesTo("xyzabc")));
}
@Test
public void testLineBreak() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.then("abc")
.lineBreak()
.then("def")
.build();
assertThat("abc then line break then def", testRegex, matchesTo("abc\r\ndef"));
assertThat("abc then line break then def", testRegex, matchesTo("abc\ndef"));
assertThat("abc then line break then space then def", testRegex, not(matchesTo("abc\r\n def")));
}
@Test
public void testMacintoshLineBreak() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.then("abc")
.lineBreak()
.then("def")
.build();
assertThat("abc then line break then def", testRegex, matchesTo("abc\r\rdef"));
}
@Test
public void testBr() {
VerbalExpression testRegexBr = new VerbalExpression.Builder()
.startOfLine()
.then("abc")
.br()
.then("def")
.build();
VerbalExpression testRegexLineBr = new VerbalExpression.Builder()
.startOfLine()
.then("abc")
.lineBreak()
.then("def")
.build();
assertThat(".br() differs from .lineBreak()", testRegexBr.toString(), equalTo(testRegexLineBr.toString()));
}
@Test
public void testTab() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.tab()
.then("abc")
.build();
assertThat("tab then abc", testRegex, matchesTo("\tabc"));
assertThat("no tab then abc", testRegex, not(matchesTo("abc")));
}
@Test
public void testWord() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.word()
.build();
assertThat("word", testRegex, matchesTo("abc123"));
assertThat("non-word", testRegex, not(matchesTo("@#")));
}
@Test
public void testMultipleNoRange() {
VerbalExpression testRegexStringOnly = new VerbalExpression.Builder()
.startOfLine()
.multiple("abc")
.build();
VerbalExpression testRegexStringAndNull = new VerbalExpression.Builder()
.startOfLine()
.multiple("abc", null)
.build();
VerbalExpression testRegexMoreThan2Ints = new VerbalExpression.Builder()
.startOfLine()
.multiple("abc", 2, 4, 8)
.build();
VerbalExpression[] testRegexesSameBehavior = {
testRegexStringOnly,
testRegexStringAndNull,
testRegexMoreThan2Ints
};
for (VerbalExpression testRegex : testRegexesSameBehavior) {
assertThat("abc once", testRegex,
matchesTo("abc"));
assertThat("abc more than once", testRegex,
matchesTo("abcabcabc"));
assertThat("no abc", testRegex,
not(matchesTo("xyz")));
}
}
@Test
public void testMultipleFrom() {
VerbalExpression testRegexFrom = new VerbalExpression.Builder()
.startOfLine()
.multiple("abc", 2)
.build();
assertThat("no abc", testRegexFrom,
not(matchesTo("xyz")));
assertThat("abc less than 2 times", testRegexFrom,
not(matchesTo("abc")));
assertThat("abc exactly 2 times", testRegexFrom,
matchesTo("abcabc"));
assertThat("abc more than 2 times", testRegexFrom,
matchesTo("abcabcabc"));
}
@Test
public void testMultipleFromTo() {
VerbalExpression testRegexFromTo = new VerbalExpression.Builder()
.startOfLine()
.multiple("abc", 2, 4)
.build();
assertThat("no abc", testRegexFromTo, not(matchesTo("xyz")));
assertThat("abc less than 2 times", testRegexFromTo,
not(matchesTo("abc")));
assertThat("abc exactly 2 times", testRegexFromTo, matchesTo("abcabc"));
assertThat("abc between 2 and 4 times", testRegexFromTo,
matchesTo("abcabcabc"));
assertThat("abc exactly 4 times", testRegexFromTo,
matchesTo("abcabcabcabc"));
assertThat("abc more than 4 times", testRegexFromTo,
not(matchesExactly("abcabcabcabcabc")));
}
@Test
public void testWithAnyCase() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.then("a")
.build();
assertThat("not case insensitive", testRegex, not(matchesTo("A")));
testRegex = new VerbalExpression.Builder()
.startOfLine()
.then("a")
.withAnyCase()
.build();
assertThat("case insensitive", testRegex, matchesTo("A"));
assertThat("case insensitive", testRegex, matchesTo("a"));
}
@Test
public void testWithAnyCaseTurnOnThenTurnOff() {
VerbalExpression testRegex = regex()
.withAnyCase()
.startOfLine()
.then("a")
.withAnyCase(false)
.build();
assertThat(testRegex, not(matchesTo("A")));
}
@Test
public void testWithAnyCaseIsFalse() {
VerbalExpression testRegex = regex()
.startOfLine()
.then("a")
.withAnyCase(false)
.build();
assertThat(testRegex, not(matchesTo("A")));
}
@Test
public void testSearchOneLine() {
VerbalExpression testRegex = regex()
.startOfLine()
.then("a")
.br()
.then("b")
.endOfLine()
.build();
assertThat("b is on the second line", testRegex, matchesTo("a\nb"));
testRegex = new VerbalExpression.Builder()
.startOfLine()
.then("a")
.br()
.then("b")
.endOfLine()
.searchOneLine(true)
.build();
assertThat("b is on the second line but we are only searching the first", testRegex, matchesTo("a\nb"));
}
@Test
public void testGetText() {
String testString = "123 https://www.google.com 456";
VerbalExpression testRegex = new VerbalExpression.Builder().add("http")
.maybe("s")
.then("://")
.then("www.")
.anythingBut(" ")
.add("com").build();
assertEquals(testRegex.getText(testString), "https://www.google.com");
}
@Test
public void testStartCapture() {
String text = "aaabcd";
VerbalExpression regex = regex()
.find("a").count(3)
.capture().find("b").anything().build();
assertThat("regex don't match string", regex.getText(text), equalTo(text));
assertThat("can't get first captured group", regex.getText(text, 1), equalTo("bcd"));
}
@Test
public void captIsSameAsCapture() {
assertThat("Capt produce defferent than capture regex", regex().capt().build().toString(),
equalTo(regex().capture().build().toString()));
}
@Test
public void shouldReturnEmptyStringWhenNoGroupFound() {
String text = "abc";
VerbalExpression regex = regex().find("d").capture().find("e").build();
assertThat("regex don't match string", regex.getText(text), equalTo(""));
assertThat("first captured group not empty string", regex.getText(text, 1), equalTo(""));
assertThat("second captured group not empty string", regex.getText(text, 2), equalTo(""));
}
@Test
public void testCountWithRange() {
String text4c = "abcccce";
String text2c = "abcce";
String text1c = "abce";
VerbalExpression regex = regex().find("c").count(2, 3).build();
assertThat("regex don't match string", regex.getText(text4c), equalTo("ccc"));
assertThat("regex don't match string", regex.getText(text2c), equalTo("cc"));
assertThat("regex don't match string", regex, not(matchesTo(text1c)));
}
@Test
public void testEndCapture() {
String text = "aaabcd";
VerbalExpression regex = regex()
.find("a")
.capture().find("b").anything().endCapture().then("cd").build();
assertThat(regex.getText(text), equalTo("abcd"));
assertThat("can't get first captured group", regex.getText(text, 1), equalTo("b"));
}
@Test
public void testMultiplyCapture() {
String text = "aaabcd";
VerbalExpression regex = regex()
.find("a").count(1)
.capture().find("b").endCapture().anything().capture().find("d").build();
assertThat("can't get first captured group", regex.getText(text, 1), equalTo("b"));
assertThat("can't get second captured group", regex.getText(text, 2), equalTo("d"));
}
@Test
public void testOrWithCapture() {
VerbalExpression testRegex = regex()
.capture()
.find("abc")
.or("def")
.build();
assertThat("Starts with abc or def", testRegex, matchesTo("defzzz"));
assertThat("Starts with abc or def", testRegex, matchesTo("abczzz"));
assertThat("Doesn't start with abc or def", testRegex, not(matchesExactly("xyzabcefg")));
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcnull"));
assertThat(testRegex.getText("xxxdefzzz", 1), equalTo("null"));
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcnull"));
}
@Test
public void testOrWithClosedCapture() {
VerbalExpression testRegex = regex()
.capture()
.find("abc")
.endCapt()
.or("def")
.build();
assertThat("Starts with abc or def", testRegex, matchesTo("defzzz"));
assertThat("Starts with abc or def", testRegex, matchesTo("abczzz"));
assertThat("Doesn't start with abc or def", testRegex, not(matchesExactly("xyzabcefg")));
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcnull"));
assertThat(testRegex.getText("xxxdefzzz", 1), equalTo("null"));
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcnull"));
}
@Test
public void addRegexBuilderWrapsItWithUnsavedGroup() throws Exception {
VerbalExpression regex = regex()
.add(regex().capt().find("string").count(2).endCapt().count(1).digit()).count(2).build();
assertThat("Added regex builder don't wrapped with unsaved group",
regex.toString(), startsWith("(?:((?:string"));
String example = "stringstring1";
String example2digit = "stringstring11";
assertThat(regex, matchesExactly(example + example));
assertThat(regex, not(matchesExactly(example2digit)));
}
@Test
public void multiplyWith1NumProduceSameAsCountResult() throws Exception {
VerbalExpression regex = regex().multiple("a", 1).build();
assertThat(regex, equalToRegex(regex().find("a").count(1)));
}
@Test
public void multiplyWith2NumProduceSameAsCountRangeResult() throws Exception {
VerbalExpression regex = regex().multiple("a", 1, 2).build();
assertThat(regex, equalToRegex(regex().find("a").count(1, 2)));
}
@Test
public void atLeast1HaveSameEffectAsOneOrMore() throws Exception {
VerbalExpression regex = regex().find("a").atLeast(1).build();
String matched = "aaaaaa";
String oneMatchedExactly = "a";
String oneMatched = "ab";
String empty = "";
assertThat(regex, matchesExactly(matched));
assertThat(regex, matchesExactly(oneMatchedExactly));
assertThat(regex, not(matchesExactly(oneMatched)));
assertThat(regex, matchesTo(oneMatched));
assertThat(regex, not(matchesTo(empty)));
}
@Test
public void oneOreMoreSameAsAtLeast1() throws Exception {
VerbalExpression regexWithOneOrMore = regex().find("a").oneOrMore().build();
String matched = "aaaaaa";
String oneMatchedExactly = "a";
String oneMatched = "ab";
String empty = "";
assertThat(regexWithOneOrMore, matchesExactly(matched));
assertThat(regexWithOneOrMore, matchesExactly(oneMatchedExactly));
assertThat(regexWithOneOrMore, not(matchesExactly(oneMatched)));
assertThat(regexWithOneOrMore, matchesTo(oneMatched));
assertThat(regexWithOneOrMore, not(matchesTo(empty)));
}
@Test
public void atLeast0HaveSameEffectAsZeroOrMore() throws Exception {
VerbalExpression regex = regex().find("a").atLeast(0).build();
String matched = "aaaaaa";
String oneMatchedExactly = "a";
String oneMatched = "ab";
String empty = "";
assertThat(regex, matchesExactly(matched));
assertThat(regex, matchesExactly(oneMatchedExactly));
assertThat(regex, not(matchesExactly(oneMatched)));
assertThat(regex, matchesTo(empty));
assertThat(regex, matchesExactly(empty));
}
@Test
public void zeroOreMoreSameAsAtLeast0() throws Exception {
VerbalExpression regexWithOneOrMore = regex().find("a").zeroOrMore().build();
String matched = "aaaaaa";
String oneMatchedExactly = "a";
String oneMatched = "ab";
String empty = "";
assertThat(regexWithOneOrMore, matchesExactly(matched));
assertThat(regexWithOneOrMore, matchesExactly(oneMatchedExactly));
assertThat(regexWithOneOrMore, not(matchesExactly(oneMatched)));
assertThat(regexWithOneOrMore, matchesTo(oneMatched));
assertThat(regexWithOneOrMore, matchesTo(empty));
assertThat(regexWithOneOrMore, matchesExactly(empty));
}
@Test
public void testOneOf() {
VerbalExpression testRegex = new VerbalExpression.Builder()
.startOfLine()
.oneOf("abc", "def")
.build();
assertThat("Starts with abc or def", testRegex, matchesTo("defzzz"));
assertThat("Starts with abc or def", testRegex, matchesTo("abczzz"));
assertThat("Doesn't start with abc nor def", testRegex, not(matchesTo("xyzabc")));
}
@Test
public void testOneOfWithCapture() {
VerbalExpression testRegex = regex()
.capture()
.oneOf("abc", "def")
.build();
assertThat("Starts with abc or def", testRegex, matchesTo("defzzz"));
assertThat("Starts with abc or def", testRegex, matchesTo("abczzz"));
assertThat("Doesn't start with abc or def", testRegex, not(matchesExactly("xyzabcefg")));
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcdef"));
assertThat(testRegex.getText("xxxdefzzz", 1), equalTo("def"));
}
@Test
public void testOneOfWithClosedCapture() {
VerbalExpression testRegex = regex()
.capture()
.oneOf("abc", "def")
.endCapt()
.build();
assertThat("Starts with abc or def", testRegex, matchesTo("defzzz"));
assertThat("Starts with abc or def", testRegex, matchesTo("abczzz"));
assertThat("Doesn't start with abc or def", testRegex, not(matchesExactly("xyzabcefg")));
assertThat(testRegex.getText("xxxabcdefzzz", 1), equalTo("abcdef"));
assertThat(testRegex.getText("xxxdefzzz", 1), equalTo("def"));
}
@Test
public void shouldAddMaybeWithOneOfFromAnotherBuilder() {
VerbalExpression.Builder namePrefix = regex().oneOf("Mr.", "Ms.");
VerbalExpression name = regex()
.maybe(namePrefix)
.space()
.zeroOrMore()
.word()
.oneOrMore()
.build();
assertThat("Is a name with prefix", name, matchesTo("Mr. Bond"));
assertThat("Is a name without prefix", name, matchesTo("James"));
}
@Test
public void testListOfTextGroups() {
String text = "SampleHelloWorldString";
VerbalExpression regex = regex()
.capt()
.oneOf("Hello", "World")
.endCapt()
.maybe("String")
.build();
List<String> groups0 = regex.getTextGroups(text, 0);
assertThat(groups0.get(0), equalTo("Hello"));
assertThat(groups0.get(1), equalTo("WorldString"));
List<String> groups1 = regex.getTextGroups(text, 1);
assertThat(groups1.get(0), equalTo("Hello"));
assertThat(groups1.get(1), equalTo("World"));
}
@Test
public void testListOfGroupSpans() {
String text = "SampleHelloWorldStringHello";
VerbalExpression regex = regex()
.capt()
.oneOf("Hello", "World")
.endCapt()
.maybe("String")
.build();
List<MatchResult> results = regex.getAllGroupSpans(text);
assertThat(results.size(), equalTo(3));
assertThat(results.get(0).groupCount(), equalTo(1));
assertThat(results.get(1).groupCount(), equalTo(1));
assertThat(results.get(2).groupCount(), equalTo(1));
// Hello
assertThat(results.get(0).start(1), equalTo(6));
assertThat(results.get(0).end(1), equalTo(11));
// World
assertThat(results.get(1).start(1), equalTo(11));
assertThat(results.get(1).end(1), equalTo(16));
// Hello
assertThat(results.get(2).start(1), equalTo(22));
assertThat(results.get(2).end(1), equalTo(27));
}
}