Minor refactorings in legacy codegen tests

Use loadFile() + getPrefix() instead of loadFile(String)
This commit is contained in:
Alexander Udalov
2016-02-23 17:56:28 +03:00
parent 5f7bc601a8
commit 1be6046fc2
9 changed files with 65 additions and 49 deletions
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.name.SpecialNames;
import org.jetbrains.kotlin.test.ConfigurationKind;
@@ -27,30 +28,34 @@ import java.util.List;
import static org.jetbrains.kotlin.codegen.CodegenTestUtil.findDeclaredMethodByName;
public class ClassGenTest extends CodegenTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
}
public void testPSVMClass() {
loadFile("classes/simpleClass.kt");
@NotNull
@Override
protected String getPrefix() {
return "classes";
}
public void testSimpleClass() {
loadFile();
Class<?> aClass = generateClass("SimpleClass");
Method[] methods = aClass.getDeclaredMethods();
// public int SimpleClass.foo()
assertEquals(1, methods.length);
}
public void testArrayListInheritance() throws Exception {
loadFile("classes/inheritingFromArrayList.kt");
public void testInheritingFromArrayList() throws Exception {
loadFile();
Class<?> aClass = generateClass("Foo");
assertInstanceOf(aClass.newInstance(), List.class);
}
public void testDelegationToVal() throws Exception {
loadFile("classes/delegationToVal.kt");
loadFile();
GeneratedClassLoader loader = generateAndCreateClassLoader();
Class<?> aClass = loader.loadClass("DelegationToValKt");
assertEquals("OK", aClass.getMethod("box").invoke(null));
@@ -82,8 +87,8 @@ public class ClassGenTest extends CodegenTestCase {
assertEquals("OKOK", iActingMethod.invoke(test3.getMethod("getActing").invoke(obj)));
}
public void testNewInstanceExplicitConstructor() throws Exception {
loadFile("classes/newInstanceDefaultConstructor.kt");
public void testNewInstanceDefaultConstructor() throws Exception {
loadFile();
Method method = generateFunction("test");
Integer returnValue = (Integer) method.invoke(null);
assertEquals(610, returnValue.intValue());
@@ -103,7 +108,7 @@ public class ClassGenTest extends CodegenTestCase {
}
public void testClassObjectInterface() throws Exception {
loadFile("classes/classObjectInterface.kt");
loadFile();
Method method = generateFunction();
Object result = method.invoke(null);
assertInstanceOf(result, Runnable.class);
@@ -132,14 +137,8 @@ public class ClassGenTest extends CodegenTestCase {
assertEquals(method.getReturnType().getName(), "java.lang.Void");
}
/*
public void testKt1213() {
// blackBoxFile("regressions/kt1213.kt");
}
*/
public void testClassObjectIsInnerClass() throws Exception {
loadFile("classes/classObjectIsInnerClass.kt");
loadFile();
GeneratedClassLoader loader = generateAndCreateClassLoader();
Class<?> a = loader.loadClass("A");
Class<?> companionObject = loader.loadClass("A$" + SpecialNames.DEFAULT_NAME_FOR_COMPANION_OBJECT.asString());
@@ -53,19 +53,19 @@ public class ControlStructuresTest extends CodegenTestCase {
}
public void testWhile() throws Exception {
factorialTest("controlStructures/while.kt");
factorialTest();
}
public void testDoWhile() throws Exception {
factorialTest("controlStructures/doWhile.kt");
factorialTest();
}
public void testBreak() throws Exception {
factorialTest("controlStructures/break.kt");
factorialTest();
}
private void factorialTest(String name) throws Exception {
loadFile(name);
private void factorialTest() throws Exception {
loadFile();
Method main = generateFunction();
assertEquals(6, main.invoke(null, 3));
assertEquals(120, main.invoke(null, 5));
@@ -42,9 +42,10 @@ import java.lang.reflect.InvocationTargetException;
import static org.jetbrains.kotlin.codegen.CodegenTestUtil.compileJava;
public class GenerateNotNullAssertionsTest extends CodegenTestCase {
@NotNull
@Override
protected void setUp() throws Exception {
super.setUp();
protected String getPrefix() {
return "notNullAssertions";
}
private void setUpEnvironment(boolean disableCallAssertions, boolean disableParamAssertions, File... extraClassPath) {
@@ -57,12 +58,16 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
myEnvironment = KotlinCoreEnvironment.createForTests(getTestRootDisposable(), configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
}
private void loadSource(@NotNull String fileName) {
loadFileByFullPath(KotlinTestUtils.getTestDataPathBase() + "/codegen/" + getPrefix() + "/" + fileName);
}
private void doTestCallAssertions(boolean disableCallAssertions) throws Exception {
File javaClassesTempDirectory = compileJava("notNullAssertions/A.java");
File javaClassesTempDirectory = compileJava(getPrefix() + "/A.java");
setUpEnvironment(disableCallAssertions, true, javaClassesTempDirectory);
loadFile("notNullAssertions/AssertionChecker.kt");
loadSource("AssertionChecker.kt");
generateFunction("checkAssertions").invoke(null, !disableCallAssertions);
}
@@ -77,37 +82,37 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
public void testNoAssertionsForKotlinFromSource() throws Exception {
setUpEnvironment(false, true);
loadFiles("notNullAssertions/noAssertionsForKotlin.kt", "notNullAssertions/noAssertionsForKotlinMain.kt");
loadFiles(getPrefix() + "/noAssertionsForKotlin.kt", getPrefix() + "/noAssertionsForKotlinMain.kt");
assertNoIntrinsicsMethodIsCalledInMyClasses(true);
}
public void testNoAssertionsForKotlinFromBinary() throws Exception {
setUpEnvironment(false, true);
loadFile("notNullAssertions/noAssertionsForKotlin.kt");
loadSource("noAssertionsForKotlin.kt");
OutputFileCollection outputFiles = generateClassesInFile();
File compiledDirectory = new File(FileUtil.getTempDirectory(), "kotlin-classes");
OutputUtilsKt.writeAllTo(outputFiles, compiledDirectory);
setUpEnvironment(false, true, compiledDirectory);
loadFile("notNullAssertions/noAssertionsForKotlinMain.kt");
loadSource("noAssertionsForKotlinMain.kt");
assertNoIntrinsicsMethodIsCalledInMyClasses(false);
}
public void testGenerateParamAssertions() throws Exception {
File javaClassesTempDirectory = compileJava("notNullAssertions/doGenerateParamAssertions.java");
File javaClassesTempDirectory = compileJava(getPrefix() + "/doGenerateParamAssertions.java");
setUpEnvironment(true, false, javaClassesTempDirectory);
loadFile("notNullAssertions/doGenerateParamAssertions.kt");
loadSource("doGenerateParamAssertions.kt");
generateFunction().invoke(null);
}
public void testDoNotGenerateParamAssertions() throws Exception {
setUpEnvironment(true, true);
loadFile("notNullAssertions/doNotGenerateParamAssertions.kt");
loadSource("doNotGenerateParamAssertions.kt");
assertNoIntrinsicsMethodIsCalled("A", true);
}
@@ -115,7 +120,7 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
public void testNoParamAssertionForPrivateMethod() throws Exception {
setUpEnvironment(true, false);
loadFile("notNullAssertions/noAssertionForPrivateMethod.kt");
loadSource("noAssertionForPrivateMethod.kt");
assertNoIntrinsicsMethodIsCalled("A", true);
}
@@ -123,7 +128,7 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
public void testArrayListGet() {
setUpEnvironment(false, false);
loadFile("notNullAssertions/arrayListGet.kt");
loadSource("arrayListGet.kt");
String text = generateToText();
assertTrue(text.contains("checkExpressionValueIsNotNull"));
@@ -131,10 +136,10 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
}
public void testJavaMultipleSubstitutions() {
File javaClassesTempDirectory = compileJava("notNullAssertions/javaMultipleSubstitutions.java");
File javaClassesTempDirectory = compileJava(getPrefix() + "/javaMultipleSubstitutions.java");
setUpEnvironment(false, false, javaClassesTempDirectory);
loadFile("notNullAssertions/javaMultipleSubstitutions.kt");
loadSource("javaMultipleSubstitutions.kt");
String text = generateToText();
assertEquals(3, StringUtil.getOccurrenceCount(text, "checkExpressionValueIsNotNull"));
@@ -144,7 +149,7 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
public void testAssertionForNotNullTypeParam() {
setUpEnvironment(false, false);
loadFile("notNullAssertions/assertionForNotNullTypeParam.kt");
loadSource("assertionForNotNullTypeParam.kt");
assertTrue(generateToText().contains("checkParameterIsNotNull"));
}
@@ -152,7 +157,7 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
public void testNoAssertionForNullableGenericMethod() {
setUpEnvironment(false, true);
loadFile("notNullAssertions/noAssertionForNullableGenericMethod.kt");
loadSource("noAssertionForNullableGenericMethod.kt");
assertNoIntrinsicsMethodIsCalledInMyClasses(true);
}
@@ -160,7 +165,7 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
public void testNoAssertionForNullableCaptured() {
setUpEnvironment(false, true);
loadFile("notNullAssertions/noAssertionForNullableCaptured.kt");
loadSource("noAssertionForNullableCaptured.kt");
assertNoIntrinsicsMethodIsCalledInMyClasses(true);
}
@@ -168,7 +173,7 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
public void testAssertionForNotNullCaptured() {
setUpEnvironment(false, true);
loadFile("notNullAssertions/assertionForNotNullCaptured.kt");
loadSource("assertionForNotNullCaptured.kt");
assertTrue(generateToText().contains("checkExpressionValueIsNotNull"));
}
@@ -176,7 +181,7 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
public void testNoAssertionForNullableGenericMethodCall() {
setUpEnvironment(false, true);
loadFile("notNullAssertions/noAssertionForNullableGenericMethodCall.kt");
loadSource("noAssertionForNullableGenericMethodCall.kt");
assertNoIntrinsicsMethodIsCalled("A", true);
}
@@ -38,9 +38,14 @@ public class InnerClassInfoGenTest extends CodegenTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
loadFile("innerClassInfo/" + getTestName(true) + ".kt");
loadFile();
}
@NotNull
@Override
protected String getPrefix() {
return "innerClassInfo";
}
public void testInnerClassInfo() {
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.test.ConfigurationKind;
@@ -43,13 +44,19 @@ public class JUnitUsageGenTest extends CodegenTestCase {
EnvironmentConfigFiles.JVM_CONFIG_FILES);
}
@NotNull
@Override
protected String getPrefix() {
return "junit";
}
public void testKt2344() throws Exception {
loadFile("junit/kt2344.kt");
loadFile();
generateFunction().invoke(null);
}
public void testKt1592() throws Exception {
loadFile("junit/kt1592.kt");
loadFile();
Class<?> packageClass = generateFacadeClass();
Method method = packageClass.getMethod("foo", Method.class);
method.setAccessible(true);
@@ -84,7 +84,7 @@ public class PropertyGenTest extends CodegenTestCase {
}
public void testFieldPropertyAccess() throws Exception {
loadFile("properties/fieldPropertyAccess.kt");
loadFile();
Method method = generateFunction("increment");
assertEquals(1, method.invoke(null));
assertEquals(2, method.invoke(null));
@@ -157,7 +157,7 @@ public class PropertyGenTest extends CodegenTestCase {
}
public void testKt1846() {
loadFile("regressions/kt1846.kt");
loadFile();
Class<?> aClass = generateClass("A");
try {
aClass.getMethod("getV1");
@@ -177,7 +177,7 @@ public class PropertyGenTest extends CodegenTestCase {
}
public void testKt2589() throws Exception {
loadFile("regressions/kt2589.kt");
loadFile();
Class<?> aClass = generateClass("Foo");
assertTrue((aClass.getModifiers() & Opcodes.ACC_FINAL) == 0);
@@ -199,7 +199,7 @@ public class PropertyGenTest extends CodegenTestCase {
}
public void testKt2677() throws Exception {
loadFile("regressions/kt2677.kt");
loadFile();
Class<?> derived = generateClass("DerivedWeatherReport");
Class<?> weatherReport = derived.getSuperclass();
@@ -232,7 +232,7 @@ public class PropertyGenTest extends CodegenTestCase {
}
public void testPrivateClassPropertyAccessors() throws Exception {
loadFile("properties/privateClassPropertyAccessors.kt");
loadFile();
Class<?> c = generateClass("C");
findDeclaredMethodByName(c, "getValWithGet");
findDeclaredMethodByName(c, "getVarWithGetSet");