Select runtime with directive in test data file instead of test name in PsiChecker tests

This commit is contained in:
Ilya Gorbunov
2016-09-02 20:06:20 +03:00
parent d103657e07
commit 8f3e6f38ed
8 changed files with 43 additions and 12 deletions
@@ -828,6 +828,20 @@ public class KotlinTestUtils {
return testClassMetadata.value();
}
/**
* @return test data file name specified in the metadata of test method
*/
@Nullable
public static String getTestDataFileName(@NotNull Class<?> testCaseClass, @NotNull String testName) {
try {
Method method = testCaseClass.getDeclaredMethod(testName);
return getMethodMetadata(method);
}
catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
public static void assertAllTestsPresentByMetadata(
@NotNull Class<?> testCaseClass,
@NotNull File testDataDir,
@@ -898,12 +912,18 @@ public class KotlinTestUtils {
}));
}
@Nullable
private static String getMethodMetadata(Method method) {
TestMetadata testMetadata = method.getAnnotation(TestMetadata.class);
return (testMetadata != null) ? testMetadata.value() : null;
}
private static Set<String> collectMethodsMetadata(Class<?> testCaseClass) {
Set<String> filePaths = Sets.newHashSet();
for (Method method : testCaseClass.getDeclaredMethods()) {
TestMetadata testMetadata = method.getAnnotation(TestMetadata.class);
if (testMetadata != null) {
filePaths.add(testMetadata.value());
String path = getMethodMetadata(method);
if (path != null) {
filePaths.add(path);
}
}
return filePaths;
@@ -128,7 +128,7 @@ abstract class KotlinLightCodeInsightFixtureTestCase : KotlinLightCodeInsightFix
protected fun isAllFilesPresentInTest(): Boolean = KotlinTestUtils.isAllFilesPresentTest(getTestName(false))
protected open fun fileName(): String
= getTestName(false) + ".kt"
= KotlinTestUtils.getTestDataFileName(this.javaClass, this.name) ?: (getTestName(false) + ".kt")
protected fun performNotWriteEditorAction(actionId: String): Boolean {
val dataContext = (myFixture.editor as EditorEx).dataContext
+3 -2
View File
@@ -1,3 +1,4 @@
// RUNTIME
fun none() {}
fun unitEmptyInfer() {}
@@ -16,8 +17,8 @@ fun bbb() {
fun foo(<warning>expr</warning>: StringBuilder): Int {
val c = 'a'
when(c) {
0.toChar() -> throw Throwable("zero")
else -> throw Throwable("nonzero" + c)
0.toChar() -> throw Exception("zero")
else -> throw Exception("nonzero" + c)
}
}
+1
View File
@@ -1,3 +1,4 @@
// RUNTIME
<error descr="[WRONG_ANNOTATION_TARGET] This annotation is not applicable to target 'class'">@JvmStatic</error>
class A {
<error descr="[WRONG_ANNOTATION_TARGET] This annotation is not applicable to target 'companion object'">@JvmStatic</error>
@@ -1,3 +1,4 @@
// RUNTIME
@file:JvmName("TopLevelMultifile")
@file:JvmMultifileClass
package test
+4 -3
View File
@@ -1,3 +1,4 @@
// RUNTIME
import java.util.*
import java.io.*
@@ -9,15 +10,15 @@ fun takeFirst(expr: StringBuilder): Char {
}
fun evaluateArg(expr: CharSequence, numbers: ArrayList<Int>): Int {
if (expr.length == 0) throw Throwable("Syntax error: Character expected");
if (expr.length == 0) throw Exception("Syntax error: Character expected");
val c = takeFirst(<error>expr</error>)
if (c >= '0' && c <= '9') {
val n = c - '0'
if (!numbers.contains(n)) throw Throwable("You used incorrect number: " + n)
if (!numbers.contains(n)) throw Exception("You used incorrect number: " + n)
numbers.remove(n)
return n
}
throw Throwable("Syntax error: Unrecognized character " + c)
throw Exception("Syntax error: Unrecognized character " + c)
}
fun evaluateAdd(expr: StringBuilder, numbers: ArrayList<Int>): Int {
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.psi.KtDeclaration;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.psi.KtTreeVisitorVoid;
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import java.io.File;
@@ -90,9 +91,9 @@ public abstract class AbstractPsiCheckerTest extends KotlinLightCodeInsightFixtu
});
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return getProjectDescriptorFromTestName();
protected String getTestDataPath() {
return KotlinTestUtils.getTestsRoot(this.getClass());
}
}
@@ -17,6 +17,8 @@
package org.jetbrains.kotlin.checkers
import com.intellij.codeInspection.ex.EntryPointsManagerBase
import com.intellij.testFramework.LightProjectDescriptor
import org.jetbrains.kotlin.test.KotlinTestUtils
class PsiCheckerCustomTest : AbstractPsiCheckerTest() {
fun testNoUnusedParameterWhenCustom() {
@@ -41,4 +43,8 @@ class PsiCheckerCustomTest : AbstractPsiCheckerTest() {
}
private fun getTestDataFile(localName: String) = "idea/testData/checker/custom/$localName"
override fun getTestDataPath(): String = KotlinTestUtils.getHomeDirectory()
override fun getProjectDescriptor(): LightProjectDescriptor = getProjectDescriptorFromTestName()
}