Expose/test Guid.{Try}Parse{Exact} span-based methods (dotnet/corefx#24204)
authorStephen Toub <stoub@microsoft.com>
Sat, 23 Sep 2017 14:32:21 +0000 (10:32 -0400)
committerGitHub <noreply@github.com>
Sat, 23 Sep 2017 14:32:21 +0000 (10:32 -0400)
Just adds the methods to the refs and copies/updates the existing parsing tests to work with the new span-based methods.

Commit migrated from https://github.com/dotnet/corefx/commit/f25f97d837a33534f5a6e2f7da9c0989210d3f4c

src/libraries/System.Runtime/ref/System.Runtime.cs
src/libraries/System.Runtime/tests/System/GuidTests.netcoreapp.cs

index d24e923..fbd1c2c 100644 (file)
@@ -1313,7 +1313,9 @@ namespace System
         public static bool operator ==(System.Guid a, System.Guid b) { throw null; }
         public static bool operator !=(System.Guid a, System.Guid b) { throw null; }
         public static System.Guid Parse(string input) { throw null; }
+        public static System.Guid Parse(System.ReadOnlySpan<char> input) { throw null; }
         public static System.Guid ParseExact(string input, string format) { throw null; }
+        public static System.Guid ParseExact(System.ReadOnlySpan<char> input, string format) { throw null; }
         public byte[] ToByteArray() { throw null; }
         public bool TryWriteBytes(Span<byte> destination) { throw null; }
         public override string ToString() { throw null; }
@@ -1321,7 +1323,9 @@ namespace System
         public string ToString(string format, System.IFormatProvider provider) { throw null; }
         public bool TryFormat(Span<char> destination, out int charsWritten, string format) { throw null; }
         public static bool TryParse(string input, out System.Guid result) { throw null; }
+        public static bool TryParse(System.ReadOnlySpan<char> input, out System.Guid result) { throw null; }
         public static bool TryParseExact(string input, string format, out System.Guid result) { throw null; }
+        public static bool TryParseExact(System.ReadOnlySpan<char> input, string format, out System.Guid result) { throw null; }
     }
     public partial interface IAsyncResult
     {
index 555aa3f..512f355 100644 (file)
@@ -72,5 +72,53 @@ namespace System.Tests
             Assert.True(guid.TryFormat(new Span<char>(chars), out int charsWritten, format));
             Assert.Equal(chars, expected.ToCharArray());
         }
+
+        [Theory]
+        [MemberData(nameof(GuidStrings_Valid_TestData))]
+        public static void Parse_Span_ValidInput_Success(string input, string format, Guid expected)
+        {
+            Assert.Equal(expected, Guid.Parse(input.AsReadOnlySpan()));
+            Assert.Equal(expected, Guid.ParseExact(input.AsReadOnlySpan(), format.ToUpperInvariant()));
+            Assert.Equal(expected, Guid.ParseExact(input.AsReadOnlySpan(), format.ToLowerInvariant())); // Format should be case insensitive
+
+            Guid result;
+
+            Assert.True(Guid.TryParse(input.AsReadOnlySpan(), out result));
+            Assert.Equal(expected, result);
+
+            Assert.True(Guid.TryParseExact(input.AsReadOnlySpan(), format.ToUpperInvariant(), out result));
+            Assert.Equal(expected, result);
+
+            Assert.True(Guid.TryParseExact(input.AsReadOnlySpan(), format.ToLowerInvariant(), out result)); // Format should be case insensitive
+            Assert.Equal(expected, result);
+        }
+
+        [Theory]
+        [MemberData(nameof(GuidStrings_Invalid_TestData))]
+        public static void Parse_Span_InvalidInput_Fails(string input, Type exceptionType)
+        {
+            if (input == null)
+            {
+                return;
+            }
+
+            // Overflow exceptions throw as format exceptions in Parse
+            if (exceptionType.Equals(typeof(OverflowException)))
+            {
+                exceptionType = typeof(FormatException);
+            }
+            Assert.Throws(exceptionType, () => Guid.Parse(input.AsReadOnlySpan()));
+
+            Assert.False(Guid.TryParse(input.AsReadOnlySpan(), out Guid result));
+            Assert.Equal(Guid.Empty, result);
+
+            foreach (string format in new[] { "N", "D", "B", "P", "X" })
+            {
+                Assert.Throws(exceptionType, () => Guid.ParseExact(input.AsReadOnlySpan(), format));
+
+                Assert.False(Guid.TryParseExact(input.AsReadOnlySpan(), format, out result));
+                Assert.Equal(Guid.Empty, result);
+            }
+        }
     }
 }