You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core-kotlin-modules/core-kotlin-lang-oop-3/src/test/kotlin/com/baeldung/implicitAndQualifiedthis/ImplicitAndQualifiedthisUnitTest.kt
+13-2Lines changed: 13 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -13,9 +13,9 @@ class ImplicitAndQualifiedthisUnitTest {
13
13
val x =5
14
14
15
15
classOuter {
16
+
funfoo() : Int=30
16
17
val x =0
17
18
val b =this
18
-
funfoo() : Int=30
19
19
20
20
funprintLine() ="Member function"
21
21
@@ -35,6 +35,7 @@ class ImplicitAndQualifiedthisUnitTest {
35
35
val x =1
36
36
37
37
funcheckInner(){
38
+
38
39
val funLit =funString.() {
39
40
assertEquals("test.funLit()", this) // Implicit: receiver of the function literal (the String itself)
40
41
}
@@ -51,15 +52,19 @@ class ImplicitAndQualifiedthisUnitTest {
51
52
52
53
fun Int.foo() {
53
54
val x =2
55
+
val y =this
56
+
assertEquals("Int", y::class.simpleName)
57
+
54
58
assertEquals(2, x)
55
59
assertEquals(1, this@Inner.x) // Qualified: specifies x of the Inner class
56
60
assertEquals(42, this) // Implicit: receiver of the extension function (the Int value 42)
57
61
assertEquals(42, this@foo) // Qualified: specifies the receiver of the extension function (42)
58
62
59
63
assertEquals(this, this@foo) // Both this keywords refer to the same receiver (42)
60
64
61
-
assertEquals(Outer::class.java.name, this@Outer::class.java.name) // Qualified: Outer class
62
65
assertEquals(Inner::class.java.name, this@Inner::class.java.name) // Qualified: Inner class
66
+
assertEquals(Outer::class.java.name, this@Outer::class.java.name) // Qualified: Outer class
67
+
63
68
64
69
assertEquals(0, this@Outer.x) // Qualified: Outer's x property
65
70
assertEquals(30, this@Outer.foo()) // Qualified: Outer's foo() function
@@ -76,6 +81,11 @@ class ImplicitAndQualifiedthisUnitTest {
76
81
}
77
82
78
83
funExtLambda("test funExtLambda")
84
+
85
+
val increase =fun(x:Int): Int {
86
+
returnthis+ x
87
+
}
88
+
assertEquals(43, increase(1))
79
89
}
80
90
}
81
91
}
@@ -89,6 +99,7 @@ class ImplicitAndQualifiedthisUnitTest {
89
99
inner.run {
90
100
42.foo() // Calls the extension function defined inside Inner
0 commit comments