Merge pull request #1 from sushant57/kotlin_string_comparison

Kotlin string comparison
This commit is contained in:
Kumar Sushant 2019-02-18 19:42:28 +01:00 committed by GitHub
commit d4237cdfbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package com.baeldung.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 }
}
}