Add tests for string comparison

This commit is contained in:
Sushant 2019-02-14 23:21:11 +01:00
parent d9cf2c05f5
commit 22b57081b7
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 compareUsingEqualsOperator() {
val first = "kotlin"
val second = "kotlin"
val firstCapitalized = "KOTLIN"
assertTrue { first == second }
assertFalse { first == firstCapitalized }
}
@Test
fun compareUsingReferencialEqualsOperator() {
val first = "kotlin"
val second = "kotlin"
val firstBuildAgain = buildString { "kotlin" }
assertTrue { first === second }
assertFalse { first === firstBuildAgain }
}
@Test
fun compareUsingEqualsMethod() {
val first = "kotlin"
val second = "kotlin"
val firstCapitalized = "KOTLIN"
assertTrue { first.equals(second) }
assertFalse { first.equals(firstCapitalized) }
assertTrue { first.equals(firstCapitalized, true) }
}
@Test
fun compareUsingCompareToMethod() {
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 }
}
}