-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added source generator diagnostics tests and fixed bug while generati…
…ng OneOfBase with interfaces (#123) * added UserDefinedConversionsToOrFromAnInterfaceAreNotAllowed diagnostic description and its usage in OneOfGenerator * bumped Microsoft.CodeAnalysis.CSharp to 4.2.0 * Added new analyzer tests project * Changed UserDefinedConversionsToOrFromAnInterfaceAreNotAllowed id to custom * rollbacked Microsoft.CodeAnalysis.CSharp to 3.9.0 due to appveyor error * ProcessClass cleanup Co-authored-by: Damian Romanowski <Damian.Romanowski@britishcouncil.org>
- Loading branch information
Showing
5 changed files
with
276 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Xunit; | ||
|
||
namespace OneOf.SourceGenerator.AnalyzerTests | ||
{ | ||
public class AnalyzerTests | ||
{ | ||
[Fact] | ||
public void GenerateOneOfAttribute() | ||
{ | ||
const string expectedCode = @"// <auto-generated /> | ||
using System; | ||
#pragma warning disable 1591 | ||
namespace OneOf | ||
{ | ||
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] | ||
internal sealed class GenerateOneOfAttribute : Attribute | ||
{ | ||
} | ||
} | ||
"; | ||
AssertCorrectSourceCodeIsGeneratedWithNoDiagnostics(string.Empty, expectedCode, "GenerateOneOfAttribute.g.cs", 2); | ||
} | ||
|
||
[Fact] | ||
public void StringOrNumber() | ||
{ | ||
const string input = @" | ||
using OneOf; | ||
namespace Foo | ||
{ | ||
[GenerateOneOf] | ||
public partial class StringOrNumber : OneOfBase<string, int, uint> { } | ||
} | ||
"; | ||
|
||
const string expectedCode = @"// <auto-generated /> | ||
#pragma warning disable 1591 | ||
namespace Foo | ||
{ | ||
public partial class StringOrNumber | ||
{ | ||
public StringOrNumber(OneOf.OneOf<string, int, uint> _) : base(_) { } | ||
public static implicit operator StringOrNumber(string _) => new StringOrNumber(_); | ||
public static explicit operator string(StringOrNumber _) => _.AsT0; | ||
public static implicit operator StringOrNumber(int _) => new StringOrNumber(_); | ||
public static explicit operator int(StringOrNumber _) => _.AsT1; | ||
public static implicit operator StringOrNumber(uint _) => new StringOrNumber(_); | ||
public static explicit operator uint(StringOrNumber _) => _.AsT2; | ||
} | ||
}"; | ||
|
||
AssertCorrectSourceCodeIsGeneratedWithNoDiagnostics(input, expectedCode, "Foo_StringOrNumber.g.cs"); | ||
} | ||
|
||
[Fact] | ||
public void Class_Must_Be_Top_Level() | ||
{ | ||
const string input = @" | ||
using OneOf; | ||
namespace Foo | ||
{ | ||
public static class A | ||
{ | ||
[GenerateOneOf] | ||
public partial class StringOrNumber : OneOfBase<string, int> { } | ||
} | ||
} | ||
"; | ||
|
||
AssertDiagnosticErrorIsReturned(input, GeneratorDiagnosticDescriptors.TopLevelError.Id); | ||
} | ||
|
||
[Fact] | ||
public void Class_Must_Be_Public() | ||
{ | ||
const string input = @" | ||
using OneOf; | ||
namespace Foo | ||
{ | ||
[GenerateOneOf] | ||
private partial class StringOrNumber : OneOfBase<string, int> { } | ||
} | ||
"; | ||
AssertDiagnosticErrorIsReturned(input, GeneratorDiagnosticDescriptors.ClassIsNotPublic.Id); | ||
} | ||
|
||
[Fact] | ||
public void Cannot_Use_Generator_With_Object_type() | ||
{ | ||
const string input = @" | ||
using OneOf; | ||
namespace Foo | ||
{ | ||
[GenerateOneOf] | ||
public partial class ObjectOrNumber : OneOfBase<object, int> { } | ||
} | ||
"; | ||
AssertDiagnosticErrorIsReturned(input, GeneratorDiagnosticDescriptors.ObjectIsOneOfType.Id); | ||
} | ||
|
||
[Fact] | ||
public void Class_Must_Be_Derived_From_OneOfBase() | ||
{ | ||
const string input = @" | ||
using OneOf; | ||
namespace Foo | ||
{ | ||
[GenerateOneOf] | ||
public partial class ObjectOrNumber : MyClass { } | ||
public class MyClass | ||
{ | ||
} | ||
} | ||
"; | ||
AssertDiagnosticErrorIsReturned(input, GeneratorDiagnosticDescriptors.WrongBaseType.Id); | ||
} | ||
|
||
[Fact] | ||
public void User_Defined_Conversions_To_Or_From_An_Interface_Are_Not_Allowed() | ||
{ | ||
const string input = @" | ||
using OneOf; | ||
namespace Foo | ||
{ | ||
[GenerateOneOf] | ||
public partial class ObjectOrNumber : OneOfBase<IFoo, int> { } | ||
public interface IFoo | ||
{ | ||
} | ||
} | ||
"; | ||
AssertDiagnosticErrorIsReturned(input, GeneratorDiagnosticDescriptors.UserDefinedConversionsToOrFromAnInterfaceAreNotAllowed.Id); | ||
} | ||
|
||
private static void AssertCorrectSourceCodeIsGeneratedWithNoDiagnostics(string inputSource, string expectedCode, string generatedFileName, int expectedCompilationFileCount = 3) | ||
{ | ||
var parsedAttribute = CSharpSyntaxTree.ParseText(expectedCode); | ||
|
||
var inputCompilation = CreateCompilation(inputSource); | ||
|
||
GeneratorDriver driver = CSharpGeneratorDriver.Create(new OneOfGenerator()); | ||
|
||
driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); | ||
|
||
Assert.True(diagnostics.IsEmpty); | ||
|
||
Assert.Equal(expectedCompilationFileCount, outputCompilation.SyntaxTrees.Count()); | ||
|
||
Assert.Empty(outputCompilation.GetDiagnostics()); | ||
|
||
var compiledAttribute = outputCompilation.SyntaxTrees.Single(e => e.FilePath.Contains(generatedFileName)); | ||
|
||
Assert.True(parsedAttribute.IsEquivalentTo(compiledAttribute)); | ||
|
||
Assert.True(outputCompilation.GetDiagnostics().IsEmpty); | ||
} | ||
|
||
private static void AssertDiagnosticErrorIsReturned(string inputSource, string diagnosticId) | ||
{ | ||
var inputCompilation = CreateCompilation(inputSource); | ||
|
||
GeneratorDriver driver = CSharpGeneratorDriver.Create(new OneOfGenerator()); | ||
|
||
driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out _, out var diagnostics); | ||
|
||
Assert.Contains(diagnostics, d => d.Id == diagnosticId && d.Severity == DiagnosticSeverity.Error); | ||
} | ||
|
||
private static Compilation CreateCompilation(string source) | ||
{ | ||
var references = new List<MetadataReference> | ||
{ | ||
MetadataReference.CreateFromFile(typeof(Binder).GetTypeInfo().Assembly.Location), | ||
MetadataReference.CreateFromFile(typeof(OneOfBase<>).GetTypeInfo().Assembly.Location), | ||
MetadataReference.CreateFromFile(AppDomain.CurrentDomain.GetAssemblies().Single(a => a.GetName().Name == "netstandard").Location) | ||
}; | ||
|
||
//https://github.com/dotnet/roslyn/issues/49498#issuecomment-734452762 | ||
foreach (var assembly in Assembly.GetEntryAssembly()!.GetReferencedAssemblies()) | ||
{ | ||
references.Add(MetadataReference.CreateFromFile(Assembly.Load(assembly).Location)); | ||
} | ||
|
||
return CSharpCompilation.Create("compilation", new[] { CSharpSyntaxTree.ParseText(source) }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
OneOf.SourceGenerator.AnalyzerTests/OneOf.SourceGenerator.AnalyzerTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.1" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.9.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\OneOf.SourceGenerator\OneOf.SourceGenerator.csproj" /> | ||
<ProjectReference Include="..\OneOf\OneOf.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters