Skip to content

Commit 308367a

Browse files
Merge branch 'main' into seo/3565-enabling-cors-in-asp-net-core
2 parents ba197b2 + c52333c commit 308367a

6 files changed

Lines changed: 131 additions & 28 deletions

File tree

‎csharp-classes-struct-record/HowToValidateGuid/HowToValidateGuid/Benchmark/HowToValidateGuidBenchmark.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using BenchmarkDotNet.Order;
55
using HowToValidateGuid;
66

7-
namespace HowToUseStringPool.Benchmark;
7+
namespace HowToValidateGuid.Benchmark;
88

99
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
1010
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByParams)]

‎csharp-classes-struct-record/HowToValidateGuid/HowToValidateGuid/GuidHelper.cs‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,63 @@ public partial class GuidHelper
77
[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}$")]
88
private static partial Regex GuidValidatorRegex();
99

10-
public static bool ValidateWithRegex(string input)
10+
public static bool ValidateWithRegex(string? input)
1111
{
12-
return GuidValidatorRegex().IsMatch(input);
12+
return input is not null && GuidValidatorRegex().IsMatch(input);
1313
}
1414

15-
public static bool ValidateWithGuidParse(string input)
15+
public static bool ValidateWithGuidParse(string? input)
1616
{
1717
try
1818
{
19-
Guid.Parse(input);
19+
Guid.Parse(input!);
2020
}
21-
catch (FormatException)
21+
catch (Exception ex) when (ex is FormatException or ArgumentNullException)
2222
{
2323
return false;
2424
}
2525

2626
return true;
2727
}
2828

29-
public static bool ValidateWithGuidParseExact(string input, string format)
29+
public static bool ValidateWithGuidParseExact(string? input, string format)
3030
{
3131
try
3232
{
33-
Guid.ParseExact(input, format);
33+
Guid.ParseExact(input!, format);
3434
}
35-
catch (FormatException)
35+
catch (Exception ex) when (ex is FormatException or ArgumentNullException)
3636
{
3737
return false;
3838
}
3939

4040
return true;
4141
}
4242

43-
public static bool ValidateWithGuidTryParse(string input)
43+
public static bool ValidateWithGuidTryParse(string? input)
4444
{
4545
return Guid.TryParse(input, out Guid _);
4646
}
4747

48-
public static bool ValidateWithGuidTryParseExact(string input, string format)
48+
public static bool ValidateWithGuidTryParseExact(string? input, string format)
4949
{
5050
return Guid.TryParseExact(input, format, out Guid _);
5151
}
5252

53-
public static bool ValidateWithNewGuid(string input)
53+
public static bool ValidateWithNewGuid(string? input)
5454
{
5555
try
5656
{
57-
var _ = new Guid(input);
57+
var _ = new Guid(input!);
5858
}
59-
catch (FormatException)
59+
catch (Exception ex) when (ex is FormatException or ArgumentNullException)
6060
{
6161
return false;
6262
}
6363

6464
return true;
6565
}
66+
67+
public static bool IsUsableGuid(string? input) =>
68+
Guid.TryParse(input, out var id) && id != Guid.Empty;
6669
}

‎csharp-classes-struct-record/HowToValidateGuid/HowToValidateGuid/HowToValidateGuid.csproj‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>disable</Nullable>
7+
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
1212
</ItemGroup>
1313

1414
</Project>

‎csharp-classes-struct-record/HowToValidateGuid/HowToValidateGuid/Program.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using BenchmarkDotNet.Running;
2-
using HowToUseStringPool.Benchmark;
2+
using HowToValidateGuid.Benchmark;
33
using HowToValidateGuid;
44

55
BenchmarkRunner.Run<HowToValidateGuidBenchmark>();

‎csharp-classes-struct-record/HowToValidateGuid/HowToValidateGuidTests/GuidHelperTests.cs‎

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace HowToValidateGuidTests
66
[TestClass]
77
public class GuidHelperTests
88
{
9-
private IFixture _fixture;
9+
private IFixture _fixture = new Fixture();
1010
private const string formatKey = "D";
1111

1212
[TestInitialize]
@@ -41,6 +41,14 @@ public void WhenValidateWithRegexCalled_ForAEmptyString_ThenResultMustBeFalse()
4141
Validate(input, GuidHelper.ValidateWithRegex, false);
4242
}
4343

44+
[TestMethod]
45+
public void WhenValidateWithRegexCalled_ForANullString_ThenResultMustBeFalse()
46+
{
47+
string? input = null;
48+
49+
Validate(input, GuidHelper.ValidateWithRegex, false);
50+
}
51+
4452
#endregion
4553

4654
#region ValidateWithGuidParse
@@ -69,6 +77,14 @@ public void WhenValidateWithGuidParseCalled_ForAEmptyString_ThenResultMustBeFals
6977
Validate(input, GuidHelper.ValidateWithGuidParse, false);
7078
}
7179

80+
[TestMethod]
81+
public void WhenValidateWithGuidParseCalled_ForANullString_ThenResultMustBeFalse()
82+
{
83+
string? input = null;
84+
85+
Validate(input, GuidHelper.ValidateWithGuidParse, false);
86+
}
87+
7288
#endregion
7389

7490
#region ValidateWithGuidParseExact
@@ -97,6 +113,22 @@ public void WhenValidateWithGuidParseExactCalled_ForAEmptyString_ThenResultMustB
97113
ValidateFormat(input, formatKey, GuidHelper.ValidateWithGuidParseExact, false);
98114
}
99115

116+
[TestMethod]
117+
public void WhenValidateWithGuidParseExactCalled_ForANullString_ThenResultMustBeFalse()
118+
{
119+
string? input = null;
120+
121+
ValidateFormat(input, formatKey, GuidHelper.ValidateWithGuidParseExact, false);
122+
}
123+
124+
[TestMethod]
125+
public void WhenValidateWithGuidParseExactCalled_ForAGuidStringInXFormat_ThenResultMustBeTrue()
126+
{
127+
var input = _fixture.Create<Guid>().ToString("X");
128+
129+
ValidateFormat(input, "X", GuidHelper.ValidateWithGuidParseExact, true);
130+
}
131+
100132
#endregion
101133

102134
#region ValidateWithGuidTryParse
@@ -125,6 +157,14 @@ public void WhenValidateWithGuidTryParseCalled_ForAEmptyString_ThenResultMustBeF
125157
Validate(input, GuidHelper.ValidateWithGuidTryParse, false);
126158
}
127159

160+
[TestMethod]
161+
public void WhenValidateWithGuidTryParseCalled_ForANullString_ThenResultMustBeFalse()
162+
{
163+
string? input = null;
164+
165+
Validate(input, GuidHelper.ValidateWithGuidTryParse, false);
166+
}
167+
128168
#endregion
129169

130170
#region ValidateWithGuidTryParseExact
@@ -153,6 +193,22 @@ public void WhenValidateWithGuidTryParseExactCalled_ForAEmptyString_ThenResultMu
153193
ValidateFormat(input, formatKey, GuidHelper.ValidateWithGuidTryParseExact, false);
154194
}
155195

196+
[TestMethod]
197+
public void WhenValidateWithGuidTryParseExactCalled_ForANullString_ThenResultMustBeFalse()
198+
{
199+
string? input = null;
200+
201+
ValidateFormat(input, formatKey, GuidHelper.ValidateWithGuidTryParseExact, false);
202+
}
203+
204+
[TestMethod]
205+
public void WhenValidateWithGuidTryParseExactCalled_ForAGuidStringInXFormat_ThenResultMustBeTrue()
206+
{
207+
var input = _fixture.Create<Guid>().ToString("X");
208+
209+
ValidateFormat(input, "X", GuidHelper.ValidateWithGuidTryParseExact, true);
210+
}
211+
156212
#endregion
157213

158214
#region ValidateWithNewGuid
@@ -181,16 +237,60 @@ public void WhenValidateWithNewGuidCalled_ForAEmptyString_ThenResultMustBeFalse(
181237
Validate(input, GuidHelper.ValidateWithNewGuid, false);
182238
}
183239

240+
[TestMethod]
241+
public void WhenValidateWithNewGuidCalled_ForANullString_ThenResultMustBeFalse()
242+
{
243+
string? input = null;
244+
245+
Validate(input, GuidHelper.ValidateWithNewGuid, false);
246+
}
247+
248+
#endregion
249+
250+
#region IsUsableGuid
251+
252+
[TestMethod]
253+
public void WhenIsUsableGuidCalled_ForANullString_ThenResultMustBeFalse()
254+
{
255+
string? input = null;
256+
257+
Validate(input, GuidHelper.IsUsableGuid, false);
258+
}
259+
260+
[TestMethod]
261+
public void WhenIsUsableGuidCalled_ForARandomString_ThenResultMustBeFalse()
262+
{
263+
var input = _fixture.Create<string>() + _fixture.Create<string>();
264+
265+
Validate(input, GuidHelper.IsUsableGuid, false);
266+
}
267+
268+
[TestMethod]
269+
public void WhenIsUsableGuidCalled_ForAnEmptyGuidString_ThenResultMustBeFalse()
270+
{
271+
var input = Guid.Empty.ToString();
272+
273+
Validate(input, GuidHelper.IsUsableGuid, false);
274+
}
275+
276+
[TestMethod]
277+
public void WhenIsUsableGuidCalled_ForAGuidString_ThenResultMustBeTrue()
278+
{
279+
var input = _fixture.Create<Guid>().ToString();
280+
281+
Validate(input, GuidHelper.IsUsableGuid, true);
282+
}
283+
184284
#endregion
185285

186-
private void Validate(string input, Func<string, bool> action, bool expected)
286+
private void Validate(string? input, Func<string?, bool> action, bool expected)
187287
{
188288
var result = action(input);
189289

190290
Assert.AreEqual(expected, result);
191291
}
192292

193-
private void ValidateFormat(string input, string format, Func<string, string, bool> action, bool expected)
293+
private void ValidateFormat(string? input, string format, Func<string?, string, bool> action, bool expected)
194294
{
195295
var result = action(input, format);
196296

‎csharp-classes-struct-record/HowToValidateGuid/HowToValidateGuidTests/HowToValidateGuidTests.csproj‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
6-
<Nullable>disable</Nullable>
6+
<Nullable>enable</Nullable>
77

88
<IsPackable>false</IsPackable>
99
<IsTestProject>true</IsTestProject>
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
14-
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
15-
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
16-
<PackageReference Include="coverlet.collector" Version="6.0.0" />
17-
<PackageReference Include="AutoFixture" Version="4.18.0" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
14+
<PackageReference Include="MSTest.TestAdapter" Version="4.5.1" />
15+
<PackageReference Include="MSTest.TestFramework" Version="4.5.1" />
16+
<PackageReference Include="coverlet.collector" Version="10.1.0" />
17+
<PackageReference Include="AutoFixture" Version="4.18.1" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

0 commit comments

Comments
 (0)