Skip to content

Commit ac078d6

Browse files
TyrrrzCopilot
andauthored
Add polyfills for ArgumentNullException.ThrowIfNull(...), ArgumentException.ThrowIfNullOrEmpty(...) and ArgumentException.ThrowIfNullOrWhiteSpace(...) (#37)
Co-authored-by: Copilot <[email protected]>
1 parent 16c3c53 commit ac078d6

4 files changed

Lines changed: 146 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using FluentAssertions;
3+
using Xunit;
4+
5+
namespace PolyShim.Tests.Net60;
6+
7+
public class ArgumentNullExceptionTests
8+
{
9+
[Fact]
10+
public void ThrowIfNull_Test()
11+
{
12+
// Arrange
13+
const string notNullValue = "Hello, World!";
14+
const string? nullValue = null;
15+
16+
// Act & assert
17+
ArgumentNullException.ThrowIfNull(notNullValue);
18+
19+
var ex = Assert.Throws<ArgumentNullException>(() =>
20+
ArgumentNullException.ThrowIfNull(nullValue)
21+
);
22+
23+
ex.ParamName.Should().Be(nameof(nullValue));
24+
}
25+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using FluentAssertions;
3+
using Xunit;
4+
5+
namespace PolyShim.Tests.Net70;
6+
7+
public class ArgumentExceptionTests
8+
{
9+
[Fact]
10+
public void ThrowIfNullOrEmpty_Test()
11+
{
12+
// Arrange
13+
const string notEmptyValue = "Hello, World!";
14+
const string emptyValue = "";
15+
const string? nullValue = null;
16+
17+
// Act & assert
18+
ArgumentException.ThrowIfNullOrEmpty(notEmptyValue);
19+
20+
var ex1 = Assert.Throws<ArgumentException>(() =>
21+
ArgumentException.ThrowIfNullOrEmpty(emptyValue)
22+
);
23+
24+
ex1.ParamName.Should().Be(nameof(emptyValue));
25+
26+
var ex2 = Assert.Throws<ArgumentNullException>(() =>
27+
ArgumentException.ThrowIfNullOrEmpty(nullValue)
28+
);
29+
30+
ex2.ParamName.Should().Be(nameof(nullValue));
31+
}
32+
33+
[Fact]
34+
public void ThrowIfNullOrWhiteSpace_Test()
35+
{
36+
// Arrange
37+
const string notWhiteSpaceValue = "Hello, World!";
38+
const string whiteSpaceValue = " ";
39+
const string? nullValue = null;
40+
41+
// Act & assert
42+
ArgumentException.ThrowIfNullOrWhiteSpace(notWhiteSpaceValue);
43+
44+
var ex1 = Assert.Throws<ArgumentException>(() =>
45+
ArgumentException.ThrowIfNullOrWhiteSpace(whiteSpaceValue)
46+
);
47+
48+
ex1.ParamName.Should().Be(nameof(whiteSpaceValue));
49+
50+
var ex2 = Assert.Throws<ArgumentNullException>(() =>
51+
ArgumentException.ThrowIfNullOrWhiteSpace(nullValue)
52+
);
53+
54+
ex2.ParamName.Should().Be(nameof(nullValue));
55+
}
56+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// The following comment is required to instruct analyzers to skip this file
2+
// <auto-generated/>
3+
4+
#if (NETCOREAPP && !NET6_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
5+
#nullable enable
6+
// ReSharper disable RedundantUsingDirective
7+
// ReSharper disable CheckNamespace
8+
// ReSharper disable InconsistentNaming
9+
// ReSharper disable PartialTypeWithSinglePart
10+
11+
using System;
12+
using System.Runtime.CompilerServices;
13+
14+
internal static partial class PolyfillExtensions
15+
{
16+
extension(ArgumentNullException)
17+
{
18+
// https://learn.microsoft.com/dotnet/api/system.argumentnullexception.throwifnull#system-argumentnullexception-throwifnull(system-object-system-string)
19+
public static void ThrowIfNull(object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
20+
{
21+
if (argument is null)
22+
throw new ArgumentNullException(paramName);
23+
}
24+
}
25+
}
26+
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// The following comment is required to instruct analyzers to skip this file
2+
// <auto-generated/>
3+
4+
#if (NETCOREAPP && !NET7_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
5+
#nullable enable
6+
// ReSharper disable RedundantUsingDirective
7+
// ReSharper disable CheckNamespace
8+
// ReSharper disable InconsistentNaming
9+
// ReSharper disable PartialTypeWithSinglePart
10+
11+
using System;
12+
using System.Runtime.CompilerServices;
13+
14+
internal static partial class PolyfillExtensions
15+
{
16+
extension(ArgumentException)
17+
{
18+
// https://learn.microsoft.com/dotnet/api/system.argumentexception.throwifnullorempty
19+
public static void ThrowIfNullOrEmpty(string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
20+
{
21+
if (argument is null)
22+
throw new ArgumentNullException(paramName);
23+
24+
if (string.IsNullOrEmpty(argument))
25+
throw new ArgumentException("The value cannot be an empty string.", paramName);
26+
}
27+
28+
// https://learn.microsoft.com/dotnet/api/system.argumentexception.throwifnullorwhitespace
29+
public static void ThrowIfNullOrWhiteSpace(string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
30+
{
31+
if (argument is null)
32+
throw new ArgumentNullException(paramName);
33+
34+
if (string.IsNullOrWhiteSpace(argument))
35+
throw new ArgumentException("The value cannot be an empty string or composed entirely of whitespace.", paramName);
36+
}
37+
}
38+
}
39+
#endif

0 commit comments

Comments
 (0)