-
-
Notifications
You must be signed in to change notification settings - Fork 747
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add more incremental tests (#1871)
- Loading branch information
1 parent
057ba9e
commit c1d7aa1
Showing
7 changed files
with
456 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.