[JS IR] Tests for TS declarations of sealed classes

Also make sure we generate proper declarations for abstract and data
classes.

#KT-39364 Fixed
#KT-47376 Fixed
This commit is contained in:
Sergej Jaskiewicz
2021-09-02 19:37:05 +03:00
committed by TeamCityServer
parent 5530080a7d
commit b3c681ee42
3 changed files with 120 additions and 2 deletions
@@ -55,5 +55,48 @@ declare namespace JS_TESTS {
readonly x: number;
};
}
class TestSealed {
protected constructor(name: string);
readonly name: string;
}
namespace TestSealed {
class AA extends foo.TestSealed {
constructor();
bar(): string;
}
class BB extends foo.TestSealed {
constructor();
baz(): string;
}
}
abstract class TestAbstract {
constructor(name: string);
readonly name: string;
}
namespace TestAbstract {
class AA extends foo.TestAbstract {
constructor();
bar(): string;
}
class BB extends foo.TestAbstract {
constructor();
baz(): string;
}
}
class TestDataClass {
constructor(name: string);
readonly name: string;
component1(): string;
copy(name: string): foo.TestDataClass;
toString(): string;
hashCode(): number;
equals(other: Nullable<any>): boolean;
}
namespace TestDataClass {
class Nested {
constructor();
readonly prop: string;
}
}
}
}
@@ -3,71 +3,98 @@
// SKIP_MINIFICATION
// SKIP_NODE_JS
@file:JsExport
package foo
// TODO: Test the same for member functions:
@JsExport
fun sum(x: Int, y: Int): Int =
x + y
@JsExport
fun varargInt(vararg x: Int): Int =
x.size
@JsExport
fun varargNullableInt(vararg x: Int?): Int =
x.size
@JsExport
fun varargWithOtherParameters(x: String, vararg y: String, z: String): Int =
x.length + y.size + z.length
@JsExport
fun varargWithComplexType(vararg x: (Array<IntArray>) -> Array<IntArray>): Int =
x.size
@JsExport
fun sumNullable(x: Int?, y: Int?): Int =
(x ?: 0) + (y ?: 0)
@JsExport
fun defaultParameters(x: Int = 10, y: String = "OK"): String =
x.toString() + y
@JsExport
fun <T> generic1(x: T): T = x
@JsExport
fun <T> generic2(x: T?): Boolean = (x == null)
@JsExport
fun <A, B, C, D, E> generic3(a: A, b: B, c: C, d: D): E? = null
@JsExport
inline fun inlineFun(x: Int, callback: (Int) -> Unit) {
callback(x)
}
// Properties
@JsExport
const val _const_val: Int = 1
@JsExport
val _val: Int = 1
@JsExport
var _var: Int = 1
@JsExport
val _valCustom: Int
get() = 1
@JsExport
val _valCustomWithField: Int = 1
get() = field + 1
@JsExport
var _varCustom: Int
get() = 1
set(value) {}
@JsExport
var _varCustomWithField: Int = 1
get() = field * 10
set(value) { field = value * 10 }
// Classes
@JsExport
class A
@JsExport
class A1(val x: Int)
@JsExport
class A2(val x: String, var y: Boolean)
@JsExport
class A3 {
val x: Int = 100
}
@JsExport
class A4 {
val _valCustom: Int
get() = 1
@@ -85,19 +112,52 @@ class A4 {
}
@JsExport
object O0
@JsExport
object O {
val x = 10
@JsName("foo") // TODO: Should work without JsName
fun foo() = 20
}
@JsExport
fun takesO(o: O): Int =
O.x + O.foo()
@JsExport
class KT_37829 {
companion object {
val x = 10
}
}
// See KT-47376, KT-39364
@JsExport
sealed class TestSealed(val name: String) {
class AA : TestSealed("AA") {
fun bar(): String = "bar"
}
class BB : TestSealed("BB") {
fun baz(): String = "baz"
}
}
// See KT-39364
@JsExport
abstract class TestAbstract(val name: String) {
class AA : TestAbstract("AA") {
fun bar(): String = "bar"
}
class BB : TestAbstract("BB") {
fun baz(): String = "baz"
}
}
@JsExport
data class TestDataClass(val name: String) {
class Nested {
val prop: String = "hello"
}
}
@@ -23,6 +23,9 @@ import A4 = JS_TESTS.foo.A4;
import O = JS_TESTS.foo.O;
import takesO = JS_TESTS.foo.takesO;
import KT_37829 = JS_TESTS.foo.KT_37829;
import TestSealed = JS_TESTS.foo.TestSealed;
import TestAbstract = JS_TESTS.foo.TestAbstract;
import TestDataClass = JS_TESTS.foo.TestDataClass;
function assert(condition: boolean) {
if (!condition) {
@@ -95,5 +98,17 @@ function box(): string {
assert(KT_37829.Companion.x == 10);
assert(new TestSealed.AA().name == "AA");
assert(new TestSealed.AA().bar() == "bar");
assert(new TestSealed.BB().name == "BB");
assert(new TestSealed.BB().baz() == "baz");
assert(new TestAbstract.AA().name == "AA");
assert(new TestAbstract.AA().bar() == "bar");
assert(new TestAbstract.BB().name == "BB");
assert(new TestAbstract.BB().baz() == "baz");
assert(new TestDataClass.Nested().prop == "hello");
return "OK";
}