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
25 changes: 25 additions & 0 deletions PolyShim.Tests/Net60/ArgumentNullExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net60;

public class ArgumentNullExceptionTests
{
[Fact]
public void ThrowIfNull_Test()
{
// Arrange
const string notNullValue = "Hello, World!";
const string? nullValue = null;

// Act & assert
ArgumentNullException.ThrowIfNull(notNullValue);

var ex = Assert.Throws<ArgumentNullException>(() =>
ArgumentNullException.ThrowIfNull(nullValue)
);

ex.ParamName.Should().Be(nameof(nullValue));
}
}
56 changes: 56 additions & 0 deletions PolyShim.Tests/Net70/ArgumentExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net70;

public class ArgumentExceptionTests
{
[Fact]
public void ThrowIfNullOrEmpty_Test()
{
// Arrange
const string notEmptyValue = "Hello, World!";
const string emptyValue = "";
const string? nullValue = null;

// Act & assert
ArgumentException.ThrowIfNullOrEmpty(notEmptyValue);

var ex1 = Assert.Throws<ArgumentException>(() =>
ArgumentException.ThrowIfNullOrEmpty(emptyValue)
);

ex1.ParamName.Should().Be(nameof(emptyValue));

var ex2 = Assert.Throws<ArgumentNullException>(() =>
ArgumentException.ThrowIfNullOrEmpty(nullValue)
);

ex2.ParamName.Should().Be(nameof(nullValue));
}

[Fact]
public void ThrowIfNullOrWhiteSpace_Test()
{
// Arrange
const string notWhiteSpaceValue = "Hello, World!";
const string whiteSpaceValue = " ";
const string? nullValue = null;

// Act & assert
ArgumentException.ThrowIfNullOrWhiteSpace(notWhiteSpaceValue);

var ex1 = Assert.Throws<ArgumentException>(() =>
ArgumentException.ThrowIfNullOrWhiteSpace(whiteSpaceValue)
);

ex1.ParamName.Should().Be(nameof(whiteSpaceValue));

var ex2 = Assert.Throws<ArgumentNullException>(() =>
ArgumentException.ThrowIfNullOrWhiteSpace(nullValue)
);

ex2.ParamName.Should().Be(nameof(nullValue));
}
}
26 changes: 26 additions & 0 deletions PolyShim/Net60/ArgumentNullException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// The following comment is required to instruct analyzers to skip this file
// <auto-generated/>

#if (NETCOREAPP && !NET6_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System;
using System.Runtime.CompilerServices;

internal static partial class PolyfillExtensions
{
extension(ArgumentNullException)
{
// https://learn.microsoft.com/dotnet/api/system.argumentnullexception.throwifnull#system-argumentnullexception-throwifnull(system-object-system-string)
public static void ThrowIfNull(object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (argument is null)
throw new ArgumentNullException(paramName);
}
}
}
#endif
39 changes: 39 additions & 0 deletions PolyShim/Net70/ArgumentException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// The following comment is required to instruct analyzers to skip this file
// <auto-generated/>

#if (NETCOREAPP && !NET7_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System;
using System.Runtime.CompilerServices;

internal static partial class PolyfillExtensions
{
extension(ArgumentException)
{
// https://learn.microsoft.com/dotnet/api/system.argumentexception.throwifnullorempty
public static void ThrowIfNullOrEmpty(string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (argument is null)
throw new ArgumentNullException(paramName);

if (string.IsNullOrEmpty(argument))
throw new ArgumentException("The value cannot be an empty string.", paramName);
}

// https://learn.microsoft.com/dotnet/api/system.argumentexception.throwifnullorwhitespace
public static void ThrowIfNullOrWhiteSpace(string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (argument is null)
throw new ArgumentNullException(paramName);

if (string.IsNullOrWhiteSpace(argument))
throw new ArgumentException("The value cannot be an empty string or composed entirely of whitespace.", paramName);
}
}
}
#endif
Loading