JS: create new common directory for all generated tests, migrate several tests there
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
package foo
|
||||
|
||||
fun <T> myRun(f: () -> T) = f()
|
||||
@@ -0,0 +1,27 @@
|
||||
package foo
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
fun test(f: () -> String): String {
|
||||
val funLit = { f() }
|
||||
return funLit()
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
val l = ArrayList<String>()
|
||||
l.add("1 ")
|
||||
l.add("foobar ")
|
||||
l.add("baz")
|
||||
|
||||
val f = {
|
||||
var s = ""
|
||||
for (e in l) s += e
|
||||
s
|
||||
}
|
||||
|
||||
val r = f()
|
||||
if (r != "1 foobar baz") return "$r"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package foo
|
||||
|
||||
fun test(f: () -> String): String {
|
||||
val funLit = { f() }
|
||||
return funLit()
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
return test { "OK" }
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package foo
|
||||
|
||||
val r = "OK"
|
||||
|
||||
fun simple(s: String? = null): String {
|
||||
if (s != null) return s
|
||||
|
||||
return myRun {
|
||||
simple("OK")
|
||||
}
|
||||
}
|
||||
|
||||
val ok = "OK"
|
||||
fun withClosure(s: String? = null): String {
|
||||
if (s != null) return s
|
||||
|
||||
return ok + myRun {
|
||||
withClosure(ok)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (simple("OK") != "OK") return "failed on simple recursion"
|
||||
|
||||
if (withClosure() != ok + ok) return "failed when closure something"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
class A<T>(val a: T) {
|
||||
val foo = { a }
|
||||
}
|
||||
|
||||
fun <T> T.bar() = { this }
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("ok", A("ok").foo())
|
||||
assertEquals("a42", "a42".bar()())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val bar = 12
|
||||
|
||||
val baz = {
|
||||
var result = "test1 "
|
||||
if (true) {
|
||||
val bar = "some text"
|
||||
result += bar
|
||||
}
|
||||
result += bar
|
||||
result
|
||||
}
|
||||
|
||||
val r1 = baz()
|
||||
if (r1 != "test1 some text12") return "r1 = $r1";
|
||||
|
||||
val boo = {
|
||||
var result = "test2 "
|
||||
result += bar
|
||||
if (true) {
|
||||
val bar = 4
|
||||
result += bar
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
val r2 = boo()
|
||||
if (r2 != "test2 124") return "r2 = $r2";
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package foo
|
||||
|
||||
fun funfun(): Boolean {
|
||||
val result = true
|
||||
|
||||
fun foo(): Boolean {
|
||||
fun bar() = result
|
||||
return bar()
|
||||
}
|
||||
|
||||
return foo()
|
||||
}
|
||||
|
||||
fun litlit(): Boolean {
|
||||
val result = true
|
||||
|
||||
return myRun {
|
||||
myRun { result }
|
||||
}
|
||||
}
|
||||
|
||||
fun funlit(): Boolean {
|
||||
val result = true
|
||||
|
||||
fun foo(): Boolean {
|
||||
return myRun { result }
|
||||
}
|
||||
|
||||
return foo()
|
||||
}
|
||||
|
||||
fun litfun(): Boolean {
|
||||
val result = true
|
||||
|
||||
return myRun {
|
||||
fun bar() = result
|
||||
bar()
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (!funfun()) return "funfun failed"
|
||||
if (!litlit()) return "litlit failed"
|
||||
if (!funlit()) return "funlit failed"
|
||||
if (!litfun()) return "litfun failed"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package foo
|
||||
|
||||
class A {
|
||||
val a = 12
|
||||
var b = 1
|
||||
|
||||
fun boo(c: Int) = c
|
||||
|
||||
fun litlit() {
|
||||
val testName = "litlit"
|
||||
myRun {
|
||||
myRun {
|
||||
assertEquals(12, a, testName)
|
||||
|
||||
assertEquals(1, b, testName)
|
||||
b = 23
|
||||
assertEquals(23, b, testName)
|
||||
|
||||
assertEquals(34, boo(34), testName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun funfun() {
|
||||
val testName = "funfun"
|
||||
fun foo() {
|
||||
fun bar() {
|
||||
assertEquals(12, a, testName)
|
||||
|
||||
assertEquals(1, b, testName)
|
||||
b = 23
|
||||
assertEquals(23, b, testName)
|
||||
|
||||
assertEquals(34, boo(34), testName)
|
||||
}
|
||||
bar()
|
||||
}
|
||||
foo()
|
||||
}
|
||||
|
||||
fun litfun() {
|
||||
val testName = "litfun"
|
||||
myRun {
|
||||
fun bar() {
|
||||
assertEquals(12, a, testName)
|
||||
|
||||
assertEquals(1, b, testName)
|
||||
b = 23
|
||||
assertEquals(23, b, testName)
|
||||
|
||||
assertEquals(34, boo(34), testName)
|
||||
}
|
||||
bar()
|
||||
}
|
||||
}
|
||||
|
||||
fun funlit() {
|
||||
val testName = "funlit"
|
||||
fun foo() {
|
||||
myRun {
|
||||
assertEquals(12, a, testName)
|
||||
|
||||
assertEquals(1, b, testName)
|
||||
b = 23
|
||||
assertEquals(23, b, testName)
|
||||
|
||||
assertEquals(34, boo(34), testName)
|
||||
}
|
||||
}
|
||||
foo()
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
A().litlit()
|
||||
A().funfun()
|
||||
A().litfun()
|
||||
A().funlit()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val t = myRun {
|
||||
object {
|
||||
fun boo(param: String): String {
|
||||
return myRun { param }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return t.boo("OK")
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// KT-4218 Nested function literal on singleton object fails
|
||||
|
||||
package foo
|
||||
|
||||
object SomeObject {
|
||||
val values = create()
|
||||
fun create() = Array<Array<String>>(1) { y ->
|
||||
Array<String>(1) { x ->
|
||||
"(${x}, ${y})"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (SomeObject.values[0][0] != "(0, 0)") return SomeObject.values[0][0]
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package foo
|
||||
|
||||
object A {
|
||||
val a = 1
|
||||
fun foo() = 31
|
||||
|
||||
val f = { a + foo() }
|
||||
}
|
||||
|
||||
class B {
|
||||
companion object {
|
||||
val a = 21
|
||||
fun foo() = 3
|
||||
|
||||
val f = { this.a + this.foo() }
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A.f()
|
||||
if (a != 32) return "a != 32, a = $a"
|
||||
|
||||
val b = B.f()
|
||||
if (b != 24) return "b != 24, b = $b"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// KT-4237 With in with
|
||||
|
||||
package foo
|
||||
|
||||
class A {
|
||||
val ok = "OK"
|
||||
}
|
||||
|
||||
class B
|
||||
|
||||
fun <T> with(o: T, body: T.() -> Unit) {
|
||||
o.body()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var o = ""
|
||||
|
||||
with(A()) {
|
||||
with(B()) {
|
||||
o = ok
|
||||
}
|
||||
}
|
||||
|
||||
return o
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// KT-4263 Wrong capturing a function literal variable
|
||||
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var foo = { 1 }
|
||||
var bar = 1
|
||||
|
||||
val t = { "${foo()} $bar" }
|
||||
fun b() = "${foo()} $bar"
|
||||
|
||||
foo = { 2 }
|
||||
bar = 2
|
||||
|
||||
assertEquals("2 2", t())
|
||||
assertEquals("2 2", b())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
fun test(): String {
|
||||
fun f(): String = "OK"
|
||||
|
||||
val funLit = { f() }
|
||||
return funLit()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return test()
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
fun simple(s: String? = null): String {
|
||||
if (s != null) return s
|
||||
|
||||
return myRun {
|
||||
simple("OK")
|
||||
}
|
||||
}
|
||||
|
||||
if (simple("OK") != "OK") return "failed on simple recursion"
|
||||
|
||||
val ok = "OK"
|
||||
fun withClosure(s: String? = null): String {
|
||||
if (s != null) return s
|
||||
|
||||
return ok + myRun {
|
||||
withClosure(ok)
|
||||
}
|
||||
}
|
||||
|
||||
if (withClosure() != ok + ok) return "failed when closure something"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package foo
|
||||
|
||||
class Foo {
|
||||
val OK = "OK";
|
||||
var result: String = ""
|
||||
init {
|
||||
fun bar(s: String? = null) {
|
||||
if (s != null) {
|
||||
result = s
|
||||
return
|
||||
}
|
||||
|
||||
myRun {
|
||||
bar(OK)
|
||||
}
|
||||
}
|
||||
bar();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return Foo().result
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var boo = "OK"
|
||||
var foo = object {
|
||||
val bar = object {
|
||||
val baz = boo
|
||||
}
|
||||
}
|
||||
|
||||
return foo.bar.baz
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
val k = { "K" }
|
||||
|
||||
fun test(): String {
|
||||
val o = { "O" }
|
||||
|
||||
val funLit = { o() + k() }
|
||||
return funLit()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return test()
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package foo
|
||||
|
||||
// workaround for Rhino
|
||||
var n = 0
|
||||
class A {
|
||||
val i = ++n
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
fun A.foo() {
|
||||
fun A.bar() {
|
||||
assertEquals(2, this.i, "check this.i in A.bar()")
|
||||
assertEquals(1, this@foo.i, "check this@foo.i in A.bar()")
|
||||
}
|
||||
val b = { assertEquals(1, this.i, "check this.i in b") }
|
||||
|
||||
assertEquals(1, this.i, "check this.i in A.foo()")
|
||||
A().bar()
|
||||
b()
|
||||
}
|
||||
|
||||
A().foo()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
var i = 0
|
||||
|
||||
fun boo() = 1
|
||||
|
||||
fun f() {
|
||||
for (j in 0..2) {
|
||||
foo {
|
||||
i += j + boo()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun A.bar(): Int {
|
||||
for (u in 4..7) {
|
||||
foo {
|
||||
i += u + boo()
|
||||
}
|
||||
}
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
fun foo(f: () -> Unit) {
|
||||
f()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
a.f()
|
||||
if (a.i != 6) return "fail1: ${a.i}"
|
||||
if (a.bar() != 32) return "fail2: ${a.bar()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
class A {
|
||||
fun foo() = "O"
|
||||
companion object {
|
||||
fun bar() = "K"
|
||||
}
|
||||
|
||||
val f = { foo() + bar() }
|
||||
}
|
||||
|
||||
fun box(): String = A().f()
|
||||
@@ -0,0 +1,35 @@
|
||||
package foo
|
||||
|
||||
class A(val a: String) {
|
||||
fun A.foo() = { a + this.a + this@foo.a + this@A.a }()
|
||||
fun bar(a: A) = this.foo() + " " + a.foo()
|
||||
|
||||
fun A.boo() = { arrayOf(this, this@boo, this@A) }()
|
||||
fun baz(a: A) = this.boo().stringify() + " " + a.boo().stringify()
|
||||
|
||||
fun Array<A>.stringify(): String {
|
||||
var result = ""
|
||||
for (a in this) {
|
||||
result += a.a
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A("a")
|
||||
val b = A("b")
|
||||
|
||||
assertEquals("aaaa aaaa", a.bar(a))
|
||||
assertEquals("aaaa bbba", a.bar(b))
|
||||
assertEquals("bbbb aaab", b.bar(a))
|
||||
assertEquals("bbbb bbbb", b.bar(b))
|
||||
|
||||
assertEquals("aaa aaa", a.baz(a))
|
||||
assertEquals("aaa bba", a.baz(b))
|
||||
assertEquals("bbb aab", b.baz(a))
|
||||
assertEquals("bbb bbb", b.baz(b))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package foo
|
||||
|
||||
// HACKS
|
||||
|
||||
@native
|
||||
const val ROOT = "Kotlin.modules.JS_TESTS"
|
||||
@native
|
||||
const val PATH_TO_F_CREATOR = "foo.B.far\$f"
|
||||
@native
|
||||
const val PATH_TO_G_CREATOR = "foo.B.gar\$f"
|
||||
|
||||
@native("$ROOT.$PATH_TO_F_CREATOR")
|
||||
val F_CREATOR: Any = noImpl
|
||||
@native("$ROOT.$PATH_TO_G_CREATOR")
|
||||
val G_CREATOR: Any = noImpl
|
||||
|
||||
|
||||
// Test
|
||||
|
||||
open class A {
|
||||
fun foo() = "A::foo"
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
fun boo() = "B::boo"
|
||||
|
||||
val far = { foo() }
|
||||
val gar = { boo() }
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
val b = B()
|
||||
val f = b.far
|
||||
val g = b.gar
|
||||
|
||||
assertEquals("A::foo", f())
|
||||
assertEquals("B::boo", g())
|
||||
|
||||
val fs = F_CREATOR.toString()
|
||||
val gs = G_CREATOR.toString().replaceAll("boo", "foo")
|
||||
|
||||
assertEquals(gs, fs)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
|
||||
// Helpers
|
||||
|
||||
@native
|
||||
fun String.replace(regexp: RegExp, replacement: String): String = noImpl
|
||||
|
||||
fun String.replaceAll(regexp: String, replacement: String): String = replace(RegExp(regexp, "g"), replacement)
|
||||
|
||||
@native
|
||||
class RegExp(regexp: String, flags: String)
|
||||
@@ -0,0 +1,63 @@
|
||||
// KT-2388
|
||||
package foo
|
||||
|
||||
// workaround for Rhino
|
||||
class Done(val i: Int)
|
||||
|
||||
var done = Done(0)
|
||||
|
||||
object foo {
|
||||
var result = "FAIL"
|
||||
|
||||
val lambda = {
|
||||
result = "foo.lambda OK"
|
||||
done = Done(3)
|
||||
}
|
||||
|
||||
val extLambda: Done.() -> Unit = {
|
||||
result = "foo.extLambda OK"
|
||||
done = this
|
||||
}
|
||||
}
|
||||
|
||||
class Foo {
|
||||
var result = "FAIL"
|
||||
|
||||
val lambda = {
|
||||
result = "Foo::lambda OK"
|
||||
done = Done(-7)
|
||||
}
|
||||
|
||||
val extLambda: Done.() -> Unit = {
|
||||
result = "Foo::extLambda OK"
|
||||
done = this
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = foo.lambda
|
||||
val b = foo.extLambda
|
||||
|
||||
val f = Foo()
|
||||
val c = f.lambda
|
||||
val d = f.extLambda
|
||||
|
||||
a()
|
||||
if (foo.result != "foo.lambda OK") return "foo.result = \"${foo.result}\", but expected \"foo.lambda OK\""
|
||||
if (done.i != 3) return "done.i = ${done.i}, but expected 3"
|
||||
|
||||
Done(23).b()
|
||||
if (foo.result != "foo.extLambda OK") return "foo.result = \"${foo.result}\", but expected \"foo.extLambda OK\""
|
||||
if (done.i != 23) return "done.i = ${done.i}, but expected 23"
|
||||
|
||||
|
||||
c()
|
||||
if (f.result != "Foo::lambda OK") return "a.result = \"${f.result}\", but expected \"Foo::lambda OK\""
|
||||
if (done.i != -7) return "done.i = ${done.i}, but expected -7"
|
||||
|
||||
Done(71).d()
|
||||
if (f.result != "Foo::extLambda OK") return "a.result = \"${f.result}\", but expected \"Foo::extLambda OK\""
|
||||
if (done.i != 71) return "done.i = ${done.i}, but expected 71"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// KT-4600 Generated wrong code when capturing `this` in extension function inside a method
|
||||
|
||||
package foo
|
||||
|
||||
public class Foo(val trigger: () -> Any) {
|
||||
fun test() = myRun { trigger() };
|
||||
}
|
||||
|
||||
fun box() = Foo({ "OK" }).test()
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
class B(val b: String)
|
||||
|
||||
class A(val a: String) {
|
||||
fun B.A() = { a + b }
|
||||
|
||||
fun foo(a: B) = a.A()()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val bar = A("bar")
|
||||
val baz = B("baz")
|
||||
|
||||
val r = bar.foo(baz)
|
||||
if (r != "barbaz") return "$r";
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// KT-4207 Closure this doesn't work in JS backend
|
||||
|
||||
package foo
|
||||
|
||||
fun String.foo() = { this }
|
||||
|
||||
class A {
|
||||
fun foo() = { this }
|
||||
val ok = "OK"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if ("OK".foo()() != "OK") return "\"OK\".foo()() != \"OK\""
|
||||
if (A().foo()().ok != "OK") return "A().foo()().ok != \"OK\""
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
object A {
|
||||
val x = 23
|
||||
val y = { x }
|
||||
fun foo() = { x }
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(23, A.foo()())
|
||||
assertEquals(23, A.y())
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package foo
|
||||
|
||||
class Foo {
|
||||
val o = "O"
|
||||
val k = "K"
|
||||
fun test(): String {
|
||||
fun bar() = o
|
||||
fun Int.baz() = k + this
|
||||
|
||||
val boo = { k }
|
||||
val cux: Int.()->String = { o + this }
|
||||
|
||||
return bar() + 17.baz() + 23.cux() + boo()
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = Foo().test()
|
||||
if (a != "OK17O23K") return "$a"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package foo
|
||||
|
||||
val f = true
|
||||
|
||||
fun box(): String {
|
||||
val bar = "test "
|
||||
val boo = "another "
|
||||
|
||||
fun baz(): String {
|
||||
var result = bar
|
||||
|
||||
if (f) {
|
||||
val bar = 42
|
||||
result += bar
|
||||
|
||||
val boo = 7
|
||||
result += boo
|
||||
}
|
||||
|
||||
result += boo
|
||||
result += bar
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
val r = baz()
|
||||
if (r != "test 427another test ") return r;
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package foo
|
||||
|
||||
val f = true
|
||||
|
||||
fun box(): String {
|
||||
var bar = ""
|
||||
var boo = 23
|
||||
|
||||
fun baz() {
|
||||
bar += "test "
|
||||
|
||||
if (f) {
|
||||
val v1 = 42
|
||||
var bar = 12
|
||||
bar += v1
|
||||
|
||||
val v2 = 7
|
||||
var boo = ""
|
||||
boo += v2
|
||||
}
|
||||
|
||||
boo += 7
|
||||
bar += "text"
|
||||
}
|
||||
|
||||
baz()
|
||||
if (bar != "test text") return "bar != \"test text\", bar = \"$bar\"";
|
||||
if (boo != 30) return "boo != 61, boo = $boo";
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
fun test(): Int {
|
||||
open class B(open val x: Int) {
|
||||
inner class C(x: Int) : B(x * 10) {
|
||||
inner class D() {
|
||||
var baz: () -> Int = { 0 }
|
||||
constructor(b: Boolean) : this() {
|
||||
baz = { x + this@B.x }
|
||||
}
|
||||
fun bar() = { 100 * (x + this@B.x) }
|
||||
}
|
||||
}
|
||||
}
|
||||
return B(3).C(2).D().bar()() + B(5).C(4).D(true).baz()
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(2345, A().test())
|
||||
return "OK"
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
fun test(): Int {
|
||||
open class B(open val x: Int) {
|
||||
inner class C(x: Int) : B(x * 10) {
|
||||
inner class D() {
|
||||
fun baz() = bar()
|
||||
}
|
||||
|
||||
fun D.bar() = { x + this@B.x }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return B(3).C(2).D().baz()()
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(23, A().test())
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package foo
|
||||
|
||||
open class A(private val x: String) {
|
||||
fun foo(): String {
|
||||
class B : A("fail1: simple nested") {
|
||||
fun bar() = x
|
||||
}
|
||||
return B().bar()
|
||||
}
|
||||
}
|
||||
|
||||
open class A1(private val x: String) {
|
||||
fun foo(): String {
|
||||
class B1 {
|
||||
fun bar(): String {
|
||||
class C1 : A1("fail2: deeply nested") {
|
||||
fun baz() = x
|
||||
}
|
||||
return C1().baz()
|
||||
}
|
||||
}
|
||||
return B1().bar()
|
||||
}
|
||||
}
|
||||
|
||||
open class A2(private val x: String) {
|
||||
fun foo(): String {
|
||||
class B2 : A2("fail3: deeply nested") {
|
||||
fun bar(): String {
|
||||
class C2 {
|
||||
fun baz() = x
|
||||
}
|
||||
return C2().baz()
|
||||
}
|
||||
}
|
||||
return B2().bar()
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val result = A("OK").foo()
|
||||
if (result != "OK") return result
|
||||
|
||||
val result1 = A1("OK").foo()
|
||||
if (result1 != "OK") return result1
|
||||
|
||||
val result2 = A2("OK").foo()
|
||||
if (result2 != "OK") return result2
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package foo
|
||||
|
||||
class A {
|
||||
fun test() = 23
|
||||
}
|
||||
|
||||
fun <T : A> T.foo(): Int {
|
||||
class B {
|
||||
fun foo() = test()
|
||||
}
|
||||
return B().foo()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(23, A().foo())
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package foo
|
||||
|
||||
import java.util.*
|
||||
|
||||
fun box(): String {
|
||||
val oneTwo = Array(2) {
|
||||
it + 1
|
||||
}
|
||||
val a = ArrayList<() -> Int>()
|
||||
for (i in oneTwo) {
|
||||
for (j in 1..2) {
|
||||
a.add({
|
||||
var res = 0
|
||||
for (t in 0..2) {
|
||||
res += i * j
|
||||
}
|
||||
res
|
||||
})
|
||||
}
|
||||
}
|
||||
var sum = 0
|
||||
for (f in a) {
|
||||
sum += f()
|
||||
}
|
||||
|
||||
if (sum != 27) return "fail: $sum"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var log = ""
|
||||
|
||||
var s: Any? = null
|
||||
for (t in arrayOf("1", "2", "3")) {
|
||||
fun foo() = {
|
||||
fun q() = { t }
|
||||
q()
|
||||
}
|
||||
|
||||
if (s == null) {
|
||||
s = foo()
|
||||
}
|
||||
|
||||
log += (s as (() -> (() -> String)))()()
|
||||
}
|
||||
|
||||
if (log != "111") return "fail1: ${log}"
|
||||
|
||||
s = null
|
||||
log = ""
|
||||
for (t in arrayOf("1", "2", "3")) {
|
||||
fun foo() = {
|
||||
val y = t
|
||||
fun q() = { y }
|
||||
q()
|
||||
}
|
||||
|
||||
if (s == null) {
|
||||
s = foo()
|
||||
}
|
||||
|
||||
log += (s as (() -> (() -> String)))()()
|
||||
}
|
||||
|
||||
if (log != "111") return "fail2: ${log}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
interface B {
|
||||
fun result(): Int
|
||||
}
|
||||
|
||||
class A(private val x: Int) {
|
||||
fun test() = object : B {
|
||||
val y = x + 1
|
||||
|
||||
override fun result() = x * 10 + y
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(23, A(2).test().result())
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package foo
|
||||
|
||||
import java.util.*
|
||||
|
||||
fun box(): String {
|
||||
val oneTwo = Array(2) {
|
||||
it + 1
|
||||
}
|
||||
val a = ArrayList<() -> Int>()
|
||||
for (i in oneTwo) {
|
||||
for (l in 1..2) {
|
||||
val j = l
|
||||
a.add({
|
||||
var res = 0
|
||||
for (t in 0..2) {
|
||||
res += i * j
|
||||
}
|
||||
res
|
||||
})
|
||||
}
|
||||
}
|
||||
var sum = 0
|
||||
for (f in a) {
|
||||
sum += f()
|
||||
}
|
||||
|
||||
if (sum != 27) return "fail: $sum"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package foo
|
||||
|
||||
object O {
|
||||
val result = "O"
|
||||
}
|
||||
|
||||
operator fun O.invoke() = result
|
||||
|
||||
|
||||
class A(val x: Int) {
|
||||
companion object {
|
||||
val result = "A"
|
||||
}
|
||||
}
|
||||
|
||||
operator fun A.Companion.invoke() = result
|
||||
|
||||
|
||||
enum class B {
|
||||
E {
|
||||
val result = "B"
|
||||
|
||||
override operator fun invoke() = result
|
||||
};
|
||||
|
||||
abstract operator fun invoke(): String
|
||||
}
|
||||
|
||||
fun f() = { O() + A() + B.E() }
|
||||
|
||||
fun box(): String {
|
||||
val result = f()()
|
||||
if (result != "OAB") return "expected 'OAB', got '$result'"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package foo
|
||||
|
||||
fun Int.foo(a: Int): Int {
|
||||
if (a == 0) {
|
||||
return this.foo(4) + foo(7) + this
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(34, 23.foo(0))
|
||||
|
||||
fun Int.bar(a: Int): Int {
|
||||
if (a == 0) {
|
||||
return this.bar(12) + bar(4) + this
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
assertEquals(19, 3.bar(0))
|
||||
|
||||
fun f() = 11
|
||||
val v = 3
|
||||
fun Int.baz(a: Int): Int {
|
||||
if (a == 0) {
|
||||
return this.baz(v) + baz(f()) + this
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
assertEquals(21, 7.baz(0))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
fun bar(i: Int = 0): Int = if (i == 7) i else bar(i - 1)
|
||||
|
||||
fun box(): String {
|
||||
val a = bar(10)
|
||||
if (a != 7) return "bar(10) = $a, but expected 7"
|
||||
|
||||
fun boo(i: Int = 0): Int = if (i == 4) i else boo(i - 1)
|
||||
val b = boo(17)
|
||||
if (b != 4) return "boo(17) = $b, but expected 4"
|
||||
|
||||
fun f() = 1
|
||||
val v = 3
|
||||
fun baz(i: Int = 0): Int = if (i == v) f() + v else baz(i - 1)
|
||||
|
||||
val c = baz(10)
|
||||
if (c != 4) return "baz(10) = $c, but expected 4"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package foo
|
||||
|
||||
fun bar(i: Int = 0): Int {
|
||||
if (i == 7) {
|
||||
val bar = i
|
||||
return bar
|
||||
}
|
||||
else {
|
||||
return bar(i - 1)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = bar(10)
|
||||
if (a != 7) return "bar(10) = $a, but expected 7"
|
||||
|
||||
fun boo(i: Int = 0): Int {
|
||||
if (i == 4) {
|
||||
val boo = i
|
||||
return boo
|
||||
} else {
|
||||
return boo(i - 1)
|
||||
}
|
||||
}
|
||||
val b = boo(17)
|
||||
if (b != 4) return "boo(17) = $b, but expected 4"
|
||||
|
||||
fun f() = 1
|
||||
val v = 3
|
||||
fun baz(i: Int = 0): Int {
|
||||
if (i == v) {
|
||||
val baz = f() + v
|
||||
return baz
|
||||
} else {
|
||||
return baz(i - 1)
|
||||
}
|
||||
}
|
||||
|
||||
val c = baz(10)
|
||||
if (c != 4) return "baz(10) = $c, but expected 4"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val t = myRun {
|
||||
object {
|
||||
fun foo() = "3"
|
||||
|
||||
fun boo(param: String): String {
|
||||
val a = object {
|
||||
fun bar() = "57"
|
||||
fun b(): String = myRun { param + bar() + foo() }
|
||||
}
|
||||
|
||||
return a.b()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val r = t.boo("OK")
|
||||
if (r != "OK573") return "r != \"OK573\", r = \"$r\""
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package foo
|
||||
|
||||
fun Any.foo(n: Int): () -> Boolean {
|
||||
var count = n
|
||||
return { --count >= 0 }
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return if (1.foo(3)() && !1.foo(0)()) "OK" else "fail"
|
||||
}
|
||||
Reference in New Issue
Block a user