File tree Expand file tree Collapse file tree
core-kotlin/src/test/kotlin/com/baeldung/stringcomparison Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments