Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6108c22400bc18223a79f112821b7ecc4d48bfa9 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2006, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Christian Plesner Hansen (plesner@quenta.org) - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.text.tests;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextStore;
import org.eclipse.jface.text.rules.FastPartitioner;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
import org.eclipse.jface.text.rules.SingleLineRule;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;
import org.eclipse.jface.text.source.ICharacterPairMatcher;
import org.eclipse.jface.text.source.ICharacterPairMatcherExtension;

/**
 * Generic test of simple character pair matchers
 *
 * @since 3.3
 */
public abstract class AbstractPairMatcherTest extends TestCase {

	private final boolean fCaretEitherSideOfBracket;

	public AbstractPairMatcherTest(boolean caretEitherSideOfBracket) {
		fCaretEitherSideOfBracket= caretEitherSideOfBracket;
	}

	/**
	 * Constructs a new character pair matcher.
	 * 
	 * @param chars the characters to match
	 * @return the character pair matcher
	 */
	protected ICharacterPairMatcher createMatcher(final String chars) {
		return new DefaultCharacterPairMatcher(chars.toCharArray(), getDocumentPartitioning(), fCaretEitherSideOfBracket);
	}

	/**
	 * Returns the partitioning treated by the matcher.
	 * 
	 * @return the partition
	 */
	protected String getDocumentPartitioning() {
		return IDocumentExtension3.DEFAULT_PARTITIONING;
	}

	/* --- T e s t s --- */

	/** Tests that the test case reader works */
	public void testTestCaseReader() {
		performReaderTest("%( )#", 0, 3, "( )");
		performReaderTest("#%", 0, 0, "");
	}

	/**
	 * Very simple checks.
	 * 
	 * @throws BadLocationException
	 */
	public void testSimpleMatchSameMatcher() throws BadLocationException {
		final ICharacterPairMatcher matcher= createMatcher("()[]{}");
		performMatch(matcher, "#(   )%");
		performMatch(matcher, "#[   ]%");
		performMatch(matcher, "#{   }%");
		performMatch(matcher, "(%   )#");
		performMatch(matcher, "[%   ]#");
		performMatch(matcher, "{%   }#");

		performMatch(matcher, "#(   %)%");
		performMatch(matcher, "#[   %]%");
		performMatch(matcher, "#{   %}%");
		performMatch(matcher, "%(%   )#");
		performMatch(matcher, "%[%   ]#");
		performMatch(matcher, "%{%   }#");

		performMatch(matcher, "#(  %  )#");
		performMatch(matcher, "#[  %  ]#");
		performMatch(matcher, "#{  %  }#");

		performMatch(matcher, "#(  % %  )#");
		performMatch(matcher, "#[  % %  ]#");
		performMatch(matcher, "#{  % %  }#");

		matcher.dispose();
	}

	/**
	 * Very simple checks.
	 * 
	 * @throws BadLocationException
	 */
	public void testSimpleMatchDifferentMatchers() throws BadLocationException {
		performMatch("()[]{}", "#(   )%");
		performMatch("()[]{}", "#[   ]%");
		performMatch("()[]{}", "#{   }%");
		performMatch("()[]{}", "(%   )#");
		performMatch("()[]{}", "[%   ]#");
		performMatch("()[]{}", "{%   }#");

		performMatch("()[]{}", "#(  %  )#");
		performMatch("()[]{}", "#[  %  ]#");
		performMatch("()[]{}", "#{  %  }#");
	}

	/**
	 * Close matches.
	 * 
	 * @throws BadLocationException
	 */
	public void testCloseMatches() throws BadLocationException {
		final ICharacterPairMatcher matcher= createMatcher("()[]{}");
		performMatch(matcher, "(%)#");
		performMatch(matcher, "#(())%");
		performMatch(matcher, "(%())#");
		performMatch(matcher, "((%)#)");

		performMatch(matcher, "%(%)#");
		performMatch(matcher, "#(()%)%");
		performMatch(matcher, "%(%())#");
		performMatch(matcher, "(%(%)#)");

		performMatch(matcher, "#(%)#");
		performMatch(matcher, "#(%())#");

		performMatch(matcher, "#(% %)#");
		performMatch(matcher, "#(% %())#");

		matcher.dispose();
	}


	/**
	 * Checks of simple situations where no matches should be found.
	 * 
	 * @throws BadLocationException
	 */
	public void testIncompleteMatch() throws BadLocationException {
		final ICharacterPairMatcher matcher= createMatcher("()[]{}");
		performMatch(matcher, "(% ");
		performMatch(matcher, "( % )");
		performMatch(matcher, "%");
		matcher.dispose();
	}

	/**
	 * Test that it doesn't match across different partitions.
	 * 
	 * @throws BadLocationException
	 */
	public void testPartitioned() throws BadLocationException {
		final ICharacterPairMatcher matcher= createMatcher("()[]{}");
		performMatch(matcher, "(% |a a| )#");
		performMatch(matcher, "#( |a a| )%");
		performMatch(matcher, "|b #( )% b|");
		performMatch(matcher, "( |b )% b|");
		performMatch(matcher, "(% |b ) b|");
		performMatch(matcher, "|a ( a| )%");
		performMatch(matcher, "|a (% a| )");
		performMatch(matcher, "|c #( c| ) ( |c )% c|");
		performMatch(matcher, "|c (% c| ) ( |c )# c|");
		performMatch(matcher, "(% |a ) a| |b ) b| |c ) c| )#");

		performMatch(matcher, "#( % |a a| )#");
		performMatch(matcher, "|b #( % )# b|");
		performMatch(matcher, "|c #( % c| ) ( |c )# c|");
		performMatch(matcher, "|c #( c| ) ( |c % )# c|");
		performMatch(matcher, "#( % |a ) a| |b ) b| |c ) c| )#");

		performMatch(matcher, "#( |a % a| )#");
		performMatch(matcher, "( |a #( a| ( |a % a| ) |a )# a| )");

		performMatch(matcher, "#( % % |a a| )#");
		performMatch(matcher, "|b #( % % )# b|");
		performMatch(matcher, "|c #( % % c| ) ( |c )# c|");
		performMatch(matcher, "|c #( c| ) ( |c % % )# c|");
		performMatch(matcher, "#( % % |a ) a| |b ) b| |c ) c| )#");
		performMatch(matcher, " #( |c ( c| % % |c ) c| )#");

		matcher.dispose();
	}

	/**
	 * Test that it works properly next to partition boundaries.
	 * 
	 * @throws BadLocationException
	 */
	public void testTightPartitioned() throws BadLocationException {
		final ICharacterPairMatcher matcher= createMatcher("()[]{}");
		performMatch(matcher, "(|b)%b|");
		performMatch(matcher, "(%|b)b|");
		performMatch(matcher, "|a(a|)%");
		performMatch(matcher, "|a(%a|)");
		performMatch(matcher, "|c#(c|)(|c)%c|");
		performMatch(matcher, "|c(%c|)(|c)#c|");
		performMatch(matcher, "(%|a)a||b)b||c)c|)#");

		performMatch(matcher, "|c#(c|)(|%c)#c|");
		performMatch(matcher, "|c#(c%|)(|c)#c|");
		performMatch(matcher, "#(%|a)a||b)b||c)c|)#");

		matcher.dispose();
	}

	/** Test that nesting works properly */
	public void testNesting() {
		final ICharacterPairMatcher matcher= createMatcher("()[]{}");
		performMatch(matcher, " ( #( ( ( ) ) ( ) )% ) ");
		performMatch(matcher, " ( (% ( ( ) ) ( ) )# ) ");
		performMatch(matcher, " ( #( { ( ) } [ ] )% ) ");
		performMatch(matcher, " ( (% { ( ) } [ ] )# ) ");
		performMatch(matcher, " ( ( #{ ( ) }% [ ] ) ) ");
		performMatch(matcher, " ( ( {% ( ) }# [ ] ) ) ");
		performMatch(matcher, "a(b#(c(d(e)f)g(h)i)%j)k");
		performMatch(matcher, "a(b(%c(d(e)f)g(h)i)#j)k");
		performMatch(matcher, "a(b#(c{d(e)f}g[h]i)%j)k");
		performMatch(matcher, "a(b(%c{d(e)f}g[h]i)#j)k");
		performMatch(matcher, "a(b(c#{d(e)f}%g[h]i)j)k");
		performMatch(matcher, "a(b(c{%d(e)f}#g[h]i)j)k");

		performMatch(matcher, " ( #( ( ( ) ) ( ) % )# ) ");
		performMatch(matcher, " ( #( % ( ( ) ) ( ) )# ) ");
		performMatch(matcher, " ( #( { ( ) } [ ] % )# ) ");
		performMatch(matcher, " ( #( % { ( ) } [ ] )# ) ");
		performMatch(matcher, " ( ( #{ ( ) % }# [ ] ) ) ");
		performMatch(matcher, " ( ( #{ % ( ) }# [ ] ) ) ");
		performMatch(matcher, "a(b#(c(d(e)f)g(h)i%)#j)k");
		performMatch(matcher, "a(b#(%c(d(e)f)g(h)i)#j)k");
		performMatch(matcher, "a(b#(c{d(e)f}g[h]i%)#j)k");
		performMatch(matcher, "a(b#(%c{d(e)f}g[h]i)#j)k");
		performMatch(matcher, "a(b(c#{d(e)f%}#g[h]i)j)k");
		performMatch(matcher, "a(b(c#{%d(e)f}#g[h]i)j)k");

		matcher.dispose();
	}

	/**
	 * Test a few boundary conditions.
	 * 
	 * * @throws BadLocationException
	 */
	public void testBoundaries() throws BadLocationException {
		final ICharacterPairMatcher matcher= createMatcher("()[]{}");
		final StringDocument doc= new StringDocument("abcdefghijkl");
		assertNull(matcher.match(null, 0));
		assertNull(matcher.match(doc, -1));
		assertNull(matcher.match(doc, doc.getLength() + 1));
		matcher.dispose();
	}

	public void testBug156426() {
		final ICharacterPairMatcher matcher= createMatcher("()[]{}<>");
		performMatch(matcher, " #( a < b )% ");
		performMatch(matcher, " (% a < b )# ");
		performMatch(matcher, " #( a > b )% ");
		performMatch(matcher, " (% a > b )# ");
		matcher.dispose();
	}

	public void testBug377417() {
		final ICharacterPairMatcher matcher= createMatcher("()[]{}");
		performMatch(matcher, "#( %  )%#");
		performMatch(matcher, "#[ %  ]%#");
		performMatch(matcher, "#{ %  }%#");
		matcher.dispose();
	}

	/* --- U t i l i t i e s --- */

	/**
	 * Checks that the test case reader reads the test case as specified.
	 * 
	 * @param testString the string to test
	 * @param expectedPos the expected position
	 * @param expectedMatch the expected match
	 * @param expectedString the expected string
	 */
	protected void performReaderTest(String testString, int expectedPos, int expectedMatch, String expectedString) {
		TestCase t0= createTestCase(testString);
		assertEquals(expectedPos, t0.fPos1);
		assertEquals(expectedMatch, t0.fMatch2);
		assertEquals(expectedString, t0.fString);
	}

	/**
	 * Checks that the given matcher matches the input as specified.
	 * 
	 * @param matcher the matcher
	 * @param testCase the test string
	 */
	protected void performMatch(final ICharacterPairMatcher matcher, final String testCase) {
		final TestCase test= createTestCase(testCase);
		matcher.clear();
		final IRegion region;

		if (test.isSelectionTestCase()) {
			assertTrue((matcher instanceof ICharacterPairMatcherExtension));
			ICharacterPairMatcherExtension matcherExtension= (ICharacterPairMatcherExtension)matcher;
			if (test.isEnclosingTestCase()) {
				region= matcherExtension.findEnclosingPeerCharacters(test.getDocument(), test.fPos1, test.fPos2 - test.fPos1);
			} else {
				region= matcherExtension.match(test.getDocument(), test.fPos1, test.fPos2 - test.fPos1);
			}
		} else {
			if (test.isEnclosingTestCase()) {
				assertTrue((matcher instanceof ICharacterPairMatcherExtension));
				ICharacterPairMatcherExtension matcherExtension= (ICharacterPairMatcherExtension)matcher;
				region= matcherExtension.findEnclosingPeerCharacters(test.getDocument(), test.fPos1, test.fPos2 - test.fPos1);
			} else {
				region= matcher.match(test.getDocument(), test.fPos1);
			}
		}

		if (test.fMatch2 == -1) {
			// if no match point has been specified there should be no match
			if (region != null) System.out.println(region.getOffset());
			assertNull(region);
		} else {
			assertNotNull(region);
			final boolean isBackward= test.isEnclosingTestCase() ? false : test.fPos1 > test.fMatch2;
			assertEquals(isBackward, matcher.getAnchor() == ICharacterPairMatcher.RIGHT);

			assertEquals(test.getLength(), region.getLength());
			assertEquals(test.getOffset(), region.getOffset());
		}
	}

	private void performMatch(final String delims, final String testCase) {
		final ICharacterPairMatcher matcher= createMatcher(delims);
		performMatch(matcher, testCase);
		matcher.dispose();
	}

	/**
	 * Creates a text case from a string. In the given string a '%' represents the position of the
	 * cursor and a '#' represents the position of the expected matching character.
	 * 
	 * @param str the string for which to create the test case
	 * @return the created test case
	 */
	public TestCase createTestCase(String str) {
		int pos1= str.indexOf("%");
		assertFalse(pos1 == -1);
		int pos2= str.lastIndexOf("%");
		boolean selectionTest= pos1 != pos2;

		int match1= str.indexOf("#");
		int match2= str.lastIndexOf("#");
		boolean enclosingTest= match1 != match2;

		// account for the length of marker characters
		if (selectionTest) {
			if (!enclosingTest) {
				assertTrue(pos2 - pos1 == 2);
				if (match1 != -1 && match1 < pos1) {
					pos1-= 1;
					pos2-= 2;
				}
				if (pos1 < match1) {
					pos2-= 1;
					match1-= 2;
				}
			} else {
				pos1-= 1;
				pos2-= 2;
				match2-= 3;
			}
		} else {
			if (!enclosingTest) {
				if (match1 != -1 && match1 < pos1)
					pos1-= 1;
				if (pos1 < match1)
					match1-= 1;
			} else {
				pos1-= 1;
				match2-= 2;
			}
			pos2= pos1;
		}

		final String stripped= str.replaceAll("%", "").replaceAll("#", "");

		if (enclosingTest) {
			return new TestCase(stripped, pos1, pos2, match1, match2);
		} else {
			if (selectionTest) {
				return new TestCase(stripped, pos1, pos2, pos1 < match1 ? pos1 : pos2, match1);
			} else {
				if (match1 == -1)
					return new TestCase(stripped, pos1, pos2, pos1, match1);

				match2= match1;
				match1= pos1;
				if (!selectionTest && !enclosingTest) {
					String chars= "()[]{}<>";
					if (fCaretEitherSideOfBracket && pos1 < stripped.length()) {
						char ch= stripped.charAt(pos1);
						char prevCh= pos1 - 1 >= 0 ? stripped.charAt(pos1 - 1) : Character.MIN_VALUE;
						if (chars.indexOf(ch) % 2 == 1 && chars.indexOf(prevCh) % 2 != 0) {
							match1++;
						}
					}
					if (pos1 - 1 >= 0) {
						char ch= stripped.charAt(pos1 - 1);
						if (chars.indexOf(ch) % 2 == 0) {
							match1--;
						}
					}
				}
				return new TestCase(stripped, pos1, pos2, match1, match2);
			}
		}
	}

	private class TestCase {

		public final String fString;

		public final int fPos1, fPos2, fMatch1, fMatch2;

		public TestCase(String string, int pos1, int pos2, int match1, int match2) {
			fString= string;
			fPos1= pos1;
			fPos2= pos2;
			fMatch1= match1;
			fMatch2= match2;
		}

		public IDocument getDocument() {
			return new StringDocument(fString);
		}

		public int getLength() {
			return Math.abs(fMatch1 - fMatch2);
		}

		public int getOffset() {
			if (fMatch1 > fMatch2)
				return fMatch2;
			return fMatch1;
		}

		public boolean isEnclosingTestCase() {
			return fPos1 != fMatch1 && fPos2 != fMatch1;
		}

		public boolean isSelectionTestCase() {
			return fPos1 != fPos2;
		}

	}

	private class StringDocument extends Document {

		public StringDocument(String str) {
			this.setTextStore(new StringTextStore(str));
			this.set(str);
			final IDocumentPartitioner part= createPartitioner();
			this.setDocumentPartitioner(getDocumentPartitioning(), part);
			part.connect(this);
		}

	}

	private static class StringTextStore implements ITextStore {

		private String fString;

		public StringTextStore(final String str) {
			fString= str;
		}

		@Override
		public char get(int offset) {
			return fString.charAt(offset);
		}

		@Override
		public String get(int offset, int length) {
			return fString.substring(offset, offset + length);
		}

		@Override
		public int getLength() {
			return fString.length();
		}

		@Override
		public void replace(int offset, int length, String text) {
			throw new UnsupportedOperationException();
		}

		@Override
		public void set(String text) {
			fString= text;
		}

	}

	private static String DEFAULT_PARTITION= IDocument.DEFAULT_CONTENT_TYPE;

	private static IDocumentPartitioner createPartitioner() {
		final RuleBasedPartitionScanner scan= new RuleBasedPartitionScanner();
		final List/*<IPredicateRule>*/ rules= new ArrayList/*<IPredicateRule>*/();
		rules.add(new SingleLineRule("|a", "a|", new Token("a")));
		rules.add(new SingleLineRule("|b", "b|", new Token("b")));
		rules.add(new SingleLineRule("|c", "c|", new Token("c")));
		scan.setPredicateRules((IPredicateRule[]) rules.toArray(new IPredicateRule[rules.size()]));
		scan.setDefaultReturnToken(new Token(DEFAULT_PARTITION));
		return new FastPartitioner(scan, new String[] { DEFAULT_PARTITION, "a", "b", "c" });
	}

}

Back to the top