summaryrefslogtreecommitdiff
path: root/plugins/groovy/structuralsearch-groovy/testSrc/com/intellij/structuralsearch/GroovyStructuralSearchTest.java
blob: 18505d01874732e38614bb466b61e88290366d26 (plain)
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
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.structuralsearch;

import com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider;
import com.intellij.idea.Bombed;
import org.jetbrains.plugins.groovy.GroovyFileType;

import java.util.Calendar;
import java.util.List;

/**
 * @author Eugene.Kudelevsky
 */
public class GroovyStructuralSearchTest extends StructuralSearchTestCase {

  public void test1() throws Exception {
    String s = "def int x = 0;\n" +
               "def y = 0;\n" +
               "int z = 10;\n" +
               "def int x1";

    doTest(s, "def $x$ = $value$;", 3, 1);
    doTest(s, "def $x$", 4, 3);
    doTest(s, "int $x$", 3, 3);
    doTest(s, "def $x$ = $value$", 3, 1);
    doTest(s, "def $x$ = 0", 2, 1);
    doTest(s, "int $x$ = 0", 1, 1);
    doTest(s, "int $x$ = $value$", 2, 2);
  }

  public void test2() throws Exception {
    String s = "def void f(int x) {}\n" +
               "def f(int x) {\n" +
               "  System.out.println(\"hello\");\n" +
               "}\n" +
               "def f(def x) {}\n" +
               "void g(x) {}\n" +
               "public def void f(def int y) {\n" +
               "  System.out.println(\"hello\");\n" +
               "}\n" +
               "def int f() {}";

    doTest(s, "def $f$($param$)", 5, 2);
    doTest(s, "def $f$($param$) {}", 3, 1);
    doTest(s, "void $f$($param$) {}", 2, 2);
    doTest(s, "void $f$(def x)", 2, 0);
    doTest(s, "def $f$(def x)", 4, 1);
    doTest(s, "void $f$(def $x$)", 3, 0);
    doTest(s, "void $f$(int $x$)", 2, 2);
    doTest(s, "def $f$(int $x$)", 3, 1);
    doTest(s, "def g($param$)", 1, 0);
    doTest(s, "def '_T1('_T2*)", 6, 2);

    // a problem with default eq is that ; is not part of statement
    doTest(s, "def '_T1('_T2*) {'_T3+}", 2, 0);
    doTest(s, "def '_T1('_T2*) {'_T3*}", 6, 1);
  }

  public void test3() throws Exception {
    String s = "public class C implements I1, I2 {\n" +
               "  void f() {\n" +
               "    def a = 1;\n" +
               "    def int b = 2;\n" +
               "  }\n" +
               "}";

    doTest(s, "class $name$", 1, 1);
    doTest(s, "class $name$ implements I1, I2", 1, 1);
    doTest(s, "class $name$ implements $interface$", 1, 0);
    doTest(s, "class '_T1 implements '_T2*", 1, 1);
    doTest(s, "class '_T1 implements '_T2+", 1, 1);
    doTest(s, "class $name$ implements I2, I1", 1, 0);
    doTest(s, "class C implements I1, I2 {}", 1, 0);
    doTest(s, "def a = 1;\n def b = 2;", 1, 0);
    doTest(s, "def a = 1\n def b = 2", 1, 0);
  }

  public void test4() throws Exception {
    String s = "for (a in list) {\n" +
               "  println(\"hello1\");\n" +
               "  println(\"hello2\");\n" +
               "}";
    doTest(s, "for ($a$ in $b$) {\n" +
              "  $st1$;\n" +
              "  $st2$\n" +
              "}", 1, 0);
    doTest(s, "for ($a$ in $b$) {\n" +
              "  $st1$;\n" +
              "  $st2$;\n" +
              "}", 1, 1);
    doTest(s, "for ($a$ in $b$) {\n" +
              "  $st1$\n" +
              "  $st2$\n" +
              "}", 1, 0);
    doTest(s, "for ($a$ in $b$) {\n" +
              "  $st$\n" +
              "}", 0, 0);
    doTest(s, "for ($a$ in $b$) {\n" +
              "  '_T*\n" +
              "}", 1, 0);
    doTest(s, "for ($a$ in $b$) {\n" +
              "  '_T+\n" +
              "}", 1, 0);
  }

  public void test5() {
    String s = "class A {\n" +
               "  def f = {\n" +
               "    println('Hello1')\n" +
               "    println('Hello2')\n" +
               "  }\n" +
               "  def f1 = {\n" +
               "    println('Hello')\n" +
               "  }\n" +
               "}";
    doTest(s, "def $name$ = {\n" +
              "  '_T+\n" +
              "}", 0, 0);
    final String old = options.getPatternContext();
    try {
      options.setPatternContext(GroovyStructuralSearchProfile.CLASS_CONTEXT);
      doTest(s, "def $name$ = {\n" +
                    "  '_T+\n" +
                    "}", 2, 2);
    }
    finally {
      options.setPatternContext(old);
    }
  }

  private void doTest(String source,
                      String pattern,
                      int expectedOccurences,
                      int expectedWithDefaultEquivalence) {
    findAndCheck(source, pattern, expectedOccurences);
    try {
      EquivalenceDescriptorProvider.ourUseDefaultEquivalence = true;
      findAndCheck(source, pattern, expectedWithDefaultEquivalence);
    }
    finally {
      EquivalenceDescriptorProvider.ourUseDefaultEquivalence = false;
    }
  }

  private void findAndCheck(String source, String pattern, int expectedOccurences) {
    testMatcher.clearContext();
    final List<MatchResult> matches =
      findMatches(source, pattern, true, GroovyFileType.GROOVY_FILE_TYPE, null, GroovyFileType.GROOVY_FILE_TYPE, null, false);
    assertEquals(expectedOccurences, matches.size());
  }
}