From 69304d142a7ae9173d50efdfa20a1150b655f54f Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 30 Aug 2026 21:40:36 +0200 Subject: [PATCH] Parse and TryParse: retarget net10.0, MSTest 4.3.3, cover the .NET 7/8 overloads Retarget both projects from net6.0 to net10.0 and bring the test stack up to the current line: Microsoft.NET.Test.Sdk 18.9.0, MSTest 4.3.3, coverlet 10.0.1. MSTest 4.x removes Assert.ThrowsException, so the twelve call sites across the eight negative tests move to Assert.Throws. Add two tests for overloads the sample did not exercise: the IFormatProvider overload introduced with IParsable, and the UTF-8 ReadOnlySpan overload introduced with IUtf8SpanParsable. Two null call sites needed a fix under enable on net10.0: int.Parse(null) emitted CS8625 and is now int.Parse(null!), and int.TryParse(null, out _) no longer compiles at all (CS0121, ambiguous between the string and ReadOnlySpan overloads) and is now (string?)null. Program.cs moves to a file-scoped namespace. Build: 0 warnings. Tests: 20/20 passing on SDK 10.0.302. --- .../ParseAndTryParseInCSharp.csproj | 2 +- .../ParseAndTryParseInCSharp/Program.cs | 9 ++-- .../ParseAndTryParseInCSharp/Tests/Tests.cs | 50 ++++++++++++++----- .../Tests/Tests.csproj | 10 ++-- 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/numbers-csharp/ParseAndTryParseInCSharp/ParseAndTryParseInCSharp/ParseAndTryParseInCSharp.csproj b/numbers-csharp/ParseAndTryParseInCSharp/ParseAndTryParseInCSharp/ParseAndTryParseInCSharp.csproj index 41f1d5ad4b..a15a29bf12 100644 --- a/numbers-csharp/ParseAndTryParseInCSharp/ParseAndTryParseInCSharp/ParseAndTryParseInCSharp.csproj +++ b/numbers-csharp/ParseAndTryParseInCSharp/ParseAndTryParseInCSharp/ParseAndTryParseInCSharp.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net10.0 diff --git a/numbers-csharp/ParseAndTryParseInCSharp/ParseAndTryParseInCSharp/Program.cs b/numbers-csharp/ParseAndTryParseInCSharp/ParseAndTryParseInCSharp/Program.cs index 82b04c590e..70f8931397 100644 --- a/numbers-csharp/ParseAndTryParseInCSharp/ParseAndTryParseInCSharp/Program.cs +++ b/numbers-csharp/ParseAndTryParseInCSharp/ParseAndTryParseInCSharp/Program.cs @@ -1,7 +1,6 @@ -namespace ParseAndTryParseInCSharp +namespace ParseAndTryParseInCSharp; + +public class Program { - public class Program - { - public static void Main(string[] args) { } - } + public static void Main(string[] args) { } } diff --git a/numbers-csharp/ParseAndTryParseInCSharp/Tests/Tests.cs b/numbers-csharp/ParseAndTryParseInCSharp/Tests/Tests.cs index e711a772ee..cf8b831d95 100644 --- a/numbers-csharp/ParseAndTryParseInCSharp/Tests/Tests.cs +++ b/numbers-csharp/ParseAndTryParseInCSharp/Tests/Tests.cs @@ -18,11 +18,11 @@ public void GivenIntParse_WhenValid_ThenConversionSuccess() [TestMethod] public void GivenIntParse_WhenInvalid_ThenConversionThrowsException() { - Assert.ThrowsException(() => int.Parse("3456.89")); + Assert.Throws(() => int.Parse("3456.89")); - Assert.ThrowsException(() => int.Parse("34343454574745")); + Assert.Throws(() => int.Parse("34343454574745")); - Assert.ThrowsException(() => int.Parse(null)); + Assert.Throws(() => int.Parse(null!)); } [TestMethod] @@ -40,11 +40,11 @@ public void GivenIntParseFormat_WhenInvalid_ThenConversionThrowsException() var culture = new CultureInfo("en-US"); culture.NumberFormat.PositiveSign = "#"; - Assert.ThrowsException(() => int.Parse("#4567")); + Assert.Throws(() => int.Parse("#4567")); - Assert.ThrowsException(() => int.Parse("$4561", culture)); + Assert.Throws(() => int.Parse("$4561", culture)); - Assert.ThrowsException(() => int.Parse("#34343454574745", culture)); + Assert.Throws(() => int.Parse("#34343454574745", culture)); } [TestMethod] @@ -60,9 +60,9 @@ public void GivenIntParseStyles_WhenValid_ThenConversionSuccess() [TestMethod] public void GivenIntParseStyles_WhenInvalid_ThenConversionThrowsException() { - Assert.ThrowsException(() => int.Parse("$45,618", NumberStyles.AllowThousands)); + Assert.Throws(() => int.Parse("$45,618", NumberStyles.AllowThousands)); - Assert.ThrowsException(() => int.Parse("56.89", NumberStyles.AllowDecimalPoint)); + Assert.Throws(() => int.Parse("56.89", NumberStyles.AllowDecimalPoint)); } [TestMethod] @@ -78,9 +78,9 @@ public void GivenIntParseFormatStyles_WhenValid_ThenConversionSuccess() [TestMethod] public void GivenIntParseFormatStyles_WhenInvalid_ThenConversionThrowsException() { - Assert.ThrowsException(() => int.Parse("$78,000", NumberStyles.Float, new CultureInfo("en-US"))); + Assert.Throws(() => int.Parse("$78,000", NumberStyles.Float, new CultureInfo("en-US"))); - Assert.ThrowsException(() => int.Parse("78.567", NumberStyles.AllowDecimalPoint, new CultureInfo("en-US"))); + Assert.Throws(() => int.Parse("78.567", NumberStyles.AllowDecimalPoint, new CultureInfo("en-US"))); } [TestMethod] @@ -96,9 +96,9 @@ public void GivenIntParseFormatStylesSpan_WhenValid_ThenConversionSuccess() [TestMethod] public void GivenIntParseFormatStylesSpan_WhenInvalid_ThenConversionThrowsException() { - Assert.ThrowsException(() => int.Parse("$78,000".AsSpan(), NumberStyles.Float, new CultureInfo("en-US"))); + Assert.Throws(() => int.Parse("$78,000".AsSpan(), NumberStyles.Float, new CultureInfo("en-US"))); - Assert.ThrowsException(() => int.Parse("78.567".AsSpan(), NumberStyles.AllowDecimalPoint, new CultureInfo("en-US"))); + Assert.Throws(() => int.Parse("78.567".AsSpan(), NumberStyles.AllowDecimalPoint, new CultureInfo("en-US"))); } [TestMethod] @@ -116,7 +116,7 @@ public void GivenIntTryParse_WhenInvalid_ThenNoExceptionReturnFalse() Assert.IsFalse(int.TryParse("34343454574745", out int overflowNum)); - Assert.IsFalse(int.TryParse(null, out int nullNum)); + Assert.IsFalse(int.TryParse((string?)null, out int nullNum)); } [TestMethod] @@ -179,6 +179,30 @@ public void GivenIntTryParseFormatStylesSpan_WhenValid_ThenConversionSuccess() Assert.AreEqual(78, usNum); } + [TestMethod] + public void GivenIntTryParseProvider_WhenValid_ThenConversionSuccess() + { + Assert.IsTrue(int.TryParse("78000", new CultureInfo("en-US"), out int usNum)); + + Assert.AreEqual(78000, usNum); + + Assert.IsFalse(int.TryParse("abc", new CultureInfo("en-US"), out int invalidNum)); + + Assert.AreEqual(0, invalidNum); + } + + [TestMethod] + public void GivenIntTryParseUtf8_WhenValid_ThenConversionSuccess() + { + Assert.IsTrue(int.TryParse("45689"u8, out int result)); + + Assert.AreEqual(45689, result); + + Assert.IsFalse(int.TryParse("3456.89"u8, out int formatNum)); + + Assert.AreEqual(0, formatNum); + } + [TestMethod] public void GivenIntTryParseFormatStylesSpan_WhenInvalid_ThenNoExceptionReturnFalse() { diff --git a/numbers-csharp/ParseAndTryParseInCSharp/Tests/Tests.csproj b/numbers-csharp/ParseAndTryParseInCSharp/Tests/Tests.csproj index baa28473e5..9808d150ac 100644 --- a/numbers-csharp/ParseAndTryParseInCSharp/Tests/Tests.csproj +++ b/numbers-csharp/ParseAndTryParseInCSharp/Tests/Tests.csproj @@ -1,17 +1,17 @@ - net6.0 + net10.0 enable false - - - - + + + +