Skip to content

Commit 0057de0

Browse files
author
Laszlo Deak
committed
Initial Test
1 parent 2b3c849 commit 0057de0

8 files changed

+133
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections.Immutable;
2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.CSharp;
4+
using Microsoft.CodeAnalysis.CSharp.Testing;
5+
using Microsoft.CodeAnalysis.Testing.Verifiers;
6+
7+
namespace ProtobufSourceGenerator.Tests;
8+
9+
public static class CSharpSourceGeneratorVerifier<TSourceGenerator> where TSourceGenerator : ISourceGenerator, new()
10+
{
11+
public class Test : CSharpSourceGeneratorTest<TSourceGenerator, XUnitVerifier>
12+
{
13+
public Test()
14+
{
15+
}
16+
17+
protected override CompilationOptions CreateCompilationOptions()
18+
{
19+
var compilationOptions = base.CreateCompilationOptions();
20+
return compilationOptions.WithSpecificDiagnosticOptions(
21+
compilationOptions.SpecificDiagnosticOptions.SetItems(GetNullableWarningsFromCompiler()));
22+
}
23+
24+
public LanguageVersion LanguageVersion { get; set; } = LanguageVersion.Default;
25+
26+
private static ImmutableDictionary<string, ReportDiagnostic> GetNullableWarningsFromCompiler()
27+
{
28+
string[] args = { "/warnaserror:nullable" };
29+
var commandLineArguments = CSharpCommandLineParser.Default.Parse(args, baseDirectory: Environment.CurrentDirectory, sdkDirectory: Environment.CurrentDirectory);
30+
var nullableWarnings = commandLineArguments.CompilationOptions.SpecificDiagnosticOptions;
31+
32+
return nullableWarnings;
33+
}
34+
35+
protected override ParseOptions CreateParseOptions()
36+
{
37+
return ((CSharpParseOptions)base.CreateParseOptions()).WithLanguageVersion(LanguageVersion);
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Microsoft.CodeAnalysis.Testing;
2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.Text;
4+
using System.Text;
5+
using VerifyCS = ProtobufSourceGenerator.Tests.CSharpSourceGeneratorVerifier<ProtobufSourceGenerator.ProtoGenerator>;
6+
using ProtoBuf;
7+
8+
namespace ProtobufSourceGenerator.Tests;
9+
10+
public class ProtoGeneratorTests
11+
{
12+
[Fact]
13+
public async Task SinglePropertyTest()
14+
{
15+
var code = @"namespace Test;
16+
17+
[ProtoBuf.ProtoContract]
18+
public partial class Entity
19+
{
20+
public int Id { get; set; }
21+
}";
22+
23+
var generated = @"namespace Test;
24+
25+
public partial class Entity
26+
{
27+
[ProtoBuf.ProtoMember(1)]
28+
private System.Int32 ProtoId { get => Id; set => Id = value; }
29+
}";
30+
var test = new VerifyCS.Test
31+
{
32+
TestState =
33+
{
34+
Sources = { code },
35+
GeneratedSources =
36+
{
37+
(typeof(ProtoGenerator), "ProtoEntity.g.cs", SourceText.From(generated, Encoding.UTF8, SourceHashAlgorithm.Sha1)),
38+
},
39+
},
40+
};
41+
42+
test.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(typeof(ProtoContractAttribute).Assembly.Location));
43+
test.TestState.ReferenceAssemblies = new ReferenceAssemblies("net7.0", new PackageIdentity("Microsoft.NETCore.App.Ref", "7.0.0"), Path.Combine("ref", "net7.0"));
44+
45+
46+
await test.RunAsync();
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="protobuf-net" Version="3.1.26" />
12+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.1" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
14+
<PackageReference Include="xunit" Version="2.4.2" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
<PackageReference Include="coverlet.collector" Version="3.1.2">
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" PrivateAssets="all" />
24+
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.4.0" PrivateAssets="all" />
25+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" />
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<ProjectReference Include="..\ProtobufSourceGenerator\ProtobufSourceGenerator.csproj" />
30+
</ItemGroup>
31+
32+
</Project>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using Xunit;

ProtobufSourceGenerator.sln

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.5.33209.295
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtobufSourceGenerator", "ProtobufSourceGenerator\ProtobufSourceGenerator.csproj", "{6AEBF24B-6F08-431F-AFDE-A1BF1E47DF2A}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProtobufSourceGenerator", "ProtobufSourceGenerator\ProtobufSourceGenerator.csproj", "{6AEBF24B-6F08-431F-AFDE-A1BF1E47DF2A}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleApp", "SampleApp\SampleApp.csproj", "{6154F071-AE91-413F-B0BE-3E8E3BFDCF1E}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleApp", "SampleApp\SampleApp.csproj", "{6154F071-AE91-413F-B0BE-3E8E3BFDCF1E}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtobufSourceGenerator.Tests", "ProtobufSourceGenerator.Tests\ProtobufSourceGenerator.Tests.csproj", "{B01D3533-FEC1-4797-9148-90CE4E91DE5A}"
911
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +23,10 @@ Global
2123
{6154F071-AE91-413F-B0BE-3E8E3BFDCF1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{6154F071-AE91-413F-B0BE-3E8E3BFDCF1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{6154F071-AE91-413F-B0BE-3E8E3BFDCF1E}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{B01D3533-FEC1-4797-9148-90CE4E91DE5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{B01D3533-FEC1-4797-9148-90CE4E91DE5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{B01D3533-FEC1-4797-9148-90CE4E91DE5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{B01D3533-FEC1-4797-9148-90CE4E91DE5A}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

ProtobufSourceGenerator/ProtoClassGenerator.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private string CreateClass(IEnumerable<PropertyInfo> propertyShadows)
4040
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(SyntaxFactory.IdentifierName(shadow.Property.Identifier.Text)))
4141
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
4242

43-
var setter = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
43+
var setter = SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
4444
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(SyntaxFactory.AssignmentExpression(
4545
SyntaxKind.SimpleAssignmentExpression,
4646
SyntaxFactory.IdentifierName(shadow.Property.Identifier.Text),
@@ -51,7 +51,7 @@ private string CreateClass(IEnumerable<PropertyInfo> propertyShadows)
5151
SyntaxFactory.AttributeList(
5252
SyntaxFactory.SingletonSeparatedList(
5353
SyntaxFactory.Attribute(
54-
SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("Protobuf"), SyntaxFactory.IdentifierName("ProtoMember")))
54+
SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("ProtoBuf"), SyntaxFactory.IdentifierName("ProtoMember")))
5555
.WithArgumentList(
5656
SyntaxFactory.AttributeArgumentList(
5757
SyntaxFactory.SingletonSeparatedList(

ProtobufSourceGenerator/ProtoSyntaxTreeWalker.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public override void VisitClassDeclaration(ClassDeclarationSyntax node)
4040
{
4141
if (attributeList.Attributes.Any(x => x.Name.ToFullString().Contains("ProtoContract")
4242
&& _semantics.GetSymbolInfo(x).Symbol is IMethodSymbol symbol
43-
&& symbol.ContainingType.ToString() == "Protobuf.ProtoContractAttribute"))
43+
&& symbol.ContainingType.ToString() == "ProtoBuf.ProtoContractAttribute"))
4444
{
4545
_collectingProperties = true;
4646
_currentClass = node;

SampleApp/SampleApp.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<ProjectReference Include="..\ProtobufSourceGenerator\ProtobufSourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
15+
<Analyzer Include="..\ProtobufSourceGenerator\ProtobufSourceGenerator.csproj" />
1616
</ItemGroup>
1717

1818
</Project>

0 commit comments

Comments
 (0)