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
4 changes: 1 addition & 3 deletions src/GuardClauses/GuardAgainstExpressionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -27,7 +27,6 @@ public static T Expression<T>(this IGuardClause guardClause,
string message,
[CallerArgumentExpression("input")] string? parameterName = null,
Func<Exception>? exceptionCreator = null)
where T : struct
{
if (func(input))
{
Expand Down Expand Up @@ -60,7 +59,6 @@ public static async Task<T> ExpressionAsync<T>(this IGuardClause guardClause,
string message,
[CallerArgumentExpression("input")] string? parameterName = null,
Func<Exception>? exceptionCreator = null)
where T : struct
{
if (await func(input))
{
Expand Down
22 changes: 20 additions & 2 deletions test/GuardClauses.UnitTests/GuardAgainstExpression.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using Ardalis.GuardClauses;
using Xunit;
Expand Down Expand Up @@ -98,7 +98,25 @@
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.Expression(x => x == 2, 2, "custom message", paramName));
Assert.NotNull(exception);
Assert.NotNull(exception.Message);
Assert.Equal(paramName, exception.ParamName);
Assert.Contains(paramName, exception.Message);
}

public class CustomClass
{
public string Name { get; set; } = string.Empty;
}

[Fact]
public void GivenReferenceTypeWhenTheExpressionEvaluatesToTrueThrowsException()
{
var testCase = new CustomClass { Name = "Test" };
Assert.Throws<ArgumentException>(() => Guard.Against.Expression((x) => x.Name == "Test", testCase, "Name cannot be Test"));
}

[Fact]
public void GivenReferenceTypeWhenTheExpressionEvaluatesToFalseDoesNothing()

Check failure on line 117 in test/GuardClauses.UnitTests/GuardAgainstExpression.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add at least one assertion to this test case.

See more on https://sonarcloud.io/project/issues?id=ardalis_GuardClauses&issues=AaAQz2Fonr57X1Rx7Vsu&open=AaAQz2Fonr57X1Rx7Vsu&pullRequest=406
{
var testCase = new CustomClass { Name = "Test" };
Guard.Against.Expression((x) => x.Name == "Other", testCase, "Name cannot be Other");
}
}
Loading