tests: Update external tests (1.1.2-dev-393)
This commit is contained in:
+19
@@ -0,0 +1,19 @@
|
||||
var result = ""
|
||||
|
||||
fun getReceiver() : Int {
|
||||
result += "getReceiver->"
|
||||
return 1
|
||||
}
|
||||
|
||||
fun getFun(b : Int.(Int)->Unit): Int.(Int)->Unit {
|
||||
result += "getFun()->"
|
||||
return b
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
getReceiver().(getFun({ result +="End" }))(1)
|
||||
|
||||
if(result != "getFun()->getReceiver->End") return "fail $result"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
//KT-1061 Can't call function defined as a val
|
||||
|
||||
object X {
|
||||
val doit = { i: Int -> i }
|
||||
}
|
||||
|
||||
fun box() : String = if (X.doit(3) == 3) "OK" else "fail"
|
||||
@@ -0,0 +1,12 @@
|
||||
//KT-1249 IllegalStateException invoking function property
|
||||
class TestClass(val body : () -> Unit) : Any() {
|
||||
fun run() {
|
||||
body()
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
TestClass({}).run()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
//KT-1290 Method property in constructor causes NPE
|
||||
|
||||
class Foo<T>(val filter: (T) -> Boolean) {
|
||||
public fun bar(tee: T) : Boolean {
|
||||
return filter(tee);
|
||||
}
|
||||
}
|
||||
|
||||
fun foo() = Foo({ i: Int -> i < 5 }).bar(2)
|
||||
|
||||
fun box() : String {
|
||||
if (!foo()) return "fail"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
fun test1(f: (Int) -> Int) = f(1)
|
||||
|
||||
fun test2(f: Int.() -> Int) = 2.f()
|
||||
|
||||
class A(val foo: Int.() -> Int)
|
||||
|
||||
fun box(): String {
|
||||
val a: (Int) -> Int = { it }
|
||||
val b: Int.() -> Int = { this }
|
||||
|
||||
if (test1(a) != 1) return "fail 1a"
|
||||
if (test1(b) != 1) return "fail 1b"
|
||||
if (test2(a) != 2) return "fail 2a"
|
||||
if (test2(b) != 2) return "fail 2b"
|
||||
|
||||
val x = A({ this })
|
||||
|
||||
if (x.foo(3) != 3) return "fail 3"
|
||||
if (with(x) { foo(4) } != 4) return "fail 4"
|
||||
if (with(x) { 5.foo() } != 5) return "fail 5"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
interface Expr {
|
||||
public fun ttFun() : Int = 12
|
||||
}
|
||||
|
||||
class Num(val value : Int) : Expr
|
||||
|
||||
fun Expr.sometest() : Int {
|
||||
if (this is Num) {
|
||||
value
|
||||
return value
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
fun box() : String {
|
||||
if (Num(11).sometest() != 11) return "fail ${Num(11).sometest()}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun box(): String {
|
||||
val sb = StringBuilder()
|
||||
operator fun String.unaryPlus() {
|
||||
sb.append(this)
|
||||
}
|
||||
|
||||
+"OK"
|
||||
return sb.toString()!!
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
class A {
|
||||
private val sb: StringBuilder = StringBuilder()
|
||||
|
||||
operator fun String.unaryPlus() {
|
||||
sb.append(this)
|
||||
}
|
||||
|
||||
fun foo(): String {
|
||||
+"OK"
|
||||
return sb.toString()!!
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String = A().foo()
|
||||
@@ -0,0 +1,32 @@
|
||||
var sayResult = ""
|
||||
|
||||
class NoiseMaker {
|
||||
fun say(str: String) { sayResult += str }
|
||||
}
|
||||
|
||||
fun noiseMaker(f: NoiseMaker.() -> Unit) {
|
||||
val noiseMaker = NoiseMaker()
|
||||
noiseMaker.f()
|
||||
}
|
||||
|
||||
abstract class Pet {
|
||||
fun <T> NoiseMaker.playWith(friend: T) {
|
||||
say("Playing with " + friend)
|
||||
}
|
||||
|
||||
abstract fun play(): Unit
|
||||
}
|
||||
|
||||
class Doggy(): Pet() {
|
||||
override fun play() = noiseMaker {
|
||||
say("Time to play! ")
|
||||
playWith("my owner!")
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
Doggy().play()
|
||||
if (sayResult != "Time to play! Playing with my owner!") return "fail: $sayResult"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
var result = ""
|
||||
fun result(r: String) { result = r }
|
||||
|
||||
object Foo {
|
||||
private operator fun String.unaryPlus() = "(" + this + ")"
|
||||
|
||||
fun foo() = { result(+"Stuff") }()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
Foo.foo()
|
||||
return if (result == "(Stuff)") "OK" else "Fail $result"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fun test(cl: Int.() -> Int):Int = 11.cl()
|
||||
|
||||
class Foo {
|
||||
val a = test { this }
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (Foo().a != 11) return "fail"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
var result = "Fail"
|
||||
|
||||
class A
|
||||
|
||||
operator fun A.inc(s: String = "OK"): A {
|
||||
result = s
|
||||
return this
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var a = A()
|
||||
a++
|
||||
if (result != "OK") return "Fail 1"
|
||||
|
||||
result = "Fail"
|
||||
++a
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
class A {
|
||||
companion object
|
||||
}
|
||||
|
||||
val foo: Any.() -> Unit = {}
|
||||
|
||||
fun test() {
|
||||
A.(foo)()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
test()
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
fun box() : String {
|
||||
val array = ArrayList<String>()
|
||||
array.add("0")
|
||||
array.add("1")
|
||||
array.add("2")
|
||||
array.last = "5"
|
||||
return if(array.length == 3 && array.last == "5") "OK" else "fail"
|
||||
}
|
||||
|
||||
var <T> ArrayList<T>.length : Int
|
||||
get() = size
|
||||
set(value: Int) = throw Error()
|
||||
|
||||
var <T> ArrayList<T>.last : T
|
||||
get() = get(size-1)!!
|
||||
set(el : T) { set(size-1, el) }
|
||||
@@ -0,0 +1,20 @@
|
||||
fun String.foo() : String {
|
||||
fun Int.bar() : String {
|
||||
fun Long.baz() : String {
|
||||
val x = this@foo
|
||||
val y = this@bar
|
||||
val z = this@baz
|
||||
return "$x $y $z"
|
||||
}
|
||||
return 0L.baz()
|
||||
}
|
||||
return 42.bar()
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
val result = "OK".foo()
|
||||
|
||||
if (result != "OK 42 0") return "fail: $result"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// TODO: muted automatically, investigate should it be ran for JS or not
|
||||
// IGNORE_BACKEND: JS, NATIVE
|
||||
|
||||
package kt606
|
||||
|
||||
//KT-606 wrong resolved call
|
||||
|
||||
class StandardPipelineFactory(val config : ChannelPipeline.() -> Unit) : ChannelPipelineFactory {
|
||||
override fun getPipeline() : ChannelPipeline {
|
||||
val pipeline : ChannelPipeline = DefaultChannelPipeline()
|
||||
pipeline.config()
|
||||
return pipeline
|
||||
}
|
||||
}
|
||||
|
||||
interface ChannelPipeline {
|
||||
fun print(any: Any)
|
||||
}
|
||||
|
||||
class DefaultChannelPipeline : ChannelPipeline {
|
||||
override fun print(any: Any) {
|
||||
System.out?.println(any)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface ChannelPipelineFactory {
|
||||
fun getPipeline() : ChannelPipeline
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
StandardPipelineFactory({ print("OK") }).getPipeline()
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
class Template() {
|
||||
val collected = ArrayList<String>()
|
||||
|
||||
operator fun String.unaryPlus() {
|
||||
collected.add(this@unaryPlus)
|
||||
}
|
||||
|
||||
fun test() {
|
||||
+ "239"
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
val u = Template()
|
||||
u.test()
|
||||
return if(u.collected.size == 1 && u.collected.get(0) == "239") "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun box() : String {
|
||||
val y = 12
|
||||
val op = { x:Int -> (x + y).toString() }
|
||||
|
||||
val op2 : Int.(Int) -> String = { op(this + it) }
|
||||
|
||||
return if("27" == 5.op2(10)) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
infix fun <T> T.mustBe(t : T) {
|
||||
assert("$this must be $t") {this == t}
|
||||
}
|
||||
|
||||
inline fun assert(message : String, condition : () -> Boolean) {
|
||||
if (!condition())
|
||||
throw AssertionError(message)
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
"lala" mustBe "lala"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun StringBuilder.first() = this.get(0)
|
||||
|
||||
fun foo() = StringBuilder("foo").first()
|
||||
|
||||
fun box() = if (foo() == 'f') "OK" else "Fail ${foo()}"
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
class Test {
|
||||
private fun <T : Any> T.self() = object{
|
||||
fun calc() : T {
|
||||
return this@self
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : Int {
|
||||
return 1.self().calc() + 1
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
return if (Test().box() == 2) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
class Request(val path: String) {
|
||||
|
||||
}
|
||||
|
||||
class Handler() {
|
||||
fun Int.times(op: ()-> Unit) {
|
||||
for(i in 0..this)
|
||||
op()
|
||||
}
|
||||
|
||||
// fun Request.getPath() : String {
|
||||
// val sb = java.lang.StringBuilder()
|
||||
// 10.times {
|
||||
// sb.append(path)?.append(this)
|
||||
// }
|
||||
// return sb.toString() as String
|
||||
// }
|
||||
|
||||
fun Request.getPath() = path
|
||||
|
||||
fun test(request: Request) = request.getPath()
|
||||
}
|
||||
|
||||
fun box() : String = if(Handler().test(Request("239")) == "239") "OK" else "fail"
|
||||
@@ -0,0 +1,27 @@
|
||||
// TODO: muted automatically, investigate should it be ran for JS or not
|
||||
// IGNORE_BACKEND: JS
|
||||
|
||||
fun StringBuilder.takeFirst(): Char {
|
||||
if (this.length == 0) return 0.toChar()
|
||||
val c = this.get(0)
|
||||
this.deleteCharAt(0)
|
||||
return c
|
||||
}
|
||||
|
||||
fun foo(expr: StringBuilder): Int {
|
||||
val c = expr.takeFirst()
|
||||
when(c) {
|
||||
0.toChar() -> throw Exception("zero")
|
||||
else -> throw Exception("nonzero" + c)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
try {
|
||||
foo(StringBuilder())
|
||||
return "Fail"
|
||||
}
|
||||
catch (e: Exception) {
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user