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 1 commit
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
Some comments aren't visible on the classic Files Changed page.
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
24 changes: 24 additions & 0 deletions
24
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,24 @@ | ||
| 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 => (({testClass})instance).Rows)"); | ||
| await Assert.That(source).Contains($"FromInstanceEnumerable(dataGeneratorMetadata, static instance => (({testClass})instance).PropertyRows)"); | ||
| await Assert.That(source).Contains($"FromEnumerable(static () => {testClass}.StaticRows())"); | ||
| await Assert.That(source).DoesNotContain("new global::TUnit.Core.InstanceMethodDataSourceAttribute(\"Rows\"),"); | ||
| }); | ||
| } |
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
188 changes: 188 additions & 0 deletions
188
tests/TUnit.Core.SourceGenerator.Tests/Tests7225.Test.verified.txt
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,188 @@ | ||
| // <auto-generated/> | ||
| #pragma warning disable | ||
|
|
||
| #nullable enable | ||
| namespace TUnit.Generated; | ||
| [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute] | ||
| [global::System.CodeDom.Compiler.GeneratedCode("TUnit", "VERSION_SCRUBBED")] | ||
| internal static class TUnit_TestProject_Bugs__7225_InstanceMethodDataSourceFieldTests__TestSource | ||
| { | ||
| private static readonly global::TUnit.Core.ClassMetadata __classMetadata = global::TUnit.Core.ClassMetadata.GetOrAdd("TestsBase`1:global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests", new global::TUnit.Core.ClassMetadata | ||
| { | ||
| Type = typeof(global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests), | ||
| TypeInfo = new global::TUnit.Core.ConcreteType(typeof(global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests)), | ||
| Name = "InstanceMethodDataSourceFieldTests", | ||
| Namespace = "TUnit.TestProject.Bugs._7225", | ||
| Assembly = global::TUnit.Core.AssemblyMetadata.GetOrAdd("TestsBase`1", "TestsBase`1"), | ||
| Parameters = global::System.Array.Empty<global::TUnit.Core.ParameterMetadata>(), | ||
| Properties = new global::TUnit.Core.PropertyMetadata[] | ||
| { | ||
| new global::TUnit.Core.PropertyMetadata | ||
| { | ||
| ReflectionInfo = typeof(global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests).GetProperty("PropertyRows", typeof(global::System.Collections.Generic.IEnumerable<int>)), | ||
| Type = typeof(global::System.Collections.Generic.IEnumerable<int>), | ||
| Name = "PropertyRows", | ||
| IsStatic = false, | ||
| IsNullable = false, | ||
| Getter = o => ((global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests)o).PropertyRows, | ||
| ClassMetadata = null!, | ||
| ContainingTypeMetadata = null! | ||
| } | ||
| }, | ||
| Parent = null | ||
| }); | ||
| private static readonly global::System.Type __classType = typeof(global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests); | ||
| private static global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests __CreateInstance(global::System.Type[] typeArgs, object?[] args) | ||
| { | ||
| return new global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests(); | ||
| } | ||
| private static global::System.Threading.Tasks.ValueTask __Invoke(global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests instance, int methodIndex, object?[] args, global::System.Threading.CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| switch (methodIndex) | ||
| { | ||
| case 0: | ||
| { | ||
| switch (args.Length) | ||
| { | ||
| case 1: | ||
| { | ||
| return new global::System.Threading.Tasks.ValueTask(instance.Instance_Field(global::TUnit.Core.Helpers.CastHelper.Cast<int>(args[0]))); | ||
| } | ||
| default: | ||
| throw new global::System.ArgumentException($"Expected exactly 1 argument, but got {args.Length}"); | ||
| } | ||
| } | ||
| case 1: | ||
| { | ||
| switch (args.Length) | ||
| { | ||
| case 1: | ||
| { | ||
| return new global::System.Threading.Tasks.ValueTask(instance.Instance_Property(global::TUnit.Core.Helpers.CastHelper.Cast<int>(args[0]))); | ||
| } | ||
| default: | ||
| throw new global::System.ArgumentException($"Expected exactly 1 argument, but got {args.Length}"); | ||
| } | ||
| } | ||
| case 2: | ||
| { | ||
| switch (args.Length) | ||
| { | ||
| case 1: | ||
| { | ||
| return new global::System.Threading.Tasks.ValueTask(instance.Static_Method(global::TUnit.Core.Helpers.CastHelper.Cast<int>(args[0]))); | ||
| } | ||
| default: | ||
| throw new global::System.ArgumentException($"Expected exactly 1 argument, but got {args.Length}"); | ||
| } | ||
| } | ||
| default: | ||
| throw new global::System.ArgumentOutOfRangeException(nameof(methodIndex)); | ||
| } | ||
| } | ||
| catch (global::System.Exception ex) | ||
| { | ||
| return new global::System.Threading.Tasks.ValueTask(global::System.Threading.Tasks.Task.FromException(ex)); | ||
| } | ||
| } | ||
| private static global::System.Attribute[] __Attributes(int groupIndex) | ||
| { | ||
| switch (groupIndex) | ||
| { | ||
| case 0: | ||
| { | ||
| return | ||
| [ | ||
| new global::TUnit.Core.TestAttribute(), | ||
| new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass) | ||
| ]; | ||
| } | ||
| default: | ||
| throw new global::System.ArgumentOutOfRangeException(nameof(groupIndex)); | ||
| } | ||
| } | ||
| private static readonly global::System.Func<global::System.Type[], object?[], global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests> __createInstance = __CreateInstance; | ||
| private static readonly global::System.Func<global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests, int, object?[], global::System.Threading.CancellationToken, global::System.Threading.Tasks.ValueTask> __invoke = __Invoke; | ||
| private static readonly global::System.Func<int, global::System.Attribute[]> __attributes = __Attributes; | ||
| public static readonly global::TUnit.Core.TestEntry<global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests>[] Entries = new global::TUnit.Core.TestEntry<global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests>[] | ||
| { | ||
| global::TUnit.Core.TestEntryFactory.CreateWithClassMetadata<global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests>( | ||
| methodName: "Instance_Field", | ||
| fullyQualifiedName: "TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests.Instance_Field", | ||
| filePath: "", | ||
| lineNumber: 18, | ||
| hasDataSource: true, | ||
| testDataSources: new global::TUnit.Core.IDataSourceAttribute[] | ||
| { | ||
| new global::TUnit.Core.InstanceMethodDataSourceAttribute("Rows") | ||
| { | ||
| Factory = static dataGeneratorMetadata => global::TUnit.Core.Helpers.DataSourceFactories.FromInstanceRuntimeValue(dataGeneratorMetadata, static instance => ((global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests)instance).Rows), | ||
| }, | ||
| }, | ||
| returnType: typeof(global::System.Threading.Tasks.Task), | ||
| parameters: global::TUnit.Core.ParameterMetadataFactory.ForMethod(typeof(global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests), "Instance_Field", false, new global::TUnit.Core.ParameterMetadata[] | ||
| { | ||
| global::TUnit.Core.ParameterMetadataFactory.Create(typeof(int), "value", new global::TUnit.Core.ConcreteType(typeof(int)), false) | ||
| }), | ||
| classMetadata: __classMetadata, | ||
| createInstance: __createInstance, | ||
| invokeBody: __invoke, | ||
| methodIndex: 0, | ||
| createAttributes: __attributes, | ||
| attributeGroupIndex: 0), | ||
| global::TUnit.Core.TestEntryFactory.CreateWithClassMetadata<global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests>( | ||
| methodName: "Instance_Property", | ||
| fullyQualifiedName: "TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests.Instance_Property", | ||
| filePath: "", | ||
| lineNumber: 25, | ||
| hasDataSource: true, | ||
| testDataSources: new global::TUnit.Core.IDataSourceAttribute[] | ||
| { | ||
| new global::TUnit.Core.InstanceMethodDataSourceAttribute("PropertyRows") | ||
| { | ||
| Factory = static dataGeneratorMetadata => global::TUnit.Core.Helpers.DataSourceFactories.FromInstanceEnumerable(dataGeneratorMetadata, static instance => ((global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests)instance).PropertyRows), | ||
| }, | ||
| }, | ||
| returnType: typeof(global::System.Threading.Tasks.Task), | ||
| parameters: global::TUnit.Core.ParameterMetadataFactory.ForMethod(typeof(global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests), "Instance_Property", false, new global::TUnit.Core.ParameterMetadata[] | ||
| { | ||
| global::TUnit.Core.ParameterMetadataFactory.Create(typeof(int), "value", new global::TUnit.Core.ConcreteType(typeof(int)), false) | ||
| }), | ||
| classMetadata: __classMetadata, | ||
| createInstance: __createInstance, | ||
| invokeBody: __invoke, | ||
| methodIndex: 1, | ||
| createAttributes: __attributes, | ||
| attributeGroupIndex: 0), | ||
| global::TUnit.Core.TestEntryFactory.CreateWithClassMetadata<global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests>( | ||
| methodName: "Static_Method", | ||
| fullyQualifiedName: "TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests.Static_Method", | ||
| filePath: "", | ||
| lineNumber: 32, | ||
| hasDataSource: true, | ||
| testDataSources: new global::TUnit.Core.IDataSourceAttribute[] | ||
| { | ||
| new global::TUnit.Core.InstanceMethodDataSourceAttribute("StaticRows") | ||
| { | ||
| Factory = static dataGeneratorMetadata => global::TUnit.Core.Helpers.DataSourceFactories.FromEnumerable(static () => global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests.StaticRows()), | ||
| }, | ||
| }, | ||
| returnType: typeof(global::System.Threading.Tasks.Task), | ||
| parameters: global::TUnit.Core.ParameterMetadataFactory.ForMethod(typeof(global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests), "Static_Method", false, new global::TUnit.Core.ParameterMetadata[] | ||
| { | ||
| global::TUnit.Core.ParameterMetadataFactory.Create(typeof(int), "value", new global::TUnit.Core.ConcreteType(typeof(int)), false) | ||
| }), | ||
| classMetadata: __classMetadata, | ||
| createInstance: __createInstance, | ||
| invokeBody: __invoke, | ||
| methodIndex: 2, | ||
| createAttributes: __attributes, | ||
| attributeGroupIndex: 0), | ||
| }; | ||
| } | ||
| internal static partial class TUnit_TestRegistration | ||
| { | ||
| static readonly int _r_TUnit_TestProject_Bugs__7225_InstanceMethodDataSourceFieldTests__TestSource = global::TUnit.Core.SourceRegistrar.RegisterEntries<global::TUnit.TestProject.Bugs._7225.InstanceMethodDataSourceFieldTests>(static () => TUnit_TestProject_Bugs__7225_InstanceMethodDataSourceFieldTests__TestSource.Entries); | ||
| } |
38 changes: 38 additions & 0 deletions
38
tests/TUnit.TestProject/Bugs/_7225/InstanceMethodDataSourceFieldTests.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,38 @@ | ||
| using TUnit.TestProject.Attributes; | ||
|
|
||
| namespace TUnit.TestProject.Bugs._7225; | ||
|
|
||
| /// <summary> | ||
| /// [InstanceMethodDataSource] naming an instance field or property must get a generated factory, like | ||
| /// [MethodDataSource] does; otherwise Native AOT falls back to a reflection lookup that finds no member. | ||
| /// </summary> | ||
| [EngineTest(ExpectedResult.Pass)] | ||
| public class InstanceMethodDataSourceFieldTests | ||
| { | ||
| public readonly object Rows = new[] { 53, 54 }; | ||
|
|
||
| public IEnumerable<int> PropertyRows => [55, 56]; | ||
|
|
||
| public static IEnumerable<int> StaticRows() => [57, 58]; | ||
|
|
||
| [Test] | ||
| [InstanceMethodDataSource(nameof(Rows))] | ||
| public async Task Instance_Field(int value) | ||
| { | ||
| await Assert.That(value).IsBetween(53, 54); | ||
| } | ||
|
|
||
| [Test] | ||
| [InstanceMethodDataSource(nameof(PropertyRows))] | ||
| public async Task Instance_Property(int value) | ||
| { | ||
| await Assert.That(value).IsBetween(55, 56); | ||
| } | ||
|
|
||
| [Test] | ||
| [InstanceMethodDataSource(nameof(StaticRows))] | ||
| public async Task Static_Method(int value) | ||
| { | ||
| await Assert.That(value).IsBetween(57, 58); | ||
| } | ||
| } |
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.