From 8f3e6f38ed5386416fac11be0492f889963433a7 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 2 Sep 2016 20:06:20 +0300 Subject: [PATCH] Select runtime with directive in test data file instead of test name in PsiChecker tests --- .../kotlin/test/KotlinTestUtils.java | 26 ++++++++++++++++--- .../KotlinLightCodeInsightFixtureTestCase.kt | 2 +- idea/testData/checker/FunctionReturnTypes.kt | 5 ++-- .../checker/JvmStaticUsagesRuntime.kt | 1 + .../topLevelMultifileRuntime.kt | 1 + .../checker/regression/DoubleDefine.kt | 7 ++--- .../checkers/AbstractPsiCheckerTest.java | 7 ++--- .../kotlin/checkers/PsiCheckerCustomTest.kt | 6 +++++ 8 files changed, 43 insertions(+), 12 deletions(-) diff --git a/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestUtils.java b/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestUtils.java index ea1f929cc83..5258346729b 100644 --- a/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestUtils.java +++ b/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestUtils.java @@ -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 collectMethodsMetadata(Class testCaseClass) { Set 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; diff --git a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt index 253b5b5e461..cc26e8cdae2 100644 --- a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt +++ b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/KotlinLightCodeInsightFixtureTestCase.kt @@ -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 diff --git a/idea/testData/checker/FunctionReturnTypes.kt b/idea/testData/checker/FunctionReturnTypes.kt index 30e0e43b9a9..8a969b6d8b2 100644 --- a/idea/testData/checker/FunctionReturnTypes.kt +++ b/idea/testData/checker/FunctionReturnTypes.kt @@ -1,3 +1,4 @@ +// RUNTIME fun none() {} fun unitEmptyInfer() {} @@ -16,8 +17,8 @@ fun bbb() { fun foo(expr: 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) } } diff --git a/idea/testData/checker/JvmStaticUsagesRuntime.kt b/idea/testData/checker/JvmStaticUsagesRuntime.kt index 3e6f1e62667..5ceefc1b758 100644 --- a/idea/testData/checker/JvmStaticUsagesRuntime.kt +++ b/idea/testData/checker/JvmStaticUsagesRuntime.kt @@ -1,3 +1,4 @@ +// RUNTIME @JvmStatic class A { @JvmStatic diff --git a/idea/testData/checker/duplicateJvmSignature/functionAndProperty/topLevelMultifileRuntime.kt b/idea/testData/checker/duplicateJvmSignature/functionAndProperty/topLevelMultifileRuntime.kt index 8fe5625beb2..2fb36d85066 100644 --- a/idea/testData/checker/duplicateJvmSignature/functionAndProperty/topLevelMultifileRuntime.kt +++ b/idea/testData/checker/duplicateJvmSignature/functionAndProperty/topLevelMultifileRuntime.kt @@ -1,3 +1,4 @@ +// RUNTIME @file:JvmName("TopLevelMultifile") @file:JvmMultifileClass package test diff --git a/idea/testData/checker/regression/DoubleDefine.kt b/idea/testData/checker/regression/DoubleDefine.kt index 6055272972a..a4716e628e9 100644 --- a/idea/testData/checker/regression/DoubleDefine.kt +++ b/idea/testData/checker/regression/DoubleDefine.kt @@ -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 { - if (expr.length == 0) throw Throwable("Syntax error: Character expected"); + if (expr.length == 0) throw Exception("Syntax error: Character expected"); val c = takeFirst(expr) 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 { diff --git a/idea/tests/org/jetbrains/kotlin/checkers/AbstractPsiCheckerTest.java b/idea/tests/org/jetbrains/kotlin/checkers/AbstractPsiCheckerTest.java index c9c43e90e32..b34e4ceb77c 100644 --- a/idea/tests/org/jetbrains/kotlin/checkers/AbstractPsiCheckerTest.java +++ b/idea/tests/org/jetbrains/kotlin/checkers/AbstractPsiCheckerTest.java @@ -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()); } + } diff --git a/idea/tests/org/jetbrains/kotlin/checkers/PsiCheckerCustomTest.kt b/idea/tests/org/jetbrains/kotlin/checkers/PsiCheckerCustomTest.kt index 2f56e94f0c2..57b62bfd1ba 100644 --- a/idea/tests/org/jetbrains/kotlin/checkers/PsiCheckerCustomTest.kt +++ b/idea/tests/org/jetbrains/kotlin/checkers/PsiCheckerCustomTest.kt @@ -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() } \ No newline at end of file