Repository navigation
fix(sourcegen): generate a data source factory for [InstanceMethodDataSource] (#7225) #7348
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
10 commits
Select commit
Hold shift + click to select a range
88c694f
fix(sourcegen): generate a data source factory for [InstanceMethodDat…
thomhurst de2b2a1
fix(sourcegen): keep runtime row expansion and private methods for [I…
thomhurst bedcaba
fix(sourcegen): skip generic overloads and keep null as an empty sour…
thomhurst 388bbb0
fix(core): read a null Task or IAsyncEnumerable data source as empty …
thomhurst 02b0d66
fix(sourcegen): leave a void [InstanceMethodDataSource] method to the…
thomhurst 90a9df5
fix(sourcegen): rebuild a single System.Tuple parameter from unwrappe…
thomhurst e57d6aa
fix(sourcegen): fall through to the argument-count switch for a singl…
thomhurst c4c00e4
fix(core): keep a tuple row whole for a single tuple parameter of [In…
thomhurst ca94d99
fix(sourcegen): preserve non-generic task source factories (#7225)
thomhurst edcf790
refactor(sourcegen): build complete reflection reads for #7225
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
159 changes: 129 additions & 30 deletions
159
src/TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs
Large diffs are not rendered by default.
Oops, something went wrong.
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
53 changes: 53 additions & 0 deletions
53
tests/TUnit.Core.SourceGenerator.Tests/Bugs/7225/Tests7225.cs
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,53 @@ | ||
| namespace TUnit.Core.SourceGenerator.Tests.Bugs._7225; | ||
|
|
||
| /// <summary> | ||
| /// [InstanceMethodDataSource] gets the same generated Factory as [MethodDataSource], so Native AOT | ||
| /// does not fall back to a reflection member lookup (#7225). | ||
| /// </summary> | ||
| internal class Tests7225 : TestsBase | ||
| { | ||
| [Test] | ||
| public Task Test() => RunTest(Path.Combine(Git.TestsDirectory.FullName, | ||
| "TUnit.TestProject", | ||
| "Bugs", | ||
| "_7225", | ||
| "InstanceMethodDataSourceFieldTests.cs"), | ||
| async generatedFiles => | ||
| { | ||
| var source = string.Join(Environment.NewLine, generatedFiles); | ||
| const string testClass = "global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests"; | ||
| await Assert.That(source).Contains($"FromInstanceRuntimeValue(dataGeneratorMetadata, static instance => (object?)(({testClass})instance).Rows ?? global::System.Array.Empty<object>())"); | ||
| await Assert.That(source).Contains($"FromInstanceEnumerable(dataGeneratorMetadata, static instance => (object?)(({testClass})instance).PropertyRows ?? global::System.Array.Empty<object>())"); | ||
| await Assert.That(source).Contains($"FromEnumerable(dataGeneratorMetadata, static () => (object?){testClass}.StaticRows() ?? global::System.Array.Empty<object>())"); | ||
| await Assert.That(source).DoesNotContain("new global::TUnit.Core.InstanceMethodDataSourceAttribute(\"Rows\"),"); | ||
| }); | ||
|
|
||
| /// <summary> | ||
| /// The factory keeps the behavior [InstanceMethodDataSource] had when it read members by reflection: an object | ||
| /// value decides its rows at run time, a null value is an empty source, and a private method is looked up | ||
| /// by a constant name that Native AOT keeps, as private fields and properties are. | ||
| /// </summary> | ||
| [Test] | ||
| public Task MemberShapes() => RunTest(Path.Combine(Git.TestsDirectory.FullName, | ||
| "TUnit.TestProject", | ||
| "Bugs", | ||
| "_7225", | ||
| "InstanceMethodDataSourceMemberShapeTests.cs"), | ||
| async generatedFiles => | ||
| { | ||
| var source = string.Join(Environment.NewLine, generatedFiles); | ||
| const string testClass = "global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceMemberShapeTests"; | ||
| await Assert.That(source).Contains($"FromInstanceRuntimeValue(dataGeneratorMetadata, static instance => (object?)(({testClass})instance).ObjectProperty ?? global::System.Array.Empty<object>())"); | ||
| await Assert.That(source).Contains($"FromInstanceRuntimeValue(dataGeneratorMetadata, static instance => (object?)(({testClass})instance).ObjectMethod() ?? global::System.Array.Empty<object>())"); | ||
| await Assert.That(source).Contains($"FromInstanceEnumerable(dataGeneratorMetadata, static instance => (object?)(({testClass})instance).TupleRows ?? global::System.Array.Empty<object>())"); | ||
| await Assert.That(source).Contains($"FromInstanceEnumerable(dataGeneratorMetadata, static instance => typeof({testClass}).GetMethod(\"PrivateRows\", "); | ||
| await Assert.That(source).DoesNotContain("new global::TUnit.Core.InstanceMethodDataSourceAttribute(\"PrivateRows\"),"); | ||
| await Assert.That(source).Contains($"FromInstanceRuntimeValue(dataGeneratorMetadata, static instance => (object?)(({testClass})instance).NonGenericTaskRows() ?? global::System.Array.Empty<object>())"); | ||
| await Assert.That(source).Contains($"FromInstanceRuntimeValue(dataGeneratorMetadata, static instance => (object?)(({testClass})instance).NonGenericValueTaskRows ?? global::System.Array.Empty<object>())"); | ||
| await Assert.That(source).Contains($"FromRuntimeValue(dataGeneratorMetadata, static () => (object?){testClass}.StaticNonGenericTaskRows ?? global::System.Array.Empty<object>())"); | ||
| await Assert.That(source).Contains($"FromRuntimeValue(dataGeneratorMetadata, static () => (object?){testClass}.StaticNonGenericValueTaskRows() ?? global::System.Array.Empty<object>())"); | ||
| // A generic overload of the same name would make the GetMethod lookup ambiguous | ||
| await Assert.That(source).Contains($"global::System.Array.Find(typeof({testClass}).GetMethods("); | ||
| await Assert.That(source).Contains("m.Name == \"OverloadedRows\" && !m.IsGenericMethodDefinition && m.GetParameters().Length == 0)!.Invoke(instance, null)"); | ||
| }); | ||
| } |
Oops, something went wrong.
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.