-
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.
✨ Add
EmailAddress.Companion.fromString(Any)
function (#635)
Adds this function in the `org.kotools.types` package with tests, tested code samples for Kotlin and Java, and documentation. Also dumps the Application Binary Interface (ABI).
- Loading branch information
Showing
6 changed files
with
156 additions
and
2 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
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 |
---|---|---|
|
@@ -3,8 +3,10 @@ package org.kotools.types | |
import kotools.types.experimental.ExperimentalKotoolsTypesApi | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
import kotlin.test.assertTrue | ||
|
||
@OptIn(ExperimentalKotoolsTypesApi::class) | ||
class EmailAddressCompanionTest { | ||
|
@@ -15,6 +17,70 @@ class EmailAddressCompanionTest { | |
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun fromString_Any_should_pass_with_a_valid_value() { | ||
val value: Any = "[email protected]" | ||
val actual: Result<EmailAddress> = kotlin.runCatching { | ||
EmailAddress.fromString(value) | ||
} | ||
assertTrue(actual.isSuccess) | ||
} | ||
|
||
@Test | ||
fun fromString_Any_should_fail_with_a_missing_at_sign_in_value() { | ||
val value: Any = "contactKotools.org" | ||
val exception: IllegalArgumentException = assertFailsWith { | ||
EmailAddress.fromString(value) | ||
} | ||
val actual: String? = exception.message | ||
val expected: String = InvalidEmailAddress(value).message | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun fromString_Any_should_fail_with_a_missing_dot_in_domain_of_value() { | ||
val value: Any = "contact@kotoolsOrg" | ||
val exception: IllegalArgumentException = assertFailsWith { | ||
EmailAddress.fromString(value) | ||
} | ||
val actual: String? = exception.message | ||
val expected: String = InvalidEmailAddress(value).message | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun fromString_Any_should_fail_with_whitespaces_in_local_part_of_value() { | ||
val value: Any = " cont act @kotools.org" | ||
val exception: IllegalArgumentException = assertFailsWith { | ||
EmailAddress.fromString(value) | ||
} | ||
val actual: String? = exception.message | ||
val expected: String = InvalidEmailAddress(value).message | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun fromString_Any_should_fail_with_whitespaces_in_domain_first_label_of_value() { | ||
val value: Any = "contact@ ko tools .org" | ||
val exception: IllegalArgumentException = assertFailsWith { | ||
EmailAddress.fromString(value) | ||
} | ||
val actual: String? = exception.message | ||
val expected: String = InvalidEmailAddress(value).message | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun fromString_Any_should_fail_with_whitespaces_in_domain_second_label_of_value() { | ||
val value: Any = "contact@kotools. or g " | ||
val exception: IllegalArgumentException = assertFailsWith { | ||
EmailAddress.fromString(value) | ||
} | ||
val actual: String? = exception.message | ||
val expected: String = InvalidEmailAddress(value).message | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun fromStringOrNull_Any_should_pass_with_a_valid_value() { | ||
val value: Any = "[email protected]" | ||
|
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 |
---|---|---|
|
@@ -6,6 +6,17 @@ void patternSample() { | |
System.out.println(pattern); // ^\S+@\S+\.\S+$ | ||
} // END | ||
|
||
void fromStringSample() { | ||
final Object value = "[email protected]"; | ||
try { | ||
EmailAddress.fromString(value); // TABS: 1 | ||
System.out.println("success"); // TABS: 1 | ||
} catch (final IllegalArgumentException exception) { | ||
System.out.println("failure"); // TABS: 1 | ||
} | ||
// Output: success | ||
} // END | ||
|
||
void fromStringOrNullSample() { | ||
final Object value = "[email protected]"; | ||
final EmailAddress address = EmailAddress.fromStringOrNull(value); | ||
|
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 |
---|---|---|
|
@@ -9,6 +9,14 @@ internal object EmailAddressCompanionKotlinSample { | |
println(pattern) // ^\S+@\S+\.\S+$ | ||
} // END | ||
|
||
fun fromStringSample() { | ||
val value: Any = "[email protected]" | ||
val result: Result<EmailAddress> = kotlin.runCatching { | ||
EmailAddress.fromString(value) // TABS: 1 | ||
} | ||
println(result.isSuccess) // true | ||
} // END | ||
|
||
fun fromStringOrNullSample() { | ||
val value: Any = "[email protected]" | ||
val address: EmailAddress? = EmailAddress.fromStringOrNull(value) | ||
|
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
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