Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2006, 2008 IBM Corporation and others. |
2 |
* Copyright (c) 2006, 2012 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 15-21
Link Here
|
15 |
|
15 |
|
16 |
import junit.framework.TestCase; |
16 |
import junit.framework.TestCase; |
17 |
|
17 |
|
18 |
import org.eclipse.jface.text.BadLocationException; |
|
|
19 |
import org.eclipse.jface.text.Document; |
18 |
import org.eclipse.jface.text.Document; |
20 |
import org.eclipse.jface.text.IDocument; |
19 |
import org.eclipse.jface.text.IDocument; |
21 |
import org.eclipse.jface.text.IDocumentPartitioner; |
20 |
import org.eclipse.jface.text.IDocumentPartitioner; |
Lines 43-48
Link Here
|
43 |
*/ |
42 |
*/ |
44 |
protected abstract ICharacterPairMatcher createMatcher(final String chars); |
43 |
protected abstract ICharacterPairMatcher createMatcher(final String chars); |
45 |
|
44 |
|
|
|
45 |
protected abstract void checkResult(PairMatcherTestCase test, IRegion region, boolean isForward); |
46 |
|
46 |
/** |
47 |
/** |
47 |
* Returns the partitioning treated by the matcher. |
48 |
* Returns the partitioning treated by the matcher. |
48 |
* |
49 |
* |
Lines 58-215
Link Here
|
58 |
super(); |
59 |
super(); |
59 |
} |
60 |
} |
60 |
|
61 |
|
61 |
/* --- T e s t s --- */ |
|
|
62 |
|
63 |
/** Tests that the test case reader works */ |
64 |
public void testTestCaseReader() { |
65 |
performReaderTest("#( )%", 3, 0, "( )"); |
66 |
performReaderTest("%( )#", 0, 3, "( )"); |
67 |
performReaderTest("( )%", 3, -1, "( )"); |
68 |
performReaderTest("#%", 0, 0, ""); |
69 |
} |
70 |
|
71 |
/** |
72 |
* Very simple checks. |
73 |
* |
74 |
* @throws BadLocationException |
75 |
*/ |
76 |
public void testSimpleMatchSameMatcher() throws BadLocationException { |
77 |
final ICharacterPairMatcher matcher= createMatcher("()[]{}"); |
78 |
performMatch(matcher, "#( )%"); |
79 |
performMatch(matcher, "#[ ]%"); |
80 |
performMatch(matcher, "#{ }%"); |
81 |
performMatch(matcher, "(% )#"); |
82 |
performMatch(matcher, "[% ]#"); |
83 |
performMatch(matcher, "{% }#"); |
84 |
matcher.dispose(); |
85 |
} |
86 |
|
87 |
/** |
88 |
* Very simple checks. |
89 |
* |
90 |
* @throws BadLocationException |
91 |
*/ |
92 |
public void testSimpleMatchDifferentMatchers() throws BadLocationException { |
93 |
performMatch("()[]{}", "#( )%"); |
94 |
performMatch("()[]{}", "#[ ]%"); |
95 |
performMatch("()[]{}", "#{ }%"); |
96 |
performMatch("()[]{}", "(% )#"); |
97 |
performMatch("()[]{}", "[% ]#"); |
98 |
performMatch("()[]{}", "{% }#"); |
99 |
} |
100 |
|
101 |
/** |
102 |
* Close matches. |
103 |
* |
104 |
* @throws BadLocationException |
105 |
*/ |
106 |
public void testCloseMatches() throws BadLocationException { |
107 |
final ICharacterPairMatcher matcher= createMatcher("()[]{}"); |
108 |
performMatch(matcher, "#()%"); |
109 |
performMatch(matcher, "(%)#"); |
110 |
performMatch(matcher, "#(())%"); |
111 |
performMatch(matcher, "(%())#"); |
112 |
performMatch(matcher, "((%)#)"); |
113 |
performMatch(matcher, "(#()%)"); |
114 |
matcher.dispose(); |
115 |
} |
116 |
|
117 |
|
118 |
/** |
119 |
* Checks of simple situations where no matches should be found. |
120 |
* |
121 |
* @throws BadLocationException |
122 |
*/ |
123 |
public void testIncompleteMatch() throws BadLocationException { |
124 |
final ICharacterPairMatcher matcher= createMatcher("()[]{}"); |
125 |
performMatch(matcher, "(% "); |
126 |
performMatch(matcher, "%( )"); |
127 |
performMatch(matcher, "( % )"); |
128 |
performMatch(matcher, "( %)"); |
129 |
performMatch(matcher, "%"); |
130 |
matcher.dispose(); |
131 |
} |
132 |
|
133 |
/** |
134 |
* Test that it doesn't match across different partitions. |
135 |
* |
136 |
* @throws BadLocationException |
137 |
*/ |
138 |
public void testPartitioned() throws BadLocationException { |
139 |
final ICharacterPairMatcher matcher= createMatcher("()[]{}"); |
140 |
performMatch(matcher, "(% |a a| )#"); |
141 |
performMatch(matcher, "#( |a a| )%"); |
142 |
performMatch(matcher, "|b #( )% b|"); |
143 |
performMatch(matcher, "( |b )% b|"); |
144 |
performMatch(matcher, "(% |b ) b|"); |
145 |
performMatch(matcher, "|a ( a| )%"); |
146 |
performMatch(matcher, "|a (% a| )"); |
147 |
performMatch(matcher, "|c #( c| ) ( |c )% c|"); |
148 |
performMatch(matcher, "|c (% c| ) ( |c )# c|"); |
149 |
performMatch(matcher, "(% |a ) a| |b ) b| |c ) c| )#"); |
150 |
matcher.dispose(); |
151 |
} |
152 |
|
153 |
/** |
154 |
* Test that it works properly next to partition boundaries. |
155 |
* |
156 |
* @throws BadLocationException |
157 |
*/ |
158 |
public void testTightPartitioned() throws BadLocationException { |
159 |
final ICharacterPairMatcher matcher= createMatcher("()[]{}"); |
160 |
performMatch(matcher, "(|b)%b|"); |
161 |
performMatch(matcher, "(%|b)b|"); |
162 |
performMatch(matcher, "|a(a|)%"); |
163 |
performMatch(matcher, "|a(%a|)"); |
164 |
performMatch(matcher, "|c#(c|)(|c)%c|"); |
165 |
performMatch(matcher, "|c(%c|)(|c)#c|"); |
166 |
performMatch(matcher, "(%|a)a||b)b||c)c|)#"); |
167 |
matcher.dispose(); |
168 |
} |
169 |
|
170 |
/** Test that nesting works properly */ |
171 |
public void testNesting() { |
172 |
final ICharacterPairMatcher matcher= createMatcher("()[]{}"); |
173 |
performMatch(matcher, " ( #( ( ( ) ) ( ) )% ) "); |
174 |
performMatch(matcher, " ( (% ( ( ) ) ( ) )# ) "); |
175 |
performMatch(matcher, " ( #( { ( ) } [ ] )% ) "); |
176 |
performMatch(matcher, " ( (% { ( ) } [ ] )# ) "); |
177 |
performMatch(matcher, " ( ( #{ ( ) }% [ ] ) ) "); |
178 |
performMatch(matcher, " ( ( {% ( ) }# [ ] ) ) "); |
179 |
performMatch(matcher, "a(b#(c(d(e)f)g(h)i)%j)k"); |
180 |
performMatch(matcher, "a(b(%c(d(e)f)g(h)i)#j)k"); |
181 |
performMatch(matcher, "a(b#(c{d(e)f}g[h]i)%j)k"); |
182 |
performMatch(matcher, "a(b(%c{d(e)f}g[h]i)#j)k"); |
183 |
performMatch(matcher, "a(b(c#{d(e)f}%g[h]i)j)k"); |
184 |
performMatch(matcher, "a(b(c{%d(e)f}#g[h]i)j)k"); |
185 |
matcher.dispose(); |
186 |
} |
187 |
|
188 |
/** |
189 |
* Test a few boundary conditions. |
190 |
* |
191 |
* * @throws BadLocationException |
192 |
*/ |
193 |
public void testBoundaries() throws BadLocationException { |
194 |
final ICharacterPairMatcher matcher= createMatcher("()[]{}"); |
195 |
final StringDocument doc= new StringDocument("abcdefghijkl"); |
196 |
assertNull(matcher.match(null, 0)); |
197 |
assertNull(matcher.match(doc, -1)); |
198 |
assertNull(matcher.match(doc, doc.getLength() + 1)); |
199 |
matcher.dispose(); |
200 |
} |
201 |
|
202 |
public void testBug156426() { |
203 |
final ICharacterPairMatcher matcher= createMatcher("()[]{}<>"); |
204 |
performMatch(matcher, " #( a < b )% "); |
205 |
performMatch(matcher, " (% a < b )# "); |
206 |
performMatch(matcher, " #( a > b )% "); |
207 |
performMatch(matcher, " (% a > b )# "); |
208 |
matcher.dispose(); |
209 |
} |
210 |
|
211 |
/* --- U t i l i t i e s --- */ |
212 |
|
213 |
/** |
62 |
/** |
214 |
* Checks that the test case reader reads the test case as specified. |
63 |
* Checks that the test case reader reads the test case as specified. |
215 |
* |
64 |
* |
Lines 218-225
Link Here
|
218 |
* @param expectedMatch the expected match |
67 |
* @param expectedMatch the expected match |
219 |
* @param expectedString the expected string |
68 |
* @param expectedString the expected string |
220 |
*/ |
69 |
*/ |
221 |
private void performReaderTest(String testString, int expectedPos, int expectedMatch, String expectedString) { |
70 |
protected void performReaderTest(String testString, int expectedPos, int expectedMatch, String expectedString) { |
222 |
TestCase t0= createTestCase(testString); |
71 |
PairMatcherTestCase t0= createTestCase(testString); |
223 |
assertEquals(expectedPos, t0.fPos); |
72 |
assertEquals(expectedPos, t0.fPos); |
224 |
assertEquals(expectedMatch, t0.fMatch); |
73 |
assertEquals(expectedMatch, t0.fMatch); |
225 |
assertEquals(expectedString, t0.fString); |
74 |
assertEquals(expectedString, t0.fString); |
Lines 232-238
Link Here
|
232 |
* @param testCase the test string |
81 |
* @param testCase the test string |
233 |
*/ |
82 |
*/ |
234 |
protected void performMatch(final ICharacterPairMatcher matcher, final String testCase) { |
83 |
protected void performMatch(final ICharacterPairMatcher matcher, final String testCase) { |
235 |
final TestCase test= createTestCase(testCase); |
84 |
final PairMatcherTestCase test= createTestCase(testCase); |
236 |
matcher.clear(); |
85 |
matcher.clear(); |
237 |
final IRegion region= matcher.match(test.getDocument(), test.fPos); |
86 |
final IRegion region= matcher.match(test.getDocument(), test.fPos); |
238 |
if (test.fMatch == -1) { |
87 |
if (test.fMatch == -1) { |
Lines 244-260
Link Here
|
244 |
assertNotNull(region); |
93 |
assertNotNull(region); |
245 |
final boolean isForward= test.fPos > test.fMatch; |
94 |
final boolean isForward= test.fPos > test.fMatch; |
246 |
assertEquals(isForward, matcher.getAnchor() == ICharacterPairMatcher.RIGHT); |
95 |
assertEquals(isForward, matcher.getAnchor() == ICharacterPairMatcher.RIGHT); |
247 |
// If the match is forward, the curser is one character |
96 |
checkResult(test, region, isForward); |
248 |
// after the start of the match, so we need to count one |
|
|
249 |
// step backwards |
250 |
final int offset= isForward ? test.getOffset() : test.getOffset() - 1; |
251 |
final int length= isForward ? test.getLength() : test.getLength() + 1; |
252 |
assertEquals(length, region.getLength()); |
253 |
assertEquals(offset, region.getOffset()); |
254 |
} |
97 |
} |
255 |
} |
98 |
} |
256 |
|
99 |
|
257 |
private void performMatch(final String delims, final String testCase) { |
100 |
protected void performMatch(final String delims, final String testCase) { |
258 |
final ICharacterPairMatcher matcher= createMatcher(delims); |
101 |
final ICharacterPairMatcher matcher= createMatcher(delims); |
259 |
performMatch(matcher, testCase); |
102 |
performMatch(matcher, testCase); |
260 |
matcher.dispose(); |
103 |
matcher.dispose(); |
Lines 267-273
Link Here
|
267 |
* @param str the string for which to create the test case |
110 |
* @param str the string for which to create the test case |
268 |
* @return the created test case |
111 |
* @return the created test case |
269 |
*/ |
112 |
*/ |
270 |
public TestCase createTestCase(String str) { |
113 |
public PairMatcherTestCase createTestCase(String str) { |
271 |
int pos= str.indexOf("%"); |
114 |
int pos= str.indexOf("%"); |
272 |
assertFalse(pos == -1); |
115 |
assertFalse(pos == -1); |
273 |
int match= str.indexOf("#"); |
116 |
int match= str.indexOf("#"); |
Lines 276-290
Link Here
|
276 |
if (match != -1 && match < pos) pos -= 1; |
119 |
if (match != -1 && match < pos) pos -= 1; |
277 |
if (pos < match) match -= 1; |
120 |
if (pos < match) match -= 1; |
278 |
final String stripped= str.replaceAll("%", "").replaceAll("#", ""); |
121 |
final String stripped= str.replaceAll("%", "").replaceAll("#", ""); |
279 |
return new TestCase(stripped, pos, match); |
122 |
return new PairMatcherTestCase(stripped, pos, match); |
280 |
} |
123 |
} |
281 |
|
124 |
|
282 |
private class TestCase { |
125 |
public class PairMatcherTestCase { |
283 |
|
126 |
|
284 |
public final String fString; |
127 |
public final String fString; |
285 |
public final int fPos, fMatch; |
128 |
public final int fPos, fMatch; |
286 |
|
129 |
|
287 |
public TestCase(String string, int pos, int match) { |
130 |
public PairMatcherTestCase(String string, int pos, int match) { |
288 |
fString= string; |
131 |
fString= string; |
289 |
fPos= pos; |
132 |
fPos= pos; |
290 |
fMatch= match; |
133 |
fMatch= match; |
Lines 305-311
Link Here
|
305 |
|
148 |
|
306 |
} |
149 |
} |
307 |
|
150 |
|
308 |
private class StringDocument extends Document { |
151 |
class StringDocument extends Document { |
309 |
|
152 |
|
310 |
public StringDocument(String str) { |
153 |
public StringDocument(String str) { |
311 |
this.setTextStore(new StringTextStore(str)); |
154 |
this.setTextStore(new StringTextStore(str)); |