Fix MailAddress.TryCreate behavior (#47574)
authorhrrrrustic <35951936+hrrrrustic@users.noreply.github.com>
Wed, 24 Feb 2021 11:10:36 +0000 (14:10 +0300)
committerGitHub <noreply@github.com>
Wed, 24 Feb 2021 11:10:36 +0000 (12:10 +0100)
* change tests

* fix TryParse behavior

* fix spacing

* review improvements, change tests

* update throwing exception

* Update src/libraries/System.Net.Mail/tests/Unit/MailAddressTests/MailAddressParsingTest.cs

* Update src/libraries/System.Net.Mail/tests/Functional/MailAddressTest.cs

Co-authored-by: Marie Píchová <11718369+ManickaP@users.noreply.github.com>
src/libraries/System.Net.Mail/src/System/Net/Mail/MailAddress.cs
src/libraries/System.Net.Mail/tests/Functional/MailAddressTest.cs
src/libraries/System.Net.Mail/tests/Unit/MailAddressTests/MailAddressParsingTest.cs

index dd19ec1..f5ea92b 100644 (file)
@@ -119,13 +119,17 @@ namespace System.Net.Mail
 
         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);
index 9abfcab..019ceee 100644 (file)
@@ -36,14 +36,16 @@ namespace System.Net.Mail.Tests
         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()
@@ -58,13 +60,27 @@ namespace System.Net.Mail.Tests
             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()
index 55b8891..f1755e9 100644 (file)
@@ -132,6 +132,8 @@ namespace System.Net.Mail.Tests
             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]
@@ -163,7 +165,19 @@ namespace System.Net.Mail.Tests
         [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);
+            }
         }
     }
 }