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
64 changes: 64 additions & 0 deletions TUnit.Assertions.Tests/ThrowInDelegateValueAssertionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,68 @@ await Assert.That(assertion)
.Throws<AssertionException>()
.WithMessageContaining("SYSTEM.EXCEPTION", StringComparison.OrdinalIgnoreCase);
}

[Test]
public async Task ThrowInDelegateValueAssertion_WithMessageNotContaining_Passes_WhenMessageDoesNotContainText()
{
var assertion = async () => await Assert.That(() =>
{
throw new Exception("This is an error message");
return true;
}).IsEqualTo(true);

await Assert.That(assertion)
.Throws<AssertionException>()
.WithMessageNotContaining("different text");
}

[Test]
public async Task ThrowInDelegateValueAssertion_WithMessageNotContaining_Fails_WhenMessageContainsText()
{
var assertion = async () => await Assert.That(() =>
{
throw new Exception("This is an error message");
return true;
}).IsEqualTo(true);

var finalAssertion = async () => await Assert.That(assertion)
.Throws<AssertionException>()
.WithMessageNotContaining("error message");

await Assert.That(finalAssertion)
.Throws<AssertionException>()
.WithMessageContaining("which message does not contain \"error message\"");
}

[Test]
public async Task ThrowInDelegateValueAssertion_WithMessageNotContaining_RespectsCaseInsensitive()
{
var assertion = async () => await Assert.That(() =>
{
throw new Exception("This is an ERROR message");
return true;
}).IsEqualTo(true);

var finalAssertion = async () => await Assert.That(assertion)
.Throws<AssertionException>()
.WithMessageNotContaining("error message", StringComparison.OrdinalIgnoreCase);

await Assert.That(finalAssertion)
.Throws<AssertionException>()
.WithMessageContaining("which message does not contain \"error message\"");
}

[Test]
public async Task ThrowInDelegateValueAssertion_WithMessageNotContaining_RespectsCaseSensitive()
{
var assertion = async () => await Assert.That(() =>
{
throw new Exception("This is an ERROR message");
return true;
}).IsEqualTo(true);

await Assert.That(assertion)
.Throws<AssertionException>()
.WithMessageNotContaining("error message", StringComparison.Ordinal);
}
}
14 changes: 14 additions & 0 deletions TUnit.Assertions/Assertions/Throws/ThrowsException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ public ThrowsException<TActual, TException> WithMessageContaining(string expecte
return this;
}

public ThrowsException<TActual, TException> WithMessageNotContaining(string expected, [CallerArgumentExpression(nameof(expected))] string? doNotPopulateThisValue = null)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: perhaps the parameter should be named notExpected instead of expected?

{
_source.RegisterAssertion(new ThrowsWithMessageNotContainingAssertCondition<TActual, TException>(expected, StringComparison.Ordinal, _selector)
, [doNotPopulateThisValue]);
return this;
}

public ThrowsException<TActual, TException> WithMessageNotContaining(string expected, StringComparison stringComparison, [CallerArgumentExpression(nameof(expected))] string? doNotPopulateThisValue = null, [CallerArgumentExpression(nameof(stringComparison))] string? doNotPopulateThisValue2 = null)
{
_source.RegisterAssertion(new ThrowsWithMessageNotContainingAssertCondition<TActual, TException>(expected, stringComparison, _selector)
, [doNotPopulateThisValue, doNotPopulateThisValue2]);
return this;
}

public ThrowsException<TActual, Exception> WithInnerException()
{
_source.AppendExpression($"{nameof(WithInnerException)}()");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using TUnit.Assertions.Extensions;

namespace TUnit.Assertions.AssertConditions.Throws;

public class ThrowsWithMessageNotContainingAssertCondition<TActual, TException>(
string expected,
StringComparison stringComparison,
Func<Exception?, Exception?> exceptionSelector)
: DelegateAssertCondition<TActual, Exception>
where TException : Exception
{
internal protected override string GetExpectation()
=> $"to throw {typeof(TException).Name.PrependAOrAn()} which message does not contain \"{expected?.ShowNewLines().TruncateWithEllipsis(100)}\"";

protected override ValueTask<AssertionResult> GetResult(
TActual? actualValue, Exception? exception,
AssertionMetadata assertionMetadata
)
{
var actualException = exceptionSelector(exception);

return AssertionResult
.FailIf(actualException is null,
"the exception is null")
.OrFailIf(actualException is not null && actualException.Message.Contains(expected, stringComparison),
$"found \"{actualException?.Message.ShowNewLines().TruncateWithEllipsis(100)}\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,13 @@ namespace .
protected override string GetExpectation() { }
protected override .<.> GetResult(TActual? actualValue, ? exception, .AssertionMetadata assertionMetadata) { }
}
public class ThrowsWithMessageNotContainingAssertCondition<TActual, TException> : .<TActual, >
where TException :
{
public ThrowsWithMessageNotContainingAssertCondition(string expected, stringComparison, <?, ?> exceptionSelector) { }
protected override string GetExpectation() { }
protected override .<.> GetResult(TActual? actualValue, ? exception, .AssertionMetadata assertionMetadata) { }
}
public class ThrowsWithParamNameAssertCondition<TActual, TException> : .<TActual, >
where TException :
{
Expand Down Expand Up @@ -2734,6 +2741,8 @@ namespace .Extensions
public .<TActual, TException> WithMessageContaining(string expected, [.("expected")] string? doNotPopulateThisValue = null) { }
public .<TActual, TException> WithMessageContaining(string expected, stringComparison, [.("expected")] string? doNotPopulateThisValue = null, [.("stringComparison")] string? doNotPopulateThisValue2 = null) { }
public .<TActual, TException> WithMessageMatching(. match, [.("match")] string? doNotPopulateThisValue = null) { }
public .<TActual, TException> WithMessageNotContaining(string expected, [.("expected")] string? doNotPopulateThisValue = null) { }
public .<TActual, TException> WithMessageNotContaining(string expected, stringComparison, [.("expected")] string? doNotPopulateThisValue = null, [.("stringComparison")] string? doNotPopulateThisValue2 = null) { }
public static .<TException?> op_Explicit(.<TActual, TException> throwsException) { }
}
public static class ThrowsExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,13 @@ namespace .
protected override string GetExpectation() { }
protected override .<.> GetResult(TActual? actualValue, ? exception, .AssertionMetadata assertionMetadata) { }
}
public class ThrowsWithMessageNotContainingAssertCondition<TActual, TException> : .<TActual, >
where TException :
{
public ThrowsWithMessageNotContainingAssertCondition(string expected, stringComparison, <?, ?> exceptionSelector) { }
protected override string GetExpectation() { }
protected override .<.> GetResult(TActual? actualValue, ? exception, .AssertionMetadata assertionMetadata) { }
}
public class ThrowsWithParamNameAssertCondition<TActual, TException> : .<TActual, >
where TException :
{
Expand Down Expand Up @@ -2734,6 +2741,8 @@ namespace .Extensions
public .<TActual, TException> WithMessageContaining(string expected, [.("expected")] string? doNotPopulateThisValue = null) { }
public .<TActual, TException> WithMessageContaining(string expected, stringComparison, [.("expected")] string? doNotPopulateThisValue = null, [.("stringComparison")] string? doNotPopulateThisValue2 = null) { }
public .<TActual, TException> WithMessageMatching(. match, [.("match")] string? doNotPopulateThisValue = null) { }
public .<TActual, TException> WithMessageNotContaining(string expected, [.("expected")] string? doNotPopulateThisValue = null) { }
public .<TActual, TException> WithMessageNotContaining(string expected, stringComparison, [.("expected")] string? doNotPopulateThisValue = null, [.("stringComparison")] string? doNotPopulateThisValue2 = null) { }
public static .<TException?> op_Explicit(.<TActual, TException> throwsException) { }
}
public static class ThrowsExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,13 @@ namespace .
protected override string GetExpectation() { }
protected override .<.> GetResult(TActual? actualValue, ? exception, .AssertionMetadata assertionMetadata) { }
}
public class ThrowsWithMessageNotContainingAssertCondition<TActual, TException> : .<TActual, >
where TException :
{
public ThrowsWithMessageNotContainingAssertCondition(string expected, stringComparison, <?, ?> exceptionSelector) { }
protected override string GetExpectation() { }
protected override .<.> GetResult(TActual? actualValue, ? exception, .AssertionMetadata assertionMetadata) { }
}
public class ThrowsWithParamNameAssertCondition<TActual, TException> : .<TActual, >
where TException :
{
Expand Down Expand Up @@ -2590,6 +2597,8 @@ namespace .Extensions
public .<TActual, TException> WithMessageContaining(string expected, [.("expected")] string? doNotPopulateThisValue = null) { }
public .<TActual, TException> WithMessageContaining(string expected, stringComparison, [.("expected")] string? doNotPopulateThisValue = null, [.("stringComparison")] string? doNotPopulateThisValue2 = null) { }
public .<TActual, TException> WithMessageMatching(. match, [.("match")] string? doNotPopulateThisValue = null) { }
public .<TActual, TException> WithMessageNotContaining(string expected, [.("expected")] string? doNotPopulateThisValue = null) { }
public .<TActual, TException> WithMessageNotContaining(string expected, stringComparison, [.("expected")] string? doNotPopulateThisValue = null, [.("stringComparison")] string? doNotPopulateThisValue2 = null) { }
public static .<TException?> op_Explicit(.<TActual, TException> throwsException) { }
}
public static class ThrowsExtensions
Expand Down
Loading