Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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) { }
}
50 changes: 37 additions & 13 deletions numbers-csharp/ParseAndTryParseInCSharp/Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public void GivenIntParse_WhenValid_ThenConversionSuccess()
[TestMethod]
public void GivenIntParse_WhenInvalid_ThenConversionThrowsException()
{
Assert.ThrowsException<FormatException>(() => int.Parse("3456.89"));
Assert.Throws<FormatException>(() => int.Parse("3456.89"));

Assert.ThrowsException<OverflowException>(() => int.Parse("34343454574745"));
Assert.Throws<OverflowException>(() => int.Parse("34343454574745"));

Assert.ThrowsException<ArgumentNullException>(() => int.Parse(null));
Assert.Throws<ArgumentNullException>(() => int.Parse(null!));
}

[TestMethod]
Expand All @@ -40,11 +40,11 @@ public void GivenIntParseFormat_WhenInvalid_ThenConversionThrowsException()
var culture = new CultureInfo("en-US");
culture.NumberFormat.PositiveSign = "#";

Assert.ThrowsException<FormatException>(() => int.Parse("#4567"));
Assert.Throws<FormatException>(() => int.Parse("#4567"));

Assert.ThrowsException<FormatException>(() => int.Parse("$4561", culture));
Assert.Throws<FormatException>(() => int.Parse("$4561", culture));

Assert.ThrowsException<OverflowException>(() => int.Parse("#34343454574745", culture));
Assert.Throws<OverflowException>(() => int.Parse("#34343454574745", culture));
}

[TestMethod]
Expand All @@ -60,9 +60,9 @@ public void GivenIntParseStyles_WhenValid_ThenConversionSuccess()
[TestMethod]
public void GivenIntParseStyles_WhenInvalid_ThenConversionThrowsException()
{
Assert.ThrowsException<FormatException>(() => int.Parse("$45,618", NumberStyles.AllowThousands));
Assert.Throws<FormatException>(() => int.Parse("$45,618", NumberStyles.AllowThousands));

Assert.ThrowsException<OverflowException>(() => int.Parse("56.89", NumberStyles.AllowDecimalPoint));
Assert.Throws<OverflowException>(() => int.Parse("56.89", NumberStyles.AllowDecimalPoint));
}

[TestMethod]
Expand All @@ -78,9 +78,9 @@ public void GivenIntParseFormatStyles_WhenValid_ThenConversionSuccess()
[TestMethod]
public void GivenIntParseFormatStyles_WhenInvalid_ThenConversionThrowsException()
{
Assert.ThrowsException<FormatException>(() => int.Parse("$78,000", NumberStyles.Float, new CultureInfo("en-US")));
Assert.Throws<FormatException>(() => int.Parse("$78,000", NumberStyles.Float, new CultureInfo("en-US")));

Assert.ThrowsException<OverflowException>(() => int.Parse("78.567", NumberStyles.AllowDecimalPoint, new CultureInfo("en-US")));
Assert.Throws<OverflowException>(() => int.Parse("78.567", NumberStyles.AllowDecimalPoint, new CultureInfo("en-US")));
}

[TestMethod]
Expand All @@ -96,9 +96,9 @@ public void GivenIntParseFormatStylesSpan_WhenValid_ThenConversionSuccess()
[TestMethod]
public void GivenIntParseFormatStylesSpan_WhenInvalid_ThenConversionThrowsException()
{
Assert.ThrowsException<FormatException>(() => int.Parse("$78,000".AsSpan(), NumberStyles.Float, new CultureInfo("en-US")));
Assert.Throws<FormatException>(() => int.Parse("$78,000".AsSpan(), NumberStyles.Float, new CultureInfo("en-US")));

Assert.ThrowsException<OverflowException>(() => int.Parse("78.567".AsSpan(), NumberStyles.AllowDecimalPoint, new CultureInfo("en-US")));
Assert.Throws<OverflowException>(() => int.Parse("78.567".AsSpan(), NumberStyles.AllowDecimalPoint, new CultureInfo("en-US")));
}

[TestMethod]
Expand All @@ -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]
Expand Down Expand Up @@ -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()
{
Expand Down
10 changes: 5 additions & 5 deletions numbers-csharp/ParseAndTryParseInCSharp/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.3.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.3.3" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading