1 /*******************************************************************************
2  * Copyright (c) 2000, 2016 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Christian Plesner Hansen (plesner@quenta.org) - integrated with the generic test for pair matchers
14  *******************************************************************************/
15 package org.eclipse.jdt.text.tests;
16 
17 import org.junit.After;
18 import org.junit.Before;
19 import static org.junit.Assert.*;
20 
21 import org.eclipse.jface.text.Document;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.rules.FastPartitioner;
25 import org.eclipse.jface.text.source.ICharacterPairMatcher;
26 import org.eclipse.jface.text.tests.AbstractPairMatcherTest;
27 
28 import org.eclipse.jdt.core.JavaCore;
29 
30 import org.eclipse.jdt.ui.text.IJavaPartitions;
31 
32 import org.eclipse.jdt.internal.ui.text.FastJavaPartitionScanner;
33 import org.eclipse.jdt.internal.ui.text.JavaPairMatcher;
34 /**
35  * Tests for the java pair matcher
36  *
37  * @since 3.3
38  */
39 public class JavaPairMatcherTest extends AbstractPairMatcherTest {
40 
41 	protected IDocument fDocument;
42 	protected JavaPairMatcher fPairMatcher;
43 
44 
JavaPairMatcherTest()45 	public JavaPairMatcherTest() {
46 		super(true);
47 	}
48 
49 	@Override
getDocumentPartitioning()50 	protected String getDocumentPartitioning() {
51 		return IJavaPartitions.JAVA_PARTITIONING;
52 	}
53 
54 	@Override
createMatcher(String chars)55 	protected ICharacterPairMatcher createMatcher(String chars) {
56 		return new JavaPairMatcher(chars.toCharArray());
57 	}
58 
59 	@Before
setUp()60 	public void setUp() {
61 		Document document= new Document("xx(yy(xx)yy)xx()/*  */");
62 		String[] types= new String[] {
63 				IJavaPartitions.JAVA_DOC,
64 				IJavaPartitions.JAVA_MULTI_LINE_COMMENT,
65 				IJavaPartitions.JAVA_SINGLE_LINE_COMMENT,
66 				IJavaPartitions.JAVA_STRING,
67 				IJavaPartitions.JAVA_CHARACTER,
68 				IDocument.DEFAULT_CONTENT_TYPE
69 		};
70 		FastPartitioner partitioner= new FastPartitioner(new FastJavaPartitionScanner(), types);
71 		partitioner.connect(document);
72 		document.setDocumentPartitioner(IJavaPartitions.JAVA_PARTITIONING, partitioner);
73 
74 		fDocument= document;
75 		fPairMatcher= new JavaPairMatcher(new char[] { '(', ')' });
76 	}
77 
78 	@After
tearDown()79 	public void tearDown () {
80 		fDocument= null;
81 		fPairMatcher= null;
82 	}
83 
testBeforeOpeningMatch()84 	public void testBeforeOpeningMatch() {
85 		IRegion match= fPairMatcher.match(fDocument, 2);
86 		assertNotNull(match);
87 		assertTrue(match.getOffset() == 2 && match.getLength() == 10);
88 
89 		match= fPairMatcher.match(fDocument, 5);
90 		assertNotNull(match);
91 		assertTrue(match.getOffset() == 5 && match.getLength() == 4);
92 	}
93 
testAfterOpeningMatch()94 	public void testAfterOpeningMatch() {
95 		IRegion match= fPairMatcher.match(fDocument, 3);
96 		assertNotNull(match);
97 		assertTrue(match.getOffset() == 2 && match.getLength() == 10);
98 
99 		match= fPairMatcher.match(fDocument, 6);
100 		assertNotNull(match);
101 		assertTrue(match.getOffset() == 5 && match.getLength() == 4);
102 	}
103 
testBeforeClosingMatch()104 	public void testBeforeClosingMatch() {
105 		IRegion match= fPairMatcher.match(fDocument, 11);
106 		assertNotNull(match);
107 		assertTrue(match.getOffset() == 2 && match.getLength() == 10);
108 
109 		match= fPairMatcher.match(fDocument, 8);
110 		assertNotNull(match);
111 		assertTrue(match.getOffset() == 5 && match.getLength() == 4);
112 
113 	}
114 
testAfterClosingMatch()115 	public void testAfterClosingMatch() {
116 		IRegion match= fPairMatcher.match(fDocument, 12);
117 		assertNotNull(match);
118 		assertTrue(match.getOffset() == 2 && match.getLength() == 10);
119 
120 		match= fPairMatcher.match(fDocument, 9);
121 		assertNotNull(match);
122 		assertTrue(match.getOffset() == 5 && match.getLength() == 4);
123 	}
124 
testBeforeClosingMatchWithNL()125 	public void testBeforeClosingMatchWithNL() {
126 		fDocument.set("x(y\ny)x");
127 		IRegion match= fPairMatcher.match(fDocument, 5);
128 		assertNotNull(match);
129 		assertTrue(match.getOffset() == 1 && match.getLength() == 5);
130 	}
131 
testAfterClosingMatchWithNL()132 	public void testAfterClosingMatchWithNL() {
133 		fDocument.set("x(y\ny)x");
134 		IRegion match= fPairMatcher.match(fDocument, 6);
135 		assertNotNull(match);
136 		assertTrue(match.getOffset() == 1 && match.getLength() == 5);
137 	}
138 
testBeforeClosingMatchWithNLAndSingleLineComment()139 	public void testBeforeClosingMatchWithNLAndSingleLineComment() {
140 		fDocument.set("x\nx(y\nx //(x\ny)x");
141 		IRegion match= fPairMatcher.match(fDocument, 14);
142 		assertNotNull(match);
143 		assertTrue(match.getOffset() == 3 && match.getLength() == 12);
144 	}
145 
testAfterClosingMatchWithNLAndSingleLineComment()146 	public void testAfterClosingMatchWithNLAndSingleLineComment() {
147 		fDocument.set("x\nx(y\nx //(x\ny)x");
148 		IRegion match= fPairMatcher.match(fDocument, 15);
149 		assertNotNull(match);
150 		assertTrue(match.getOffset() == 3 && match.getLength() == 12);
151 	}
152 
testEnclosingMatch()153 	public void testEnclosingMatch() {
154 		IRegion match= fPairMatcher.findEnclosingPeerCharacters(fDocument, 4, 0);
155 		assertNotNull(match);
156 		assertTrue(match.getOffset() == 2 && match.getLength() == 10);
157 
158 		match= fPairMatcher.findEnclosingPeerCharacters(fDocument, 7, 0);
159 		assertNotNull(match);
160 		assertTrue(match.getOffset() == 5 && match.getLength() == 4);
161 	}
162 
testAngleBrackets1_4()163 	public void testAngleBrackets1_4() {
164 		final JavaPairMatcher matcher= (JavaPairMatcher) createMatcher("(){}[]<>");
165 		matcher.setSourceVersion(JavaCore.VERSION_1_4);
166 		performMatch(matcher, " <>% ");
167 		performMatch(matcher, " <%> ");
168 		performMatch(matcher, " 2 < 3 || 4 >% 5 ");
169 		performMatch(matcher, " 2 <% 3 || 4 > 5 ");
170 		performMatch(matcher, " List<String>% ");
171 		performMatch(matcher, " foo < T >% ");
172 		performMatch(matcher, " foo <% T > ");
173 		performMatch(matcher, " foo < T >% ");
174 		performMatch(matcher, " final <% T > ");
175 		matcher.dispose();
176 	}
177 
testAngleBrackets1_5()178 	public void testAngleBrackets1_5() {
179 		final JavaPairMatcher matcher= (JavaPairMatcher) createMatcher("(){}[]<>");
180 		matcher.setSourceVersion(JavaCore.VERSION_1_5);
181 		performMatch(matcher, " #< %> ");
182 		performMatch(matcher, " <%># ");
183 		performMatch(matcher, " 2 < 3 || 4 >% 5 ");
184 		performMatch(matcher, " 2 <% 3 || 4 > 5 ");
185 		performMatch(matcher, " List#<String>% ");
186 		performMatch(matcher, " foo < T >% ");
187 		performMatch(matcher, " foo <% T > ");
188 		performMatch(matcher, " foo < T >% ");
189 		performMatch(matcher, " final <% T ># ");
190 		matcher.dispose();
191 	}
192 
testBug209505()193 	public void testBug209505() {
194 		fDocument.set("(xny)/*  */");
195 		IRegion match= fPairMatcher.match(fDocument, 4);
196 		assertNotNull(match);
197 		assertTrue(match.getOffset() == 0 && match.getLength() == 5);
198 	}
199 
200 
201 }
202