Migrate AbstractLineNumberTest to CodegenTestCase stuff

This commit is contained in:
Mikhael Bogdanov
2018-08-24 12:05:03 +02:00
parent 9c68f2be36
commit 25b32b8e1d
3 changed files with 94 additions and 141 deletions
@@ -17,90 +17,55 @@
package org.jetbrains.kotlin.codegen;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import kotlin.Pair;
import kotlin.collections.CollectionsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.backend.common.output.OutputFile;
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.psi.KtFile;
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 org.jetbrains.org.objectweb.asm.*;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
public abstract class AbstractLineNumberTest extends CodegenTestCase {
private static final String LINE_NUMBER_FUN = "lineNumber";
private static final Pattern TEST_LINE_NUMBER_PATTERN = Pattern.compile("^.*test." + LINE_NUMBER_FUN + "\\(\\).*$");
@NotNull
private KotlinCoreEnvironment createEnvironment() {
return KotlinCoreEnvironment.createForTests(
myTestRootDisposable,
KotlinTestUtils.newConfiguration(
ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, KotlinTestUtils.getAnnotationsJar(), tmpdir
),
EnvironmentConfigFiles.JVM_CONFIG_FILES
private static TestFile createLineNumberDeclaration() {
return new TestFile(
LINE_NUMBER_FUN + ".kt",
"package test;\n\npublic fun " + LINE_NUMBER_FUN + "(): Int = 0\n"
);
}
@Override
public void setUp() throws Exception {
super.setUp();
KotlinCoreEnvironment environment = createEnvironment();
KtFile psiFile = KotlinTestUtils.createFile(
LINE_NUMBER_FUN + ".kt",
"package test;\n\npublic fun " + LINE_NUMBER_FUN + "(): Int = 0\n",
environment.getProject()
);
GenerationUtils.compileFileTo(psiFile, environment, tmpdir);
}
@NotNull
private Pair<KtFile, KotlinCoreEnvironment> createPsiFile(@NotNull String filename) {
File file = new File(filename);
KotlinCoreEnvironment environment = createEnvironment();
String text;
try {
text = FileUtil.loadFile(file, true);
}
catch (IOException e) {
throw ExceptionUtilsKt.rethrow(e);
protected void doMultiFileTest(
@NotNull File wholeFile, @NotNull List<TestFile> files, @Nullable File javaFilesDir
) {
boolean isCustomTest = wholeFile.getParentFile().getName().equalsIgnoreCase("custom");
if (!isCustomTest) {
files.add(createLineNumberDeclaration());
}
compile(files, javaFilesDir);
return new Pair<>(KotlinTestUtils.createFile(file.getName(), text, environment.getProject()), environment);
}
private void doTest(@NotNull String filename, boolean custom) {
Pair<KtFile, KotlinCoreEnvironment> fileAndEnv = createPsiFile(filename);
KtFile psiFile = fileAndEnv.getFirst();
KotlinCoreEnvironment environment = fileAndEnv.getSecond();
ClassFileFactory classFileFactory = GenerationUtils.compileFile(psiFile, environment);
KtFile psiFile = CollectionsKt.single(myFiles.getPsiFiles(), file -> file.getName().equals(wholeFile.getName()));
try {
if (custom) {
if (isCustomTest) {
List<String> actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, false);
String text = psiFile.getText();
String newFileText = text.substring(0, text.indexOf("// ")) + getActualLineNumbersAsString(actualLineNumbers);
KotlinTestUtils.assertEqualsToFile(new File(filename), newFileText);
KotlinTestUtils.assertEqualsToFile(wholeFile, newFileText);
}
else {
List<String> expectedLineNumbers = extractSelectedLineNumbersFromSource(psiFile);
List<String> actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, true);
assertFalse( "Missed 'lineNumbers' calls in test data", expectedLineNumbers.isEmpty());
assertSameElements(actualLineNumbers, expectedLineNumbers);
}
} catch (Throwable e) {
@@ -132,14 +97,6 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
return actualLineNumbers;
}
protected void doTest(String path) {
doTest(path, false);
}
protected void doTestCustom(String path) {
doTest(path, true);
}
@NotNull
private static List<String> extractSelectedLineNumbersFromSource(@NotNull KtFile file) {
String fileContent = file.getText();
@@ -17,104 +17,101 @@ import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/lineNumber")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class LineNumberTestGenerated extends AbstractLineNumberTest {
@TestMetadata("compiler/testData/lineNumber")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class LineNumber extends AbstractLineNumberTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInLineNumber() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/lineNumber"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, false);
}
public void testAllFilesPresentInLineNumber() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/lineNumber"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("anonymousFunction.kt")
public void testAnonymousFunction() throws Exception {
runTest("compiler/testData/lineNumber/anonymousFunction.kt");
}
@TestMetadata("anonymousFunction.kt")
public void testAnonymousFunction() throws Exception {
runTest("compiler/testData/lineNumber/anonymousFunction.kt");
}
@TestMetadata("class.kt")
public void testClass() throws Exception {
runTest("compiler/testData/lineNumber/class.kt");
}
@TestMetadata("class.kt")
public void testClass() throws Exception {
runTest("compiler/testData/lineNumber/class.kt");
}
@TestMetadata("classObject.kt")
public void testClassObject() throws Exception {
runTest("compiler/testData/lineNumber/classObject.kt");
}
@TestMetadata("classObject.kt")
public void testClassObject() throws Exception {
runTest("compiler/testData/lineNumber/classObject.kt");
}
@TestMetadata("defaultParameter.kt")
public void testDefaultParameter() throws Exception {
runTest("compiler/testData/lineNumber/defaultParameter.kt");
}
@TestMetadata("defaultParameter.kt")
public void testDefaultParameter() throws Exception {
runTest("compiler/testData/lineNumber/defaultParameter.kt");
}
@TestMetadata("enum.kt")
public void testEnum() throws Exception {
runTest("compiler/testData/lineNumber/enum.kt");
}
@TestMetadata("enum.kt")
public void testEnum() throws Exception {
runTest("compiler/testData/lineNumber/enum.kt");
}
@TestMetadata("for.kt")
public void testFor() throws Exception {
runTest("compiler/testData/lineNumber/for.kt");
}
@TestMetadata("for.kt")
public void testFor() throws Exception {
runTest("compiler/testData/lineNumber/for.kt");
}
@TestMetadata("if.kt")
public void testIf() throws Exception {
runTest("compiler/testData/lineNumber/if.kt");
}
@TestMetadata("if.kt")
public void testIf() throws Exception {
runTest("compiler/testData/lineNumber/if.kt");
}
@TestMetadata("inlineSimpleCall.kt")
public void testInlineSimpleCall() throws Exception {
runTest("compiler/testData/lineNumber/inlineSimpleCall.kt");
}
@TestMetadata("inlineSimpleCall.kt")
public void testInlineSimpleCall() throws Exception {
runTest("compiler/testData/lineNumber/inlineSimpleCall.kt");
}
@TestMetadata("localFunction.kt")
public void testLocalFunction() throws Exception {
runTest("compiler/testData/lineNumber/localFunction.kt");
}
@TestMetadata("localFunction.kt")
public void testLocalFunction() throws Exception {
runTest("compiler/testData/lineNumber/localFunction.kt");
}
@TestMetadata("object.kt")
public void testObject() throws Exception {
runTest("compiler/testData/lineNumber/object.kt");
}
@TestMetadata("object.kt")
public void testObject() throws Exception {
runTest("compiler/testData/lineNumber/object.kt");
}
@TestMetadata("propertyAccessor.kt")
public void testPropertyAccessor() throws Exception {
runTest("compiler/testData/lineNumber/propertyAccessor.kt");
}
@TestMetadata("propertyAccessor.kt")
public void testPropertyAccessor() throws Exception {
runTest("compiler/testData/lineNumber/propertyAccessor.kt");
}
@TestMetadata("psvm.kt")
public void testPsvm() throws Exception {
runTest("compiler/testData/lineNumber/psvm.kt");
}
@TestMetadata("psvm.kt")
public void testPsvm() throws Exception {
runTest("compiler/testData/lineNumber/psvm.kt");
}
@TestMetadata("simpleSmap.kt")
public void testSimpleSmap() throws Exception {
runTest("compiler/testData/lineNumber/simpleSmap.kt");
}
@TestMetadata("simpleSmap.kt")
public void testSimpleSmap() throws Exception {
runTest("compiler/testData/lineNumber/simpleSmap.kt");
}
@TestMetadata("topLevel.kt")
public void testTopLevel() throws Exception {
runTest("compiler/testData/lineNumber/topLevel.kt");
}
@TestMetadata("topLevel.kt")
public void testTopLevel() throws Exception {
runTest("compiler/testData/lineNumber/topLevel.kt");
}
@TestMetadata("trait.kt")
public void testTrait() throws Exception {
runTest("compiler/testData/lineNumber/trait.kt");
}
@TestMetadata("trait.kt")
public void testTrait() throws Exception {
runTest("compiler/testData/lineNumber/trait.kt");
}
@TestMetadata("tryCatch.kt")
public void testTryCatch() throws Exception {
runTest("compiler/testData/lineNumber/tryCatch.kt");
}
@TestMetadata("tryCatch.kt")
public void testTryCatch() throws Exception {
runTest("compiler/testData/lineNumber/tryCatch.kt");
}
@TestMetadata("while.kt")
public void testWhile() throws Exception {
runTest("compiler/testData/lineNumber/while.kt");
}
@TestMetadata("while.kt")
public void testWhile() throws Exception {
runTest("compiler/testData/lineNumber/while.kt");
}
@TestMetadata("compiler/testData/lineNumber/custom")
@@ -122,7 +119,7 @@ public class LineNumberTestGenerated extends AbstractLineNumberTest {
@RunWith(JUnit3RunnerWithInners.class)
public static class Custom extends AbstractLineNumberTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestCustom, TargetBackend.ANY, testDataFilePath);
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInCustom() throws Exception {
@@ -336,8 +336,7 @@ fun main(args: Array<String>) {
}
testClass<AbstractLineNumberTest> {
model("lineNumber", recursive = false)
model("lineNumber/custom", testMethod = "doTestCustom")
model("lineNumber")
}
testClass<AbstractLocalClassProtoTest> {