package com.njp.coroutinesdemo
import org.junit.Test
import org.junit.Assert.*
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class Test {
private var user: User? = null
/**
* run
* {} 内为this
* 返回值默认Unit,可指定返回
* public inline fun <T, R> T.run(block: T.() -> R): R {
*/
@Test
fun test1() {
user = User("xq", 18)
val run1 = user?.run {
name = "qx"
printLog("run : $this")
this
}
printLog("返回值 : $run1")
printLog("=============华丽分割线==============")
val run2 = user?.run {
name = "qx"
printLog("run : $this")
// this
}
printLog("返回值 : $run2")
}
//==========run : User(name=qx, age=18)===============
//==========返回值 : User(name=qx, age=18)===============
//=======================华丽分割线=============================
//==========run : User(name=qx, age=18)===============
//==========返回值 : kotlin.Unit===============
/**
* let
* {} 内为it
* 返回值默认Unit,可指定返回
* public inline fun <T, R> T.let(block: (T) -> R): R {
*/
@Test
fun test2() {
user = User("xq", 18)
val let1 = user?.let {
it.name = "qx"
printLog("let : $it")
it
}
printLog("返回值 : $let1")
printLog("=============华丽分割线==============")
val let2 = user?.let {
it.name = "qx"
printLog("let : $it")
// it
}
printLog("返回值 : $let2")
}
//==========let : User(name=qx, age=18)===============
//==========返回值 : User(name=qx, age=18)===============
//=======================华丽分割线=============================
//==========let : User(name=qx, age=18)===============
//==========返回值 : kotlin.Unit===============
/**
* apply
* {} 内为this
* 返回值为对象本身,且不需要执行返回
* public inline fun <T> T.apply(block: T.() -> Unit): T {
*/
@Test
fun test4() {
user = User("xq", 18)
val apply1 = user?.apply {
name = "qx"
printLog("apply : $this")
this //多余
}
printLog("返回值 : $apply1")
printLog("=============华丽分割线==============")
val apply2 = user?.apply {
name = "qx"
printLog("apply2 : $this")
// this
}
printLog("返回值 : $apply2")
}
//==========apply : User(name=qx, age=18)===============
//==========返回值 : User(name=qx, age=18)===============
//=======================华丽分割线=============================
//==========apply2 : User(name=qx, age=18)===============
//==========返回值 : User(name=qx, age=18)===============
/**
* also
* {} 内为it
* 返回值为对象本身,且不需要执行返回
* public inline fun <T> T.also(block: (T) -> Unit): T {
*/
@Test
fun test3() {
user = User("xq", 18)
val also1 = user?.also {
it.name = "qx"
printLog("also : $it")
it //多余
}
printLog("$also1")
printLog("=============华丽分割线==============")
val also2 = user?.also {
it.name = "qx"
printLog("also : $it")
// it
}
printLog("$also2")
}
//==========also : User(name=qx, age=18)===============
//==========User(name=qx, age=18)===============
//=======================华丽分割线=============================
//==========also : User(name=qx, age=18)===============
//==========User(name=qx, age=18)===============
fun printLog(s: String) {
println("==========$s===============")
}
}
data class User(var name: String = "", var age: Int = 0)
测试二:
btn1.text = ""
btn1.setTextColor(Color.parseColor("#ff0000"))
btn1.textSize = 18f
//public inline fun <T, R> with(receiver: T, block: T.() -> R): R {
with(btn1) {
text = ""
setTextColor(Color.parseColor("#ff0000"))
textSize = 18f
return@with "返回值自定义"
}
btn1.run {
text = ""
setTextColor(Color.parseColor("#ff0000"))
textSize = 18f
return@run "返回值自定义"
}
btn1.apply {
text = ""
setTextColor(Color.parseColor("#ff0000"))
textSize = 18f
return@apply //只能返回自己
}