-
Notifications
You must be signed in to change notification settings - Fork 256
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
It is hard to use test framework for source generators #754
Comments
In the original comment, you mentioned adding a Before: await new VerifyCS.Test
{
TestState =
{
Sources =
{
@"// Comment",
},
},
FixedState =
{
Sources =
{
@"{|#0:|}// Comment",
("Microsoft.CodeAnalysis.SourceGenerators.Testing.UnitTests\\Microsoft.CodeAnalysis.Testing.TestGenerators.AddEmptyFileWithDiagnostic\\EmptyGeneratedFile.cs", SourceText.From(string.Empty, Encoding.UTF8)),
},
ExpectedDiagnostics =
{
// /0/Test0.cs(1,1): warning SG0001: Message
new DiagnosticResult(AddEmptyFileWithDiagnostic.Descriptor).WithLocation(0),
},
},
}.RunAsync(); After: await new VerifyCS.Test
{
TestState =
{
Sources =
{
@"{|#0:|}// Comment",
},
GeneratedSources =
{
("Microsoft.CodeAnalysis.SourceGenerators.Testing.UnitTests\\Microsoft.CodeAnalysis.Testing.TestGenerators.AddEmptyFileWithDiagnostic\\EmptyGeneratedFile.cs", SourceText.From(string.Empty, Encoding.UTF8)),
}
ExpectedDiagnostics =
{
// /0/Test0.cs(1,1): warning SG0001: Message
new DiagnosticResult(AddEmptyFileWithDiagnostic.Descriptor).WithLocation(0),
},
},
}.RunAsync(); |
This seems fine. If the compiler complains about the lack of source files, the test could just declare that compilation error as an expected diagnostic. |
I'm not inclined to do anything with the specific goal of making this easier. Determinism is a highly desirable property of source generators. One interim solution would be overriding |
Yeah, this is a mess. What about this? GeneratedSources =
{
- ("Microsoft.CodeAnalysis.SourceGenerators.Testing.UnitTests\\Microsoft.CodeAnalysis.Testing.TestGenerators.AddEmptyFileWithDiagnostic\\EmptyGeneratedFile.cs", SourceText.From(string.Empty, Encoding.UTF8)),
+ (typeof(AddEmptyFileWithDiagnostic), "EmptyGeneratedFile.cs", string.Empty),
} The new API uses the provided
|
If the Source Generator uses StringBuilder.AppendLine, the output will not be deterministic because newline characters are dependent on the Operating System. |
GeneratedSources
Thank you. The reason I deleted this code in the issue was that it seemed difficult to deal with non-deterministic source code. If deterministic source code does not be supportted, I want But I think In addition, I think it's better to remove the I wrote the code. sharwell#1 deterministic
I understand. I’m willing to open a new issue. |
I'll create an API and updated tests for |
from #602 (comment)
It is hard to use test framework for source generators. Some
SourceGeneratorTest<TVerifier>
API are not suitable for source generators.roslyn-sdk/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.SourceGenerators.Testing/SourceGeneratorTest`1.cs
Lines 61 to 79 in 50e2a55
It is hard to use
FixedCode
It is hard to use
FixedCode
becauseFixedState.Sources
needs original codes and generated codes.Verify.NotEmpty
should be removedVerify.NotEmpty
should be removed. Source generators can work in a project without any C# code like CsvGenerator. Even ifTestState.Sources
is empty, test can be valid.The generated source code is not always deterministic
The generated source code is not always deterministic when the source generator is using
System.Guid
,System.Random
,System.DateTime
etc.The output can also change due to the indefinite order of
Dictionary<TKey, TValue>
andImmutableDictionary<TKey, TValue>
.Name of source file
Name of genereted file is especially problematic.
In Windows, name of generated file is
{AssemblyName}\{ClassName}\{hintName}
, but in Mac/Linux, the name is{AssemblyName}/{ClassName}/{hintName}
.The text was updated successfully, but these errors were encountered: