Skip to content

Commit 1d55d01

Browse files
authored
Test generator when used in Visual Basic and when the Options only exist. (#23)
Signed-off-by: AraHaan <seandhunt_7@yahoo.com>
1 parent c41bb66 commit 1d55d01

File tree

1 file changed

+76
-15
lines changed

1 file changed

+76
-15
lines changed

tests/SourceGeneratorTests.cs

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
using Microsoft.CodeAnalysis;
99
using Microsoft.CodeAnalysis.CSharp;
1010
using Microsoft.CodeAnalysis.Text;
11+
using Microsoft.CodeAnalysis.VisualBasic;
1112
using Xunit;
1213

1314
public class SourceGeneratorTests
1415
{
1516
[Fact]
1617
public void TestGeneratingNonGeneric()
1718
{
18-
var result = DoTest("TestNamespace.Test", false);
19+
var result = DoTest("TestNamespace.Test", false, TestGenerate);
1920
Assert.Equal(@"// <autogenerated/>
2021
using Elskom.Generic.Libs;
2122
using TestNamespace;
@@ -27,7 +28,7 @@ public void TestGeneratingNonGeneric()
2728
[Fact]
2829
public void TestGeneratingGeneric()
2930
{
30-
var result = DoTest("TestNamespace.Test", true);
31+
var result = DoTest("TestNamespace.Test", true, TestGenerate);
3132
Assert.Equal(@"// <autogenerated/>
3233
using Elskom.Generic.Libs;
3334
using TestNamespace;
@@ -38,12 +39,30 @@ public void TestGeneratingGeneric()
3839

3940
[Fact]
4041
public void TestGeneratingFailure()
41-
=> Assert.Throws<InvalidOperationException>(() => DoTest(string.Empty, false));
42+
=> Assert.Throws<InvalidOperationException>(
43+
() => DoTest(
44+
null,
45+
false,
46+
TestGenerate));
47+
48+
[Fact]
49+
public void TestVBGeneratingAbort()
50+
{
51+
var result = DoTest("TestNamespace.Test", false, TestGenerateVB);
52+
Assert.Equal(string.Empty, result);
53+
}
54+
55+
[Fact]
56+
public void TestGenerateWithOnlyOptions()
57+
{
58+
var result = DoTest("TestNamespace.Test", TestGenerateSingle);
59+
Assert.Equal(string.Empty, result);
60+
}
4261

4362
// the values on GitInfo are not valid in terms of results
4463
// that would normally be from git.
45-
private static string DoTest(string assemblyType, bool generic)
46-
=> TestGenerate(
64+
private static string DoTest(string? assemblyType, bool generic, Func<string, string, string> func)
65+
=> func.Invoke(
4766
$@"{{
4867
""$schema"": ""https://raw.githubusercontent.com/Elskom/GitBuildInfo.SourceGenerator/main/settings.schema.json"",
4968
""AssemblyType"": ""{assemblyType}"",
@@ -55,23 +74,65 @@ private static string DoTest(string assemblyType, bool generic)
5574
""GitBranch"": ""vsdfvfdsv"",
5675
}");
5776

77+
private static string DoTest(string? assemblyType, Func<string, string> func)
78+
=> func.Invoke($@"{{
79+
""$schema"": ""https://raw.githubusercontent.com/Elskom/GitBuildInfo.SourceGenerator/main/settings.schema.json"",
80+
""AssemblyType"": ""{assemblyType}"",
81+
""IsGeneric"": false,
82+
}}");
83+
5884
private static string TestGenerate(string optionsText, string gitInfoText)
85+
=> TestGenerateInternal(
86+
CreateCSharpCompilation(),
87+
ImmutableArray.Create<AdditionalText>(
88+
new CustomAdditionalText("GitBuildInfo.json", optionsText),
89+
new CustomAdditionalText("GitInfo.json", gitInfoText)));
90+
91+
private static string TestGenerateSingle(string optionsText)
92+
=> TestGenerateInternal(
93+
CreateCSharpCompilation(),
94+
ImmutableArray.Create<AdditionalText>(
95+
new CustomAdditionalText("GitBuildInfo.json", optionsText)));
96+
97+
private static string TestGenerateVB(string optionsText, string gitInfoText)
98+
=> TestGenerateInternal(
99+
VisualBasicCompilation.Create(
100+
"TestAssembly",
101+
Array.Empty<SyntaxTree>(),
102+
Array.Empty<MetadataReference>(),
103+
new VisualBasicCompilationOptions(
104+
OutputKind.DynamicallyLinkedLibrary)),
105+
ImmutableArray.Create<AdditionalText>(
106+
new CustomAdditionalText("GitBuildInfo.json", optionsText),
107+
new CustomAdditionalText("GitInfo.json", gitInfoText)));
108+
109+
private static string TestGenerateInternal(Compilation compilation, ImmutableArray<AdditionalText> additionalTexts)
59110
{
60-
var compilation = CSharpCompilation.Create(
111+
var driver = CSharpGeneratorDriver.Create(new SourceGenerator()).AddAdditionalTexts(additionalTexts);
112+
_ = driver.RunGeneratorsAndUpdateCompilation(
113+
compilation,
114+
out var outputCompilation,
115+
out var generateDiagnostics);
116+
Assert.False(
117+
generateDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error),
118+
$"Failed: {generateDiagnostics.FirstOrDefault()?.GetMessage()}");
119+
try
120+
{
121+
return outputCompilation.SyntaxTrees.Last().ToString();
122+
}
123+
catch (InvalidOperationException) when (compilation is VisualBasicCompilation || additionalTexts.Count() is 1)
124+
{
125+
return string.Empty;
126+
}
127+
}
128+
129+
private static Compilation CreateCSharpCompilation()
130+
=> CSharpCompilation.Create(
61131
"TestAssembly",
62132
Array.Empty<SyntaxTree>(),
63133
Array.Empty<MetadataReference>(),
64134
new CSharpCompilationOptions(
65135
OutputKind.DynamicallyLinkedLibrary));
66-
var driver = CSharpGeneratorDriver.Create(new SourceGenerator())
67-
.AddAdditionalTexts(
68-
ImmutableArray.Create<AdditionalText>(
69-
new CustomAdditionalText("GitBuildInfo.json", optionsText),
70-
new CustomAdditionalText("GitInfo.json", gitInfoText)));
71-
_ = driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var generateDiagnostics);
72-
Assert.False(generateDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error), $"Failed: {generateDiagnostics.FirstOrDefault()?.GetMessage()}");
73-
return outputCompilation.SyntaxTrees.Last().ToString();
74-
}
75136

76137
private class CustomAdditionalText : AdditionalText
77138
{

0 commit comments

Comments
 (0)