private static bool TryParse(string address, string? displayName, Encoding? displayNameEncoding, out (string displayName, string user, string host, Encoding displayNameEncoding) parsedData, bool throwExceptionIfFail)
{
- if (address == null)
+ if (string.IsNullOrEmpty(address))
{
- throw new ArgumentNullException(nameof(address));
- }
- if (address.Length == 0)
- {
- throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(address)), nameof(address));
+ if (throwExceptionIfFail)
+ {
+ throw address is null ?
+ new ArgumentNullException(nameof(address)) :
+ new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(address)), nameof(address));
+ }
+
+ parsedData = default;
+ return false;
}
displayNameEncoding ??= Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
public void TestConstructorWithNullString()
{
Assert.Throws<ArgumentNullException>(() => new MailAddress(null));
- Assert.Throws<ArgumentNullException>(() => _ = MailAddress.TryCreate(null, out MailAddress _));
+ Assert.False(MailAddress.TryCreate(null, out MailAddress address));
+ Assert.Null(address);
}
[Fact]
public void TestConstructorWithEmptyString()
{
AssertExtensions.Throws<ArgumentException>("address", () => new MailAddress(""));
- AssertExtensions.Throws<ArgumentException>("address", () => _ = MailAddress.TryCreate("", out MailAddress _));
+ Assert.False(MailAddress.TryCreate("", out MailAddress address));
+ Assert.Null(address);
}
public static IEnumerable<object[]> GetInvalid_Address()
yield return new object[] { "\"MrFo@Bar\"<>" };
yield return new object[] { " " };
yield return new object[] { "forbar" };
+ yield return new object[] { "" };
+ yield return new object[] { null };
}
[Theory]
[MemberData(nameof(GetInvalid_Address))]
- public void TestInvalidAddressInConstructo_Address(string invalidAddress)
+ public void TestInvalidAddressInConstructor_Address(string invalidAddress)
{
- Assert.Throws<FormatException>(() => new MailAddress(invalidAddress));
+ Action act = () => new MailAddress(invalidAddress);
+ if (invalidAddress is null)
+ {
+ Assert.Throws<ArgumentNullException>(act);
+ }
+ else if (invalidAddress == string.Empty)
+ {
+ Assert.Throws<ArgumentException>(act);
+ }
+ else
+ {
+ Assert.Throws<FormatException>(act);
+ }
}
public static IEnumerable<object[]> GetInvalid_AddressDisplayName()
yield return new object[] { "invalid@unicode\uD800.com" }; // D800 is a high surrogate
yield return new object[] { "invalid\uD800@unicode.com" }; // D800 is a high surrogate
yield return new object[] { "\uD800 invalid@unicode.com" }; // D800 is a high surrogate
+ yield return new object[] { null };
+ yield return new object[] { "" };
}
[Theory]
[MemberData(nameof(GetInvalidEmailTestData))]
public void TestInvalidEmailAddresses(string address)
{
- Assert.Throws<FormatException>(() => { new MailAddress(address); });
+ Action act = () => new MailAddress(address);
+ if (address is null)
+ {
+ Assert.Throws<ArgumentNullException>(act);
+ }
+ else if (address == string.Empty)
+ {
+ Assert.Throws<ArgumentException>(act);
+ }
+ else
+ {
+ Assert.Throws<FormatException>(act);
+ }
}
}
}