1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.velocity.tools; |
21 |
|
|
22 |
|
import java.util.List; |
23 |
|
import java.util.regex.Matcher; |
24 |
|
import java.util.regex.Pattern; |
25 |
|
|
26 |
|
import org.junit.Assert; |
27 |
|
import org.junit.Test; |
28 |
|
import org.xwiki.velocity.tools.RegexTool.RegexResult; |
29 |
|
|
30 |
|
|
31 |
|
@link |
32 |
|
|
33 |
|
@version |
34 |
|
@since |
35 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (64) |
Complexity: 9 |
Complexity Density: 0.16 |
|
36 |
|
public class RegexToolTest |
37 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
38 |
1 |
@Test... |
39 |
|
public void testFind() |
40 |
|
{ |
41 |
1 |
RegexTool tool = new RegexTool(); |
42 |
1 |
List<RegexResult> result = |
43 |
|
tool.find("<h1><span>header</span></h1> whatever", "<[hH][12].*?><span>(.*?)</span></[hH][12]>"); |
44 |
|
|
45 |
1 |
Assert.assertEquals(2, result.size()); |
46 |
1 |
Assert.assertEquals("<h1><span>header</span></h1>", result.get(0).getGroup()); |
47 |
1 |
Assert.assertEquals(0, result.get(0).getStart()); |
48 |
1 |
Assert.assertEquals(28, result.get(0).getEnd()); |
49 |
1 |
Assert.assertEquals("header", result.get(1).getGroup()); |
50 |
1 |
Assert.assertEquals(10, result.get(1).getStart()); |
51 |
1 |
Assert.assertEquals(16, result.get(1).getEnd()); |
52 |
|
} |
53 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
54 |
1 |
@Test... |
55 |
|
public void testFindWithoutMatches() |
56 |
|
{ |
57 |
1 |
RegexTool tool = new RegexTool(); |
58 |
1 |
List<RegexResult> result = tool.find("nothing here", "something"); |
59 |
|
|
60 |
1 |
Assert.assertEquals(0, result.size()); |
61 |
|
} |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
63 |
1 |
@Test... |
64 |
|
public void findAll() |
65 |
|
{ |
66 |
1 |
RegexTool tool = new RegexTool(); |
67 |
1 |
List<List<RegexResult>> result = |
68 |
|
tool.findAll("one :two three (:four) five :six seven=:eight", ":(\\w+) (\\w+)"); |
69 |
|
|
70 |
1 |
Assert.assertEquals(2, result.size()); |
71 |
1 |
Assert.assertEquals(":two three", result.get(0).get(0).getGroup()); |
72 |
1 |
Assert.assertEquals(":six seven", result.get(1).get(0).getGroup()); |
73 |
|
|
74 |
1 |
Assert.assertEquals(3, result.get(0).size()); |
75 |
1 |
Assert.assertEquals("two", result.get(0).get(1).getGroup()); |
76 |
1 |
Assert.assertEquals("three", result.get(0).get(2).getGroup()); |
77 |
|
|
78 |
1 |
Assert.assertEquals(3, result.get(1).size()); |
79 |
1 |
Assert.assertEquals("six", result.get(1).get(1).getGroup()); |
80 |
1 |
Assert.assertEquals("seven", result.get(1).get(2).getGroup()); |
81 |
|
} |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
83 |
1 |
@Test... |
84 |
|
public void findAllNoMatch() |
85 |
|
{ |
86 |
1 |
RegexTool tool = new RegexTool(); |
87 |
1 |
List<List<RegexResult>> result = tool.findAll("nothing here", "something"); |
88 |
|
|
89 |
1 |
Assert.assertEquals(0, result.size()); |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
95 |
1 |
@Test... |
96 |
|
public void testCompileValidRegex() |
97 |
|
{ |
98 |
1 |
RegexTool tool = new RegexTool(); |
99 |
1 |
Pattern p = tool.compile("ab?"); |
100 |
1 |
Assert.assertNotNull(p); |
101 |
|
|
102 |
1 |
Matcher m = p.matcher("xyz"); |
103 |
1 |
Assert.assertFalse(m.matches()); |
104 |
1 |
m = p.matcher("a"); |
105 |
1 |
Assert.assertTrue(m.matches()); |
106 |
1 |
m = p.matcher("aba"); |
107 |
1 |
Assert.assertFalse(m.matches()); |
108 |
1 |
m.reset(); |
109 |
1 |
Assert.assertTrue(m.find() && m.find() && m.hitEnd()); |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
115 |
1 |
@Test... |
116 |
|
public void testCompileRegexWithFlags() |
117 |
|
{ |
118 |
1 |
RegexTool tool = new RegexTool(); |
119 |
1 |
Pattern p = tool.compile("(?im)^ab?$"); |
120 |
1 |
Assert.assertNotNull(p); |
121 |
|
|
122 |
1 |
Matcher m = p.matcher("xyz"); |
123 |
1 |
Assert.assertFalse(m.matches()); |
124 |
1 |
m = p.matcher("A"); |
125 |
1 |
Assert.assertTrue(m.matches()); |
126 |
1 |
m = p.matcher("ab\na"); |
127 |
1 |
Assert.assertFalse(m.matches()); |
128 |
1 |
m.reset(); |
129 |
1 |
Assert.assertTrue(m.find() && m.find() && m.hitEnd()); |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
135 |
1 |
@Test... |
136 |
|
public void testCompileInvalidRegex() |
137 |
|
{ |
138 |
1 |
RegexTool tool = new RegexTool(); |
139 |
1 |
Pattern p = tool.compile("*"); |
140 |
1 |
Assert.assertNull(p); |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
146 |
1 |
@Test... |
147 |
|
public void testQuote() |
148 |
|
{ |
149 |
1 |
RegexTool tool = new RegexTool(); |
150 |
1 |
Assert.assertEquals(Pattern.quote("^(\\)[]"), tool.quote("^(\\)[]")); |
151 |
|
} |
152 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
153 |
1 |
@Test... |
154 |
|
public void testQuoteReplacement() |
155 |
|
{ |
156 |
1 |
RegexTool tool = new RegexTool(); |
157 |
1 |
Assert.assertEquals(Matcher.quoteReplacement("$1 \\$2"), tool.quoteReplacement("$1 \\$2")); |
158 |
|
} |
159 |
|
} |