Skip to content

Commit

Permalink
feat: add more incremental tests (#1871)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison authored Oct 9, 2024
1 parent 057ba9e commit c1d7aa1
Show file tree
Hide file tree
Showing 7 changed files with 456 additions and 138 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
}

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

namespace Refit.GeneratorTests.Incremental;

public class FunctionTest
{
private const string DefaultInterface =
"""
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 IGitHubApi
{
[Get("/users/{user}")]
Task<string> GetUser(string user);
}
""";

// [Fact]
public void ModifyParameterNameDoesRegenerate()
{
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/{myUser}")]
Task<string> GetUser(string myUser);
}
""";
var compilation2 = TestHelper.ReplaceMemberDeclaration(compilation1, "IGitHubApi", newInterface);

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

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

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

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

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

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

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

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

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

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

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

// add parameter
var newInterface =
"""
public interface IGitHubApi
{
[Get("/users/{user}")]
Task<string> GetUser(string user, [Query] int myParam);
}
""";
var compilation2 = TestHelper.ReplaceMemberDeclaration(compilation1, "IGitHubApi", newInterface);

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

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

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

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

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

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

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

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

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);
}
}
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,
};
}
Loading

0 comments on commit c1d7aa1

Please sign in to comment.