-
Notifications
You must be signed in to change notification settings - Fork 279
Fixed bug that would cause a struct to not match and throw a runtime error. #895
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
304NotModified
merged 5 commits into
nsubstitute:main
from
Notallthatevil:766-Argument-Matcher-For-A-Struct
Dec 26, 2025
+36
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5edb54a
Resolved a bug that would cause a struct to not match and throw a run…
Notallthatevil 276df59
Add tests for struct with default constructor
Notallthatevil 1c12153
Fixed class constructor curly brace format
Notallthatevil 2885899
Refactor default value handling logic
Notallthatevil e0aeb81
Merge branch 'nsubstitute:main' into 766-Argument-Matcher-For-A-Struct
Notallthatevil 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm doubting if we should use
Activator.CreateInstance(returnType)!;for non-stucts, to keep things 100% the same for the previous cases.Although all tests works, so not sure.
@nsubstitute/core-team any opinion on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd feel more comfortable if we keep the existing behaviour for non-structs. Never sure how reflection stuff is going to break 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this fixed? This isn't marked as resolved?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So after some reading, this is large change?
Calls the (default) constructor - that is indeed an issue with structs
Won't call the constructor.
I'm not sure if this is a good idea.
I'm not sure how DefaultValue is used exactly and if this poses issues.
Anyway, without resolving this discussion, I cannot approve this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method itself is attempting to get the default value for the type. i.e.
0forint. For reference types, as you know, this would returnnullwhen you try to doobject o = default;.For structs, when you set them to default, for example, when the parameter list has something along the lines of
void MyMethod(CancellationToken cancellationToken = default)and then you try to create a matcher against it usingArg.Any<CancellationToken>(), it results in two instances of a struct that will not match, becauseArg.Any<CancellationToken>()does not equaldefault(CancellationToken), which is whyGetUninitializedObjectis needed.