Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit d45d9fc

Browse files
authored
Merge pull request #79 from amis92/feature/document-transform-tests
DocumentTransform unit tests
2 parents 8113bbd + ca6f949 commit d45d9fc

File tree

7 files changed

+552
-32
lines changed

7 files changed

+552
-32
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Andrew Arnott. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
3+
4+
namespace CodeGeneration.Roslyn.Tests.Generators
5+
{
6+
using System;
7+
using System.Diagnostics;
8+
9+
[AttributeUsage(AttributeTargets.All, Inherited = false)]
10+
[CodeGenerationAttribute(typeof(EmptyPartialGenerator))]
11+
[Conditional("CodeGeneration")]
12+
public class EmptyPartialAttribute : Attribute
13+
{
14+
}
15+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Andrew Arnott. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
3+
4+
namespace CodeGeneration.Roslyn.Tests.Generators
5+
{
6+
using System;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Microsoft.CodeAnalysis;
10+
using Microsoft.CodeAnalysis.CSharp;
11+
using Microsoft.CodeAnalysis.CSharp.Syntax;
12+
13+
public class EmptyPartialGenerator : ICodeGenerator
14+
{
15+
16+
public EmptyPartialGenerator(AttributeData attributeData)
17+
{
18+
}
19+
20+
public Task<SyntaxList<MemberDeclarationSyntax>> GenerateAsync(TransformationContext context, IProgress<Diagnostic> progress, CancellationToken cancellationToken)
21+
{
22+
if (!(context.ProcessingNode is TypeDeclarationSyntax typeDeclaration))
23+
{
24+
return Task.FromResult(SyntaxFactory.List<MemberDeclarationSyntax>());
25+
}
26+
27+
var partial = typeDeclaration is StructDeclarationSyntax structDeclaration
28+
? StructPartial(structDeclaration)
29+
: ClassPartial((ClassDeclarationSyntax) typeDeclaration);
30+
var results = SyntaxFactory.SingletonList(partial);
31+
return Task.FromResult(results);
32+
}
33+
34+
private static MemberDeclarationSyntax ClassPartial(ClassDeclarationSyntax declaration)
35+
{
36+
return SyntaxFactory.ClassDeclaration(declaration.Identifier)
37+
.WithTypeParameterList(declaration.TypeParameterList)
38+
.WithModifiers(SyntaxTokenList.Create(SyntaxFactory.Token(SyntaxKind.PartialKeyword)));
39+
}
40+
41+
private static MemberDeclarationSyntax StructPartial(StructDeclarationSyntax declaration)
42+
{
43+
return SyntaxFactory.StructDeclaration(declaration.Identifier)
44+
.WithTypeParameterList(declaration.TypeParameterList)
45+
.WithModifiers(SyntaxTokenList.Create(SyntaxFactory.Token(SyntaxKind.PartialKeyword)));
46+
}
47+
}
48+
}

src/CodeGeneration.Roslyn.Tests/CodeGeneration.Roslyn.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.2.0" />
1718
<PackageReference Include="Microsoft.CSharp" Version="4.4.1" />
1819
<PackageReference Include="Xunit" Version="2.3.1" />
1920
<PackageReference Include="Xunit.runner.visualstudio" Version="2.3.1" />

0 commit comments

Comments
 (0)