From 22b57081b7fac00138caa958e58b4489a84e4cee Mon Sep 17 00:00:00 2001 From: Sushant Date: Thu, 14 Feb 2019 23:21:11 +0100 Subject: [PATCH] Add tests for string comparison --- .../stringcomparison/StringComparisonTest.kt | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/stringcomparison/StringComparisonTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/stringcomparison/StringComparisonTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/stringcomparison/StringComparisonTest.kt new file mode 100644 index 0000000000..b8eefdb144 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/stringcomparison/StringComparisonTest.kt @@ -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 } + } +} \ No newline at end of file