Migrate bytecode text tests to multi-file framework

Get rid of BytecodeTextMultifileTestGenerated
This commit is contained in:
Alexander Udalov
2016-02-25 14:34:18 +03:00
parent fbc2fe74ad
commit 647e188a08
20 changed files with 214 additions and 208 deletions
@@ -1,3 +1,32 @@
// FILE: JClass.java
import org.jetbrains.annotations.NotNull;
public class JClass {
public final static int PrimitiveInt = 9000;
public final static int BigPrimitiveInt = 59000;
public final static long PrimitiveLong = 100000;
public final static short PrimitiveShort = 901;
public final static boolean PrimitiveBool = false;
public final static float PrimitiveFloat = 36.6;
public final static double PrimitiveDouble = 42.4242;
public final static byte PrimitiveByte = -8;
public final static char PrimitiveChar = 'K';
public final static String Str = ":J";
@Nullable
public final static String StrNullable = "nullable";
@NotNull
public final static Integer BoxedInt = 9500;
public static int NonFinal = 9700;
public final int NonStatic = 9800;
}
// FILE: test.kt
object KoKobject {
@JvmField
val JvmStatic: Int = 1
@@ -31,7 +60,6 @@ fun test() {
KoKobject.JvmStaticString
}
// @TestKt.class:
// 1 LDC -2147483648
// 1 LDC 9223372036854775807
// 1 SIPUSH 9000
@@ -51,4 +79,4 @@ fun test() {
// 1 GETSTATIC KoKobject.JvmStatic : I
// 1 GETSTATIC KoKobject.JvmStaticString : Ljava/lang/String
// 3 POP2
// 18 POP
// 18 POP
@@ -1,3 +1,23 @@
// FILE: Child.java
class Child extends Parent {
public static int b = 3;
public static int c = 4;
public static void bar() {}
public static void baz() {}
}
// FILE: Parent.java
class Parent {
public static int a = 1;
public static int b = 2;
public static void foo() {}
public static void baz() {}
}
// FILE: test.kt
fun test() {
Parent.a
Parent.b
@@ -12,7 +32,6 @@ fun test() {
Child.baz()
}
// @TestKt.class:
// 1 GETSTATIC Parent.a : I
// 1 GETSTATIC Parent.b : I
// 1 INVOKESTATIC Parent.foo()
@@ -1,3 +1,13 @@
// FILE: otherFile.kt
@file:[JvmName("Util") JvmMultifileClass]
package test
internal fun internalInOtherFile() {}
public fun publicInOtherFile() {}
// FILE: thisFile.kt
@file:[JvmName("Util") JvmMultifileClass]
package test
@@ -20,4 +30,4 @@ public fun publicInThisFile() {}
// 1 INVOKESTATIC test/Util.internalInThisFile
// 1 INVOKESTATIC test/Util.publicInThisFile
// 1 INVOKESTATIC test/Util.internalInOtherFile
// 1 INVOKESTATIC test/Util.publicInOtherFile
// 1 INVOKESTATIC test/Util.publicInOtherFile
@@ -1,3 +1,12 @@
// FILE: otherFile.kt
@file:[JvmName("Util") JvmMultifileClass]
package test
public fun publicInOtherFile() {}
// FILE: thisFile.kt
@file:[JvmName("Util") JvmMultifileClass]
package test
@@ -15,4 +24,4 @@ fun bar() {
// @test/Util__ThisFileKt.class:
// 2 INVOKESTATIC test/Util.publicInThisFile
// 2 INVOKESTATIC test/Util.publicInOtherFile
// 2 INVOKESTATIC test/Util.publicInOtherFile
@@ -1,3 +1,32 @@
// FILE: JClass.java
import org.jetbrains.annotations.NotNull;
public class JClass {
public final static int PrimitiveInt = 9000;
public final static int BigPrimitiveInt = 59000;
public final static long PrimitiveLong = 100000;
public final static short PrimitiveShort = 901;
public final static boolean PrimitiveBool = false;
public final static float PrimitiveFloat = 36.6;
public final static double PrimitiveDouble = 42.4242;
public final static byte PrimitiveByte = -8;
public final static char PrimitiveChar = 'K';
public final static String Str = ":J";
@Nullable
public final static String StrNullable = "nullable";
@NotNull
public final static Integer BoxedInt = 9500;
public static int NonFinal = 9700;
public final int NonStatic = 9800;
}
// FILE: test.kt
enum class EClass {
VALUE
}
@@ -34,7 +63,6 @@ fun test() {
"res8: " + EClass::class
}
// @TestKt.class:
// 1 LDC "res1: -2147483648 9223372036854775807 9000 59000 -8 K 100000 901 false 36.6 42.4242 :J nullable"
// 1 LDC "res2: "
// 1 LDC "res3: "
@@ -1,24 +0,0 @@
import org.jetbrains.annotations.NotNull;
public class JClass {
public final static int PrimitiveInt = 9000;
public final static int BigPrimitiveInt = 59000;
public final static long PrimitiveLong = 100000;
public final static short PrimitiveShort = 901;
public final static boolean PrimitiveBool = false;
public final static float PrimitiveFloat = 36.6;
public final static double PrimitiveDouble = 42.4242;
public final static byte PrimitiveByte = -8;
public final static char PrimitiveChar = 'K';
public final static String Str = ":J";
@Nullable
public final static String StrNullable = "nullable";
@NotNull
public final static Integer BoxedInt = 9500;
public static int NonFinal = 9700;
public final int NonStatic = 9800;
}
@@ -1,6 +0,0 @@
class Child extends Parent {
public static int b = 3;
public static int c = 4;
public static void bar() {}
public static void baz() {}
}
@@ -1,6 +0,0 @@
class Parent {
public static int a = 1;
public static int b = 2;
public static void foo() {}
public static void baz() {}
}
@@ -1,5 +0,0 @@
@file:[JvmName("Util") JvmMultifileClass]
package test
internal fun internalInOtherFile() {}
public fun publicInOtherFile() {}
@@ -1,4 +0,0 @@
@file:[JvmName("Util") JvmMultifileClass]
package test
public fun publicInOtherFile() {}
@@ -1,24 +0,0 @@
import org.jetbrains.annotations.NotNull;
public class JClass {
public final static int PrimitiveInt = 9000;
public final static int BigPrimitiveInt = 59000;
public final static long PrimitiveLong = 100000;
public final static short PrimitiveShort = 901;
public final static boolean PrimitiveBool = false;
public final static float PrimitiveFloat = 36.6;
public final static double PrimitiveDouble = 42.4242;
public final static byte PrimitiveByte = -8;
public final static char PrimitiveChar = 'K';
public final static String Str = ":J";
@Nullable
public final static String StrNullable = "nullable";
@NotNull
public final static Integer BoxedInt = 9500;
public static int NonFinal = 9700;
public final int NonStatic = 9800;
}
@@ -93,6 +93,11 @@ public abstract class KotlinMultiFileTestWithJava<M, F> extends KotlinLiteFixtur
return false;
}
@NotNull
protected final File getJavaFilesDir() {
return javaFilesDir;
}
protected void doTest(String filePath) throws Exception {
File file = new File(filePath);
@@ -92,19 +92,6 @@ public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase {
blackBox();
}
protected void loadMultiFiles(@NotNull List<TestFile> files) {
Collections.sort(files);
List<KtFile> ktFiles = new ArrayList<KtFile>(files.size());
for (TestFile file : files) {
if (file.name.endsWith(".kt")) {
ktFiles.add(KotlinTestUtils.createFile(file.name, file.content, myEnvironment.getProject()));
}
}
myFiles = CodegenTestFiles.create(ktFiles);
}
// NOTE: tests under fullJdk/ are run with FULL_JDK instead of MOCK_JDK
@NotNull
private static TestJdkKind getTestJdkKind(@NotNull String sourcePath) {
@@ -18,15 +18,16 @@ package org.jetbrains.kotlin.codegen;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Processor;
import kotlin.text.Charsets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.test.ConfigurationKind;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
import java.io.File;
import java.util.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -35,20 +36,31 @@ public abstract class AbstractBytecodeTextTest extends CodegenTestCase {
private static final Pattern EXPECTED_OCCURRENCES_PATTERN = Pattern.compile("^\\s*//\\s*(\\d+)\\s*(.*)$");
@Override
public void doTest(@NotNull String filename) throws Exception {
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.ALL);
loadFileByFullPath(filename);
List<OccurrenceInfo> expected = readExpectedOccurrences(filename);
countAndCompareActualOccurrences(expected);
protected void doMultiFileTest(File file, Map<String, ModuleAndDependencies> modules, List<TestFile> files) throws Exception {
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.ALL, getJavaFilesDir());
loadMultiFiles(files);
if (isMultiFileTest(files)) {
doTestMultiFile(files);
}
else {
List<OccurrenceInfo> expected = readExpectedOccurrences(file.getPath());
String actual = generateToText();
checkGeneratedTextAgainstExpectedOccurrences(actual, expected);
}
}
protected void countAndCompareActualOccurrences(@NotNull List<OccurrenceInfo> expectedOccurrences) {
String text = generateToText();
checkGeneratedTextAgainstExpectedOccurrences(text, expectedOccurrences);
private static boolean isMultiFileTest(@NotNull List<TestFile> files) {
int kotlinFiles = 0;
for (TestFile file : files) {
if (file.name.endsWith(".kt")) {
kotlinFiles++;
}
}
return kotlinFiles > 1;
}
private static void checkGeneratedTextAgainstExpectedOccurrences(
protected static void checkGeneratedTextAgainstExpectedOccurrences(
@NotNull String text,
@NotNull List<OccurrenceInfo> expectedOccurrences
) {
@@ -70,31 +82,10 @@ public abstract class AbstractBytecodeTextTest extends CodegenTestCase {
}
}
public void doTestMultiFile(@NotNull String folderName) throws Exception {
final List<String> files = new ArrayList<String>(2);
FileUtil.processFilesRecursively(new File(folderName), new Processor<File>() {
@Override
public boolean process(File file) {
if (file.getName().endsWith(".kt")) {
files.add(relativePath(file));
}
return true;
}
});
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.ALL, new File(folderName));
doTestMultiFile(files);
}
private void doTestMultiFile(@NotNull List<String> filenames) throws Exception {
Collections.sort(filenames);
String[] sortedFilenames = ArrayUtil.toStringArray(filenames);
loadFiles(sortedFilenames);
private void doTestMultiFile(@NotNull List<TestFile> files) throws Exception {
Map<String, List<OccurrenceInfo>> expectedOccurrencesByOutputFile = new LinkedHashMap<String, List<OccurrenceInfo>>();
for (String filename : filenames) {
readExpectedOccurrencesForMultiFileTest(filename, expectedOccurrencesByOutputFile);
for (TestFile file : files) {
readExpectedOccurrencesForMultiFileTest(file, expectedOccurrencesByOutputFile);
}
Map<String, String> generated = generateEachFileToText();
@@ -132,15 +123,19 @@ public abstract class AbstractBytecodeTextTest extends CodegenTestCase {
return result;
}
protected void readExpectedOccurrencesForMultiFileTest(@NotNull String filename, @NotNull Map<String, List<OccurrenceInfo>> occurrenceMap) throws Exception {
String[] lines = FileUtil.loadFile(new File(codegenTestBasePath() + filename), true).split("\n");
private static void readExpectedOccurrencesForMultiFileTest(
@NotNull TestFile file,
@NotNull Map<String, List<OccurrenceInfo>> occurrenceMap
) {
List<OccurrenceInfo> currentOccurrenceInfos = null;
for (String line : lines) {
for (String line : file.content.split("\n")) {
Matcher atOutputFileMatcher = AT_OUTPUT_FILE_PATTERN.matcher(line);
if (atOutputFileMatcher.matches()) {
String outputFileName = atOutputFileMatcher.group(1);
if (occurrenceMap.containsKey(outputFileName)) {
throw new AssertionError(filename + ": Expected occurrences for output file " + outputFileName + " were already provided");
throw new AssertionError(
file.name + ": Expected occurrences for output file " + outputFileName + " were already provided"
);
}
currentOccurrenceInfos = new ArrayList<OccurrenceInfo>();
occurrenceMap.put(outputFileName, currentOccurrenceInfos);
@@ -149,7 +144,9 @@ public abstract class AbstractBytecodeTextTest extends CodegenTestCase {
Matcher expectedOccurrencesMatcher = EXPECTED_OCCURRENCES_PATTERN.matcher(line);
if (expectedOccurrencesMatcher.matches()) {
if (currentOccurrenceInfos == null) {
throw new AssertionError(filename + ": Should specify output file with '// @<OUTPUT_FILE_NAME>:' before expectations");
throw new AssertionError(
file.name + ": Should specify output file with '// @<OUTPUT_FILE_NAME>:' before expectations"
);
}
OccurrenceInfo occurrenceInfo = parseOccurrenceInfo(expectedOccurrencesMatcher);
currentOccurrenceInfos.add(occurrenceInfo);
@@ -16,10 +16,10 @@
package org.jetbrains.kotlin.codegen;
import kotlin.collections.CollectionsKt;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Processor;
import kotlin.collections.CollectionsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
@@ -75,6 +75,7 @@ public abstract class AbstractTopLevelMembersInvocationTest extends AbstractByte
loadFiles(ArrayUtil.toStringArray(sourceFiles));
List<OccurrenceInfo> expected = readExpectedOccurrences(KotlinTestUtils.getTestDataPathBase() + "/codegen/" + sourceFiles.get(0));
countAndCompareActualOccurrences(expected);
String actual = generateToText();
checkGeneratedTextAgainstExpectedOccurrences(actual, expected);
}
}
@@ -1,67 +0,0 @@
/*
* Copyright 2010-2016 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.codegen;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
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/codegen/bytecodeTextMultifile")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class BytecodeTextMultifileTestGenerated extends AbstractBytecodeTextTest {
public void testAllFilesPresentInBytecodeTextMultifile() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/bytecodeTextMultifile"), Pattern.compile("^([^\\.]+)$"), false);
}
@TestMetadata("inlineJavaStaticFields")
public void testInlineJavaStaticFields() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeTextMultifile/inlineJavaStaticFields/");
doTestMultiFile(fileName);
}
@TestMetadata("javaStatics")
public void testJavaStatics() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeTextMultifile/javaStatics/");
doTestMultiFile(fileName);
}
@TestMetadata("partMembersCall")
public void testPartMembersCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeTextMultifile/partMembersCall/");
doTestMultiFile(fileName);
}
@TestMetadata("partMembersInline")
public void testPartMembersInline() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeTextMultifile/partMembersInline/");
doTestMultiFile(fileName);
}
@TestMetadata("preEvaluateInlineJavaStaticFields")
public void testPreEvaluateInlineJavaStaticFields() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeTextMultifile/preEvaluateInlineJavaStaticFields/");
doTestMultiFile(fileName);
}
}
@@ -113,6 +113,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName);
}
@TestMetadata("inlineJavaStaticFields.kt")
public void testInlineJavaStaticFields() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/inlineJavaStaticFields.kt");
doTest(fileName);
}
@TestMetadata("intConstantNotNull.kt")
public void testIntConstantNotNull() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/intConstantNotNull.kt");
@@ -161,6 +167,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName);
}
@TestMetadata("javaStatics.kt")
public void testJavaStatics() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/javaStatics.kt");
doTest(fileName);
}
@TestMetadata("jvmField.kt")
public void testJvmField() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/jvmField.kt");
@@ -251,6 +263,24 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName);
}
@TestMetadata("partMembersCall.kt")
public void testPartMembersCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/partMembersCall.kt");
doTest(fileName);
}
@TestMetadata("partMembersInline.kt")
public void testPartMembersInline() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/partMembersInline.kt");
doTest(fileName);
}
@TestMetadata("preEvaluateInlineJavaStaticFields.kt")
public void testPreEvaluateInlineJavaStaticFields() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/preEvaluateInlineJavaStaticFields.kt");
doTest(fileName);
}
@TestMetadata("prefixIntVarIncrement.kt")
public void testPrefixIntVarIncrement() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/prefixIntVarIncrement.kt");
@@ -23,6 +23,7 @@ import com.intellij.util.SmartList;
import kotlin.text.Charsets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.backend.common.output.OutputFile;
import org.jetbrains.kotlin.checkers.CheckerTestUtil;
import org.jetbrains.kotlin.checkers.KotlinMultiFileTestWithJava;
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
@@ -31,6 +32,7 @@ import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
import org.jetbrains.kotlin.config.CompilerConfiguration;
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.test.ConfigurationKind;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestJdkKind;
@@ -52,6 +54,7 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -127,6 +130,20 @@ public abstract class CodegenTestCase extends KotlinMultiFileTestWithJava<Void,
loadFile(getPrefix() + "/" + getTestName(true) + ".kt");
}
protected void loadMultiFiles(@NotNull List<TestFile> files) {
Collections.sort(files);
List<KtFile> ktFiles = new ArrayList<KtFile>(files.size());
for (TestFile file : files) {
if (file.name.endsWith(".kt")) {
String content = CheckerTestUtil.parseDiagnosedRanges(file.content, new ArrayList<CheckerTestUtil.DiagnosedRange>(0));
ktFiles.add(KotlinTestUtils.createFile(file.name, content, myEnvironment.getProject()));
}
}
myFiles = CodegenTestFiles.create(ktFiles);
}
@NotNull
protected String codegenTestBasePath() {
return "compiler/testData/codegen/";
@@ -339,6 +356,21 @@ public abstract class CodegenTestCase extends KotlinMultiFileTestWithJava<Void,
public int compareTo(@NotNull TestFile o) {
return name.compareTo(o.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof TestFile && ((TestFile) obj).name.equals(name);
}
@Override
public String toString() {
return name;
}
}
@Override
@@ -221,14 +221,10 @@ fun main(args: Array<String>) {
model("codegen/script", extension = "kts")
}
testClass(AbstractBytecodeTextTest::class.java) {
testClass<AbstractBytecodeTextTest>() {
model("codegen/bytecodeText")
}
testClass(AbstractBytecodeTextTest::class.java, "BytecodeTextMultifileTestGenerated") {
model("codegen/bytecodeTextMultifile", extension = null, recursive = false, testMethod = "doTestMultiFile")
}
testClass<AbstractBytecodeListingTest>() {
model("codegen/bytecodeListing")
}
@@ -19,9 +19,8 @@ package org.jetbrains.kotlin.lang.resolve.android.test
import org.jetbrains.kotlin.codegen.AbstractBytecodeTextTest
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.test.ConfigurationKind
import org.jetbrains.kotlin.test.TestJdkKind
import org.jetbrains.kotlin.test.KotlinTestUtils
import java.io.File
import org.jetbrains.kotlin.test.TestJdkKind
abstract class AbstractAndroidBytecodeShapeTest : AbstractBytecodeTextTest() {
@@ -39,6 +38,7 @@ abstract class AbstractAndroidBytecodeShapeTest : AbstractBytecodeTextTest() {
createAndroidAPIEnvironment(path)
loadFileByFullPath(fileName)
val expected = readExpectedOccurrences(fileName)
countAndCompareActualOccurrences(expected)
val actual = generateToText()
checkGeneratedTextAgainstExpectedOccurrences(actual, expected)
}
}
}