Skip to content

Commit 393422e

Browse files
committed
update
1 parent 49734c6 commit 393422e

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ImplicitAndQualifiedthisUnitTest {
1515
class Outer {
1616
fun foo() : Int = 30
1717
val x = 0
18-
val b = this
18+
val a = this
1919

2020
fun printLine() = "Member function"
2121

@@ -28,6 +28,10 @@ class ImplicitAndQualifiedthisUnitTest {
2828

2929
assertEquals("Member function", this.printLine())
3030
assertEquals("Top-level function", printLine())
31+
32+
fun checkThisToo(){
33+
val c = this
34+
}
3135
}
3236

3337
inner class Inner {
@@ -48,6 +52,14 @@ class ImplicitAndQualifiedthisUnitTest {
4852
}
4953

5054
funInnerLambda("test inner lambda")
55+
56+
val numbers = listOf(0, 1, 2, 3, 4)
57+
numbers.forEach { number ->
58+
number.run {
59+
assertEquals(number, this)
60+
}
61+
}
62+
5163
}
5264

5365
fun Int.foo() {
@@ -56,16 +68,28 @@ class ImplicitAndQualifiedthisUnitTest {
5668
assertEquals("Int", y::class.simpleName)
5769

5870
assertEquals(2, x)
59-
assertEquals(1, this@Inner.x) // Qualified: specifies x of the Inner class
71+
6072
assertEquals(42, this) // Implicit: receiver of the extension function (the Int value 42)
6173
assertEquals(42, this@foo) // Qualified: specifies the receiver of the extension function (42)
6274

6375
assertEquals(this, this@foo) // Both this keywords refer to the same receiver (42)
6476

77+
fun Int.bar() {
78+
assertEquals(32, this) // Implicit: receiver of the extension function (the Int value 32)
79+
assertEquals(42, this@foo) // Qualified: specifies the receiver of the extension function (42)
80+
81+
fun Int.baz(){
82+
assertEquals(20, this) // Implicit: receiver of the extension function (the Int value 20)
83+
assertEquals(42, this@foo) // Qualified: specifies the receiver of the extension function (42)
84+
}
85+
20.baz()
86+
}
87+
32.bar()
88+
6589
assertEquals(Inner::class.java.name, this@Inner::class.java.name) // Qualified: Inner class
6690
assertEquals(Outer::class.java.name, this@Outer::class.java.name) // Qualified: Outer class
6791

68-
92+
assertEquals(1, this@Inner.x) // Qualified: specifies x of the Inner class
6993
assertEquals(0, this@Outer.x) // Qualified: Outer's x property
7094
assertEquals(30, this@Outer.foo()) // Qualified: Outer's foo() function
7195

0 commit comments

Comments
 (0)