Skip to content

Commit d4237cd

Browse files
authored
Merge pull request eugenp#1 from sushant57/kotlin_string_comparison
Kotlin string comparison
2 parents d9cf2c0 + 19b0b02 commit d4237cd

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.stringcomparison
2+
3+
import org.junit.Test
4+
import kotlin.test.assertFalse
5+
import kotlin.test.assertTrue
6+
7+
class StringComparisonUnitTest {
8+
9+
@Test
10+
fun `compare using equals operator`() {
11+
val first = "kotlin"
12+
val second = "kotlin"
13+
val firstCapitalized = "KOTLIN"
14+
assertTrue { first == second }
15+
assertFalse { first == firstCapitalized }
16+
}
17+
18+
@Test
19+
fun `compare using referential equals operator`() {
20+
val first = "kotlin"
21+
val second = "kotlin"
22+
val copyOfFirst = buildString { "kotlin" }
23+
assertTrue { first === second }
24+
assertFalse { first === copyOfFirst }
25+
}
26+
27+
@Test
28+
fun `compare using equals method`() {
29+
val first = "kotlin"
30+
val second = "kotlin"
31+
val firstCapitalized = "KOTLIN"
32+
assertTrue { first.equals(second) }
33+
assertFalse { first.equals(firstCapitalized) }
34+
assertTrue { first.equals(firstCapitalized, true) }
35+
}
36+
37+
@Test
38+
fun `compare using compare method`() {
39+
val first = "kotlin"
40+
val second = "kotlin"
41+
val firstCapitalized = "KOTLIN"
42+
assertTrue { first.compareTo(second) == 0 }
43+
assertTrue { first.compareTo(firstCapitalized) == 32 }
44+
assertTrue { firstCapitalized.compareTo(first) == -32 }
45+
assertTrue { first.compareTo(firstCapitalized, true) == 0 }
46+
}
47+
}

0 commit comments

Comments
 (0)