Skip to content

Commit 5e03831

Browse files
committed
split solutions
1 parent 1f30b0c commit 5e03831

File tree

1 file changed

+84
-65
lines changed

1 file changed

+84
-65
lines changed

core-kotlin-modules/core-kotlin-10/src/test/kotlin/com/baeldung/variableshadowing/VariableShadowingUnitTest.kt

Lines changed: 84 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ class VariableShadowingUnitTest {
2020
assertEquals(10, number)
2121
}
2222

23+
@Test
24+
fun `solution to avoid top-level variable shadowing`(){
25+
val topLevelNumber = 10 // Top-level variable
26+
27+
fun upNumber() : Int {
28+
val localNumber = 20 // Local variable without shadowing
29+
return localNumber
30+
}
31+
32+
assertEquals(20, upNumber())
33+
assertEquals(10, topLevelNumber)
34+
}
35+
2336
@Test
2437
fun `shadowing class member`(){
2538
class Car {
@@ -35,6 +48,26 @@ class VariableShadowingUnitTest {
3548
assertEquals(200, Car().upSpeed())
3649
}
3750

51+
@Test
52+
fun `solution to avoid shadowing class member`(){
53+
class Car {
54+
val speed: Int = 100
55+
56+
fun newSpeed() : Int {
57+
val newSpeed = speed * 2 // Using a new variable name to avoid shadowing
58+
return newSpeed
59+
}
60+
61+
fun upSpeed() : Int {
62+
return this.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+
3871
@Test
3972
fun `local variable shadowing`(){
4073
fun calculateTotalPrice(discount: Int) {
@@ -55,6 +88,25 @@ class VariableShadowingUnitTest {
5588
calculateTotalPrice(20)
5689
}
5790

91+
@Test
92+
fun `solution to avoid local variable shadowing`(){
93+
fun calculateTotalPrice(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+
fun applyDiscount(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+
58110

59111
@Test
60112
fun `shadowing in loop`(){
@@ -66,6 +118,17 @@ class VariableShadowingUnitTest {
66118
}
67119
}
68120

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+
69132
@Test
70133
fun `shadowing in extension`(){
71134
val numbers = listOf(1, 2, 3, 4, 5)
@@ -81,6 +144,26 @@ class VariableShadowingUnitTest {
81144
assertEquals(30, numbers.sum())
82145
}
83146

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+
84167
@Test
85168
fun `shadowing in lambda`(){
86169
val numbers = listOf(1, 2, 3, 4, 5)
@@ -99,74 +182,10 @@ class VariableShadowingUnitTest {
99182
}
100183
}
101184

102-
103185
@Test
104-
fun `solution to avoid shadowing`(){
105-
val topLevelNumber = 10 // Top-level variable
106-
107-
fun upNumber() : 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-
class Car {
117-
val speed: Int = 100
118-
119-
fun newSpeed() : Int {
120-
val newSpeed = speed * 2 // Using a new variable name to avoid shadowing
121-
return newSpeed
122-
}
123-
124-
fun upSpeed() : Int {
125-
return this.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-
fun calculateTotalPrice(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-
fun applyDiscount(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`(){
149187
val numbers = listOf(1, 2, 3, 4, 5)
150188

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
168-
assertEquals(30, doubledSum)
169-
170189
// in lambda
171190
var sum = 0
172191

0 commit comments

Comments
 (0)