Incorrect documentation in roslyn/docs/features /source-generators.cookbook.md #76122
-
The Source Generators Cookbook has some outdated documentation regarding the unit testing nugets. It contains the following:Unit Testing of GeneratorsUser scenario: As a generator author, I want to be able to unit test my generators to make development easier and ensure correctness. Solution A: The recommended approach is to use Microsoft.CodeAnalysis.Testing packages:
This works in the same way as analyzers and codefix testing. You add a class like the following: using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;
public static class CSharpSourceGeneratorVerifier<TSourceGenerator>
where TSourceGenerator : ISourceGenerator, new()
{
public class Test : CSharpSourceGeneratorTest<TSourceGenerator, XUnitVerifier>
{
public Test()
{
}
protected override CompilationOptions CreateCompilationOptions()
{
var compilationOptions = base.CreateCompilationOptions();
return compilationOptions.WithSpecificDiagnosticOptions(
compilationOptions.SpecificDiagnosticOptions.SetItems(GetNullableWarningsFromCompiler()));
}
public LanguageVersion LanguageVersion { get; set; } = LanguageVersion.Default;
private static ImmutableDictionary<string, ReportDiagnostic> GetNullableWarningsFromCompiler()
{
string[] args = { "/warnaserror:nullable" };
var commandLineArguments = CSharpCommandLineParser.Default.Parse(args, baseDirectory: Environment.CurrentDirectory, sdkDirectory: Environment.CurrentDirectory);
var nullableWarnings = commandLineArguments.CompilationOptions.SpecificDiagnosticOptions;
return nullableWarnings;
}
protected override ParseOptions CreateParseOptions()
{
return ((CSharpParseOptions)base.CreateParseOptions()).WithLanguageVersion(LanguageVersion);
}
}
} Then, in your test file: using VerifyCS = CSharpSourceGeneratorVerifier<YourGenerator>; And use the following in your test method: var code = "initial code";
var generated = "expected generated code";
await new VerifyCS.Test
{
TestState =
{
Sources = { code },
GeneratedSources =
{
(typeof(YourGenerator), "GeneratedFileName", SourceText.From(generated, Encoding.UTF8, SourceHashAlgorithm.Sha256)),
},
},
}.RunAsync(); But the
The link above tells me to use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The very first line of the document states:
While we would likely accept a PR updating the content of that section, the document itself covers techniques that have been obsoleted. This isn't particularly different in that respect. |
Beta Was this translation helpful? Give feedback.
The very first line of the document states:
While we would likely accept a PR updating the content of that section, the document itself covers techniques that have been obsoleted. This isn't particularly different in that respect.