forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#6371 from sushant57/master
String Comparison in Kotlin
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
core-kotlin-2/src/test/kotlin/stringcomparison/StringComparisonTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package stringcomparison | ||
|
||
import org.junit.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class StringComparisonUnitTest { | ||
|
||
@Test | ||
fun `compare using equals operator`() { | ||
val first = "kotlin" | ||
val second = "kotlin" | ||
val firstCapitalized = "KOTLIN" | ||
assertTrue { first == second } | ||
assertFalse { first == firstCapitalized } | ||
} | ||
|
||
@Test | ||
fun `compare using referential equals operator`() { | ||
val first = "kotlin" | ||
val second = "kotlin" | ||
val copyOfFirst = buildString { "kotlin" } | ||
assertTrue { first === second } | ||
assertFalse { first === copyOfFirst } | ||
} | ||
|
||
@Test | ||
fun `compare using equals method`() { | ||
val first = "kotlin" | ||
val second = "kotlin" | ||
val firstCapitalized = "KOTLIN" | ||
assertTrue { first.equals(second) } | ||
assertFalse { first.equals(firstCapitalized) } | ||
assertTrue { first.equals(firstCapitalized, true) } | ||
} | ||
|
||
@Test | ||
fun `compare using compare method`() { | ||
val first = "kotlin" | ||
val second = "kotlin" | ||
val firstCapitalized = "KOTLIN" | ||
assertTrue { first.compareTo(second) == 0 } | ||
assertTrue { first.compareTo(firstCapitalized) == 32 } | ||
assertTrue { firstCapitalized.compareTo(first) == -32 } | ||
assertTrue { first.compareTo(firstCapitalized, true) == 0 } | ||
} | ||
} |