Skip to content

Commit

Permalink
Add tests for the new incremental syntax generator
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Aug 7, 2021
1 parent 3868ecb commit 2422617
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<SignAssembly>true</SignAssembly>
<IsRoslynComponent>true</IsRoslynComponent>
<Nullable>enable</Nullable>
<DefineConstants>$(DefineConstants);ROSLYN_4</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ namespace Refit.Generator
// defn's

[Generator]
#if ROSLYN_4
public class InterfaceStubGeneratorV2 : ISourceGenerator
#else
public class InterfaceStubGenerator : ISourceGenerator
#endif
{
#pragma warning disable RS2008 // Enable analyzer release tracking
static readonly DiagnosticDescriptor InvalidRefitMember = new(
Expand Down
46 changes: 46 additions & 0 deletions Refit.Tests/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using Task = System.Threading.Tasks.Task;
using VerifyCS = Refit.Tests.CSharpSourceGeneratorVerifier<Refit.Generator.InterfaceStubGenerator>;
using VerifyCSV2 = Refit.Tests.CSharpSourceGeneratorVerifier<Refit.Generator.InterfaceStubGeneratorV2>;

namespace Refit.Tests
{
Expand Down Expand Up @@ -102,6 +103,7 @@ static Compilation CreateCompilation(params string[] sourceFiles)
public async Task NoRefitInterfacesSmokeTest()
{
var input = File.ReadAllText(IntegrationTestHelper.GetPath("IInterfaceWithoutRefit.cs"));

await new VerifyCS.Test
{
ReferenceAssemblies = ReferenceAssemblies,
Expand All @@ -111,6 +113,16 @@ public async Task NoRefitInterfacesSmokeTest()
Sources = { input },
},
}.RunAsync();

await new VerifyCSV2.Test
{
ReferenceAssemblies = ReferenceAssemblies,
TestState =
{
AdditionalReferences = { RefitAssembly },
Sources = { input },
},
}.RunAsync();
}

[Fact]
Expand Down Expand Up @@ -640,6 +652,24 @@ public RefitTestsTestNestedINestedGitHubApi(global::System.Net.Http.HttpClient c
},
},
}.RunAsync();

await new VerifyCSV2.Test
{
ReferenceAssemblies = ReferenceAssemblies,
TestState =
{
AdditionalReferences = { RefitAssembly },
Sources = { input },
GeneratedSources =
{
(typeof(InterfaceStubGeneratorV2), "PreserveAttribute.g.cs", output1),
(typeof(InterfaceStubGeneratorV2), "Generated.g.cs", output1_5),
(typeof(InterfaceStubGeneratorV2), "IGitHubApi.g.cs", output2),
(typeof(InterfaceStubGeneratorV2), "IGitHubApiDisposable.g.cs", output3),
(typeof(InterfaceStubGeneratorV2), "INestedGitHubApi.g.cs", output4),
},
},
}.RunAsync();
}


Expand Down Expand Up @@ -768,6 +798,22 @@ public IServiceWithoutNamespace(global::System.Net.Http.HttpClient client, globa
},
},
}.RunAsync();

await new VerifyCSV2.Test
{
ReferenceAssemblies = ReferenceAssemblies,
TestState =
{
AdditionalReferences = { RefitAssembly },
Sources = { input },
GeneratedSources =
{
(typeof(InterfaceStubGeneratorV2), "PreserveAttribute.g.cs", output1),
(typeof(InterfaceStubGeneratorV2), "Generated.g.cs", output1_5),
(typeof(InterfaceStubGeneratorV2), "IServiceWithoutNamespace.g.cs", output2),
},
},
}.RunAsync();
}
}

Expand Down
3 changes: 2 additions & 1 deletion Refit.Tests/Refit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.0.0-2.final" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.0" />
<PackageReference Include="RichardSzalay.MockHttp" Version="6.0.0" />
<ProjectReference Include="..\Refit.HttpClientFactory\Refit.HttpClientFactory.csproj" />
<ProjectReference Include="..\Refit.Newtonsoft.Json\Refit.Newtonsoft.Json.csproj" />
<ProjectReference Include="..\InterfaceStubGenerator.Roslyn38\InterfaceStubGenerator.Roslyn38.csproj"
OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
<ProjectReference Include="..\InterfaceStubGenerator.Roslyn40\InterfaceStubGenerator.Roslyn40.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net461' ">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Collections.Generic;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;

namespace Refit.Tests
{
public static partial class CSharpIncrementalSourceGeneratorVerifier<TIncrementalGenerator>
where TIncrementalGenerator : IIncrementalGenerator, new()
{
public class Test : CSharpSourceGeneratorTest<EmptySourceGeneratorProvider, XUnitVerifier>
{
public Test()
{
SolutionTransforms.Add((solution, projectId) =>
{
var compilationOptions = solution.GetProject(projectId).CompilationOptions;
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(
compilationOptions.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings));
solution = solution.WithProjectCompilationOptions(projectId, compilationOptions);
return solution;
});
}

protected override IEnumerable<ISourceGenerator> GetSourceGenerators()
{
yield return new TIncrementalGenerator().AsSourceGenerator();
}

protected override ParseOptions CreateParseOptions()
{
var parseOptions = (CSharpParseOptions)base.CreateParseOptions();
return parseOptions.WithLanguageVersion(LanguageVersion.Preview);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.CodeAnalysis;

namespace Refit.Tests
{
public static partial class CSharpIncrementalSourceGeneratorVerifier<TIncrementalGenerator>
where TIncrementalGenerator : IIncrementalGenerator, new()
{
}
}

0 comments on commit 2422617

Please sign in to comment.