From 5884d4be7946d341b3b4a314698f553ee1970cdc Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 23 Feb 2016 20:54:29 +0300 Subject: [PATCH] Get rid of code duplication in kt-against-kt tests --- ...actCompileKotlinAgainstInlineKotlinTest.kt | 34 +---------- ...bstractCompileKotlinAgainstKotlinTest.java | 37 +++++++++++- ...CompileKotlinAgainstMultifileKotlinTest.kt | 57 ------------------- ...inAgainstMultifileKotlinTestGenerated.java | 2 +- .../kotlin/generators/tests/GenerateTests.kt | 2 +- 5 files changed, 37 insertions(+), 95 deletions(-) delete mode 100644 compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstMultifileKotlinTest.kt diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstInlineKotlinTest.kt b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstInlineKotlinTest.kt index 4fe8c162d2b..c41b954b659 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstInlineKotlinTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstInlineKotlinTest.kt @@ -16,18 +16,12 @@ package org.jetbrains.kotlin.jvm.compiler -import org.jetbrains.kotlin.codegen.ClassFileFactory import org.jetbrains.kotlin.codegen.InlineTestUtil import org.jetbrains.kotlin.codegen.filterClassFiles -import java.io.File -import java.util.Collections abstract class AbstractCompileKotlinAgainstInlineKotlinTest : AbstractCompileKotlinAgainstKotlinTest(), AbstractSMAPBaseTest { - fun doBoxTestWithInlineCheck(firstFileName: String) { - val inputFiles = listOf(firstFileName, firstFileName.substringBeforeLast("1.kt") + "2.kt") - - val (factory1, factory2) = doBoxTest(inputFiles) + val (factory1, factory2) = doBoxTest(firstFileName) val allGeneratedFiles = factory1.asList() + factory2.asList() try { @@ -40,30 +34,4 @@ abstract class AbstractCompileKotlinAgainstInlineKotlinTest : AbstractCompileKot throw e } } - - private fun doBoxTest(files: List): Pair { - Collections.sort(files) - - var factory1: ClassFileFactory? = null - var factory2: ClassFileFactory? = null - try { - factory1 = compileA(File(files[1])) - factory2 = compileB(File(files[0])) - invokeBox(files[0]) - } - catch (e: Throwable) { - var result = "" - if (factory1 != null) { - result += "FIRST: \n\n" + factory1.createText() - } - if (factory2 != null) { - result += "\n\nSECOND: \n\n" + factory2.createText() - } - System.out.println(result) - throw e - } - - return Pair(factory1!!, factory2!!) - } - } diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstKotlinTest.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstKotlinTest.java index 8878d2e0981..c1650a5d2be 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstKotlinTest.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstKotlinTest.java @@ -20,6 +20,8 @@ import com.intellij.openapi.Disposable; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.io.FileUtil; import com.intellij.util.ArrayUtil; +import kotlin.Pair; +import kotlin.text.StringsKt; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.cli.common.modules.ModuleBuilder; import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsKt; @@ -36,12 +38,15 @@ import org.jetbrains.kotlin.test.ConfigurationKind; import org.jetbrains.kotlin.test.KotlinTestUtils; import org.jetbrains.kotlin.test.TestCaseWithTmpdir; import org.jetbrains.kotlin.test.TestJdkKind; +import org.jetbrains.kotlin.utils.ExceptionUtilsKt; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; +import java.util.Arrays; +import java.util.List; public abstract class AbstractCompileKotlinAgainstKotlinTest extends TestCaseWithTmpdir { private File aDir; @@ -69,7 +74,7 @@ public abstract class AbstractCompileKotlinAgainstKotlinTest extends TestCaseWit main.invoke(null, new Object[] {ArrayUtil.EMPTY_STRING_ARRAY}); } - protected void invokeBox(@NotNull String fileName) throws Exception { + private void invokeBox(@NotNull String fileName) throws Exception { Method box = generatedClass(fileName).getMethod("box"); String result = (String) box.invoke(null); assertEquals("OK", result); @@ -85,13 +90,13 @@ public abstract class AbstractCompileKotlinAgainstKotlinTest extends TestCaseWit return classLoader.loadClass(PackagePartClassUtils.getFilePartShortName(fileLastName)); } - protected ClassFileFactory compileA(@NotNull File ktAFile) throws IOException { + private ClassFileFactory compileA(@NotNull File ktAFile) throws IOException { KotlinCoreEnvironment jetCoreEnvironment = KotlinTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(getTestRootDisposable(), ConfigurationKind.ALL); return compileKotlin(ktAFile, aDir, jetCoreEnvironment, getTestRootDisposable()); } - protected ClassFileFactory compileB(@NotNull File ktBFile) throws IOException { + private ClassFileFactory compileB(@NotNull File ktBFile) throws IOException { CompilerConfiguration configurationWithADirInClasspath = KotlinTestUtils .compilerConfigurationForTests(ConfigurationKind.ALL, TestJdkKind.MOCK_JDK, KotlinTestUtils.getAnnotationsJar(), aDir); @@ -119,4 +124,30 @@ public abstract class AbstractCompileKotlinAgainstKotlinTest extends TestCaseWit Disposer.dispose(disposable); return outputFiles; } + + @NotNull + protected Pair doBoxTest(@NotNull String firstFileName) { + List files = Arrays.asList(firstFileName, StringsKt.substringBeforeLast(firstFileName, "1.kt", "") + "2.kt"); + + ClassFileFactory factory1 = null; + ClassFileFactory factory2 = null; + try { + factory1 = compileA(new File(files.get(1))); + factory2 = compileB(new File(files.get(0))); + invokeBox(files.get(0)); + } + catch (Throwable e) { + String result = ""; + if (factory1 != null) { + result += "FIRST: \n\n" + factory1.createText(); + } + if (factory2 != null) { + result += "\n\nSECOND: \n\n" + factory2.createText(); + } + System.out.println(result); + throw ExceptionUtilsKt.rethrow(e); + } + + return new Pair(factory1, factory2); + } } diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstMultifileKotlinTest.kt b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstMultifileKotlinTest.kt deleted file mode 100644 index 341dce0fb29..00000000000 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileKotlinAgainstMultifileKotlinTest.kt +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2010-2015 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 org.jetbrains.kotlin.jvm.compiler - -import org.jetbrains.kotlin.codegen.ClassFileFactory -import org.jetbrains.kotlin.codegen.InlineTestUtil -import org.jetbrains.kotlin.codegen.filterClassFiles -import java.io.File -import java.util.Collections - -abstract class AbstractCompileKotlinAgainstMultifileKotlinTest : AbstractCompileKotlinAgainstKotlinTest(), AbstractSMAPBaseTest { - - fun doBoxTest(firstFileName: String) { - val inputFiles = listOf(firstFileName, firstFileName.substringBeforeLast("1.kt") + "2.kt") - doBoxTest(inputFiles) - } - - private fun doBoxTest(files: List): Pair { - Collections.sort(files) - - var factory1: ClassFileFactory? = null - var factory2: ClassFileFactory? = null - try { - factory1 = compileA(File(files[1])) - factory2 = compileB(File(files[0])) - invokeBox(files[0]) - } - catch (e: Throwable) { - var result = "" - if (factory1 != null) { - result += "FIRST: \n\n" + factory1.createText() - } - if (factory2 != null) { - result += "\n\nSECOND: \n\n" + factory2.createText() - } - System.out.println(result) - throw e - } - - return Pair(factory1!!, factory2!!) - } - -} diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstMultifileKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstMultifileKotlinTestGenerated.java index 91f9b6f65ad..0c6704b314a 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstMultifileKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstMultifileKotlinTestGenerated.java @@ -30,7 +30,7 @@ import java.util.regex.Pattern; @TestMetadata("compiler/testData/codegen/boxMultifileClasses") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) -public class CompileKotlinAgainstMultifileKotlinTestGenerated extends AbstractCompileKotlinAgainstMultifileKotlinTest { +public class CompileKotlinAgainstMultifileKotlinTestGenerated extends AbstractCompileKotlinAgainstKotlinTest { public void testAllFilesPresentInBoxMultifileClasses() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxMultifileClasses"), Pattern.compile("^(.+)\\.1.kt$"), true); } diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 1d6402d73ac..dd3b73a2e42 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -203,7 +203,7 @@ fun main(args: Array) { model("codegen/boxInline", extension = "1.kt", testMethod = "doBoxTestWithInlineCheck") } - testClass(AbstractCompileKotlinAgainstMultifileKotlinTest::class.java, "CompileKotlinAgainstMultifileKotlinTestGenerated") { + testClass(AbstractCompileKotlinAgainstKotlinTest::class.java, "CompileKotlinAgainstMultifileKotlinTestGenerated") { model("codegen/boxMultifileClasses", extension = "1.kt", testMethod = "doBoxTest") }