Skip to content

Commit 3ed2fc7

Browse files
committed
skip
1 parent 11cdc2d commit 3ed2fc7

File tree

1 file changed

+57
-23
lines changed

1 file changed

+57
-23
lines changed

core-kotlin-modules/core-kotlin-lang-oop-3/src/test/kotlin/com/baeldung/implicitAndQualifiedthis/ImplicitAndQualifiedthisUnitTest.kt

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,86 @@ class ImplicitAndQualifiedthisUnitTest {
1010
fun `Qualified this and implicit this Test`() {
1111

1212
fun printLine() = "Top-level function"
13+
val x = 5
1314

1415
class Outer {
16+
val x = 0
17+
val b = this
18+
fun foo() : Int = 30
19+
20+
fun printLine() = "Member function"
1521

1622
fun checkThis() {
17-
val b = this
23+
val b = this // Implicit: refers to the current instance of Outer
1824
assertEquals(Outer::class.java.name, b::class.java.name)
25+
26+
assertEquals(1, this.x + 1) // Implicit: refers to the x property of the current Outer instance
27+
assertEquals(6, x + 1) // Implicit: refers to the x property of the current Outer instance
28+
29+
assertEquals("Member function", this.printLine())
30+
assertEquals("Top-level function", printLine())
1931
}
2032

2133
inner class Inner {
2234

23-
fun Int.foo() {
24-
25-
assertEquals(42, this)
26-
assertEquals(42, this@foo)
27-
assertEquals(this, this@foo)
28-
assertEquals(Outer::class.java.name, this@Outer::class.java.name)
29-
assertEquals(Inner::class.java.name, this@Inner::class.java.name)
35+
val x = 1
3036

37+
fun checkInner(){
3138
val funLit = fun String.() {
32-
assertEquals("test.funLit()", this)
39+
assertEquals("test.funLit()", this) // Implicit: receiver of the function literal (the String itself)
3340
}
3441

3542
"test.funLit()".funLit()
3643

37-
val funLitNoReceiver = { s: String ->
38-
assertEquals("test funLitNoReceiver", s)
39-
assertEquals(42, this)
44+
val funInnerLambda = { _: String ->
45+
val r = this
46+
assertEquals(Inner::class.java.name, r::class.java.name)
4047
}
4148

42-
funLitNoReceiver("test funLitNoReceiver")
49+
funInnerLambda("test inner lambda")
4350
}
44-
}
4551

46-
fun printLine() = "Member function"
52+
fun Int.foo() {
53+
val x = 2
54+
assertEquals(2, x)
55+
assertEquals(1, this@Inner.x) // Qualified: specifies x of the Inner class
56+
assertEquals(42, this) // Implicit: receiver of the extension function (the Int value 42)
57+
assertEquals(42, this@foo) // Qualified: specifies the receiver of the extension function (42)
58+
59+
assertEquals(this, this@foo) // Both this keywords refer to the same receiver (42)
60+
61+
assertEquals(Outer::class.java.name, this@Outer::class.java.name) // Qualified: Outer class
62+
assertEquals(Inner::class.java.name, this@Inner::class.java.name) // Qualified: Inner class
63+
64+
assertEquals(0, this@Outer.x) // Qualified: Outer's x property
65+
assertEquals(30, this@Outer.foo()) // Qualified: Outer's foo() function
4766

48-
fun invokePrintLine(omitThis: Boolean = false): String {
49-
return if (omitThis) printLine() else this.printLine()
67+
val funLit = fun String.() {
68+
assertEquals("test.funLit()", this) // Implicit: receiver of the function literal (the String itself)
69+
}
70+
71+
"test.funLit()".funLit()
72+
73+
val funExtLambda = { _: String ->
74+
val r = this
75+
assertEquals("Int", r::class.simpleName) // Implicit: this inside a lambda with no receiver refers to the enclosing context (Int.foo())
76+
}
77+
78+
funExtLambda("test funExtLambda")
79+
}
5080
}
5181
}
5282

53-
val inner = Outer().Inner()
54-
inner.run { 42.foo() }
55-
56-
Outer().checkThis()
83+
val outer = Outer()
84+
outer.checkThis()
85+
outer.foo()
5786

58-
assertEquals("Member function", Outer().invokePrintLine())
59-
assertEquals("Top-level function", Outer().invokePrintLine(omitThis = true))
87+
val inner = outer.Inner()
88+
inner.checkInner()
89+
inner.run {
90+
42.foo() // Calls the extension function defined inside Inner
91+
}
6092
}
6193
}
94+
95+

0 commit comments

Comments
 (0)