Skip to content
Merged
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 @@ -4,7 +4,7 @@
using BenchmarkDotNet.Order;
using HowToValidateGuid;

namespace HowToUseStringPool.Benchmark;
namespace HowToValidateGuid.Benchmark;

[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByParams)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,63 @@ public partial class GuidHelper
[GeneratedRegex("^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$")]
private static partial Regex GuidValidatorRegex();

public static bool ValidateWithRegex(string input)
public static bool ValidateWithRegex(string? input)
{
return GuidValidatorRegex().IsMatch(input);
return input is not null && GuidValidatorRegex().IsMatch(input);
}

public static bool ValidateWithGuidParse(string input)
public static bool ValidateWithGuidParse(string? input)
{
try
{
Guid.Parse(input);
Guid.Parse(input!);
}
catch (FormatException)
catch (Exception ex) when (ex is FormatException or ArgumentNullException)
{
return false;
}

return true;
}

public static bool ValidateWithGuidParseExact(string input, string format)
public static bool ValidateWithGuidParseExact(string? input, string format)
{
try
{
Guid.ParseExact(input, format);
Guid.ParseExact(input!, format);
}
catch (FormatException)
catch (Exception ex) when (ex is FormatException or ArgumentNullException)
{
return false;
}

return true;
}

public static bool ValidateWithGuidTryParse(string input)
public static bool ValidateWithGuidTryParse(string? input)
{
return Guid.TryParse(input, out Guid _);
}

public static bool ValidateWithGuidTryParseExact(string input, string format)
public static bool ValidateWithGuidTryParseExact(string? input, string format)
{
return Guid.TryParseExact(input, format, out Guid _);
}

public static bool ValidateWithNewGuid(string input)
public static bool ValidateWithNewGuid(string? input)
{
try
{
var _ = new Guid(input);
var _ = new Guid(input!);
}
catch (FormatException)
catch (Exception ex) when (ex is FormatException or ArgumentNullException)
{
return false;
}

return true;
}

public static bool IsUsableGuid(string? input) =>
Guid.TryParse(input, out var id) && id != Guid.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using BenchmarkDotNet.Running;
using HowToUseStringPool.Benchmark;
using HowToValidateGuid.Benchmark;
using HowToValidateGuid;

BenchmarkRunner.Run<HowToValidateGuidBenchmark>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace HowToValidateGuidTests
[TestClass]
public class GuidHelperTests
{
private IFixture _fixture;
private IFixture _fixture = new Fixture();
private const string formatKey = "D";

[TestInitialize]
Expand Down Expand Up @@ -41,6 +41,14 @@ public void WhenValidateWithRegexCalled_ForAEmptyString_ThenResultMustBeFalse()
Validate(input, GuidHelper.ValidateWithRegex, false);
}

[TestMethod]
public void WhenValidateWithRegexCalled_ForANullString_ThenResultMustBeFalse()
{
string? input = null;

Validate(input, GuidHelper.ValidateWithRegex, false);
}

#endregion

#region ValidateWithGuidParse
Expand Down Expand Up @@ -69,6 +77,14 @@ public void WhenValidateWithGuidParseCalled_ForAEmptyString_ThenResultMustBeFals
Validate(input, GuidHelper.ValidateWithGuidParse, false);
}

[TestMethod]
public void WhenValidateWithGuidParseCalled_ForANullString_ThenResultMustBeFalse()
{
string? input = null;

Validate(input, GuidHelper.ValidateWithGuidParse, false);
}

#endregion

#region ValidateWithGuidParseExact
Expand Down Expand Up @@ -97,6 +113,22 @@ public void WhenValidateWithGuidParseExactCalled_ForAEmptyString_ThenResultMustB
ValidateFormat(input, formatKey, GuidHelper.ValidateWithGuidParseExact, false);
}

[TestMethod]
public void WhenValidateWithGuidParseExactCalled_ForANullString_ThenResultMustBeFalse()
{
string? input = null;

ValidateFormat(input, formatKey, GuidHelper.ValidateWithGuidParseExact, false);
}

[TestMethod]
public void WhenValidateWithGuidParseExactCalled_ForAGuidStringInXFormat_ThenResultMustBeTrue()
{
var input = _fixture.Create<Guid>().ToString("X");

ValidateFormat(input, "X", GuidHelper.ValidateWithGuidParseExact, true);
}

#endregion

#region ValidateWithGuidTryParse
Expand Down Expand Up @@ -125,6 +157,14 @@ public void WhenValidateWithGuidTryParseCalled_ForAEmptyString_ThenResultMustBeF
Validate(input, GuidHelper.ValidateWithGuidTryParse, false);
}

[TestMethod]
public void WhenValidateWithGuidTryParseCalled_ForANullString_ThenResultMustBeFalse()
{
string? input = null;

Validate(input, GuidHelper.ValidateWithGuidTryParse, false);
}

#endregion

#region ValidateWithGuidTryParseExact
Expand Down Expand Up @@ -153,6 +193,22 @@ public void WhenValidateWithGuidTryParseExactCalled_ForAEmptyString_ThenResultMu
ValidateFormat(input, formatKey, GuidHelper.ValidateWithGuidTryParseExact, false);
}

[TestMethod]
public void WhenValidateWithGuidTryParseExactCalled_ForANullString_ThenResultMustBeFalse()
{
string? input = null;

ValidateFormat(input, formatKey, GuidHelper.ValidateWithGuidTryParseExact, false);
}

[TestMethod]
public void WhenValidateWithGuidTryParseExactCalled_ForAGuidStringInXFormat_ThenResultMustBeTrue()
{
var input = _fixture.Create<Guid>().ToString("X");

ValidateFormat(input, "X", GuidHelper.ValidateWithGuidTryParseExact, true);
}

#endregion

#region ValidateWithNewGuid
Expand Down Expand Up @@ -181,16 +237,60 @@ public void WhenValidateWithNewGuidCalled_ForAEmptyString_ThenResultMustBeFalse(
Validate(input, GuidHelper.ValidateWithNewGuid, false);
}

[TestMethod]
public void WhenValidateWithNewGuidCalled_ForANullString_ThenResultMustBeFalse()
{
string? input = null;

Validate(input, GuidHelper.ValidateWithNewGuid, false);
}

#endregion

#region IsUsableGuid

[TestMethod]
public void WhenIsUsableGuidCalled_ForANullString_ThenResultMustBeFalse()
{
string? input = null;

Validate(input, GuidHelper.IsUsableGuid, false);
}

[TestMethod]
public void WhenIsUsableGuidCalled_ForARandomString_ThenResultMustBeFalse()
{
var input = _fixture.Create<string>() + _fixture.Create<string>();

Validate(input, GuidHelper.IsUsableGuid, false);
}

[TestMethod]
public void WhenIsUsableGuidCalled_ForAnEmptyGuidString_ThenResultMustBeFalse()
{
var input = Guid.Empty.ToString();

Validate(input, GuidHelper.IsUsableGuid, false);
}

[TestMethod]
public void WhenIsUsableGuidCalled_ForAGuidString_ThenResultMustBeTrue()
{
var input = _fixture.Create<Guid>().ToString();

Validate(input, GuidHelper.IsUsableGuid, true);
}

#endregion

private void Validate(string input, Func<string, bool> action, bool expected)
private void Validate(string? input, Func<string?, bool> action, bool expected)
{
var result = action(input);

Assert.AreEqual(expected, result);
}

private void ValidateFormat(string input, string format, Func<string, string, bool> action, bool expected)
private void ValidateFormat(string? input, string format, Func<string?, string, bool> action, bool expected)
{
var result = action(input, format);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="AutoFixture" Version="4.18.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.1" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.1" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
<PackageReference Include="AutoFixture" Version="4.18.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading