@@ -8,10 +8,10 @@ import org.junit.jupiter.api.Test
8
8
class VariableShadowingUnitTest {
9
9
10
10
@Test
11
- fun `top-level variable shadowing` (){
11
+ fun `top-level variable shadowing` () {
12
12
val number = 10 // Top-level variable
13
13
14
- fun upNumber () : Int { // top-level function
14
+ fun upNumber (): Int { // top-level function
15
15
val number = 20 // shadowing top-level variable
16
16
return number
17
17
}
@@ -21,10 +21,10 @@ class VariableShadowingUnitTest {
21
21
}
22
22
23
23
@Test
24
- fun `solution to avoid top-level variable shadowing` (){
24
+ fun `solution to avoid top-level variable shadowing` () {
25
25
val topLevelNumber = 10 // Top-level variable
26
26
27
- fun upNumber () : Int {
27
+ fun upNumber (): Int {
28
28
val localNumber = 20 // Local variable without shadowing
29
29
return localNumber
30
30
}
@@ -34,11 +34,11 @@ class VariableShadowingUnitTest {
34
34
}
35
35
36
36
@Test
37
- fun `shadowing class member` (){
37
+ fun `shadowing class member` () {
38
38
class Car {
39
39
val speed: Int = 100
40
40
41
- fun upSpeed () : Int {
41
+ fun upSpeed (): Int {
42
42
val speed = speed * 2 // Shadowing class member named 'speed'
43
43
return speed
44
44
}
@@ -49,16 +49,16 @@ class VariableShadowingUnitTest {
49
49
}
50
50
51
51
@Test
52
- fun `solution to avoid shadowing class member` (){
52
+ fun `solution to avoid shadowing class member` () {
53
53
class Car {
54
54
val speed: Int = 100
55
55
56
- fun newSpeed () : Int {
56
+ fun newSpeed (): Int {
57
57
val newSpeed = speed * 2 // Using a new variable name to avoid shadowing
58
58
return newSpeed
59
59
}
60
60
61
- fun upSpeed () : Int {
61
+ fun upSpeed (): Int {
62
62
return this .speed * 2 // Use the outer speed directly with this keyword
63
63
}
64
64
}
@@ -69,7 +69,7 @@ class VariableShadowingUnitTest {
69
69
}
70
70
71
71
@Test
72
- fun `local variable shadowing` (){
72
+ fun `local variable shadowing` () {
73
73
fun calculateTotalPrice (discount : Int ) {
74
74
val discount = discount + 10 // Shadowing the parameter 'discount'
75
75
assertEquals(30 , discount)
@@ -89,7 +89,7 @@ class VariableShadowingUnitTest {
89
89
}
90
90
91
91
@Test
92
- fun `solution to avoid local variable shadowing` (){
92
+ fun `solution to avoid local variable shadowing` () {
93
93
fun calculateTotalPrice (discount : Int ) {
94
94
val updatedDiscount = discount + 10 // Using a new variable name to avoid shadowing
95
95
assertEquals(30 , updatedDiscount)
@@ -109,7 +109,7 @@ class VariableShadowingUnitTest {
109
109
110
110
111
111
@Test
112
- fun `shadowing in loop` (){
112
+ fun `shadowing in loop` () {
113
113
val numbers = listOf (1 , 2 , 3 , 4 , 5 )
114
114
115
115
// shadowing in loop
@@ -119,7 +119,7 @@ class VariableShadowingUnitTest {
119
119
}
120
120
121
121
@Test
122
- fun `solution to avoiding shadowing in loop` (){
122
+ fun `solution to avoiding shadowing in loop` () {
123
123
val numbers = listOf (1 , 2 , 3 , 4 , 5 )
124
124
125
125
// avoiding shadowing in loop
@@ -130,7 +130,7 @@ class VariableShadowingUnitTest {
130
130
131
131
132
132
@Test
133
- fun `shadowing in extension` (){
133
+ fun `shadowing in extension` () {
134
134
val numbers = listOf (1 , 2 , 3 , 4 , 5 )
135
135
136
136
assertEquals(15 , numbers.sum())
@@ -145,7 +145,7 @@ class VariableShadowingUnitTest {
145
145
}
146
146
147
147
@Test
148
- fun `solution to avoiding shadowing in extension` (){
148
+ fun `solution to avoiding shadowing in extension` () {
149
149
val numbers = listOf (1 , 2 , 3 , 4 , 5 )
150
150
151
151
// in extension
@@ -165,7 +165,7 @@ class VariableShadowingUnitTest {
165
165
166
166
167
167
@Test
168
- fun `shadowing in lambda` (){
168
+ fun `shadowing in lambda` () {
169
169
val numbers = listOf (1 , 2 , 3 , 4 , 5 )
170
170
171
171
var sum = 0
@@ -177,13 +177,13 @@ class VariableShadowingUnitTest {
177
177
178
178
assertEquals(0 , sum)
179
179
180
- val lambda = { number : Int ->
180
+ val lambda = { number: Int ->
181
181
val number = 1 // Local variable in lambda
182
182
}
183
183
}
184
184
185
185
@Test
186
- fun `solution to avoiding shadowing in lambda` (){
186
+ fun `solution to avoiding shadowing in lambda` () {
187
187
val numbers = listOf (1 , 2 , 3 , 4 , 5 )
188
188
189
189
// in lambda
0 commit comments