VerifyTests / Verify.SourceGenerators

Extends Verify to allow verification of C# Source Generators.
MIT License
72 stars 11 forks source link

Add verify for single file #295

Closed KondzioSSJ4 closed 1 month ago

KondzioSSJ4 commented 1 month ago

Issue

Sometimes generators generate plenty of files and in tests many ppl doesn't care about full output, but for 1 specified file.

We could verify using just Verify but the problem is that output would be txt so no syntax coloring

Solution

Add VerifierSettings.RegisterFileConverter<GeneratedSourceResult>(Convert); into VerifySourceGenerators.Initialize method. The Convert method could be like:

private static ConversionResult Convert(
    GeneratedSourceResult source,
    IReadOnlyDictionary<string, object> context)
{
    var target = SourceToTarget(source);
    return new(null, new[] { target });
}

everything else already exists

so final code in test liblary would be:

[Fact]
public Task RunResult()
{
    var driver = BuildDriver();

    var result = driver.GetRunResult()
        .Results
        .SelectMany(x => x.GeneratedSources)
        .Where(x => x.HintName == "helloWorld")
        .FirstOrDefault();

    return Verify(result);
}

It looks like it would match to the logic from readme file

P.S. I don't need it, I'm able to add it by my own but I think that could be helpful for other ppl who use source generators and Verify

KondzioSSJ4 commented 1 month ago

Never mind... It's possible using IgnoreGeneratedResult

sorry