-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NonTypedDataSourceGeneratorAttribute
- Loading branch information
Showing
10 changed files
with
111 additions
and
11 deletions.
There are no files selected for viewing
21 changes: 20 additions & 1 deletion
21
TUnit.Core.SourceGenerator.Tests/DataSourceGeneratorTests.cs
This file contains 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 |
---|---|---|
@@ -1,15 +1,34 @@ | ||
using TUnit.Core.SourceGenerator.CodeGenerators; | ||
using TUnit.Core.SourceGenerator.Tests.Options; | ||
|
||
namespace TUnit.Core.SourceGenerator.Tests; | ||
|
||
internal class DataSourceGeneratorTests : TestsBase<TestsGenerator> | ||
{ | ||
[Test] | ||
public Task Test() => RunTest(Path.Combine(Git.RootDirectory.FullName, | ||
public Task Typed() => RunTest(Path.Combine(Git.RootDirectory.FullName, | ||
"TUnit.TestProject", | ||
"DataSourceGeneratorTests.cs"), | ||
async generatedFiles => | ||
{ | ||
await Assert.That(generatedFiles.Length).IsEqualTo(3); | ||
}); | ||
|
||
[Test] | ||
public Task NonTyped() => RunTest(Path.Combine(Git.RootDirectory.FullName, | ||
"TUnit.TestProject", | ||
"AutoDataTests.cs"), | ||
new RunTestOptions() | ||
{ | ||
AdditionalFiles = [ | ||
Path.Combine(Git.RootDirectory.FullName, | ||
"TUnit.TestProject", | ||
"Attributes", | ||
"AutoDataAttribute.cs") | ||
] | ||
}, | ||
async generatedFiles => | ||
{ | ||
await Assert.That(generatedFiles.Length).IsEqualTo(1); | ||
}); | ||
} |
This file contains 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 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 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 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
3 changes: 3 additions & 0 deletions
3
TUnit.Core/Attributes/TestData/INonTypedDataSourceGeneratorAttribute.cs
This file contains 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,3 @@ | ||
namespace TUnit.Core; | ||
|
||
internal interface INonTypedDataSource : IDataAttribute; |
7 changes: 7 additions & 0 deletions
7
TUnit.Core/Attributes/TestData/NonTypedDataSourceGeneratorAttribute.cs
This file contains 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,7 @@ | ||
namespace TUnit.Core; | ||
|
||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true)] | ||
public abstract class NonTypedDataSourceGeneratorAttribute : TestDataAttribute, INonTypedDataSource | ||
{ | ||
public abstract IEnumerable<Func<object?[]?>> GenerateDataSources(DataGeneratorMetadata dataGeneratorMetadata); | ||
} |
This file contains 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,26 @@ | ||
using AutoFixture.Kernel; | ||
|
||
namespace TUnit.TestProject.Attributes; | ||
|
||
public class AutoDataAttribute : NonTypedDataSourceGeneratorAttribute | ||
{ | ||
private static AutoFixture.Fixture _fixture = new(); | ||
|
||
public override IEnumerable<Func<object?[]?>> GenerateDataSources(DataGeneratorMetadata dataGeneratorMetadata) | ||
{ | ||
yield return () => GenerateRow(dataGeneratorMetadata); | ||
} | ||
|
||
private object?[] GenerateRow(DataGeneratorMetadata dataGeneratorMetadata) | ||
{ | ||
return GenerateRowEnumerable(dataGeneratorMetadata).ToArray(); | ||
} | ||
|
||
private static IEnumerable<object> GenerateRowEnumerable(DataGeneratorMetadata dataGeneratorMetadata) | ||
{ | ||
foreach (var parameterInfo in dataGeneratorMetadata.ParameterInfos ?? []) | ||
{ | ||
yield return _fixture.Create(parameterInfo.ParameterType, new SpecimenContext(_fixture)); | ||
} | ||
} | ||
} |
This file contains 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,13 @@ | ||
using TUnit.TestProject.Attributes; | ||
|
||
namespace TUnit.TestProject; | ||
|
||
public class AutoDataTests | ||
{ | ||
[AutoData] | ||
[Test] | ||
public Task Test1(string value1, int value2, double value3, bool value4) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains 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