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-10/src/test/kotlin/com/baeldung/variableshadowing/VariableShadowingUnitTest.kt
+84-65Lines changed: 84 additions & 65 deletions
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,19 @@ class VariableShadowingUnitTest {
20
20
assertEquals(10, number)
21
21
}
22
22
23
+
@Test
24
+
fun`solution to avoid top-level variable shadowing`(){
25
+
val topLevelNumber =10// Top-level variable
26
+
27
+
funupNumber() : Int {
28
+
val localNumber =20// Local variable without shadowing
29
+
return localNumber
30
+
}
31
+
32
+
assertEquals(20, upNumber())
33
+
assertEquals(10, topLevelNumber)
34
+
}
35
+
23
36
@Test
24
37
fun`shadowing class member`(){
25
38
classCar {
@@ -35,6 +48,26 @@ class VariableShadowingUnitTest {
35
48
assertEquals(200, Car().upSpeed())
36
49
}
37
50
51
+
@Test
52
+
fun`solution to avoid shadowing class member`(){
53
+
classCar {
54
+
val speed:Int=100
55
+
56
+
funnewSpeed() : Int {
57
+
val newSpeed = speed *2// Using a new variable name to avoid shadowing
58
+
return newSpeed
59
+
}
60
+
61
+
funupSpeed() : Int {
62
+
returnthis.speed *2// Use the outer speed directly with this keyword
63
+
}
64
+
}
65
+
66
+
assertEquals(100, Car().speed)
67
+
assertEquals(200, Car().upSpeed())
68
+
assertEquals(200, Car().newSpeed())
69
+
}
70
+
38
71
@Test
39
72
fun`local variable shadowing`(){
40
73
funcalculateTotalPrice(discount:Int) {
@@ -55,6 +88,25 @@ class VariableShadowingUnitTest {
55
88
calculateTotalPrice(20)
56
89
}
57
90
91
+
@Test
92
+
fun`solution to avoid local variable shadowing`(){
93
+
funcalculateTotalPrice(discount:Int) {
94
+
val updatedDiscount = discount +10// Using a new variable name to avoid shadowing
95
+
assertEquals(30, updatedDiscount)
96
+
97
+
val price =100// local variable
98
+
val discountRate =0.1
99
+
100
+
funapplyDiscount(price:Int): Double {
101
+
return price * (1- discountRate) // Use the outer discountRate directly
102
+
}
103
+
104
+
assertEquals(90.0, applyDiscount(price))
105
+
}
106
+
107
+
calculateTotalPrice(20)
108
+
}
109
+
58
110
59
111
@Test
60
112
fun`shadowing in loop`(){
@@ -66,6 +118,17 @@ class VariableShadowingUnitTest {
66
118
}
67
119
}
68
120
121
+
@Test
122
+
fun`solution to avoiding shadowing in loop`(){
123
+
val numbers =listOf(1, 2, 3, 4, 5)
124
+
125
+
// avoiding shadowing in loop
126
+
for (number in numbers) {
127
+
val newNumber = number *2// Using a new variable name to avoid shadowing
128
+
}
129
+
}
130
+
131
+
69
132
@Test
70
133
fun`shadowing in extension`(){
71
134
val numbers =listOf(1, 2, 3, 4, 5)
@@ -81,6 +144,26 @@ class VariableShadowingUnitTest {
81
144
assertEquals(30, numbers.sum())
82
145
}
83
146
147
+
@Test
148
+
fun`solution to avoiding shadowing in extension`(){
149
+
val numbers =listOf(1, 2, 3, 4, 5)
150
+
151
+
// in extension
152
+
assertEquals(15, numbers.sum())
153
+
154
+
fun List<Int>.sumByTwo(): Int { // using another name to avoid shadowing
155
+
var sum =0
156
+
this.forEach { sum += it *2 }
157
+
return sum
158
+
}
159
+
160
+
assertEquals(30, numbers.sumByTwo())
161
+
162
+
val doubledSum = numbers.sumOf { it *2 } // Modify lambda in sum
163
+
assertEquals(30, doubledSum)
164
+
}
165
+
166
+
84
167
@Test
85
168
fun`shadowing in lambda`(){
86
169
val numbers =listOf(1, 2, 3, 4, 5)
@@ -99,74 +182,10 @@ class VariableShadowingUnitTest {
99
182
}
100
183
}
101
184
102
-
103
185
@Test
104
-
fun`solution to avoid shadowing`(){
105
-
val topLevelNumber =10// Top-level variable
106
-
107
-
funupNumber() : Int {
108
-
val localNumber =20// Local variable without shadowing
109
-
return localNumber
110
-
}
111
-
112
-
assertEquals(20, upNumber())
113
-
assertEquals(10, topLevelNumber)
114
-
115
-
// in class member
116
-
classCar {
117
-
val speed:Int=100
118
-
119
-
funnewSpeed() : Int {
120
-
val newSpeed = speed *2// Using a new variable name to avoid shadowing
121
-
return newSpeed
122
-
}
123
-
124
-
funupSpeed() : Int {
125
-
returnthis.speed *2// Use the outer speed directly with this keyword
126
-
}
127
-
}
128
-
129
-
assertEquals(100, Car().speed)
130
-
assertEquals(200, Car().upSpeed())
131
-
assertEquals(200, Car().newSpeed())
132
-
133
-
funcalculateTotalPrice(discount:Int) {
134
-
val updatedDiscount = discount +10// Using a new variable name to avoid shadowing
135
-
assertEquals(30, updatedDiscount)
136
-
137
-
val price =100// local variable
138
-
val discountRate =0.1
139
-
140
-
funapplyDiscount(price:Int): Double {
141
-
return price * (1- discountRate) // Use the outer discountRate directly
142
-
}
143
-
144
-
assertEquals(90.0, applyDiscount(price))
145
-
}
146
-
147
-
calculateTotalPrice(20)
148
-
186
+
fun`solution to avoiding shadowing in lambda`(){
149
187
val numbers =listOf(1, 2, 3, 4, 5)
150
188
151
-
// in loop
152
-
for (number in numbers) {
153
-
val newNumber = number *2// Using a new variable name to avoid shadowing
154
-
}
155
-
156
-
// in extension
157
-
assertEquals(15, numbers.sum())
158
-
159
-
fun List<Int>.sumByTwo(): Int { // shadowing built-in function sum()
160
-
var sum =0
161
-
this.forEach { sum += it *2 }
162
-
return sum
163
-
}
164
-
165
-
assertEquals(30, numbers.sumByTwo())
166
-
167
-
val doubledSum = numbers.sumOf { it *2 } // Modify lambda in sum
0 commit comments