Skip to content

Commit

Permalink
feat: add more incremental tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Oct 8, 2024
1 parent 057ba9e commit 3958a86
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 18 deletions.
1 change: 0 additions & 1 deletion InterfaceStubGenerator.Shared/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ internal static class DiagnosticDescriptors
);
#pragma warning restore RS2008 // Enable analyzer release tracking
}

144 changes: 144 additions & 0 deletions Refit.GeneratorTests/Incremental/GenericTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using Microsoft.CodeAnalysis.CSharp;

namespace Refit.GeneratorTests.Incremental;

public class GenericTest
{
private const string GenericInterface =
"""
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Refit;
namespace RefitGeneratorTest;
public interface IGeneratedInterface<T1>
{
[Get("/users")]
Task<string> Get();
}
""";

// [Fact]
public void RenameGenericTypeDoesRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(GenericInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);

// rename generic type
var compilation2 = TestHelper.ReplaceMemberDeclaration(
compilation1,
"IGeneratedInterface",
"""
public interface IGeneratedInterface<T>
{
[Get("/users")]
Task<string> Get();
}
"""
);
var driver2 = driver1.RunGenerators(compilation2);
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
}

// [Fact]
public void AddGenericConstraintDoesRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(GenericInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);

// add generic constraint
var compilation2 = TestHelper.ReplaceMemberDeclaration(
compilation1,
"IGeneratedInterface",
"""
public interface IGeneratedInterface<T1>
where T1 : class
{
[Get("/users")]
Task<string> Get();
}
"""
);
var driver2 = driver1.RunGenerators(compilation2);
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);

// add new generic constraint
var compilation3 = TestHelper.ReplaceMemberDeclaration(
compilation2,
"IGeneratedInterface",
"""
public interface IGeneratedInterface<T1>
where T1 : class, new()
{
[Get("/users")]
Task<string> Get();
}
"""
);
var driver3 = driver2.RunGenerators(compilation3);
TestHelper.AssertRunReasons(driver3, IncrementalGeneratorRunReasons.ModifiedSource);
}

// [Fact]
public void AddObjectGenericConstraintDoesRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(GenericInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);

// add object generic constraint
var compilation2 = TestHelper.ReplaceMemberDeclaration(
compilation1,
"IGeneratedInterface",
"""
public interface IGeneratedInterface<T1>
where T1 : IDisposable
{
[Get("/users")]
Task<string> Get();
}
"""
);
var driver2 = driver1.RunGenerators(compilation2);
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
}

// [Fact]
public void AddGenericTypeDoesRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(GenericInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);

// add second generic type
var compilation2 = TestHelper.ReplaceMemberDeclaration(
compilation1,
"IGeneratedInterface",
"""
public interface IGeneratedInterface<T1, T2>
{
[Get("/users")]
Task<string> Get();
}
"""
);
var driver2 = driver1.RunGenerators(compilation2);
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Refit.GeneratorTests.Incremental;

internal record IncrementalGeneratorRunReasons(
IncrementalStepRunReason BuildMediatorStep,
IncrementalStepRunReason BuildRefitStep,
IncrementalStepRunReason ReportDiagnosticsStep
)
{
Expand All @@ -20,12 +20,12 @@ IncrementalStepRunReason ReportDiagnosticsStep
public static readonly IncrementalGeneratorRunReasons Modified = Cached with
{
ReportDiagnosticsStep = IncrementalStepRunReason.Modified,
BuildMediatorStep = IncrementalStepRunReason.Modified,
BuildRefitStep = IncrementalStepRunReason.Modified,
};

public static readonly IncrementalGeneratorRunReasons ModifiedSource = Cached with
{
ReportDiagnosticsStep = IncrementalStepRunReason.Unchanged,
BuildMediatorStep = IncrementalStepRunReason.Modified,
BuildRefitStep = IncrementalStepRunReason.Modified,
};
}
75 changes: 63 additions & 12 deletions Refit.GeneratorTests/Incremental/IncrementalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Refit.GeneratorTests.Incremental;

public class IncrementalTest
{
private const string Default =
private const string DefaultInterface =
"""
using System;
using System.Collections.Generic;
Expand All @@ -27,7 +27,7 @@ public interface IGitHubApi
// [Fact]
public void AddUnrelatedTypeDoesntRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(Default, CSharpParseOptions.Default);
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
Expand All @@ -41,13 +41,13 @@ public void AddUnrelatedTypeDoesntRegenerate()
// [Fact]
public void SmallChangeDoesntRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(Default, CSharpParseOptions.Default);
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);

// only change body, don't change the method
// update syntax tree by replacing interface with itself
var compilation2 = TestHelper.ReplaceMemberDeclaration(
compilation1,
"IGitHubApi",
Expand All @@ -56,8 +56,6 @@ public interface IGitHubApi
{
[Get("/users/{user}")]
Task<string> GetUser(string user);
private record Temp();
}
"""
);
Expand All @@ -68,7 +66,7 @@ private record Temp();
// [Fact]
public void ModifyParameterNameDoesRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(Default, CSharpParseOptions.Default);
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
Expand All @@ -92,7 +90,7 @@ public interface IGitHubApi
// [Fact]
public void ModifyParameterTypeDoesRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(Default, CSharpParseOptions.Default);
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
Expand All @@ -116,7 +114,7 @@ public interface IGitHubApi
// [Fact]
public void ModifyParameterNullabilityDoesRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(Default, CSharpParseOptions.Default);
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
Expand All @@ -140,7 +138,7 @@ public interface IGitHubApi
// [Fact]
public void AddParameterDoesRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(Default, CSharpParseOptions.Default);
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
Expand All @@ -164,7 +162,7 @@ public interface IGitHubApi
// [Fact]
public void ModifyReturnTypeDoesRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(Default, CSharpParseOptions.Default);
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
Expand All @@ -188,7 +186,7 @@ public interface IGitHubApi
// [Fact]
public void ModifyReturnNullabilityDoesRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(Default, CSharpParseOptions.Default);
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
Expand All @@ -208,4 +206,57 @@ public interface IGitHubApi
var driver2 = driver1.RunGenerators(compilation2);
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
}

// [Fact]
public void AddNonRefitMethodDoesRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);

// change parameter name
var newInterface =
"""
public interface IGitHubApi
{
[Get("/users/{user}")]
Task<string> GetUser(string user);
void NonRefitMethod();
}
""";
var compilation2 = TestHelper.ReplaceMemberDeclaration(compilation1, "IGitHubApi", newInterface);

var driver2 = driver1.RunGenerators(compilation2);
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.Modified);
}

// [Fact]
public void AddNewMemberDoesRegenerate()
{
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
var compilation1 = Fixture.CreateLibrary(syntaxTree);

var driver1 = TestHelper.GenerateTracked(compilation1);
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);

// add unrelated member, don't change the method
var compilation2 = TestHelper.ReplaceMemberDeclaration(
compilation1,
"IGitHubApi",
"""
public interface IGitHubApi
{
[Get("/users/{user}")]
Task<string> GetUser(string user);
private record Temp();
}
"""
);
var driver2 = driver1.RunGenerators(compilation2);
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
}
}
Loading

0 comments on commit 3958a86

Please sign in to comment.