Fix ArgumentsSource on external types not actually working #2820
+15
−3
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.
Followup to #2748. As of BDN 0.15.2, you can't use
[ArgumentsSource]
with an external type, because the generated code has compiler errors.When I made #2748, I guess I must have forgotten to actually test the functionality, which is very embarrassing of me and which I apologize for! I just tried to use the functionality in my project and it didn't work, so here I am fixing it 😭
The issue
If you have code like this:
Then BDN currently generates code like this:
The problem is that
MethodACases
is not in the type that the generated class (Runnable_0
) inherits from, it's in an external type (DiagnosticsCases
). So there's a compiler error here ("error CS0103: The name 'MethodACases' does not exist in the current context)The fix
In this example, we need to put the fully qualified type name before the method call. The last line of the generated code should instead read:
However, we can't always add the fully qualified type name, because
ArgumentsSource
can also be used with non-static methods/properties.So, this PR checks if
ArgumentsSource
is being used with a static type. If so, the fully qualified type name is added at this location; otherwise, the generated code is unchanged compared to before this PR.I am worried about external instance methods
Using
ArgumentsSource
with different kinds of members:As you can see from this helpful table, this PR fixes one broken case but there is still one broken case remaining. How best to fix this? Should the generated code create an instance of the member's type, and call the member from the new instance?
To be honest, I think there's no benefit to supporting instance members at all. @timcassell also dislikes that we support instance members and we discussed it a bit here: #2744 (comment).
Maybe we should take this opportunity to just drop support for instance members in
ArgumentsSource
andParamsSource
?Other things I am worried about
BenchmarkTestExecutor.CanExecute
. Why didn't those tests catch that the test case actually couldn't execute?BenchmarkTestExecutor.CanExecute
?BenchmarkTestExecutor.CanExecute
be updated to account for this category of problem? (I'm assuming it can't catch problems with the codegen, but I haven't investigated)