{"id":18937430,"url":"https://github.com/verifytests/verify.sourcegenerators","last_synced_at":"2025-05-16T08:03:20.700Z","repository":{"id":38213964,"uuid":"372097978","full_name":"VerifyTests/Verify.SourceGenerators","owner":"VerifyTests","description":"Extends Verify to allow verification of C# Source Generators.","archived":false,"fork":false,"pushed_at":"2025-05-14T10:38:39.000Z","size":529,"stargazers_count":82,"open_issues_count":0,"forks_count":12,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-16T08:03:11.785Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/VerifyTests.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"license.txt","code_of_conduct":"code_of_conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"VerifyTests"}},"created_at":"2021-05-30T01:09:31.000Z","updated_at":"2025-05-14T11:15:00.000Z","dependencies_parsed_at":"2023-12-27T04:34:57.829Z","dependency_job_id":"9364a096-7a90-4140-a33c-c512e869eca2","html_url":"https://github.com/VerifyTests/Verify.SourceGenerators","commit_stats":{"total_commits":242,"total_committers":8,"mean_commits":30.25,"dds":"0.21074380165289253","last_synced_commit":"85c1db97bbe98ba55336b724df9501bf91568fb6"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VerifyTests%2FVerify.SourceGenerators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VerifyTests%2FVerify.SourceGenerators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VerifyTests%2FVerify.SourceGenerators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VerifyTests%2FVerify.SourceGenerators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VerifyTests","download_url":"https://codeload.github.com/VerifyTests/Verify.SourceGenerators/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254493381,"owners_count":22080126,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T12:11:07.986Z","updated_at":"2025-05-16T08:03:20.677Z","avatar_url":"https://github.com/VerifyTests.png","language":"C#","funding_links":["https://github.com/sponsors/VerifyTests"],"categories":[],"sub_categories":[],"readme":"# \u003cimg src=\"/src/icon.png\" height=\"30px\"\u003e Verify.SourceGenerators\n\n[![Discussions](https://img.shields.io/badge/Verify-Discussions-yellow?svg=true\u0026label=)](https://github.com/orgs/VerifyTests/discussions)\n[![Build status](https://ci.appveyor.com/api/projects/status/2ip7do6jk0gevt0v?svg=true)](https://ci.appveyor.com/project/SimonCropp/Verify-SourceGenerators)\n[![NuGet Status](https://img.shields.io/nuget/v/Verify.SourceGenerators.svg)](https://www.nuget.org/packages/Verify.SourceGenerators/)\n\nExtends [Verify](https://github.com/VerifyTests/Verify) to allow verification of C# Source Generators.\n\n**See [Milestones](../../milestones?state=closed) for release notes.**\n\n\n## NuGet package\n\nhttps://nuget.org/packages/Verify.SourceGenerators/\n\nInstall one of the Verify [testing framework adapters](https://github.com/verifytests/verify#nuget-packages) NuGet packages.\n\n\n## Initialize\n\n\u003c!-- snippet: enable --\u003e\n\u003ca id='snippet-enable'\u003e\u003c/a\u003e\n```cs\n[ModuleInitializer]\npublic static void Init() =\u003e\n    VerifySourceGenerators.Initialize();\n```\n\u003csup\u003e\u003ca href='/src/Tests/ModuleInitializer.cs#L3-L9' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-enable' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n## Generator\n\nGiven a Source Generator:\n\n\u003c!-- snippet: HelloWorldGenerator.cs --\u003e\n\u003ca id='snippet-HelloWorldGenerator.cs'\u003e\u003c/a\u003e\n```cs\nusing Microsoft.CodeAnalysis;\nusing Microsoft.CodeAnalysis.Text;\n\n[Generator]\npublic class HelloWorldGenerator :\n    ISourceGenerator\n{\n    public void Execute(GeneratorExecutionContext context)\n    {\n        var source1 = \"\"\"\n                      using System;\n                      public static class Helper\n                      {\n                          public static void Method()\n                          {\n                          }\n                      }\n                      \"\"\";\n        context.AddSource(\"helper\", SourceText.From(source1, Encoding.UTF8));\n\n        var source2 = \"\"\"\n                      using System;\n                      public static class HelloWorld\n                      {\n                          public static void SayHello()\n                          {\n                              Console.WriteLine(\"Hello from generated code!\");\n                          }\n                      }\n                      \"\"\";\n        var sourceText = SourceText.From(source2, Encoding.UTF8);\n        context.AddSource(\"helloWorld\", sourceText);\n\n        var descriptor = new DiagnosticDescriptor(\n            id: \"theId\",\n            title: \"the title\",\n            messageFormat: \"the message from {0}\",\n            category: \"the category\",\n            DiagnosticSeverity.Info,\n            isEnabledByDefault: true);\n\n        var location = Location.Create(\n            Path.Combine(\"dir\", \"theFile.cs\"),\n            new(1, 2),\n            new(\n                new(1, 2),\n                new(3, 4)));\n        var diagnostic = Diagnostic.Create(descriptor, location, \"hello world generator\");\n        context.ReportDiagnostic(diagnostic);\n    }\n\n    public void Initialize(GeneratorInitializationContext context)\n    {\n    }\n}\n```\n\u003csup\u003e\u003ca href='/src/SampleGenerator/HelloWorldGenerator.cs#L1-L55' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-HelloWorldGenerator.cs' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n## Test\n\nCan be tested as follows:\n\nThis snippets assumes use of the XUnit Verify adapter, change the `using VerifyXUnit` if using other testing frameworks.\n\n\u003c!-- snippet: SampleTest.cs --\u003e\n\u003ca id='snippet-SampleTest.cs'\u003e\u003c/a\u003e\n```cs\npublic class SampleTest\n{\n    [Fact]\n    public Task Driver()\n    {\n        var driver = BuildDriver();\n\n        return Verify(driver);\n    }\n\n    [Fact]\n    public Task RunResults()\n    {\n        var driver = BuildDriver();\n\n        var results = driver.GetRunResult();\n        return Verify(results);\n    }\n\n    [Fact]\n    public Task RunResult()\n    {\n        var driver = BuildDriver();\n\n        var result = driver.GetRunResult().Results.Single();\n        return Verify(result);\n    }\n\n    static GeneratorDriver BuildDriver()\n    {\n        var compilation = CSharpCompilation.Create(\"name\");\n        var generator = new HelloWorldGenerator();\n\n        var driver = CSharpGeneratorDriver.Create(generator);\n        return driver.RunGenerators(compilation);\n    }\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/SampleTest.cs#L1-L37' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-SampleTest.cs' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n## Results\n\nAnd will result in the following verified files:\n\n\n### Info file\n\nAn info file containing all metadata about the current state. eg any Diagnostics.\n\n\u003c!-- snippet: SampleTest.Driver.verified.txt --\u003e\n\u003ca id='snippet-SampleTest.Driver.verified.txt'\u003e\u003c/a\u003e\n```txt\n{\n  Diagnostics: [\n    {\n      Location: dir\\theFile.cs: (1,2)-(3,4),\n      Message: the message from hello world generator,\n      Severity: Info,\n      WarningLevel: 1,\n      Descriptor: {\n        Id: theId,\n        Title: the title,\n        MessageFormat: the message from {0},\n        Category: the category,\n        DefaultSeverity: Info,\n        IsEnabledByDefault: true\n      }\n    }\n  ]\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/SampleTest.Driver.verified.txt#L1-L18' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-SampleTest.Driver.verified.txt' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Source Files\n\nMultiple source files. One for each `GeneratorDriverRunResult.Results.GeneratedSources`.\n\n\u003c!-- snippet: SampleTest.Driver#helloWorld.verified.cs --\u003e\n\u003ca id='snippet-SampleTest.Driver#helloWorld.verified.cs'\u003e\u003c/a\u003e\n```cs\n//HintName: helloWorld.cs\nusing System;\npublic static class HelloWorld\n{\n    public static void SayHello()\n    {\n        Console.WriteLine(\"Hello from generated code!\");\n    }\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/SampleTest.Driver#helloWorld.verified.cs#L1-L9' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-SampleTest.Driver#helloWorld.verified.cs' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n## Manipulating Source\n\nTo manipulating the source of the generated cs files, use [Scrubbers](https://github.com/VerifyTests/Verify/blob/main/docs/scrubbers.md).\n\nFor example to remove all lines start with `using`:\n\n\u003c!-- snippet: ScrubLines --\u003e\n\u003ca id='snippet-ScrubLines'\u003e\u003c/a\u003e\n```cs\n[Fact]\npublic Task ScrubLines()\n{\n    var driver = GeneratorDriver();\n\n    return Verify(driver)\n        .ScrubLines(_ =\u003e _.StartsWith(\"using \"));\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/ScrubTest.cs#L3-L12' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-ScrubLines' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n \n\n  ## Ignoring Files\n\nTo ignore specific source text use `IgnoreGeneratedResult`. This uses an expression of type `Func\u003cGeneratedSourceResult, bool\u003e` to determine which outputs are ignored.\n\nFor example to ignore files with the name `helper` or that contain the text `static void SayHello()`:\n\n\u003c!-- snippet: IgnoreFile --\u003e\n\u003ca id='snippet-IgnoreFile'\u003e\u003c/a\u003e\n```cs\n[Fact]\npublic Task IgnoreFile()\n{\n    var driver = GeneratorDriver();\n\n    return Verify(driver)\n        .IgnoreGeneratedResult(\n            _ =\u003e _.HintName.Contains(\"helper\") ||\n                 _.SourceText\n                     .ToString()\n                     .Contains(\"static void SayHello()\"));\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/IgnoreTest.cs#L3-L18' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-IgnoreFile' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n## Notes:\n\n * [Source Generators Cookbook / Testing](https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md#unit-testing-of-generators)\n\n\n## Icon\n\n[Sauce](https://thenounproject.com/term/sauce/952995/) designed by [April Hsuan](https://thenounproject.com/AprilHsuan/) from [The Noun Project](https://thenounproject.com/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fverifytests%2Fverify.sourcegenerators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fverifytests%2Fverify.sourcegenerators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fverifytests%2Fverify.sourcegenerators/lists"}