1 package com.atlassian.plugin.util;
2
3 import java.util.Arrays;
4 import java.util.regex.Pattern;
5
6 import org.junit.Test;
7
8 import static org.hamcrest.MatcherAssert.assertThat;
9 import static org.hamcrest.core.Is.is;
10
11 public class TestRegularExpressions
12 {
13 @Test
14 public void anyOfEmptyDoesntMatchAnything()
15 {
16 final String compound = RegularExpressions.anyOf(Arrays.<String>asList());
17 final Pattern pattern = Pattern.compile(compound);
18 assertThat(pattern.matcher("").matches(), is(false));
19 assertThat(pattern.matcher("a").matches(), is(false));
20
21 assertThat(pattern.matcher("a/A").matches(), is(false));
22 assertThat(pattern.matcher("a\\A").matches(), is(false));
23 assertThat(pattern.matcher("a\\\\A").matches(), is(false));
24 }
25
26 @Test
27 public void anyOfOneMatchesIt()
28 {
29 final String compound = RegularExpressions.anyOf(Arrays.asList("a+"));
30 final Pattern pattern = Pattern.compile(compound);
31 assertThat(pattern.matcher("").matches(), is(false));
32 assertThat(pattern.matcher("a").matches(), is(true));
33 assertThat(pattern.matcher("aa").matches(), is(true));
34 assertThat(pattern.matcher("b").matches(), is(false));
35 assertThat(pattern.matcher("bb").matches(), is(false));
36 assertThat(pattern.matcher("ab").matches(), is(false));
37 }
38
39 @Test
40 public void anyOfTwoMatchesEither()
41 {
42 final String compound = RegularExpressions.anyOf(Arrays.asList("a+", "b+"));
43 final Pattern pattern = Pattern.compile(compound);
44 assertThat(pattern.matcher("").matches(), is(false));
45 assertThat(pattern.matcher("a").matches(), is(true));
46 assertThat(pattern.matcher("aa").matches(), is(true));
47 assertThat(pattern.matcher("b").matches(), is(true));
48 assertThat(pattern.matcher("bb").matches(), is(true));
49 assertThat(pattern.matcher("ab").matches(), is(false));
50 assertThat(pattern.matcher("c").matches(), is(false));
51 assertThat(pattern.matcher("cc").matches(), is(false));
52 }
53
54 @Test
55 public void anyOfWithOverlap()
56 {
57 final String compound = RegularExpressions.anyOf(Arrays.asList("a(a|b)+b", "a(a|b)+b"));
58 final Pattern pattern = Pattern.compile(compound);
59 assertThat(pattern.matcher("").matches(), is(false));
60 assertThat(pattern.matcher("ab").matches(), is(false));
61 assertThat(pattern.matcher("aab").matches(), is(true));
62 assertThat(pattern.matcher("abb").matches(), is(true));
63 assertThat(pattern.matcher("acb").matches(), is(false));
64 }
65
66
67 @Test
68 public void naiveComplexExample()
69 {
70 final String compound = RegularExpressions.anyOf(Arrays.asList("ab", "c(d|e)f", "gh*g+"));
71 final Pattern pattern = Pattern.compile(compound);
72 assertThat(pattern.matcher("").matches(), is(false));
73 assertThat(pattern.matcher("a").matches(), is(false));
74 assertThat(pattern.matcher("ab").matches(), is(true));
75 assertThat(pattern.matcher("cf").matches(), is(false));
76 assertThat(pattern.matcher("cdf").matches(), is(true));
77 assertThat(pattern.matcher("cef").matches(), is(true));
78 assertThat(pattern.matcher("cdef").matches(), is(false));
79 assertThat(pattern.matcher("g").matches(), is(false));
80 assertThat(pattern.matcher("gg").matches(), is(true));
81 assertThat(pattern.matcher("ghg").matches(), is(true));
82 assertThat(pattern.matcher("ghgh").matches(), is(false));
83 }
84 }