2 * Copyright 2000-2015 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.
18 import com.intellij.codeInspection.InspectionProfileEntry;
19 import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
20 import com.intellij.util.ArrayUtil;
21 import org.intellij.lang.annotations.Language;
22 import org.jetbrains.annotations.NotNull;
23 import org.jetbrains.annotations.Nullable;
26 * @author Bas Leijdekkers
28 public abstract class LightInspectionTestCase extends LightCodeInsightFixtureTestCase {
31 protected void setUp() throws Exception {
34 for (String environmentClass : getEnvironmentClasses()) {
35 myFixture.addClass(environmentClass);
37 final InspectionProfileEntry inspection = getInspection();
38 if (inspection != null) {
39 myFixture.enableInspections(inspection);
44 protected abstract InspectionProfileEntry getInspection();
47 @SuppressWarnings("LanguageMismatch")
48 protected String[] getEnvironmentClasses() {
49 return ArrayUtil.EMPTY_STRING_ARRAY;
52 protected void addEnvironmentClass(@Language("JAVA") @NotNull String classText) {
53 myFixture.addClass(classText);
56 protected final void doStatementTest(@Language(value="JAVA", prefix="class X { void m() {", suffix="}}") @NotNull String statementText) {
57 doTest("class X { void m() {" + statementText + "}}");
60 protected final void doMemberTest(@Language(value="JAVA", prefix="class X {", suffix="}") @NotNull String memberText) {
61 doTest("class X {" + memberText + "}");
64 protected final void doTest(@Language("JAVA") @NotNull String classText) {
65 final StringBuilder newText = new StringBuilder();
67 int end = classText.indexOf("/*");
69 newText.append(classText, start, end);
71 end = classText.indexOf("*/", end);
73 throw new IllegalArgumentException("invalid class text");
75 final String text = classText.substring(start, end);
77 newText.append("</warning>");
79 else if ("_".equals(text)) {
80 newText.append("<caret>");
83 newText.append("<warning descr=\"").append(text).append("\">");
86 end = classText.indexOf("/*", end + 1);
88 newText.append(classText, start, classText.length());
89 myFixture.configureByText("X.java", newText.toString());
90 myFixture.testHighlighting(true, false, false);
94 protected String getBasePath() {
95 final InspectionProfileEntry inspection = getInspection();
96 assertNotNull("File-based tests should either return an inspection or override this method", inspection);
97 final String className = inspection.getClass().getName();
98 final String[] words = className.split("\\.");
99 final StringBuilder basePath = new StringBuilder("/plugins/InspectionGadgets/test/");
100 final int lastWordIndex = words.length - 1;
101 for (int i = 0; i < lastWordIndex; i++) {
102 String word = words[i];
103 if (word.equals("ig")) {
104 //noinspection SpellCheckingInspection
107 basePath.append(word).append('/');
109 String lastWord = words[lastWordIndex];
110 if (lastWord.endsWith("Inspection")) {
111 lastWord = lastWord.substring(0, lastWord.length() - 10);
113 final int length = lastWord.length();
114 boolean upperCase = false;
115 for (int i = 0; i < length; i++) {
116 final char ch = lastWord.charAt(i);
117 if (Character.isUpperCase(ch)) {
121 basePath.append('_');
124 basePath.append(Character.toLowerCase(ch));
131 return basePath.toString();
134 protected final void doTest() {
135 doNamedTest(getTestName(false));
138 protected final void doNamedTest(String name) {
139 myFixture.configureByFile(name + ".java");
140 myFixture.testHighlighting(true, false, false);