Remove some legacy codegen tests, move some to generated
This commit is contained in:
committed by
Alexander Udalov
parent
f8dfaf4599
commit
bab127ad33
+1
-10
@@ -26,7 +26,6 @@ import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.psi.KtScript
|
||||
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
|
||||
import java.util.*
|
||||
|
||||
object PackagePartClassUtils {
|
||||
@@ -58,14 +57,6 @@ object PackagePartClassUtils {
|
||||
return packageFqName.child(Name.identifier(partClassName))
|
||||
}
|
||||
|
||||
@Deprecated("Migrate to JvmFileClassesProvider")
|
||||
@JvmStatic fun getPackagePartInternalName(file: KtFile): String =
|
||||
JvmClassName.byFqNameWithoutInnerClasses(getPackagePartFqName(file)).internalName
|
||||
|
||||
@Deprecated("Migrate to JvmFileClassesProvider")
|
||||
@JvmStatic fun getPackagePartFqName(file: KtFile): FqName =
|
||||
getPackagePartFqName(file.packageFqName, file.name)
|
||||
|
||||
@JvmStatic fun getFilesWithCallables(files: Collection<KtFile>): List<KtFile> =
|
||||
files.filter { fileHasTopLevelCallables(it) }
|
||||
|
||||
@@ -79,4 +70,4 @@ object PackagePartClassUtils {
|
||||
@JvmStatic fun getFilePartShortName(fileName: String): String =
|
||||
getPartClassName(FileUtil.getNameWithoutExtension(fileName))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
class A {
|
||||
fun f(): () -> String {
|
||||
val s = "OK"
|
||||
return { -> s }
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val lambdaClass = A().f().javaClass
|
||||
val fields = lambdaClass.getDeclaredFields().toList()
|
||||
if (fields.size != 1) return "Fail: lambda should only capture 's': $fields"
|
||||
|
||||
val fieldName = fields[0].getName()
|
||||
if (fieldName != "\$s") return "Fail: captured variable should be named '\$s': $fields"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+7
-1
@@ -1,4 +1,4 @@
|
||||
fun continue_test(i: Int): Int {
|
||||
fun foo(i: Int): Int {
|
||||
var count = i;
|
||||
var result = 0;
|
||||
while(count > 0) {
|
||||
@@ -8,3 +8,9 @@ fun continue_test(i: Int): Int {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (foo(4) != 3) return "Fail 1"
|
||||
if (foo(5) != 7) return "Fail 2"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
fun facWhile(i: Int): Int {
|
||||
var count = 1;
|
||||
var result = 1;
|
||||
while(count < i) {
|
||||
count = count + 1;
|
||||
result = result * count;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
fun facBreak(i: Int): Int {
|
||||
var count = 1;
|
||||
var result = 1;
|
||||
while(true) {
|
||||
count = count + 1;
|
||||
result = result * count;
|
||||
if (count == i) break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
fun facDoWhile(i: Int): Int {
|
||||
var count = 1;
|
||||
var result = 1;
|
||||
do {
|
||||
count = count + 1;
|
||||
result = result * count;
|
||||
} while(count != i);
|
||||
return result;
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(6, facWhile(3))
|
||||
assertEquals(6, facBreak(3))
|
||||
assertEquals(6, facDoWhile(3))
|
||||
assertEquals(120, facWhile(5))
|
||||
assertEquals(120, facBreak(5))
|
||||
assertEquals(120, facDoWhile(5))
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// WITH_RUNTIME
|
||||
// FULL_JDK
|
||||
|
||||
interface IActing {
|
||||
fun act(): String
|
||||
}
|
||||
|
||||
class CActing(val value: String = "OK") : IActing {
|
||||
override fun act(): String = value
|
||||
}
|
||||
|
||||
// final so no need in delegate field
|
||||
class Test(val acting: CActing = CActing()) : IActing by acting {
|
||||
}
|
||||
|
||||
// even if open so we don't need delegate field
|
||||
open class Test2(open val acting: CActing = CActing()) : IActing by acting {
|
||||
}
|
||||
|
||||
// even if open the backing field is final, so we don't need delegate field
|
||||
class Test3() : Test2() {
|
||||
override val acting = CActing("OKOK")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
try {
|
||||
Test::class.java.getDeclaredField("\$delegate_0")
|
||||
return "\$delegate_0 field generated for class Test but should not"
|
||||
}
|
||||
catch (e: NoSuchFieldException) {
|
||||
// ok
|
||||
}
|
||||
|
||||
try {
|
||||
Test2::class.java.getDeclaredField("\$delegate_0")
|
||||
return "\$delegate_0 field generated for class Test but should not"
|
||||
}
|
||||
catch (e: NoSuchFieldException) {
|
||||
// ok
|
||||
}
|
||||
|
||||
if (Test3().acting.act() != "OKOK") return "Fail Test3"
|
||||
|
||||
val test = Test()
|
||||
return test.act()
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
enum class IssueState {
|
||||
DEFAULT,
|
||||
FIXED {
|
||||
override fun ToString() = "K"
|
||||
};
|
||||
|
||||
open fun ToString(): String = "O"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val field = IssueState::class.java.getField("FIXED")
|
||||
|
||||
val typeName = field.type.name
|
||||
if (typeName != "IssueState") return "Fail type name: $typeName"
|
||||
|
||||
val className = field.get(null).javaClass.name
|
||||
if (className != "IssueState\$FIXED") return "Fail class name: $className"
|
||||
|
||||
val classLoader = IssueState::class.java.classLoader
|
||||
classLoader.loadClass("IssueState\$FIXED")
|
||||
try {
|
||||
classLoader.loadClass("IssueState\$DEFAULT")
|
||||
return "Fail: no class should have been generated for DEFAULT"
|
||||
}
|
||||
catch (e: Exception) {
|
||||
// ok
|
||||
}
|
||||
|
||||
return IssueState.DEFAULT.ToString() + IssueState.FIXED.ToString()
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// WITH_RUNTIME
|
||||
// FULL_JDK
|
||||
|
||||
import java.lang.reflect.Modifier
|
||||
|
||||
enum class En {
|
||||
Y
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val klass = En::class.java
|
||||
val superclass = klass.superclass.name
|
||||
if (superclass != "java.lang.Enum") "Fail superclass: $superclass"
|
||||
|
||||
val enumModifiers = klass.modifiers
|
||||
if ((enumModifiers and 0x4000) == 0) return "Fail ACC_ENUM on class"
|
||||
if ((enumModifiers and Modifier.FINAL) == 0) return "Fail FINAL on class"
|
||||
|
||||
val entry = klass.getField("Y")
|
||||
val entryModifiers = entry.modifiers
|
||||
if ((entryModifiers and 0x4000) == 0) return "Fail ACC_ENUM on entry"
|
||||
if ((entryModifiers and Modifier.FINAL) == 0) return "Fail FINAL on entry"
|
||||
if ((entryModifiers and Modifier.STATIC) == 0) return "Fail FINAL on entry"
|
||||
if ((entryModifiers and Modifier.PUBLIC) == 0) return "Fail FINAL on entry"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
-6
@@ -1,6 +0,0 @@
|
||||
enum class State {
|
||||
O,
|
||||
K
|
||||
}
|
||||
|
||||
fun box() = "${State.O.name}${State.K.name}"
|
||||
@@ -0,0 +1,14 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
enum class State {
|
||||
O,
|
||||
K
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val field = State::class.java.getField("O")
|
||||
val className = field.get(null).javaClass.name
|
||||
if (className != "State") return "Fail: $className"
|
||||
|
||||
return "${State.O.name}${State.K.name}"
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
fun box() = IssueState.DEFAULT.ToString() + IssueState.FIXED.ToString()
|
||||
|
||||
enum class IssueState {
|
||||
DEFAULT,
|
||||
FIXED {
|
||||
override fun ToString() = "K"
|
||||
};
|
||||
|
||||
open fun ToString() : String = "O"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
class N {
|
||||
fun foo() = null
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val method = N::class.java.getDeclaredMethod("foo")
|
||||
if (method.returnType.name != "java.lang.Void") return "Fail: Nothing should be mapped to Void"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
fun isZero(x: Int) = when(x) {
|
||||
0 -> true
|
||||
else -> throw Exception()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
try {
|
||||
isZero(1)
|
||||
}
|
||||
catch (e: Exception) {
|
||||
return "OK"
|
||||
}
|
||||
return "Fail"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fun foo(x: Any) =
|
||||
when (x) {
|
||||
0, 1 -> "bit"
|
||||
else -> "something"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (foo(0) != "bit") return "Fail 0"
|
||||
if (foo(1) != "bit") return "Fail 1"
|
||||
if (foo(2) != "something") return "Fail 2"
|
||||
return "OK"
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: String, y: Any?) = x + y + 120
|
||||
|
||||
// 0 stringPlus
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: String?, y: Any?) = x + y
|
||||
|
||||
// 1 stringPlus
|
||||
@@ -1,9 +0,0 @@
|
||||
class C() {
|
||||
fun getInstance(): Runnable = C
|
||||
|
||||
companion object: Runnable {
|
||||
override fun run(): Unit { }
|
||||
}
|
||||
}
|
||||
|
||||
fun foo() = C().getInstance()
|
||||
@@ -1,3 +0,0 @@
|
||||
class A {
|
||||
companion object
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
interface IActing {
|
||||
fun act(): String
|
||||
}
|
||||
|
||||
class CActing(val value: String = "OK"): IActing {
|
||||
override fun act(): String = value
|
||||
}
|
||||
|
||||
// final so no need in delegate field
|
||||
class Test(val acting: CActing = CActing()): IActing by acting {
|
||||
}
|
||||
|
||||
// even if open so we don't need delegate field
|
||||
open class Test2(open val acting: CActing = CActing()): IActing by acting {
|
||||
}
|
||||
|
||||
// even if open the backing field is final, so we don't need delegate field
|
||||
class Test3() : Test2() {
|
||||
override val acting = CActing("OKOK")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val test = Test()
|
||||
return test.act()
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
class Foo() : java.util.ArrayList<Int>()
|
||||
@@ -1,8 +0,0 @@
|
||||
class SimpleClass() {
|
||||
fun foo() = 610
|
||||
}
|
||||
|
||||
fun test() : Int {
|
||||
val c = SimpleClass()
|
||||
return c.foo()
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
class SimpleClass {
|
||||
fun foo() : Int {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
fun fac(i: Int): Int {
|
||||
var count = 1;
|
||||
var result = 1;
|
||||
while(true) {
|
||||
count = count + 1;
|
||||
result = result * count;
|
||||
if (count == i) break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
fun fac(i: Int): Int {
|
||||
var count = 1;
|
||||
var result = 1;
|
||||
do {
|
||||
count = count + 1;
|
||||
result = result * count;
|
||||
} while(count != i);
|
||||
return result;
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
import java.util.*
|
||||
|
||||
fun concat(l: List<String>): String? {
|
||||
val sb = StringBuilder()
|
||||
for(s in l) {
|
||||
sb.append(s)
|
||||
}
|
||||
return sb.toString()
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
fun concat(l: Array<String>): String? {
|
||||
val sb = StringBuilder()
|
||||
for(s in l) {
|
||||
sb.append(s)
|
||||
}
|
||||
return sb.toString()
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
fun foo(b: Boolean): Int { return if (b) 15 else 20 }
|
||||
@@ -1,10 +0,0 @@
|
||||
import java.util.*
|
||||
|
||||
fun concat(l: List<String>): String? {
|
||||
val sb = StringBuilder()
|
||||
for(s in l) {
|
||||
val x = if(l.size > 1) { "T" } else { "F" };
|
||||
sb.append(x)
|
||||
}
|
||||
return sb.toString()
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
fun f(x: Int, b: Boolean): Int {
|
||||
var result = x;
|
||||
if (b) else result = result + 5;
|
||||
return result;
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
fun foo(b: Boolean) : Int {
|
||||
if (b) return 15;
|
||||
return 20;
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
fun foo(s: String): String? {
|
||||
try {
|
||||
Integer.parseInt(s);
|
||||
return "no message";
|
||||
}
|
||||
catch(e: NumberFormatException) {
|
||||
return e.message // Work around an overload-resolution bug
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
fun f(sb: StringBuilder, s: String): Unit {
|
||||
try {
|
||||
sb.append("foo");
|
||||
sb.append(Integer.parseInt(s));
|
||||
}
|
||||
finally {
|
||||
sb.append("bar");
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
fun fac(i: Int): Int {
|
||||
var count = 1;
|
||||
var result = 1;
|
||||
while(count < i) {
|
||||
count = count + 1;
|
||||
result = result * count;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
-6
@@ -1,6 +0,0 @@
|
||||
enum class State {
|
||||
O,
|
||||
K
|
||||
}
|
||||
|
||||
fun box() = "${State.O.name}${State.K.name}"
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
fun box() = IssueState.DEFAULT.ToString() + IssueState.FIXED.ToString()
|
||||
|
||||
enum class IssueState {
|
||||
DEFAULT,
|
||||
FIXED {
|
||||
override fun ToString() = "K"
|
||||
};
|
||||
|
||||
open fun ToString() : String = "O"
|
||||
}
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
enum class Season {
|
||||
WINTER,
|
||||
SPRING,
|
||||
SUMMER,
|
||||
AUTUMN
|
||||
}
|
||||
|
||||
fun foo(): Season = Season.SPRING
|
||||
|
||||
fun box() =
|
||||
if (foo() == Season.SPRING) "OK"
|
||||
else "fail"
|
||||
-3
@@ -1,3 +0,0 @@
|
||||
import org.junit.Test
|
||||
|
||||
@Test fun foo(m : java.lang.reflect.Method) = "OK"
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
import kotlin.test.*
|
||||
import org.junit.Test as test
|
||||
|
||||
public class Test {
|
||||
@test fun f(): Unit {
|
||||
assertEquals(true, !false)
|
||||
}
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
Test().f()
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
fun isZero(x: Int) = when(x) {
|
||||
0 -> true
|
||||
else -> false
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
fun isZero(x: Int) = when(x) {
|
||||
0 -> true
|
||||
else -> throw Exception()
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
fun isDigit(a: Int) : String {
|
||||
val aa = java.util.ArrayList<Int> ()
|
||||
aa.add(239)
|
||||
|
||||
if(a in aa) return "array list"
|
||||
if(a in 0..9) return "digit"
|
||||
if(a !in 0..100) return "not small"
|
||||
return "something"
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
fun isString(x: Any) = when(x) {
|
||||
is String -> "string"
|
||||
else -> "something"
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
fun isDigit(a: Char) = when(a) {
|
||||
in '0'..'9' -> "digit"
|
||||
else -> "something"
|
||||
}
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
package interactive
|
||||
|
||||
class Shape(var height : Double = 1.0, var fillColor : String = "#AAAAAA") {
|
||||
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
var a : Shape? = Shape()
|
||||
a?.height = 1.0
|
||||
return "OK"
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
// KT-297 Overload resolution ambiguity with required in interface
|
||||
interface ALE<T> : java.util.ArrayList<T> {
|
||||
fun getOrValue(index: Int, value : T) : T = if(index >= 0 && index < size) get(index) else value
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package kt799
|
||||
|
||||
fun foo(b: Boolean) : String {
|
||||
val a = if (b) true else return "false"
|
||||
return "$a"
|
||||
}
|
||||
|
||||
fun box() = if (foo(true) == "true" && foo(false) == "false") "OK" else "fail"
|
||||
@@ -18,29 +18,18 @@ package org.jetbrains.kotlin.codegen;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.load.kotlin.PackagePartClassUtils;
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.*;
|
||||
|
||||
public class AnnotationGenTest extends CodegenTestCase {
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.ALL);
|
||||
}
|
||||
|
||||
private ClassLoader loadFileGetClassLoader(@NotNull String text) {
|
||||
loadText(text);
|
||||
return generateAndCreateClassLoader();
|
||||
}
|
||||
|
||||
private Class<?> getPackageSrcClass(@NotNull ClassLoader loader) throws ClassNotFoundException {
|
||||
return loader.loadClass(PackagePartClassUtils.getPackagePartInternalName(myFiles.getPsiFile()));
|
||||
}
|
||||
|
||||
public void testVolatileProperty() throws Exception {
|
||||
loadText("abstract class Foo { @Volatile public var x: String = \"\"; }");
|
||||
Class<?> aClass = generateClass("Foo");
|
||||
@@ -49,34 +38,34 @@ public class AnnotationGenTest extends CodegenTestCase {
|
||||
}
|
||||
|
||||
public void testPropField() throws Exception {
|
||||
ClassLoader loader = loadFileGetClassLoader("@[java.lang.Deprecated] var x = 0");
|
||||
Class<?> srcClass = getPackageSrcClass(loader);
|
||||
loadText("@[java.lang.Deprecated] var x = 0");
|
||||
Class<?> srcClass = generateFacadeClass();
|
||||
assertNull(srcClass.getDeclaredMethod("getX").getAnnotation(Deprecated.class));
|
||||
assertNull(srcClass.getDeclaredMethod("setX", int.class).getAnnotation(Deprecated.class));
|
||||
assertNotNull(srcClass.getDeclaredField("x").getAnnotation(Deprecated.class));
|
||||
}
|
||||
|
||||
public void testPropGetter() throws Exception {
|
||||
ClassLoader loader = loadFileGetClassLoader("var x = 0\n" +
|
||||
loadText("var x = 0\n" +
|
||||
"@[java.lang.Deprecated] get");
|
||||
Class<?> srcClass = getPackageSrcClass(loader);
|
||||
Class<?> srcClass = generateFacadeClass();
|
||||
assertNotNull(srcClass.getDeclaredMethod("getX").getAnnotation(Deprecated.class));
|
||||
assertNull(srcClass.getDeclaredMethod("setX", int.class).getAnnotation(Deprecated.class));
|
||||
assertNull(srcClass.getDeclaredField("x").getAnnotation(Deprecated.class));
|
||||
}
|
||||
|
||||
public void testPropSetter() throws Exception {
|
||||
ClassLoader loader = loadFileGetClassLoader("var x = 0\n" +
|
||||
loadText("var x = 0\n" +
|
||||
"@[java.lang.Deprecated] set");
|
||||
Class<?> scrClass = getPackageSrcClass(loader);
|
||||
Class<?> scrClass = generateFacadeClass();
|
||||
assertNull(scrClass.getDeclaredMethod("getX").getAnnotation(Deprecated.class));
|
||||
assertNotNull(scrClass.getDeclaredMethod("setX", int.class).getAnnotation(Deprecated.class));
|
||||
assertNull(scrClass.getDeclaredField("x").getAnnotation(Deprecated.class));
|
||||
}
|
||||
|
||||
public void testAnnotationForParamInTopLevelFunction() throws Exception {
|
||||
ClassLoader loader = loadFileGetClassLoader("fun x(@[java.lang.Deprecated] i: Int) {}");
|
||||
Class<?> srcClass = getPackageSrcClass(loader);
|
||||
loadText("fun x(@[java.lang.Deprecated] i: Int) {}");
|
||||
Class<?> srcClass = generateFacadeClass();
|
||||
Method srcClassMethod = srcClass.getMethod("x", int.class);
|
||||
assertNotNull(srcClassMethod);
|
||||
assertNotNull(getDeprecatedAnnotationFromList(srcClassMethod.getParameterAnnotations()[0]));
|
||||
@@ -147,18 +136,13 @@ public class AnnotationGenTest extends CodegenTestCase {
|
||||
}
|
||||
|
||||
public void testAnnotationWithParamForParamInFunction() throws Exception {
|
||||
ClassLoader loader = loadFileGetClassLoader("import java.lang.annotation.*\n" +
|
||||
loadText("import java.lang.annotation.*\n" +
|
||||
"@java.lang.annotation.Retention(RetentionPolicy.RUNTIME) annotation class A(val a: String)\n" +
|
||||
"fun x(@A(\"239\") i: Int) {}");
|
||||
Class<?> packageClass = getPackageSrcClass(loader);
|
||||
Class<?> packageClass = generateFacadeClass();
|
||||
Method packageClassMethod = packageClass.getMethod("x", int.class);
|
||||
assertNotNull(packageClassMethod);
|
||||
assertNotNull(getAnnotationByName(packageClassMethod.getParameterAnnotations()[0], "A"));
|
||||
|
||||
Class<?> srcClass = getPackageSrcClass(loader);
|
||||
Method srcClassMethod = srcClass.getMethod("x", int.class);
|
||||
assertNotNull(srcClassMethod);
|
||||
assertNotNull(getAnnotationByName(srcClassMethod.getParameterAnnotations()[0], "A"));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -189,11 +173,10 @@ public class AnnotationGenTest extends CodegenTestCase {
|
||||
}
|
||||
|
||||
public void testMethod() throws Exception {
|
||||
ClassLoader loader = loadFileGetClassLoader("@[java.lang.Deprecated] fun x () {}");
|
||||
Class<?> srcClass = getPackageSrcClass(loader);
|
||||
loadText("@[java.lang.Deprecated] fun x () {}");
|
||||
Class<?> srcClass = generateFacadeClass();
|
||||
Method srcClassMethod = srcClass.getDeclaredMethod("x");
|
||||
assertNotNull(srcClassMethod.getAnnotation(Deprecated.class));
|
||||
|
||||
}
|
||||
|
||||
public void testClass() throws NoSuchFieldException, NoSuchMethodException {
|
||||
|
||||
@@ -3124,6 +3124,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("noRefToOuter.kt")
|
||||
public void testNoRefToOuter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/closures/noRefToOuter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("recursiveClosure.kt")
|
||||
public void testRecursiveClosure() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/closures/recursiveClosure.kt");
|
||||
@@ -3433,6 +3439,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("continueInWhile.kt")
|
||||
public void testContinueInWhile() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/controlStructures/continueInWhile.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("continueToLabelInFor.kt")
|
||||
public void testContinueToLabelInFor() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/controlStructures/continueToLabelInFor.kt");
|
||||
@@ -3475,6 +3487,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("factorialTest.kt")
|
||||
public void testFactorialTest() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/controlStructures/factorialTest.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("finallyOnEmptyReturn.kt")
|
||||
public void testFinallyOnEmptyReturn() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/controlStructures/finallyOnEmptyReturn.kt");
|
||||
@@ -4892,6 +4910,21 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/delegation")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Delegation extends AbstractBlackBoxCodegenTest {
|
||||
public void testAllFilesPresentInDelegation() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/delegation"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("delegationToVal.kt")
|
||||
public void testDelegationToVal() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/delegation/delegationToVal.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/diagnostics")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
@@ -5296,6 +5329,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("classForEnumEntry.kt")
|
||||
public void testClassForEnumEntry() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/classForEnumEntry.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("companionObjectInEnum.kt")
|
||||
public void testCompanionObjectInEnum() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/companionObjectInEnum.kt");
|
||||
@@ -5380,9 +5419,15 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("name.kt")
|
||||
public void testName() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/name.kt");
|
||||
@TestMetadata("modifierFlags.kt")
|
||||
public void testModifierFlags() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/modifierFlags.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("noClassForSimpleEnum.kt")
|
||||
public void testNoClassForSimpleEnum() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/noClassForSimpleEnum.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@@ -5392,12 +5437,6 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("openMethod.kt")
|
||||
public void testOpenMethod() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/openMethod.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("ordinal.kt")
|
||||
public void testOrdinal() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/ordinal.kt");
|
||||
@@ -13906,6 +13945,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt309.kt")
|
||||
public void testKt309() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typeMapping/kt309.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt3286.kt")
|
||||
public void testKt3286() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typeMapping/kt3286.kt");
|
||||
@@ -14140,6 +14185,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("exceptionOnNoMatch.kt")
|
||||
public void testExceptionOnNoMatch() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/exceptionOnNoMatch.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("exhaustiveBoolean.kt")
|
||||
public void testExhaustiveBoolean() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/exhaustiveBoolean.kt");
|
||||
@@ -14212,6 +14263,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("multipleEntries.kt")
|
||||
public void testMultipleEntries() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/multipleEntries.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("noElseExhaustive.kt")
|
||||
public void testNoElseExhaustive() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/noElseExhaustive.kt");
|
||||
|
||||
@@ -1159,6 +1159,18 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("nonNullableStringPlus.kt")
|
||||
public void testNonNullableStringPlus() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/stringOperations/nonNullableStringPlus.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("nullableStringPlus.kt")
|
||||
public void testNullableStringPlus() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/stringOperations/nullableStringPlus.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("plusAssign.kt")
|
||||
public void testPlusAssign() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/stringOperations/plusAssign.kt");
|
||||
|
||||
@@ -1,148 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.name.SpecialNames;
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
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);
|
||||
}
|
||||
|
||||
@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 testInheritingFromArrayList() throws Exception {
|
||||
loadFile();
|
||||
Class<?> aClass = generateClass("Foo");
|
||||
assertInstanceOf(aClass.newInstance(), List.class);
|
||||
}
|
||||
|
||||
public void testDelegationToVal() throws Exception {
|
||||
loadFile();
|
||||
GeneratedClassLoader loader = generateAndCreateClassLoader();
|
||||
Class<?> aClass = loader.loadClass("DelegationToValKt");
|
||||
assertEquals("OK", aClass.getMethod("box").invoke(null));
|
||||
|
||||
Class<?> test = loader.loadClass("Test");
|
||||
try {
|
||||
test.getDeclaredField("$delegate_0");
|
||||
fail("$delegate_0 field generated for class Test but should not");
|
||||
}
|
||||
catch (NoSuchFieldException e) {
|
||||
// ok
|
||||
}
|
||||
|
||||
Class<?> test2 = loader.loadClass("Test2");
|
||||
try {
|
||||
test2.getDeclaredField("$delegate_0");
|
||||
fail("$delegate_0 field generated for class Test2 but should not");
|
||||
}
|
||||
catch (NoSuchFieldException e) {
|
||||
// ok
|
||||
}
|
||||
|
||||
Class<?> test3 = loader.loadClass("Test3");
|
||||
Class<?> iActing = loader.loadClass("IActing");
|
||||
Object obj = test3.newInstance();
|
||||
assertTrue(iActing.isInstance(obj));
|
||||
Method iActingMethod = iActing.getMethod("act");
|
||||
assertEquals("OK", iActingMethod.invoke(obj));
|
||||
assertEquals("OKOK", iActingMethod.invoke(test3.getMethod("getActing").invoke(obj)));
|
||||
}
|
||||
|
||||
public void testNewInstanceDefaultConstructor() throws Exception {
|
||||
loadFile();
|
||||
Method method = generateFunction("test");
|
||||
Integer returnValue = (Integer) method.invoke(null);
|
||||
assertEquals(610, returnValue.intValue());
|
||||
}
|
||||
|
||||
public void testAbstractMethod() throws Exception {
|
||||
loadText("abstract class Foo { abstract fun x(): String; fun y(): Int = 0 }");
|
||||
Class<?> aClass = generateClass("Foo");
|
||||
assertNotNull(aClass.getMethod("x"));
|
||||
findDeclaredMethodByName(aClass, "y");
|
||||
}
|
||||
|
||||
public void testAbstractClass() throws Exception {
|
||||
loadText("abstract class SimpleClass() { }");
|
||||
Class<?> aClass = generateClass("SimpleClass");
|
||||
assertTrue((aClass.getModifiers() & Modifier.ABSTRACT) != 0);
|
||||
}
|
||||
|
||||
public void testClassObjectInterface() throws Exception {
|
||||
loadFile();
|
||||
Method method = generateFunction();
|
||||
Object result = method.invoke(null);
|
||||
assertInstanceOf(result, Runnable.class);
|
||||
}
|
||||
|
||||
public void testEnumClass() throws Exception {
|
||||
loadText("enum class Direction { NORTH, SOUTH, EAST, WEST; }");
|
||||
Class<?> direction = generateClass("Direction");
|
||||
Field north = direction.getField("NORTH");
|
||||
assertEquals(direction, north.getType());
|
||||
assertInstanceOf(north.get(null), direction);
|
||||
}
|
||||
|
||||
public void testEnumConstantConstructors() throws Exception {
|
||||
loadText("enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00); }");
|
||||
Class<?> colorClass = generateClass("Color");
|
||||
Field redField = colorClass.getField("RED");
|
||||
Object redValue = redField.get(null);
|
||||
Method rgbMethod = colorClass.getMethod("getRgb");
|
||||
assertEquals(0xFF0000, rgbMethod.invoke(redValue));
|
||||
}
|
||||
|
||||
public void testKt309() {
|
||||
loadText("fun box() = null");
|
||||
Method method = generateFunction("box");
|
||||
assertEquals(method.getReturnType().getName(), "java.lang.Void");
|
||||
}
|
||||
|
||||
public void testClassObjectIsInnerClass() throws Exception {
|
||||
loadFile();
|
||||
GeneratedClassLoader loader = generateAndCreateClassLoader();
|
||||
Class<?> a = loader.loadClass("A");
|
||||
Class<?> companionObject = loader.loadClass("A$" + SpecialNames.DEFAULT_NAME_FOR_COMPANION_OBJECT.asString());
|
||||
assertSameElements(a.getDeclaredClasses(), companionObject);
|
||||
assertEquals(a, companionObject.getDeclaringClass());
|
||||
}
|
||||
}
|
||||
@@ -231,12 +231,6 @@ public abstract class CodegenTestCase extends UsefulTestCase {
|
||||
return generateClass(facadeClassFqName.asString());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected Class<?> generateFileClass() {
|
||||
FqName fileClassFqName = JvmFileClassUtil.getFileClassInfoNoResolve(myFiles.getPsiFile()).getFileClassFqName();
|
||||
return generateClass(fileClassFqName.asString());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected Class<?> generateClass(@NotNull String name) {
|
||||
try {
|
||||
|
||||
@@ -16,12 +16,9 @@
|
||||
|
||||
package org.jetbrains.kotlin.codegen;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.kotlin.codegen.CodegenTestUtil.assertThrows;
|
||||
|
||||
@@ -32,59 +29,6 @@ public class ControlStructuresTest extends CodegenTestCase {
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getPrefix() {
|
||||
return "controlStructures";
|
||||
}
|
||||
|
||||
public void testIf() throws Exception {
|
||||
loadFile();
|
||||
Method main = generateFunction();
|
||||
assertEquals(15, main.invoke(null, true));
|
||||
assertEquals(20, main.invoke(null, false));
|
||||
}
|
||||
|
||||
public void testSingleBranchIf() throws Exception {
|
||||
loadFile();
|
||||
Method main = generateFunction();
|
||||
assertEquals(15, main.invoke(null, true));
|
||||
assertEquals(20, main.invoke(null, false));
|
||||
}
|
||||
|
||||
public void testWhile() throws Exception {
|
||||
factorialTest();
|
||||
}
|
||||
|
||||
public void testDoWhile() throws Exception {
|
||||
factorialTest();
|
||||
}
|
||||
|
||||
public void testBreak() throws Exception {
|
||||
factorialTest();
|
||||
}
|
||||
|
||||
private void factorialTest() throws Exception {
|
||||
loadFile();
|
||||
Method main = generateFunction();
|
||||
assertEquals(6, main.invoke(null, 3));
|
||||
assertEquals(120, main.invoke(null, 5));
|
||||
}
|
||||
|
||||
public void testContinue() throws Exception {
|
||||
loadFile();
|
||||
Method main = generateFunction();
|
||||
assertEquals(3, main.invoke(null, 4));
|
||||
assertEquals(7, main.invoke(null, 5));
|
||||
}
|
||||
|
||||
public void testIfNoElse() throws Exception {
|
||||
loadFile();
|
||||
Method main = generateFunction();
|
||||
assertEquals(5, main.invoke(null, 5, true));
|
||||
assertEquals(10, main.invoke(null, 5, false));
|
||||
}
|
||||
|
||||
public void testCondJumpOnStack() throws Exception {
|
||||
loadText("import java.lang.Boolean as jlBoolean; fun foo(a: String): Int = if (jlBoolean.parseBoolean(a)) 5 else 10");
|
||||
Method main = generateFunction();
|
||||
@@ -92,29 +36,6 @@ public class ControlStructuresTest extends CodegenTestCase {
|
||||
assertEquals(10, main.invoke(null, "false"));
|
||||
}
|
||||
|
||||
public void testFor() throws Exception {
|
||||
loadFile();
|
||||
Method main = generateFunction();
|
||||
List<String> args = Arrays.asList("IntelliJ", " ", "IDEA");
|
||||
assertEquals("IntelliJ IDEA", main.invoke(null, args));
|
||||
}
|
||||
|
||||
public void testIfBlock() throws Exception {
|
||||
loadFile();
|
||||
Method main = generateFunction();
|
||||
List<String> args = Arrays.asList("IntelliJ", " ", "IDEA");
|
||||
assertEquals("TTT", main.invoke(null, args));
|
||||
args = Arrays.asList("JetBrains");
|
||||
assertEquals("F", main.invoke(null, args));
|
||||
}
|
||||
|
||||
public void testForInArray() throws Exception {
|
||||
loadFile();
|
||||
Method main = generateFunction();
|
||||
String[] args = new String[] { "IntelliJ", " ", "IDEA" };
|
||||
assertEquals("IntelliJ IDEA", main.invoke(null, new Object[] { args }));
|
||||
}
|
||||
|
||||
public void testForInRange() throws Exception {
|
||||
loadText("fun foo(sb: StringBuilder) { for(x in 1..4) sb.append(x) }");
|
||||
Method main = generateFunction();
|
||||
@@ -129,24 +50,6 @@ public class ControlStructuresTest extends CodegenTestCase {
|
||||
assertThrows(main, Exception.class, null);
|
||||
}
|
||||
|
||||
public void testTryCatch() throws Exception {
|
||||
loadFile();
|
||||
Method main = generateFunction();
|
||||
assertEquals("no message", main.invoke(null, "0"));
|
||||
assertEquals("For input string: \"a\"", main.invoke(null, "a"));
|
||||
}
|
||||
|
||||
public void testTryFinally() throws Exception {
|
||||
loadFile();
|
||||
Method main = generateFunction();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
main.invoke(null, sb, "9");
|
||||
assertEquals("foo9bar", sb.toString());
|
||||
sb = new StringBuilder();
|
||||
assertThrows(main, NumberFormatException.class, null, sb, "x");
|
||||
assertEquals("foobar", sb.toString());
|
||||
}
|
||||
|
||||
public void testCompareToZero() throws Exception {
|
||||
loadText("fun foo(a: Int, b: Int): Boolean = a == 0 && b != 0 && 0 == a && 0 != b");
|
||||
String text = generateToText();
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
public class EnumGenTest extends CodegenTestCase {
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
|
||||
}
|
||||
|
||||
public void testSuperclassIsEnum() throws Exception {
|
||||
loadFile("enum/simple.kt");
|
||||
Class<?> season = generateClass("Season");
|
||||
assertEquals("java.lang.Enum", season.getSuperclass().getName());
|
||||
}
|
||||
|
||||
public void testEnumClassModifiers() throws Exception {
|
||||
loadFile("enum/simple.kt");
|
||||
Class<?> season = generateClass("Season");
|
||||
int modifiers = season.getModifiers();
|
||||
assertTrue((modifiers & 0x4000) != 0); // ACC_ENUM
|
||||
assertTrue((modifiers & Modifier.FINAL) != 0);
|
||||
}
|
||||
|
||||
public void testEnumFieldModifiers() throws Exception {
|
||||
loadFile("enum/simple.kt");
|
||||
Class<?> season = generateClass("Season");
|
||||
Field summer = season.getField("SUMMER");
|
||||
int modifiers = summer.getModifiers();
|
||||
assertTrue((modifiers & 0x4000) != 0); // ACC_ENUM
|
||||
assertTrue((modifiers & Modifier.FINAL) != 0);
|
||||
assertTrue((modifiers & Modifier.STATIC) != 0);
|
||||
assertTrue((modifiers & Modifier.PUBLIC) != 0);
|
||||
}
|
||||
|
||||
public void testEnumConstantConstructors() throws Exception {
|
||||
loadText("enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00); }");
|
||||
Class<?> colorClass = generateClass("Color");
|
||||
Field redField = colorClass.getField("RED");
|
||||
Object redValue = redField.get(null);
|
||||
Method rgbMethod = colorClass.getMethod("getRgb");
|
||||
assertEquals(0xFF0000, rgbMethod.invoke(redValue));
|
||||
}
|
||||
|
||||
public void testNoClassForSimpleEnum() throws Exception {
|
||||
loadFile("enum/name.kt");
|
||||
Class<?> cls = generateClass("State");
|
||||
Field field = cls.getField("O");
|
||||
assertEquals("State", field.get(null).getClass().getName());
|
||||
}
|
||||
|
||||
public void testYesClassForComplexEnum() throws Exception {
|
||||
loadFile("enum/openMethod.kt");
|
||||
Class<?> cls = generateClass("IssueState");
|
||||
Field field = cls.getField("DEFAULT");
|
||||
assertEquals("IssueState", field.get(null).getClass().getName());
|
||||
field = cls.getField("FIXED");
|
||||
assertEquals("IssueState", field.getType().getName());
|
||||
assertEquals("IssueState$FIXED", field.get(null).getClass().getName());
|
||||
assertNotNull(cls.getClassLoader().loadClass("IssueState$FIXED"));
|
||||
try {
|
||||
cls.getClassLoader().loadClass("IssueState$DEFAULT");
|
||||
fail();
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
// ok
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class FunctionGenTest extends CodegenTestCase {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
|
||||
}
|
||||
|
||||
public void testAnyEqualsNullable() throws InvocationTargetException, IllegalAccessException {
|
||||
loadText("fun foo(x: Any?) = x?.equals(\"lala\")");
|
||||
Method foo = generateFunction();
|
||||
assertTrue((Boolean) foo.invoke(null, "lala"));
|
||||
assertFalse((Boolean) foo.invoke(null, "mama"));
|
||||
}
|
||||
|
||||
public void testNoRefToOuter() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException {
|
||||
loadText("class A() { fun f() : ()->String { val s = \"OK\"; return { -> s } } }");
|
||||
Class<?> foo = generateClass("A");
|
||||
Object obj = foo.newInstance();
|
||||
Method f = foo.getMethod("f");
|
||||
Object closure = f.invoke(obj);
|
||||
Class<?> aClass = closure.getClass();
|
||||
Field[] fields = aClass.getDeclaredFields();
|
||||
assertEquals(1, fields.length);
|
||||
assertEquals("$s", fields[0].getName());
|
||||
}
|
||||
|
||||
public void testAnyEquals() throws InvocationTargetException, IllegalAccessException {
|
||||
loadText("fun foo(x: Any) = x.equals(\"lala\")");
|
||||
Method foo = generateFunction();
|
||||
assertTrue((Boolean) foo.invoke(null, "lala"));
|
||||
assertFalse((Boolean) foo.invoke(null, "mama"));
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 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;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.TestJdkKind;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class JUnitUsageGenTest extends CodegenTestCase {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
File junitJar = new File("libraries/lib/junit-4.11.jar");
|
||||
|
||||
if (!junitJar.exists()) {
|
||||
throw new AssertionError("JUnit jar wasn't found");
|
||||
}
|
||||
|
||||
myEnvironment = KotlinCoreEnvironment.createForTests(
|
||||
getTestRootDisposable(),
|
||||
KotlinTestUtils.compilerConfigurationForTests(ConfigurationKind.ALL, TestJdkKind.MOCK_JDK, junitJar),
|
||||
EnvironmentConfigFiles.JVM_CONFIG_FILES);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getPrefix() {
|
||||
return "junit";
|
||||
}
|
||||
|
||||
public void testKt2344() throws Exception {
|
||||
loadFile();
|
||||
generateFunction().invoke(null);
|
||||
}
|
||||
|
||||
public void testKt1592() throws Exception {
|
||||
loadFile();
|
||||
Class<?> packageClass = generateFacadeClass();
|
||||
Method method = packageClass.getMethod("foo", Method.class);
|
||||
method.setAccessible(true);
|
||||
Annotation annotation = method.getAnnotation(loadAnnotationClassQuietly(Test.class.getName()));
|
||||
assertEquals(CodegenTestUtil.getAnnotationAttribute(annotation, "timeout"), Long.valueOf(0));
|
||||
Class<?> expected = (Class<?>) CodegenTestUtil.getAnnotationAttribute(annotation, "expected");
|
||||
assertNotNull(expected);
|
||||
assertEquals(Test.None.class.getName(), expected.getName());
|
||||
}
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static org.jetbrains.kotlin.codegen.CodegenTestUtil.assertThrows;
|
||||
|
||||
public class PatternMatchingTest extends CodegenTestCase {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getPrefix() {
|
||||
return "patternMatching";
|
||||
}
|
||||
|
||||
public void testConstant() throws Exception {
|
||||
loadFile();
|
||||
Method foo = generateFunction();
|
||||
assertTrue((Boolean) foo.invoke(null, 0));
|
||||
assertFalse((Boolean) foo.invoke(null, 1));
|
||||
}
|
||||
|
||||
public void testExceptionOnNoMatch() throws Exception {
|
||||
loadFile();
|
||||
Method foo = generateFunction();
|
||||
assertTrue((Boolean) foo.invoke(null, 0));
|
||||
assertThrows(foo, Exception.class, null, 1);
|
||||
}
|
||||
|
||||
public void testPattern() throws Exception {
|
||||
loadFile();
|
||||
Method foo = generateFunction();
|
||||
assertEquals("string", foo.invoke(null, ""));
|
||||
assertEquals("something", foo.invoke(null, new Object()));
|
||||
}
|
||||
|
||||
public void testInrange() throws Exception {
|
||||
loadFile();
|
||||
Method foo = generateFunction();
|
||||
assertEquals("array list", foo.invoke(null, 239));
|
||||
assertEquals("digit", foo.invoke(null, 0));
|
||||
assertEquals("digit", foo.invoke(null, 9));
|
||||
assertEquals("digit", foo.invoke(null, 5));
|
||||
assertEquals("not small", foo.invoke(null, 190));
|
||||
assertEquals("something", foo.invoke(null, 19));
|
||||
}
|
||||
|
||||
public void testRangeChar() throws Exception {
|
||||
loadFile();
|
||||
Method foo = generateFunction();
|
||||
assertEquals("digit", foo.invoke(null, '0'));
|
||||
assertEquals("something", foo.invoke(null, 'A'));
|
||||
}
|
||||
|
||||
public void testWildcardPattern() throws Exception {
|
||||
loadText("fun foo(x: String) = when(x) { else -> \"something\" }");
|
||||
Method foo = generateFunction();
|
||||
assertEquals("something", foo.invoke(null, ""));
|
||||
}
|
||||
|
||||
public void testNoReturnType() throws Exception {
|
||||
loadText("fun foo(x: String) = when(x) { else -> \"x\" }");
|
||||
Method foo = generateFunction();
|
||||
assertEquals("x", foo.invoke(null, ""));
|
||||
}
|
||||
|
||||
public void testCall() throws Exception {
|
||||
loadText("fun foo(s: String) = when { s[0] == 'J' -> \"JetBrains\"; else -> \"something\" }");
|
||||
Method foo = generateFunction();
|
||||
assertEquals("JetBrains", foo.invoke(null, "Java"));
|
||||
assertEquals("something", foo.invoke(null, "C#"));
|
||||
}
|
||||
|
||||
public void testMultipleConditions() throws Exception {
|
||||
loadText("fun foo(x: Any) = when(x) { 0, 1 -> \"bit\"; else -> \"something\" }");
|
||||
Method foo = generateFunction();
|
||||
assertEquals("bit", foo.invoke(null, 0));
|
||||
assertEquals("bit", foo.invoke(null, 1));
|
||||
assertEquals("something", foo.invoke(null, 2));
|
||||
}
|
||||
}
|
||||
@@ -159,7 +159,7 @@ public class PrimitiveTypesTest extends CodegenTestCase {
|
||||
|
||||
public void testCastOnStack() throws Exception {
|
||||
loadText("fun foo(l: Long): Double = l.toDouble()");
|
||||
Class<?> mainClass = generateFileClass();
|
||||
Class<?> mainClass = generateFacadeClass();
|
||||
Method main = mainClass.getDeclaredMethod("foo", long.class);
|
||||
double result = (Double) main.invoke(null, 42L);
|
||||
assertTrue(Math.abs(42L - result) <= 1e-9);
|
||||
|
||||
@@ -73,7 +73,7 @@ public class PropertyGenTest extends CodegenTestCase {
|
||||
|
||||
public void testPrivatePropertyInPackage() throws Exception {
|
||||
loadText("private val x = 239");
|
||||
Class<?> nsClass = generateFileClass();
|
||||
Class<?> nsClass = generateFacadeClass();
|
||||
Field[] fields = nsClass.getDeclaredFields();
|
||||
assertEquals(1, fields.length);
|
||||
Field field = fields[0];
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class StringsTest extends CodegenTestCase {
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
|
||||
}
|
||||
|
||||
public void testAnyToString () throws InvocationTargetException, IllegalAccessException {
|
||||
loadText("fun foo(x: Any) = x.toString()");
|
||||
Method foo = generateFunction();
|
||||
assertEquals("something", foo.invoke(null, "something"));
|
||||
}
|
||||
|
||||
public void testNullableAnyToString () throws InvocationTargetException, IllegalAccessException {
|
||||
loadText("fun foo(x: Any?) = x.toString()");
|
||||
Method foo = generateFunction();
|
||||
assertEquals("something", foo.invoke(null, "something"));
|
||||
assertEquals("null", foo.invoke(null, new Object[]{null}));
|
||||
|
||||
}
|
||||
|
||||
public void testNullableStringPlus () throws InvocationTargetException, IllegalAccessException {
|
||||
loadText("fun foo(x: String?, y: Any?) = x + y");
|
||||
String text = generateToText();
|
||||
assertTrue(text.contains(".stringPlus"));
|
||||
Method foo = generateFunction();
|
||||
assertEquals("something239", foo.invoke(null, "something", 239));
|
||||
assertEquals("null239", foo.invoke(null, null, 239));
|
||||
assertEquals("239null", foo.invoke(null, "239", null));
|
||||
assertEquals("nullnull", foo.invoke(null, null, null));
|
||||
|
||||
}
|
||||
|
||||
public void testNonNullableStringPlus () throws InvocationTargetException, IllegalAccessException {
|
||||
loadText("fun foo(x: String, y: Any?) = x + y + 120");
|
||||
String text = generateToText();
|
||||
assertFalse(text.contains(".stringPlus"));
|
||||
Method foo = generateFunction();
|
||||
assertEquals("something239120", foo.invoke(null, "something", 239));
|
||||
assertEquals("239null120", foo.invoke(null, "239", null));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user