don't hardcode methods that reference testdata, use annotation instead

This commit is contained in:
Dmitry Jemerov
2010-01-21 15:54:16 +03:00
parent 27b8fdac25
commit 19486d693a
8 changed files with 57 additions and 22 deletions
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2010 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.testFramework;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Marks the annotated parameter as referencing a file in the testdata directory.
*
* @author yole
*/
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.PARAMETER})
public @interface TestDataFile {
}
@@ -44,16 +44,19 @@ public class TestDataReferenceCollector {
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
String callText = expression.getMethodExpression().getReferenceName();
if (callText == null) return;
if (callText.equals("configureByFile") || callText.equals("checkResultByFile")) {
processCallArgument(expression, argumentMap, result, 0);
}
else if (callText.equals("doFileTest")) {
processCallArgument(expression, argumentMap, result, 0);
processCallArgument(expression, argumentMap, result, 1);
}
else if (expression.getMethodExpression().getQualifierExpression() == null) {
final PsiMethod callee = expression.resolveMethod();
if (callee != null) {
final PsiMethod callee = expression.resolveMethod();
if (callee != null) {
boolean haveAnnotatedParameters = false;
final PsiParameter[] psiParameters = callee.getParameterList().getParameters();
for (int i = 0, psiParametersLength = psiParameters.length; i < psiParametersLength; i++) {
PsiParameter psiParameter = psiParameters[i];
final PsiModifierList modifierList = psiParameter.getModifierList();
if (modifierList != null && modifierList.findAnnotation("com.intellij.testFramework.TestDataFile") != null) {
processCallArgument(expression, argumentMap, result, i);
haveAnnotatedParameters = true;
}
}
if (expression.getMethodExpression().getQualifierExpression() == null && !haveAnnotatedParameters) {
result.addAll(collectTestDataReferences(callee, buildArgumentMap(expression, callee)));
}
}
@@ -3,4 +3,8 @@ public class ATest extends LightCodeInsightFixtureTestCase {
public void testFixtureConfigureByFile() throws Exception {
doFileTest("before", "after");
}
private void doFileTest(@com.intellij.testFramework.TestDataFile String before,
@com.intellij.testFramework.TestDataFile String after) {
}
}
@@ -11,4 +11,7 @@ public class ATest extends LightCodeInsightFixtureTestCase {
private void doTest(String extension) throws Exception {
configureByFile(getTestName(true) + "." + extension);
}
private void configureByFile(@com.intellij.testFramework.TestDataFile String file) {
}
}
@@ -1,6 +0,0 @@
public class ATest extends LightCodeInsightFixtureTestCase {
public void testFixtureConfigureByFile() throws Exception {
myFixture.configureByFile(getTestName(true));
}
}
@@ -7,4 +7,7 @@ public class ATest extends LightCodeInsightFixtureTestCase {
private void resolve() throws Exception {
configureByFile("before");
}
private void configureByFile(@com.intellij.testFramework.TestDataFile String file) {
}
}
@@ -7,4 +7,7 @@ public class ATest extends LightCodeInsightFixtureTestCase {
private void doTest(String testName) throws Exception {
configureByFile("before" + testName);
}
private void configureByFile(@com.intellij.testFramework.TestDataFile String file) {
}
}
@@ -28,12 +28,6 @@ import java.util.List;
*/
@TestDataPath("$CONTENT_ROOT/testData/")
public class TestDataReferenceCollectorTest extends LightCodeInsightFixtureTestCase {
public void testFixtureConfigureByFile() throws Exception {
final List<String> references = doTest();
assertEquals(1, references.size());
assertEquals("fixtureConfigureByFile", references.get(0));
}
public void testDoTestParameters() throws Exception {
final List<String> references = doTest();
assertEquals(1, references.size());