Migrate AbstractLineNumberTest to CodegenTestCase stuff
This commit is contained in:
+17
-60
@@ -17,90 +17,55 @@
|
|||||||
package org.jetbrains.kotlin.codegen;
|
package org.jetbrains.kotlin.codegen;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.intellij.openapi.util.io.FileUtil;
|
|
||||||
import com.intellij.openapi.util.text.StringUtil;
|
import com.intellij.openapi.util.text.StringUtil;
|
||||||
import kotlin.Pair;
|
|
||||||
import kotlin.collections.CollectionsKt;
|
import kotlin.collections.CollectionsKt;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.kotlin.backend.common.output.OutputFile;
|
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.psi.KtFile;
|
||||||
import org.jetbrains.kotlin.test.ConfigurationKind;
|
|
||||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
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.kotlin.utils.ExceptionUtilsKt;
|
||||||
import org.jetbrains.org.objectweb.asm.*;
|
import org.jetbrains.org.objectweb.asm.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
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 String LINE_NUMBER_FUN = "lineNumber";
|
||||||
private static final Pattern TEST_LINE_NUMBER_PATTERN = Pattern.compile("^.*test." + LINE_NUMBER_FUN + "\\(\\).*$");
|
private static final Pattern TEST_LINE_NUMBER_PATTERN = Pattern.compile("^.*test." + LINE_NUMBER_FUN + "\\(\\).*$");
|
||||||
|
|
||||||
@NotNull
|
private static TestFile createLineNumberDeclaration() {
|
||||||
private KotlinCoreEnvironment createEnvironment() {
|
return new TestFile(
|
||||||
return KotlinCoreEnvironment.createForTests(
|
LINE_NUMBER_FUN + ".kt",
|
||||||
myTestRootDisposable,
|
"package test;\n\npublic fun " + LINE_NUMBER_FUN + "(): Int = 0\n"
|
||||||
KotlinTestUtils.newConfiguration(
|
|
||||||
ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, KotlinTestUtils.getAnnotationsJar(), tmpdir
|
|
||||||
),
|
|
||||||
EnvironmentConfigFiles.JVM_CONFIG_FILES
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setUp() throws Exception {
|
protected void doMultiFileTest(
|
||||||
super.setUp();
|
@NotNull File wholeFile, @NotNull List<TestFile> files, @Nullable File javaFilesDir
|
||||||
|
) {
|
||||||
KotlinCoreEnvironment environment = createEnvironment();
|
boolean isCustomTest = wholeFile.getParentFile().getName().equalsIgnoreCase("custom");
|
||||||
KtFile psiFile = KotlinTestUtils.createFile(
|
if (!isCustomTest) {
|
||||||
LINE_NUMBER_FUN + ".kt",
|
files.add(createLineNumberDeclaration());
|
||||||
"package test;\n\npublic fun " + LINE_NUMBER_FUN + "(): Int = 0\n",
|
|
||||||
environment.getProject()
|
|
||||||
);
|
|
||||||
|
|
||||||
GenerationUtils.compileFileTo(psiFile, environment, tmpdir);
|
|
||||||
}
|
}
|
||||||
|
compile(files, javaFilesDir);
|
||||||
|
|
||||||
@NotNull
|
KtFile psiFile = CollectionsKt.single(myFiles.getPsiFiles(), file -> file.getName().equals(wholeFile.getName()));
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (custom) {
|
if (isCustomTest) {
|
||||||
List<String> actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, false);
|
List<String> actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, false);
|
||||||
String text = psiFile.getText();
|
String text = psiFile.getText();
|
||||||
String newFileText = text.substring(0, text.indexOf("// ")) + getActualLineNumbersAsString(actualLineNumbers);
|
String newFileText = text.substring(0, text.indexOf("// ")) + getActualLineNumbersAsString(actualLineNumbers);
|
||||||
KotlinTestUtils.assertEqualsToFile(new File(filename), newFileText);
|
KotlinTestUtils.assertEqualsToFile(wholeFile, newFileText);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
List<String> expectedLineNumbers = extractSelectedLineNumbersFromSource(psiFile);
|
List<String> expectedLineNumbers = extractSelectedLineNumbersFromSource(psiFile);
|
||||||
List<String> actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, true);
|
List<String> actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, true);
|
||||||
|
assertFalse( "Missed 'lineNumbers' calls in test data", expectedLineNumbers.isEmpty());
|
||||||
assertSameElements(actualLineNumbers, expectedLineNumbers);
|
assertSameElements(actualLineNumbers, expectedLineNumbers);
|
||||||
}
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
@@ -132,14 +97,6 @@ public abstract class AbstractLineNumberTest extends TestCaseWithTmpdir {
|
|||||||
return actualLineNumbers;
|
return actualLineNumbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void doTest(String path) {
|
|
||||||
doTest(path, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void doTestCustom(String path) {
|
|
||||||
doTest(path, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static List<String> extractSelectedLineNumbersFromSource(@NotNull KtFile file) {
|
private static List<String> extractSelectedLineNumbersFromSource(@NotNull KtFile file) {
|
||||||
String fileContent = file.getText();
|
String fileContent = file.getText();
|
||||||
|
|||||||
@@ -17,18 +17,16 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
|
@TestMetadata("compiler/testData/lineNumber")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
public class LineNumberTestGenerated extends AbstractLineNumberTest {
|
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 {
|
private void runTest(String testDataFilePath) throws Exception {
|
||||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAllFilesPresentInLineNumber() throws Exception {
|
public void testAllFilesPresentInLineNumber() throws Exception {
|
||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/lineNumber"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, false);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/lineNumber"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("anonymousFunction.kt")
|
@TestMetadata("anonymousFunction.kt")
|
||||||
@@ -115,14 +113,13 @@ public class LineNumberTestGenerated extends AbstractLineNumberTest {
|
|||||||
public void testWhile() throws Exception {
|
public void testWhile() throws Exception {
|
||||||
runTest("compiler/testData/lineNumber/while.kt");
|
runTest("compiler/testData/lineNumber/while.kt");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("compiler/testData/lineNumber/custom")
|
@TestMetadata("compiler/testData/lineNumber/custom")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
public static class Custom extends AbstractLineNumberTest {
|
public static class Custom extends AbstractLineNumberTest {
|
||||||
private void runTest(String testDataFilePath) throws Exception {
|
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 {
|
public void testAllFilesPresentInCustom() throws Exception {
|
||||||
|
|||||||
@@ -336,8 +336,7 @@ fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
testClass<AbstractLineNumberTest> {
|
testClass<AbstractLineNumberTest> {
|
||||||
model("lineNumber", recursive = false)
|
model("lineNumber")
|
||||||
model("lineNumber/custom", testMethod = "doTestCustom")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
testClass<AbstractLocalClassProtoTest> {
|
testClass<AbstractLocalClassProtoTest> {
|
||||||
|
|||||||
Reference in New Issue
Block a user