-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Refactor tests of
org.kotools.types.EmailAddress
(#635)
- Loading branch information
Showing
1 changed file
with
33 additions
and
22 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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) | ||
|