-
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.orNull(String, String)
(#692)
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 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 |
---|---|---|
|
@@ -62,6 +62,14 @@ class EmailAddressCompanionCommonSample { | |
assertNotNull(emailAddress) | ||
} | ||
|
||
@Test | ||
fun orNullStringString() { | ||
val text = "[email protected]" | ||
val pattern = """^[a-z]+@[a-z]+\.[a-z]+$""" | ||
val emailAddress: EmailAddress? = EmailAddress.orNull(text, pattern) | ||
assertNotNull(emailAddress) | ||
} | ||
|
||
@Test | ||
fun orThrowString() { | ||
val text = "[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 |
---|---|---|
|
@@ -265,6 +265,43 @@ class EmailAddressCompanionTest { | |
assertNull(actual, message) | ||
} | ||
|
||
@Test | ||
fun orNullStringStringShouldPassWithValidTextAndPattern() { | ||
val text: String = Values.VALID | ||
val pattern = """^[a-z]+@[a-z]+\.[a-z]+$""" | ||
val actual: EmailAddress? = EmailAddress.orNull(text, pattern) | ||
val message: String = simpleNameOf<EmailAddress>() | ||
.let { | ||
"Creating an instance of $it from '$text' with '$pattern' " + | ||
"should pass." | ||
} | ||
assertNotNull(actual, message) | ||
} | ||
|
||
@Test | ||
fun orNullStringStringShouldFailWithInvalidText() { | ||
val text = "[email protected]" | ||
val pattern = """^[a-z]+@[a-z]+\.[a-z]+$""" | ||
this.orNullShouldFailWith(text, pattern) | ||
} | ||
|
||
@Test | ||
fun orNullStringStringShouldFailWithInvalidPattern() { | ||
val text: String = Values.VALID | ||
val pattern = """^[a-z]+\.[a-z]+$""" | ||
this.orNullShouldFailWith(text, pattern) | ||
} | ||
|
||
private fun orNullShouldFailWith(text: String, pattern: String) { | ||
val actual: EmailAddress? = EmailAddress.orNull(text, pattern) | ||
val message: String = simpleNameOf<EmailAddress>() | ||
.let { | ||
"Creating an instance of $it from '$text' with '$pattern' " + | ||
"should fail." | ||
} | ||
assertNull(actual, message) | ||
} | ||
|
||
@Test | ||
fun orThrowStringShouldPassWithValidText() { | ||
EmailAddress.orThrow(text = Values.VALID) | ||
|