Skip to content

Commit

Permalink
dotnet#1074: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Mar 25, 2023
1 parent af56aa9 commit 0e758f6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -261,6 +262,49 @@ public async Task AddImplicitSimpleFileWithDiagnostic()
}.RunAsync();
}

[Fact]
public async Task AddSampleClassWithDefaultCheck()
{
await new CSharpSourceGeneratorTest<AddSampleClass>
{
TestState =
{
Sources =
{
@"{|#0:|}// Comment",
},
GeneratedSources =
{
($"Microsoft.CodeAnalysis.Testing.Utilities\\Microsoft.CodeAnalysis.Testing.TestGenerators.{nameof(AddSampleClass)}\\{AddSampleClass.FileName}", SourceText.From(AddSampleClass.GeneratedSource, Encoding.UTF8) )
}
},
}.RunAsync();
}

[Fact]
public async Task AddSampleClassWithCustomCheck()
{
var test = new CSharpSourceGeneratorTest<AddSampleClass>
{
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck,
TestState =
{
Sources =
{
@"{|#0:|}// Comment",
},
},
};

await test.RunAsync();

var verifier = new DefaultVerifier();
verifier.Equal(1, test.GeneratedTrees.Length);
var tree = test.GeneratedTrees[0];
verifier.Equal(AddSampleClass.FileName, Path.GetFileName(tree.FilePath));
verifier.Equal(AddSampleClass.GeneratedSource, (await tree.GetTextAsync()).ToString());
}

private class CSharpSourceGeneratorTest<TSourceGenerator> : SourceGeneratorTest<DefaultVerifier>
where TSourceGenerator : ISourceGenerator, new()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.Testing.TestGenerators
{
public class AddSampleClass : ISourceGenerator
{
public const string FileName = "SampleClass.cs";
public const string GeneratedSource = "class Sample {}";

public void Initialize(GeneratorInitializationContext context)
{
}

public virtual void Execute(GeneratorExecutionContext context)
{
context.AddSource(FileName, GeneratedSource);
}
}
}

0 comments on commit 0e758f6

Please sign in to comment.