Skip to content

Commit

Permalink
Test generator when used in Visual Basic and when the Options only ex…
Browse files Browse the repository at this point in the history
…ist. (#23)

Signed-off-by: AraHaan <[email protected]>
  • Loading branch information
AraHaan authored May 6, 2021
1 parent c41bb66 commit 1d55d01
Showing 1 changed file with 76 additions and 15 deletions.
91 changes: 76 additions & 15 deletions tests/SourceGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VisualBasic;
using Xunit;

public class SourceGeneratorTests
{
[Fact]
public void TestGeneratingNonGeneric()
{
var result = DoTest("TestNamespace.Test", false);
var result = DoTest("TestNamespace.Test", false, TestGenerate);
Assert.Equal(@"// <autogenerated/>
using Elskom.Generic.Libs;
using TestNamespace;
Expand All @@ -27,7 +28,7 @@ public void TestGeneratingNonGeneric()
[Fact]
public void TestGeneratingGeneric()
{
var result = DoTest("TestNamespace.Test", true);
var result = DoTest("TestNamespace.Test", true, TestGenerate);
Assert.Equal(@"// <autogenerated/>
using Elskom.Generic.Libs;
using TestNamespace;
Expand All @@ -38,12 +39,30 @@ public void TestGeneratingGeneric()

[Fact]
public void TestGeneratingFailure()
=> Assert.Throws<InvalidOperationException>(() => DoTest(string.Empty, false));
=> Assert.Throws<InvalidOperationException>(
() => DoTest(
null,
false,
TestGenerate));

[Fact]
public void TestVBGeneratingAbort()
{
var result = DoTest("TestNamespace.Test", false, TestGenerateVB);
Assert.Equal(string.Empty, result);
}

[Fact]
public void TestGenerateWithOnlyOptions()
{
var result = DoTest("TestNamespace.Test", TestGenerateSingle);
Assert.Equal(string.Empty, result);
}

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

private static string DoTest(string? assemblyType, Func<string, string> func)
=> func.Invoke($@"{{
""$schema"": ""https://raw.githubusercontent.com/Elskom/GitBuildInfo.SourceGenerator/main/settings.schema.json"",
""AssemblyType"": ""{assemblyType}"",
""IsGeneric"": false,
}}");

private static string TestGenerate(string optionsText, string gitInfoText)
=> TestGenerateInternal(
CreateCSharpCompilation(),
ImmutableArray.Create<AdditionalText>(
new CustomAdditionalText("GitBuildInfo.json", optionsText),
new CustomAdditionalText("GitInfo.json", gitInfoText)));

private static string TestGenerateSingle(string optionsText)
=> TestGenerateInternal(
CreateCSharpCompilation(),
ImmutableArray.Create<AdditionalText>(
new CustomAdditionalText("GitBuildInfo.json", optionsText)));

private static string TestGenerateVB(string optionsText, string gitInfoText)
=> TestGenerateInternal(
VisualBasicCompilation.Create(
"TestAssembly",
Array.Empty<SyntaxTree>(),
Array.Empty<MetadataReference>(),
new VisualBasicCompilationOptions(
OutputKind.DynamicallyLinkedLibrary)),
ImmutableArray.Create<AdditionalText>(
new CustomAdditionalText("GitBuildInfo.json", optionsText),
new CustomAdditionalText("GitInfo.json", gitInfoText)));

private static string TestGenerateInternal(Compilation compilation, ImmutableArray<AdditionalText> additionalTexts)
{
var compilation = CSharpCompilation.Create(
var driver = CSharpGeneratorDriver.Create(new SourceGenerator()).AddAdditionalTexts(additionalTexts);
_ = driver.RunGeneratorsAndUpdateCompilation(
compilation,
out var outputCompilation,
out var generateDiagnostics);
Assert.False(
generateDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error),
$"Failed: {generateDiagnostics.FirstOrDefault()?.GetMessage()}");
try
{
return outputCompilation.SyntaxTrees.Last().ToString();
}
catch (InvalidOperationException) when (compilation is VisualBasicCompilation || additionalTexts.Count() is 1)
{
return string.Empty;
}
}

private static Compilation CreateCSharpCompilation()
=> CSharpCompilation.Create(
"TestAssembly",
Array.Empty<SyntaxTree>(),
Array.Empty<MetadataReference>(),
new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary));
var driver = CSharpGeneratorDriver.Create(new SourceGenerator())
.AddAdditionalTexts(
ImmutableArray.Create<AdditionalText>(
new CustomAdditionalText("GitBuildInfo.json", optionsText),
new CustomAdditionalText("GitInfo.json", gitInfoText)));
_ = driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var generateDiagnostics);
Assert.False(generateDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error), $"Failed: {generateDiagnostics.FirstOrDefault()?.GetMessage()}");
return outputCompilation.SyntaxTrees.Last().ToString();
}

private class CustomAdditionalText : AdditionalText
{
Expand Down

0 comments on commit 1d55d01

Please sign in to comment.