Skip to content

Commit 93ae0b7

Browse files
author
Bart Koelman
committed
Added unit tests for controller source generator
1 parent 14b2ace commit 93ae0b7

6 files changed

+1031
-1
lines changed

JsonApiDotNetCore.sln

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestBuildingBlocks", "test\
4646
EndProject
4747
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCore.SourceGenerators", "src\JsonApiDotNetCore.SourceGenerators\JsonApiDotNetCore.SourceGenerators.csproj", "{952C0FDE-AFC8-455C-986F-6CC882ED8953}"
4848
EndProject
49-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGeneratorDebugger", "test\SourceGeneratorDebugger\SourceGeneratorDebugger.csproj", "{87D066F9-3540-4AC7-A748-134900969EE5}"
49+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGeneratorDebugger", "test\SourceGeneratorDebugger\SourceGeneratorDebugger.csproj", "{87D066F9-3540-4AC7-A748-134900969EE5}"
50+
EndProject
51+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGeneratorTests", "test\SourceGeneratorTests\SourceGeneratorTests.csproj", "{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}"
5052
EndProject
5153
Global
5254
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -238,6 +240,18 @@ Global
238240
{87D066F9-3540-4AC7-A748-134900969EE5}.Release|x64.Build.0 = Release|Any CPU
239241
{87D066F9-3540-4AC7-A748-134900969EE5}.Release|x86.ActiveCfg = Release|Any CPU
240242
{87D066F9-3540-4AC7-A748-134900969EE5}.Release|x86.Build.0 = Release|Any CPU
243+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
244+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}.Debug|Any CPU.Build.0 = Debug|Any CPU
245+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}.Debug|x64.ActiveCfg = Debug|Any CPU
246+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}.Debug|x64.Build.0 = Debug|Any CPU
247+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}.Debug|x86.ActiveCfg = Debug|Any CPU
248+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}.Debug|x86.Build.0 = Debug|Any CPU
249+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}.Release|Any CPU.ActiveCfg = Release|Any CPU
250+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}.Release|Any CPU.Build.0 = Release|Any CPU
251+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}.Release|x64.ActiveCfg = Release|Any CPU
252+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}.Release|x64.Build.0 = Release|Any CPU
253+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}.Release|x86.ActiveCfg = Release|Any CPU
254+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9}.Release|x86.Build.0 = Release|Any CPU
241255
EndGlobalSection
242256
GlobalSection(SolutionProperties) = preSolution
243257
HideSolutionNode = FALSE
@@ -258,6 +272,7 @@ Global
258272
{210FD61E-FF5D-4CEE-8E0D-C739ECCCBA21} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
259273
{952C0FDE-AFC8-455C-986F-6CC882ED8953} = {7A2B7ADD-ECB5-4D00-AA6A-D45BD11C97CF}
260274
{87D066F9-3540-4AC7-A748-134900969EE5} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
275+
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
261276
EndGlobalSection
262277
GlobalSection(ExtensibilityGlobals) = postSolution
263278
SolutionGuid = {A2421882-8F0A-4905-928F-B550B192F9A4}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using JsonApiDotNetCore.Resources.Annotations;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.CodeAnalysis;
6+
using Microsoft.CodeAnalysis.CSharp;
7+
using Microsoft.Extensions.Logging;
8+
9+
namespace SourceGeneratorTests
10+
{
11+
internal sealed class CompilationBuilder
12+
{
13+
private static readonly CSharpCompilationOptions DefaultOptions = new(OutputKind.DynamicallyLinkedLibrary);
14+
15+
private readonly HashSet<SyntaxTree> _syntaxTrees = new();
16+
private readonly HashSet<MetadataReference> _references = new();
17+
18+
public Compilation Build()
19+
{
20+
return CSharpCompilation.Create("compilation", _syntaxTrees, _references, DefaultOptions);
21+
}
22+
23+
public CompilationBuilder WithDefaultReferences()
24+
{
25+
WithSystemReferences();
26+
WithLoggerFactoryReference();
27+
WithJsonApiDotNetCoreReferences();
28+
return this;
29+
}
30+
31+
public CompilationBuilder WithSystemReferences()
32+
{
33+
string objectLocation = typeof(object).Assembly.Location;
34+
35+
PortableExecutableReference systemRuntimeReference =
36+
MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(objectLocation)!, "System.Runtime.dll"));
37+
38+
foreach (var reference in new[]
39+
{
40+
MetadataReference.CreateFromFile(objectLocation),
41+
systemRuntimeReference
42+
})
43+
{
44+
_references.Add(reference);
45+
}
46+
47+
return this;
48+
}
49+
50+
public CompilationBuilder WithLoggerFactoryReference()
51+
{
52+
PortableExecutableReference loggerFactoryReference = MetadataReference.CreateFromFile(typeof(ILoggerFactory).Assembly.Location);
53+
_references.Add(loggerFactoryReference);
54+
55+
return this;
56+
}
57+
58+
public CompilationBuilder WithJsonApiDotNetCoreReferences()
59+
{
60+
foreach (var reference in new[]
61+
{
62+
MetadataReference.CreateFromFile(typeof(ControllerBase).Assembly.Location),
63+
MetadataReference.CreateFromFile(typeof(AttrAttribute).Assembly.Location)
64+
})
65+
{
66+
_references.Add(reference);
67+
}
68+
69+
return this;
70+
}
71+
72+
public CompilationBuilder WithSourceCode(string source)
73+
{
74+
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source);
75+
_syntaxTrees.Add(syntaxTree);
76+
return this;
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)