-
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.fromStringOrNull(Any)
(#635)
Adds this function to the type in the `org.kotools.types` package, with tests, tested Kotlin and Java code samples, and documentation. Also dumps the Application Binary Interface (ABI).
- Loading branch information
Showing
7 changed files
with
120 additions
and
3 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
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,6 +3,8 @@ package org.kotools.types | |
import kotools.types.experimental.ExperimentalKotoolsTypesApi | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNull | ||
|
||
@OptIn(ExperimentalKotoolsTypesApi::class) | ||
class EmailAddressCompanionTest { | ||
|
@@ -12,4 +14,46 @@ class EmailAddressCompanionTest { | |
val expected = "^\\S+@\\S+\\.\\S+\$" | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun fromStringOrNull_Any_should_pass_with_a_valid_value() { | ||
val value: Any = "[email protected]" | ||
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 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 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 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 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 actual: EmailAddress? = EmailAddress.fromStringOrNull(value) | ||
assertNull(actual) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,4 +5,10 @@ void patternSample() { | |
final String pattern = EmailAddress.PATTERN; | ||
System.out.println(pattern); // ^\S+@\S+\.\S+$ | ||
} | ||
|
||
void fromStringOrNullSample() { | ||
final Object value = "[email protected]"; | ||
final EmailAddress address = EmailAddress.fromStringOrNull(value); | ||
System.out.println(address != null); // true | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,10 +2,16 @@ package org.kotools.types | |
|
||
import kotools.types.experimental.ExperimentalKotoolsTypesApi | ||
|
||
@OptIn(ExperimentalKotoolsTypesApi::class) | ||
internal object EmailAddressCompanionKotlinSample { | ||
@OptIn(ExperimentalKotoolsTypesApi::class) | ||
fun patternSample() { | ||
val pattern: String = EmailAddress.PATTERN | ||
println(pattern) // ^\S+@\S+\.\S+$ | ||
} | ||
|
||
fun fromStringOrNullSample() { | ||
val value: Any = "[email protected]" | ||
val address: EmailAddress? = EmailAddress.fromStringOrNull(value) | ||
println(address != null) // true | ||
} | ||
} |
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