-
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, Any)
(#635)
- Loading branch information
Showing
7 changed files
with
152 additions
and
4 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 |
---|---|---|
|
@@ -81,6 +81,40 @@ class EmailAddressCompanionTest { | |
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun fromString_Any_Any_should_pass_with_valid_value_and_pattern() { | ||
val value: Any = "[email protected]" | ||
val pattern: Any = "^[a-z]+@[a-z]+\\.[a-z]+\$" | ||
val result: Result<EmailAddress> = kotlin.runCatching { | ||
EmailAddress.fromString(value, pattern) | ||
} | ||
assertTrue(result.isSuccess) | ||
} | ||
|
||
@Test | ||
fun fromString_Any_Any_should_fail_with_invalid_value() { | ||
val value: Any = "[email protected]" | ||
val pattern: Any = "^[a-z]+@[a-z]+\\.[a-z]+\$" | ||
val exception: IllegalArgumentException = assertFailsWith { | ||
EmailAddress.fromString(value, pattern) | ||
} | ||
val actual: String? = exception.message | ||
val expected: String = InvalidEmailAddress(value, pattern).message | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun fromString_Any_Any_should_fail_with_invalid_pattern() { | ||
val value: Any = "[email protected]" | ||
val pattern: Any = "^[a-z]+\\.[a-z]+\$" | ||
val exception: IllegalArgumentException = assertFailsWith { | ||
EmailAddress.fromString(value, pattern) | ||
} | ||
val actual: String? = exception.message | ||
val expected: String = InvalidEmailAddressPattern(pattern).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 |
---|---|---|
|
@@ -17,6 +17,18 @@ void fromString_Any() { | |
// Output: success | ||
} // END | ||
|
||
void fromString_Any_Any() { | ||
final Object value = "[email protected]"; | ||
final Object pattern = "^[a-z]+@[a-z]+\\.[a-z]+$"; | ||
try { | ||
EmailAddress.fromString(value, pattern); // TABS: 1 | ||
System.out.println("success"); // TABS: 1 | ||
} catch (final IllegalArgumentException exception) { | ||
System.out.println("failure"); // TABS: 1 | ||
} | ||
// Output: success | ||
} // END | ||
|
||
void fromStringOrNull_Any() { | ||
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 |
---|---|---|
|
@@ -18,6 +18,16 @@ internal object EmailAddressCompanionKotlinSample { | |
println(result.isSuccess) // true | ||
} // END | ||
|
||
@Suppress("FunctionName") | ||
fun fromString_Any_Any() { | ||
val value: Any = "[email protected]" | ||
val pattern: Any = "^[a-z]+@[a-z]+\\.[a-z]+\$" | ||
val result: Result<EmailAddress> = kotlin.runCatching { | ||
EmailAddress.fromString(value, pattern) // TABS: 1 | ||
} | ||
println(result.isSuccess) // true | ||
} // END | ||
|
||
@Suppress("FunctionName") | ||
fun fromStringOrNull_Any() { | ||
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
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