Skip to content

Commit 49734c6

Browse files
committed
add fun
1 parent 3ed2fc7 commit 49734c6

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class ImplicitAndQualifiedthisUnitTest {
1313
val x = 5
1414

1515
class Outer {
16+
fun foo() : Int = 30
1617
val x = 0
1718
val b = this
18-
fun foo() : Int = 30
1919

2020
fun printLine() = "Member function"
2121

@@ -35,6 +35,7 @@ class ImplicitAndQualifiedthisUnitTest {
3535
val x = 1
3636

3737
fun checkInner(){
38+
3839
val funLit = fun String.() {
3940
assertEquals("test.funLit()", this) // Implicit: receiver of the function literal (the String itself)
4041
}
@@ -51,15 +52,19 @@ class ImplicitAndQualifiedthisUnitTest {
5152

5253
fun Int.foo() {
5354
val x = 2
55+
val y = this
56+
assertEquals("Int", y::class.simpleName)
57+
5458
assertEquals(2, x)
5559
assertEquals(1, this@Inner.x) // Qualified: specifies x of the Inner class
5660
assertEquals(42, this) // Implicit: receiver of the extension function (the Int value 42)
5761
assertEquals(42, this@foo) // Qualified: specifies the receiver of the extension function (42)
5862

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

61-
assertEquals(Outer::class.java.name, this@Outer::class.java.name) // Qualified: Outer class
6265
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+
6368

6469
assertEquals(0, this@Outer.x) // Qualified: Outer's x property
6570
assertEquals(30, this@Outer.foo()) // Qualified: Outer's foo() function
@@ -76,6 +81,11 @@ class ImplicitAndQualifiedthisUnitTest {
7681
}
7782

7883
funExtLambda("test funExtLambda")
84+
85+
val increase = fun(x: Int): Int {
86+
return this + x
87+
}
88+
assertEquals(43, increase(1))
7989
}
8090
}
8191
}
@@ -89,6 +99,7 @@ class ImplicitAndQualifiedthisUnitTest {
8999
inner.run {
90100
42.foo() // Calls the extension function defined inside Inner
91101
}
102+
92103
}
93104
}
94105

0 commit comments

Comments
 (0)