2 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
3 * Use is subject to license terms.
5 package org.jetbrains.plugins.groovy.lang;
7 import com.intellij.codeInspection.LocalInspectionTool;
8 import com.intellij.openapi.module.Module;
9 import com.intellij.openapi.roots.ContentEntry;
10 import com.intellij.openapi.roots.ModifiableRootModel;
11 import com.intellij.openapi.roots.OrderRootType;
12 import com.intellij.openapi.roots.libraries.Library;
13 import com.intellij.openapi.vfs.JarFileSystem;
14 import com.intellij.openapi.vfs.VirtualFile;
15 import com.intellij.testFramework.LightProjectDescriptor;
16 import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor;
17 import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
18 import org.jetbrains.annotations.NotNull;
19 import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection;
20 import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyUncheckedAssignmentOfMemberOfRawTypeInspection;
21 import org.jetbrains.plugins.groovy.codeInspection.bugs.GroovyResultOfObjectAllocationIgnoredInspection;
22 import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialConditionalInspection;
23 import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialIfInspection;
24 import org.jetbrains.plugins.groovy.codeInspection.metrics.GroovyOverlyLongMethodInspection;
25 import org.jetbrains.plugins.groovy.codeInspection.unassignedVariable.UnassignedVariableAccessInspection;
26 import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GroovyUnresolvedAccessInspection;
27 import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GroovyUntypedAccessInspection;
28 import org.jetbrains.plugins.groovy.codeInspection.unusedDef.UnusedDefInspection;
29 import org.jetbrains.plugins.groovy.util.TestUtils;
31 import java.io.IOException;
36 public class GroovyHighlightingTest extends LightCodeInsightFixtureTestCase {
37 public static final DefaultLightProjectDescriptor GROOVY_17_PROJECT_DESCRIPTOR = new DefaultLightProjectDescriptor() {
39 public void configureModule(Module module, ModifiableRootModel model, ContentEntry contentEntry) {
40 final Library.ModifiableModel modifiableModel = model.getModuleLibraryTable().createLibrary("GROOVY").getModifiableModel();
41 final VirtualFile groovyJar =
42 JarFileSystem.getInstance().refreshAndFindFileByPath(TestUtils.getMockGroovy1_7LibraryName()+"!/");
43 modifiableModel.addRoot(groovyJar, OrderRootType.CLASSES);
44 modifiableModel.commit();
49 protected String getBasePath() {
50 return TestUtils.getTestDataPath() + "highlighting/";
55 protected LightProjectDescriptor getProjectDescriptor() {
56 return GROOVY_17_PROJECT_DESCRIPTOR;
59 public void testDuplicateClosurePrivateVariable() throws Throwable {
63 public void testClosureRedefiningVariable() throws Throwable {
67 private void doTest(LocalInspectionTool... tools) throws Exception {
68 myFixture.enableInspections(tools);
69 myFixture.testHighlighting(true, false, false, getTestName(false) + ".groovy");
72 public void testCircularInheritance() throws Throwable {
76 public void testEmptyTupleType() throws Throwable {
80 public void testMapDeclaration() throws Throwable {
84 public void testShouldntImplementGroovyObjectMethods() throws Throwable {
86 myFixture.addFileToProject("Foo.groovy", "class Foo {}");
87 myFixture.testHighlighting(false, false, false, getTestName(false) + ".java");
90 public void testJavaClassImplementingGroovyInterface() throws Throwable {
92 myFixture.addFileToProject("Foo.groovy", "interface Foo {}");
93 myFixture.testHighlighting(false, false, false, getTestName(false) + ".java");
96 private void addGroovyObject() throws IOException {
97 myFixture.addClass("package groovy.lang;" +
98 "public interface GroovyObject {\n" +
99 " java.lang.Object invokeMethod(java.lang.String s, java.lang.Object o);\n" +
100 " java.lang.Object getProperty(java.lang.String s);\n" +
101 " void setProperty(java.lang.String s, java.lang.Object o);\n" +
102 " groovy.lang.MetaClass getMetaClass();\n" +
103 " void setMetaClass(groovy.lang.MetaClass metaClass);\n" +
107 public void testDuplicateFields() throws Throwable {
111 public void testNoDuplicationThroughClosureBorder() throws Throwable {
112 myFixture.addClass("package groovy.lang; public interface Closure {}");
116 public void testRecursiveMethodTypeInference() throws Throwable {
120 public void testSuperClassNotExists() throws Exception {
123 public void testDontSimplifyString() throws Throwable { doTest(new GroovyTrivialIfInspection(), new GroovyTrivialConditionalInspection()); }
125 public void testRawMethodAccess() throws Throwable { doTest(new GroovyUncheckedAssignmentOfMemberOfRawTypeInspection()); }
127 public void testRawFieldAccess() throws Throwable { doTest(new GroovyUncheckedAssignmentOfMemberOfRawTypeInspection()); }
129 public void testRawArrayStyleAccess() throws Throwable { doTest(new GroovyUncheckedAssignmentOfMemberOfRawTypeInspection()); }
131 public void testRawArrayStyleAccessToMap() throws Throwable { doTest(new GroovyUncheckedAssignmentOfMemberOfRawTypeInspection()); }
133 public void testRawArrayStyleAccessToList() throws Throwable { doTest(new GroovyUncheckedAssignmentOfMemberOfRawTypeInspection()); }
135 public void testIncompatibleTypesAssignments() throws Throwable { doTest(new GroovyAssignabilityCheckInspection()); }
137 public void testAnonymousClassConstructor() throws Throwable {doTest();}
138 public void testAnonymousClassAbstractMethod() throws Throwable {doTest();}
139 public void testAnonymousClassStaticMethod() throws Throwable {doTest();}
140 public void testAnonymousClassShoudImplementMethods() throws Throwable {doTest();}
143 public void testDefaultMapConstructorNamedArgs() throws Throwable {doTest();}
144 public void testDefaultMapConstructorNamedArgsError() throws Throwable {doTest();}
145 public void testDefaultMapConstructorWhenDefConstructorExists() throws Throwable {doTest();}
147 public void testSingleAllocationInClosure() throws Throwable {doTest(new GroovyResultOfObjectAllocationIgnoredInspection());}
149 public void testUnresolvedLhsAssignment() throws Throwable { doTest(new GroovyUnresolvedAccessInspection()); }
151 public void testUnresolvedMethodCallWithTwoDeclarations() throws Throwable{
155 public void testUnresolvedAccess() throws Exception { doTest(new GroovyUnresolvedAccessInspection()); }
156 public void testUntypedAccess() throws Exception { doTest(new GroovyUntypedAccessInspection()); }
158 public void testUnassigned1() throws Exception { doTest(new UnassignedVariableAccessInspection()); }
159 public void testUnassigned2() throws Exception { doTest(new UnassignedVariableAccessInspection()); }
160 public void testUnassigned3() throws Exception { doTest(new UnassignedVariableAccessInspection()); }
161 public void testUnassignedTryFinally() throws Exception { doTest(new UnassignedVariableAccessInspection()); }
163 public void testUnusedVariable() throws Exception { doTest(new UnusedDefInspection()); }
164 public void testDefinitionUsedInClosure() throws Exception { doTest(new UnusedDefInspection()); }
165 public void testDefinitionUsedInClosure2() throws Exception { doTest(new UnusedDefInspection()); }
166 public void testDuplicateInnerClass() throws Throwable{doTest();}
168 public void testThisInStaticContext() throws Throwable {doTest();}
169 public void testSuperWithNotEnclosingClass() throws Throwable {doTest();}
170 public void testThisWithWrongQualifier() throws Throwable {doTest();}
172 public void testModifiersInPackageAndImportStatements() throws Throwable {
173 myFixture.copyFileToProject(getTestName(false) + ".groovy", "x/"+getTestName(false)+".groovy");
174 myFixture.testHighlighting(true, false, false, "x/"+getTestName(false)+".groovy");
177 public void testBreakOutside() throws Exception {doTest();}
178 public void testUndefinedLabel() throws Exception {doTest();}
179 public void testUsedLabel() throws Exception {doTest();}
181 public void testNestedMethods() throws Throwable {
185 public void testRawOverridedMethod() throws Exception {doTest();}
187 public void testFQNJavaClassesUsages() throws Exception {
191 public void testGstringAssignableToString() throws Exception{doTest();}
192 public void testGstringAssignableToStringInClosureParameter() throws Exception{doTest();}
193 public void testEverythingAssignableToString() throws Exception {doTest(new GroovyAssignabilityCheckInspection());}
195 public void testEachOverRange() throws Exception {doTest();}
197 public void testMethodCallWithDefaultParameters() throws Exception {doTest();}
198 public void testClosureWithDefaultParameters() throws Exception {doTest();}
199 public void testClosureCallMethodWithInapplicableArguments() throws Exception {doTest();}
201 public void testOverlyLongMethodInspection() throws Exception {
202 doTest(new GroovyOverlyLongMethodInspection());
205 public void testStringAndGStringUpperBound() throws Exception {doTest();}
207 public void testWithMethod() throws Exception {doTest();}
208 public void testByteArrayArgument() throws Exception {doTest();}
210 public void testForLoopWithNestedEndlessLoop() throws Exception {doTest(new UnassignedVariableAccessInspection());}
211 public void testIfIncrementElseReturn() throws Exception {doTest(new UnusedDefInspection()); }
213 public void testArrayLikeAccess() throws Exception {doTest();}
215 public void testSetInitializing() throws Exception {doTest();}
217 public void testEmptyTupleAssignability() throws Exception {doTest();}
219 public void testGrDefFieldsArePrivateInJavaCode() throws Exception {
220 myFixture.configureByText("X.groovy", "public class X{def x=5}");
221 myFixture.testHighlighting(true, false, false, getTestName(false) + ".java");
224 public void testSuperConstructorInvocation() throws Exception {doTest();}