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