Repository navigation
fix(mocks): Is<T>(predicate) passes T, not T?, for non-nullable T #7405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2dd5984
fix(mocks): pass T, not T?, to Is<T>(predicate) for non-nullable T (#…
thomhurst ce3cf5e
test(mocks): enforce predicate nullable diagnostics for #7304
thomhurst 68112ca
docs(mocks): clarify null predicate exception behavior (#7304)
thomhurst 9239682
fix(mocks): preserve predicate exception propagation (#7304)
thomhurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| using TUnit.Mocks; | ||
| using TUnit.Mocks.Arguments; | ||
|
|
||
| namespace TUnit.Mocks.Tests; | ||
|
|
||
| public record PredicateUser(string Name); | ||
|
|
||
| public interface IPredicateUserRepo | ||
| { | ||
| string Save(PredicateUser user); | ||
| } | ||
|
|
||
| public interface INullablePredicateUserRepo | ||
| { | ||
| string Save(PredicateUser? user); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Regression tests for #7304: <c>Is<T>(predicate)</c> passes <c>T</c> (not <c>T?</c>) for a | ||
| /// non-nullable reference type, so predicates can dereference their argument without a null guard. | ||
| /// </summary> | ||
| public class PredicateNullabilityTests | ||
| { | ||
| [Test] | ||
| public async Task Predicate_For_NonNullable_Reference_Type_Dereferences_Without_Null_Guard() | ||
| { | ||
| var mock = IPredicateUserRepo.Mock(); | ||
| mock.Save(Is<PredicateUser>(u => u.Name == "Alice")).Returns("alice"); | ||
|
|
||
| IPredicateUserRepo repo = mock.Object; | ||
|
|
||
| await Assert.That(repo.Save(new PredicateUser("Alice"))).IsEqualTo("alice"); | ||
| await Assert.That(repo.Save(new PredicateUser("Bob"))).IsNotEqualTo("alice"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Null_Argument_Dereference_Propagates_Exception() | ||
| { | ||
| var mock = IPredicateUserRepo.Mock(); | ||
| mock.Save(Is<PredicateUser>(u => u.Name == "Alice")).Returns("alice"); | ||
|
|
||
| IPredicateUserRepo repo = mock.Object; | ||
|
|
||
| await Assert.That(() => repo.Save(null!)).Throws<NullReferenceException>(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Null_Argument_Does_Not_Match_Explicit_Null_Guard() | ||
| { | ||
| var mock = INullablePredicateUserRepo.Mock(); | ||
| mock.Save(Is<PredicateUser?>(u => u is not null && u.Name == "Alice")).Returns("alice"); | ||
|
|
||
| INullablePredicateUserRepo repo = mock.Object; | ||
|
|
||
| await Assert.That(repo.Save(null)).IsEqualTo(string.Empty); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Null_Argument_Preserves_Unrelated_Predicate_Exception() | ||
| { | ||
| var mock = INullablePredicateUserRepo.Mock(); | ||
| var failure = new InvalidOperationException("from predicate"); | ||
| mock.Save(Is<PredicateUser?>(u => throw failure)).Returns("never"); | ||
|
|
||
| INullablePredicateUserRepo repo = mock.Object; | ||
|
|
||
| await Assert.That(() => repo.Save(null)).Throws<InvalidOperationException>(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Nullable_Predicate_Can_Match_Null() | ||
| { | ||
| var mock = INullablePredicateUserRepo.Mock(); | ||
| mock.Save(Is<PredicateUser?>(u => u is null)).Returns("null user"); | ||
|
|
||
| INullablePredicateUserRepo repo = mock.Object; | ||
|
|
||
| await Assert.That(repo.Save(null)).IsEqualTo("null user"); | ||
| await Assert.That(repo.Save(new PredicateUser("Alice"))).IsNotEqualTo("null user"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Predicate_Exception_For_NonNull_Argument_Still_Propagates() | ||
| { | ||
| var mock = IPredicateUserRepo.Mock(); | ||
| mock.Save(Is<PredicateUser>(u => throw new NullReferenceException("from predicate"))).Returns("never"); | ||
|
|
||
| IPredicateUserRepo repo = mock.Object; | ||
|
|
||
| await Assert.That(() => repo.Save(new PredicateUser("Alice"))).Throws<NullReferenceException>(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.