Skip to content

Commit

Permalink
♻️ Refactor tests of org.kotools.types.EmailAddress (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Apr 2, 2024
1 parent 3b0f6fb commit e909782
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions src/commonTest/kotlin/org/kotools/types/EmailAddressTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@ import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

private object Values {
const val VALID: String = "[email protected]"
const val MISSING_AT_SIGN: String = "contactKotools.org"
const val MISSING_DOMAIN_DOT: String = "contact@kotoolsOrg"
const val WHITESPACES_IN_LOCAL_PART: String = " cont act @kotools.org"
const val WHITESPACES_IN_DOMAIN_FIRST_LABEL: String =
"contact@ ko tools .org"
const val WHITESPACES_IN_DOMAIN_SECOND_LABEL: String =
"contact@kotools. or g "
}

@OptIn(ExperimentalKotoolsTypesApi::class)
class EmailAddressTest {
@Test
fun structural_equality_should_pass_with_the_same_instance() {
val first: EmailAddress = EmailAddress.fromString("[email protected]")
val first: EmailAddress = EmailAddress.fromString(Values.VALID)
val second: Any = first
val equality: Boolean = first.equals(second)
assertTrue(equality)
Expand All @@ -25,7 +36,7 @@ class EmailAddressTest {

@Test
fun structural_equality_should_pass_with_another_EmailAddress_having_the_same_string_representation() {
val value: Any = "[email protected]"
val value: Any = Values.VALID
val first: EmailAddress = EmailAddress.fromString(value)
val second: Any = EmailAddress.fromString(value)
val equality: Boolean = first.equals(second)
Expand All @@ -37,7 +48,7 @@ class EmailAddressTest {

@Test
fun structural_equality_should_fail_with_null() {
val first: EmailAddress = EmailAddress.fromString("[email protected]")
val first: EmailAddress = EmailAddress.fromString(Values.VALID)
val second: Any? = null
val equality: Boolean = first.equals(second)
assertFalse(equality)
Expand All @@ -48,7 +59,7 @@ class EmailAddressTest {

@Test
fun structural_equality_should_fail_with_another_object_having_another_type_than_EmailAddress() {
val value: Any = "[email protected]"
val value: Any = Values.VALID
val first: EmailAddress = EmailAddress.fromString(value)
val second: Any = value
val equality: Boolean = first.equals(second)
Expand All @@ -60,7 +71,7 @@ class EmailAddressTest {

@Test
fun structural_equality_should_fail_with_another_EmailAddress_having_another_string_representation() {
val first: EmailAddress = EmailAddress.fromString("[email protected]")
val first: EmailAddress = EmailAddress.fromString(Values.VALID)
val second: Any = EmailAddress.fromString("[email protected]")
val equality: Boolean = first.equals(second)
assertFalse(equality)
Expand All @@ -71,7 +82,7 @@ class EmailAddressTest {

@Test
fun toString_should_pass() {
val value: Any = "[email protected]"
val value: Any = Values.VALID
val address: EmailAddress = EmailAddress.fromString(value)
val actual: String = address.toString()
assertEquals(expected = value, actual)
Expand All @@ -89,7 +100,7 @@ class EmailAddressCompanionTest {

@Test
fun fromString_Any_should_pass_with_a_valid_value() {
val value: Any = "[email protected]"
val value: Any = Values.VALID
val actual: Result<EmailAddress> = kotlin.runCatching {
EmailAddress.fromString(value)
}
Expand All @@ -98,7 +109,7 @@ class EmailAddressCompanionTest {

@Test
fun fromString_Any_should_fail_with_a_missing_at_sign_in_value() {
val value: Any = "contactKotools.org"
val value: Any = Values.MISSING_AT_SIGN
val exception: IllegalArgumentException = assertFailsWith {
EmailAddress.fromString(value)
}
Expand All @@ -109,7 +120,7 @@ class EmailAddressCompanionTest {

@Test
fun fromString_Any_should_fail_with_a_missing_dot_in_domain_of_value() {
val value: Any = "contact@kotoolsOrg"
val value: Any = Values.MISSING_DOMAIN_DOT
val exception: IllegalArgumentException = assertFailsWith {
EmailAddress.fromString(value)
}
Expand All @@ -120,7 +131,7 @@ class EmailAddressCompanionTest {

@Test
fun fromString_Any_should_fail_with_whitespaces_in_local_part_of_value() {
val value: Any = " cont act @kotools.org"
val value: Any = Values.WHITESPACES_IN_LOCAL_PART
val exception: IllegalArgumentException = assertFailsWith {
EmailAddress.fromString(value)
}
Expand All @@ -131,7 +142,7 @@ class EmailAddressCompanionTest {

@Test
fun fromString_Any_should_fail_with_whitespaces_in_domain_first_label_of_value() {
val value: Any = "contact@ ko tools .org"
val value: Any = Values.WHITESPACES_IN_DOMAIN_FIRST_LABEL
val exception: IllegalArgumentException = assertFailsWith {
EmailAddress.fromString(value)
}
Expand All @@ -142,7 +153,7 @@ class EmailAddressCompanionTest {

@Test
fun fromString_Any_should_fail_with_whitespaces_in_domain_second_label_of_value() {
val value: Any = "contact@kotools. or g "
val value: Any = Values.WHITESPACES_IN_DOMAIN_SECOND_LABEL
val exception: IllegalArgumentException = assertFailsWith {
EmailAddress.fromString(value)
}
Expand All @@ -153,7 +164,7 @@ class EmailAddressCompanionTest {

@Test
fun fromString_Any_Any_should_pass_with_valid_value_and_pattern() {
val value: Any = "[email protected]"
val value: Any = Values.VALID
val pattern: Any = "^[a-z]+@[a-z]+\\.[a-z]+\$"
val result: Result<EmailAddress> = kotlin.runCatching {
EmailAddress.fromString(value, pattern)
Expand All @@ -175,7 +186,7 @@ class EmailAddressCompanionTest {

@Test
fun fromString_Any_Any_should_fail_with_invalid_pattern() {
val value: Any = "[email protected]"
val value: Any = Values.VALID
val pattern: Any = "^[a-z]+\\.[a-z]+\$"
val exception: IllegalArgumentException = assertFailsWith {
EmailAddress.fromString(value, pattern)
Expand All @@ -187,49 +198,49 @@ class EmailAddressCompanionTest {

@Test
fun fromStringOrNull_Any_should_pass_with_a_valid_value() {
val value: Any = "[email protected]"
val value: Any = Values.VALID
val actual: EmailAddress? = EmailAddress.fromStringOrNull(value)
assertNotNull(actual)
}

@Test
fun fromStringOrNull_Any_should_fail_with_a_missing_at_sign_in_value() {
val value: Any = "contactKotools.org"
val value: Any = Values.MISSING_AT_SIGN
val actual: EmailAddress? = EmailAddress.fromStringOrNull(value)
assertNull(actual)
}

@Test
fun fromStringOrNull_Any_should_fail_with_a_missing_dot_in_domain_of_value() {
val value: Any = "contact@kotoolsOrg"
val value: Any = Values.MISSING_DOMAIN_DOT
val actual: EmailAddress? = EmailAddress.fromStringOrNull(value)
assertNull(actual)
}

@Test
fun fromStringOrNull_Any_should_fail_with_whitespaces_in_local_part_of_value() {
val value: Any = " cont act @kotools.org"
val value: Any = Values.WHITESPACES_IN_LOCAL_PART
val actual: EmailAddress? = EmailAddress.fromStringOrNull(value)
assertNull(actual)
}

@Test
fun fromStringOrNull_Any_should_fail_with_whitespaces_in_domain_first_label_of_value() {
val value: Any = "contact@ ko tools .org"
val value: Any = Values.WHITESPACES_IN_DOMAIN_FIRST_LABEL
val actual: EmailAddress? = EmailAddress.fromStringOrNull(value)
assertNull(actual)
}

@Test
fun fromStringOrNull_Any_should_fail_with_whitespaces_in_domain_second_label_of_value() {
val value: Any = "contact@kotools. or g "
val value: Any = Values.WHITESPACES_IN_DOMAIN_SECOND_LABEL
val actual: EmailAddress? = EmailAddress.fromStringOrNull(value)
assertNull(actual)
}

@Test
fun fromStringOrNull_Any_Any_should_pass_with_valid_value_and_pattern() {
val value: Any = "[email protected]"
val value: Any = Values.VALID
val pattern: Any = "^[a-z]+@[a-z]+\\.[a-z]+\$"
val actual: EmailAddress? =
EmailAddress.fromStringOrNull(value, pattern)
Expand All @@ -247,7 +258,7 @@ class EmailAddressCompanionTest {

@Test
fun fromStringOrNull_Any_Any_should_fail_with_invalid_pattern() {
val value: Any = "[email protected]"
val value: Any = Values.VALID
val pattern: Any = "^[a-z]+\\.[a-z]+\$"
val actual: EmailAddress? =
EmailAddress.fromStringOrNull(value, pattern)
Expand Down

0 comments on commit e909782

Please sign in to comment.