2 * Copyright 2000-2016 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.structuralsearch.impl.matcher.compiler;
18 import com.intellij.structuralsearch.MalformedPatternException;
19 import com.intellij.structuralsearch.MatchOptions;
20 import com.intellij.structuralsearch.MatchVariableConstraint;
21 import com.intellij.structuralsearch.UnsupportedPatternException;
22 import com.intellij.structuralsearch.plugin.ui.Configuration;
23 import org.junit.Before;
24 import org.junit.Test;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.assertFalse;
31 * @author Bas Leijdekkers
33 public class StringToConstraintsTransformerTest {
35 private MatchOptions myOptions;
38 public void setUp() throws Exception {
39 myOptions = new MatchOptions();
42 @Test(expected = MalformedPatternException.class)
43 public void testCharacterExpectedAfterQuote() {
47 @Test(expected = MalformedPatternException.class)
48 public void testCharacterExpectedAfterQuote2() {
52 @Test(expected = MalformedPatternException.class)
53 public void testUnexpectedEndOfPattern() {
57 @Test(expected = MalformedPatternException.class)
58 public void testDigitExpected() {
62 @Test(expected = MalformedPatternException.class)
63 public void testDigitExpected2() {
68 public void testZeroOccurs() {
70 final MatchVariableConstraint constraint = myOptions.getVariableConstraint("a");
71 assertEquals(0, constraint.getMinCount());
72 assertEquals(0, constraint.getMaxCount());
75 @Test(expected = MalformedPatternException.class)
76 public void testOverflow() {
77 test("'a{2147483648}");
80 @Test(expected = MalformedPatternException.class)
81 public void testMissingBrace() {
85 @Test(expected = MalformedPatternException.class)
86 public void testNoOptions() {
91 public void testColon() {
92 test("for('_t 'a : '_b) {}");
93 assertEquals("for($t$ $a$ : $b$) {}", myOptions.getSearchPattern());
96 @Test(expected = MalformedPatternException.class)
97 public void testNoOptions2() {
101 @Test(expected = MalformedPatternException.class)
102 public void testUnclosedCondition() {
106 @Test(expected = MalformedPatternException.class)
107 public void testClosedCondition() {
111 @Test(expected = MalformedPatternException.class)
112 public void testEmptyNegated() {
116 @Test(expected = UnsupportedPatternException.class)
117 public void testCondition() {
121 @Test(expected = MalformedPatternException.class)
122 public void testIncompleteCondition() {
126 @Test(expected = MalformedPatternException.class)
127 public void testIncompleteCondition2() {
128 test("'a:[regex()]");
131 @Test(expected = MalformedPatternException.class)
132 public void testIncompleteMultipleCondition() {
133 test("'a:[regex( a ) &&]");
136 @Test(expected = MalformedPatternException.class)
137 public void testInvalidRegularExpression() {
141 @Test(expected = MalformedPatternException.class)
142 public void testRepeatingConstraints() {
143 test("'a*:foo 'a+:[regex( bla )]");
146 @Test(expected = MalformedPatternException.class)
147 public void testRepeatingConstraints2() {
152 public void testMethodReference() {
154 assertEquals("$a$::$b$", myOptions.getSearchPattern());
158 public void testCompleteMatchConditions() {
159 test("[within( \"if('_a) { 'st*; }\" )]1+1");
160 assertEquals("1+1", myOptions.getSearchPattern());
161 final MatchVariableConstraint constraint = myOptions.getVariableConstraint(Configuration.CONTEXT_VAR_NAME);
162 assertEquals("\"if('_a) { 'st*; }\"", constraint.getWithinConstraint());
165 @Test(expected = MalformedPatternException.class)
166 public void testBadWithin() {
167 test("'_type 'a:[within( \"if ('_a) { '_st*; }\" )] = '_b;");
171 public void testNestedPatterns() {
172 test("[within( \"if ('_a:[regex( .*e.* )]) { '_st*; }\" )]'_type 'a = '_b;");
173 assertEquals("$type$ $a$ = $b$;", myOptions.getSearchPattern());
174 final MatchVariableConstraint constraint = myOptions.getVariableConstraint(Configuration.CONTEXT_VAR_NAME);
175 assertEquals("\"if ('_a:[regex( .*e.* )]) { '_st*; }\"", constraint.getWithinConstraint());
177 test("[!within( \"<'_tag:[regex( ul|ol )] />\" )]<li />");
178 assertEquals("<li />", myOptions.getSearchPattern());
179 final MatchVariableConstraint constraint2 = myOptions.getVariableConstraint(Configuration.CONTEXT_VAR_NAME);
180 assertEquals("\"<'_tag:[regex( ul|ol )] />\"", constraint2.getWithinConstraint());
181 assertTrue(constraint2.isInvertWithinConstraint());
183 test("[within( \"if ('_a:[regex( \".*\" )]) { '_st*; }\" )]'_type 'a = '_b;");
184 assertEquals("$type$ $a$ = $b$;", myOptions.getSearchPattern());
185 final MatchVariableConstraint constraint3 = myOptions.getVariableConstraint(Configuration.CONTEXT_VAR_NAME);
186 assertEquals("\"if ('_a:[regex( \".*\" )]) { '_st*; }\"", constraint3.getWithinConstraint());
190 public void testEscaping() {
191 test("\\[within( \"if ('_a) { '_st*; }\" )]");
192 assertEquals("[within( \"if ($a$) { $st$; }\" )]", myOptions.getSearchPattern());
194 assertEquals("[", myOptions.getSearchPattern());
196 assertEquals("'aaa", myOptions.getSearchPattern());
198 assertEquals("$a$:", myOptions.getSearchPattern());
202 public void testQuotes() {
204 assertEquals("'", myOptions.getSearchPattern());
206 assertEquals("'$c$", myOptions.getSearchPattern());
208 assertEquals("'a'$b$", myOptions.getSearchPattern());
210 assertEquals("$aa$$bb$", myOptions.getSearchPattern());
212 assertEquals("'\\n'$z$", myOptions.getSearchPattern());
214 assertEquals("'\\u0123'$a$", myOptions.getSearchPattern());
218 public void testComplexRegexes() {
219 test("'_t:[regex( *Object\\[\\] ) ] '_t2");
220 assertEquals("$t$ $t2$", myOptions.getSearchPattern());
221 final MatchVariableConstraint constraint = myOptions.getVariableConstraint("t");
222 assertTrue(constraint.isWithinHierarchy());
223 assertEquals("Object\\[\\]", constraint.getRegExp());
225 test("// 'Comment:[regex( .*(?:comment).* )]");
226 assertEquals("// $Comment$", myOptions.getSearchPattern());
227 final MatchVariableConstraint constraint1 = myOptions.getVariableConstraint("Comment");
228 assertEquals(".*(?:comment).*", constraint1.getRegExp());
232 public void testInvert() {
233 test("'a:[!read&&write]");
234 assertEquals("$a$", myOptions.getSearchPattern());
235 final MatchVariableConstraint constraint = myOptions.getVariableConstraint("a");
236 assertTrue(constraint.isReadAccess());
237 assertTrue(constraint.isInvertReadAccess());
238 assertTrue(constraint.isWriteAccess());
239 assertFalse(constraint.isInvertWriteAccess());
242 @Test(expected = MalformedPatternException.class)
243 public void testAmpersandsExpected() {
244 test("'a:[read write]");
247 @Test(expected = MalformedPatternException.class)
248 public void testUnexpectedAmpersands() {
252 @Test(expected = MalformedPatternException.class)
253 public void testUnbalancedSpacesSurroundingContent() {
254 test("'a:[regex( .* ) ]");
257 @Test(expected = MalformedPatternException.class)
258 public void testInvalidRegex() {
263 public void testNoSpacesSurroundingRegexNeeded() {
264 test("'t:[regex(a)]");
265 final MatchVariableConstraint constraint = myOptions.getVariableConstraint("t");
266 assertEquals("a", constraint.getRegExp());
269 private void test(String pattern) {
270 myOptions.setSearchPattern(pattern);
271 StringToConstraintsTransformer.transformOldPattern(myOptions);