Skip to content

Commit c1d7aa1

Browse files
feat: add more incremental tests (#1871)
1 parent 057ba9e commit c1d7aa1

File tree

7 files changed

+456
-138
lines changed

7 files changed

+456
-138
lines changed

InterfaceStubGenerator.Shared/DiagnosticDescriptors.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,3 @@ internal static class DiagnosticDescriptors
2626
);
2727
#pragma warning restore RS2008 // Enable analyzer release tracking
2828
}
29-
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
using Microsoft.CodeAnalysis.CSharp;
2+
3+
namespace Refit.GeneratorTests.Incremental;
4+
5+
public class FunctionTest
6+
{
7+
private const string DefaultInterface =
8+
"""
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using System.Net.Http;
13+
using System.Text;
14+
using System.Threading;
15+
using System.Threading.Tasks;
16+
using Refit;
17+
18+
namespace RefitGeneratorTest;
19+
20+
public interface IGitHubApi
21+
{
22+
[Get("/users/{user}")]
23+
Task<string> GetUser(string user);
24+
}
25+
""";
26+
27+
// [Fact]
28+
public void ModifyParameterNameDoesRegenerate()
29+
{
30+
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
31+
var compilation1 = Fixture.CreateLibrary(syntaxTree);
32+
33+
var driver1 = TestHelper.GenerateTracked(compilation1);
34+
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);
35+
36+
// change parameter name
37+
var newInterface =
38+
"""
39+
public interface IGitHubApi
40+
{
41+
[Get("/users/{myUser}")]
42+
Task<string> GetUser(string myUser);
43+
}
44+
""";
45+
var compilation2 = TestHelper.ReplaceMemberDeclaration(compilation1, "IGitHubApi", newInterface);
46+
47+
var driver2 = driver1.RunGenerators(compilation2);
48+
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
49+
}
50+
51+
// [Fact]
52+
public void ModifyParameterTypeDoesRegenerate()
53+
{
54+
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
55+
var compilation1 = Fixture.CreateLibrary(syntaxTree);
56+
57+
var driver1 = TestHelper.GenerateTracked(compilation1);
58+
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);
59+
60+
// change parameter type
61+
var newInterface =
62+
"""
63+
public interface IGitHubApi
64+
{
65+
[Get("/users/{user}")]
66+
Task<string> GetUser(int user);
67+
}
68+
""";
69+
var compilation2 = TestHelper.ReplaceMemberDeclaration(compilation1, "IGitHubApi", newInterface);
70+
71+
var driver2 = driver1.RunGenerators(compilation2);
72+
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
73+
}
74+
75+
// [Fact]
76+
public void ModifyParameterNullabilityDoesRegenerate()
77+
{
78+
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
79+
var compilation1 = Fixture.CreateLibrary(syntaxTree);
80+
81+
var driver1 = TestHelper.GenerateTracked(compilation1);
82+
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);
83+
84+
// change parameter nullability
85+
var newInterface =
86+
"""
87+
public interface IGitHubApi
88+
{
89+
[Get("/users/{user}")]
90+
Task<string> GetUser(string? user);
91+
}
92+
""";
93+
var compilation2 = TestHelper.ReplaceMemberDeclaration(compilation1, "IGitHubApi", newInterface);
94+
95+
var driver2 = driver1.RunGenerators(compilation2);
96+
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
97+
}
98+
99+
// [Fact]
100+
public void AddParameterDoesRegenerate()
101+
{
102+
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
103+
var compilation1 = Fixture.CreateLibrary(syntaxTree);
104+
105+
var driver1 = TestHelper.GenerateTracked(compilation1);
106+
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);
107+
108+
// add parameter
109+
var newInterface =
110+
"""
111+
public interface IGitHubApi
112+
{
113+
[Get("/users/{user}")]
114+
Task<string> GetUser(string user, [Query] int myParam);
115+
}
116+
""";
117+
var compilation2 = TestHelper.ReplaceMemberDeclaration(compilation1, "IGitHubApi", newInterface);
118+
119+
var driver2 = driver1.RunGenerators(compilation2);
120+
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
121+
}
122+
123+
// [Fact]
124+
public void ModifyReturnTypeDoesRegenerate()
125+
{
126+
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
127+
var compilation1 = Fixture.CreateLibrary(syntaxTree);
128+
129+
var driver1 = TestHelper.GenerateTracked(compilation1);
130+
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);
131+
132+
// change return type
133+
var newInterface =
134+
"""
135+
public interface IGitHubApi
136+
{
137+
[Get("/users/{user}")]
138+
Task<int> GetUser(string user);
139+
}
140+
""";
141+
var compilation2 = TestHelper.ReplaceMemberDeclaration(compilation1, "IGitHubApi", newInterface);
142+
143+
var driver2 = driver1.RunGenerators(compilation2);
144+
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
145+
}
146+
147+
// [Fact]
148+
public void ModifyReturnNullabilityDoesRegenerate()
149+
{
150+
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
151+
var compilation1 = Fixture.CreateLibrary(syntaxTree);
152+
153+
var driver1 = TestHelper.GenerateTracked(compilation1);
154+
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);
155+
156+
// change return nullability
157+
var newInterface =
158+
"""
159+
public interface IGitHubApi
160+
{
161+
[Get("/users/{user}")]
162+
Task<string?> GetUser(string user);
163+
}
164+
""";
165+
var compilation2 = TestHelper.ReplaceMemberDeclaration(compilation1, "IGitHubApi", newInterface);
166+
167+
var driver2 = driver1.RunGenerators(compilation2);
168+
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
169+
}
170+
171+
// [Fact]
172+
public void AddNonRefitMethodDoesRegenerate()
173+
{
174+
var syntaxTree = CSharpSyntaxTree.ParseText(DefaultInterface, CSharpParseOptions.Default);
175+
var compilation1 = Fixture.CreateLibrary(syntaxTree);
176+
177+
var driver1 = TestHelper.GenerateTracked(compilation1);
178+
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);
179+
180+
// change parameter name
181+
var newInterface =
182+
"""
183+
public interface IGitHubApi
184+
{
185+
[Get("/users/{user}")]
186+
Task<string> GetUser(string user);
187+
188+
void NonRefitMethod();
189+
}
190+
""";
191+
var compilation2 = TestHelper.ReplaceMemberDeclaration(compilation1, "IGitHubApi", newInterface);
192+
193+
var driver2 = driver1.RunGenerators(compilation2);
194+
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.Modified);
195+
}
196+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
using Microsoft.CodeAnalysis.CSharp;
2+
3+
namespace Refit.GeneratorTests.Incremental;
4+
5+
public class GenericTest
6+
{
7+
private const string GenericInterface =
8+
"""
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using System.Net.Http;
13+
using System.Text;
14+
using System.Threading;
15+
using System.Threading.Tasks;
16+
using Refit;
17+
18+
namespace RefitGeneratorTest;
19+
20+
public interface IGeneratedInterface<T1>
21+
{
22+
[Get("/users")]
23+
Task<string> Get();
24+
}
25+
""";
26+
27+
// [Fact]
28+
public void RenameGenericTypeDoesRegenerate()
29+
{
30+
var syntaxTree = CSharpSyntaxTree.ParseText(GenericInterface, CSharpParseOptions.Default);
31+
var compilation1 = Fixture.CreateLibrary(syntaxTree);
32+
33+
var driver1 = TestHelper.GenerateTracked(compilation1);
34+
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);
35+
36+
// rename generic type
37+
var compilation2 = TestHelper.ReplaceMemberDeclaration(
38+
compilation1,
39+
"IGeneratedInterface",
40+
"""
41+
public interface IGeneratedInterface<T>
42+
{
43+
[Get("/users")]
44+
Task<string> Get();
45+
}
46+
"""
47+
);
48+
var driver2 = driver1.RunGenerators(compilation2);
49+
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
50+
}
51+
52+
// [Fact]
53+
public void AddGenericConstraintDoesRegenerate()
54+
{
55+
var syntaxTree = CSharpSyntaxTree.ParseText(GenericInterface, CSharpParseOptions.Default);
56+
var compilation1 = Fixture.CreateLibrary(syntaxTree);
57+
58+
var driver1 = TestHelper.GenerateTracked(compilation1);
59+
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);
60+
61+
// add generic constraint
62+
var compilation2 = TestHelper.ReplaceMemberDeclaration(
63+
compilation1,
64+
"IGeneratedInterface",
65+
"""
66+
public interface IGeneratedInterface<T1>
67+
where T1 : class
68+
{
69+
[Get("/users")]
70+
Task<string> Get();
71+
}
72+
"""
73+
);
74+
var driver2 = driver1.RunGenerators(compilation2);
75+
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
76+
77+
// add new generic constraint
78+
var compilation3 = TestHelper.ReplaceMemberDeclaration(
79+
compilation2,
80+
"IGeneratedInterface",
81+
"""
82+
public interface IGeneratedInterface<T1>
83+
where T1 : class, new()
84+
{
85+
[Get("/users")]
86+
Task<string> Get();
87+
}
88+
"""
89+
);
90+
var driver3 = driver2.RunGenerators(compilation3);
91+
TestHelper.AssertRunReasons(driver3, IncrementalGeneratorRunReasons.ModifiedSource);
92+
}
93+
94+
// [Fact]
95+
public void AddObjectGenericConstraintDoesRegenerate()
96+
{
97+
var syntaxTree = CSharpSyntaxTree.ParseText(GenericInterface, CSharpParseOptions.Default);
98+
var compilation1 = Fixture.CreateLibrary(syntaxTree);
99+
100+
var driver1 = TestHelper.GenerateTracked(compilation1);
101+
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);
102+
103+
// add object generic constraint
104+
var compilation2 = TestHelper.ReplaceMemberDeclaration(
105+
compilation1,
106+
"IGeneratedInterface",
107+
"""
108+
public interface IGeneratedInterface<T1>
109+
where T1 : IDisposable
110+
{
111+
[Get("/users")]
112+
Task<string> Get();
113+
}
114+
"""
115+
);
116+
var driver2 = driver1.RunGenerators(compilation2);
117+
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
118+
}
119+
120+
// [Fact]
121+
public void AddGenericTypeDoesRegenerate()
122+
{
123+
var syntaxTree = CSharpSyntaxTree.ParseText(GenericInterface, CSharpParseOptions.Default);
124+
var compilation1 = Fixture.CreateLibrary(syntaxTree);
125+
126+
var driver1 = TestHelper.GenerateTracked(compilation1);
127+
TestHelper.AssertRunReasons(driver1, IncrementalGeneratorRunReasons.New);
128+
129+
// add second generic type
130+
var compilation2 = TestHelper.ReplaceMemberDeclaration(
131+
compilation1,
132+
"IGeneratedInterface",
133+
"""
134+
public interface IGeneratedInterface<T1, T2>
135+
{
136+
[Get("/users")]
137+
Task<string> Get();
138+
}
139+
"""
140+
);
141+
var driver2 = driver1.RunGenerators(compilation2);
142+
TestHelper.AssertRunReasons(driver2, IncrementalGeneratorRunReasons.ModifiedSource);
143+
}
144+
}

Refit.GeneratorTests/Incremental/IncrementalGeneratorRunReasons.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Refit.GeneratorTests.Incremental;
44

55
internal record IncrementalGeneratorRunReasons(
6-
IncrementalStepRunReason BuildMediatorStep,
6+
IncrementalStepRunReason BuildRefitStep,
77
IncrementalStepRunReason ReportDiagnosticsStep
88
)
99
{
@@ -20,12 +20,12 @@ IncrementalStepRunReason ReportDiagnosticsStep
2020
public static readonly IncrementalGeneratorRunReasons Modified = Cached with
2121
{
2222
ReportDiagnosticsStep = IncrementalStepRunReason.Modified,
23-
BuildMediatorStep = IncrementalStepRunReason.Modified,
23+
BuildRefitStep = IncrementalStepRunReason.Modified,
2424
};
2525

2626
public static readonly IncrementalGeneratorRunReasons ModifiedSource = Cached with
2727
{
2828
ReportDiagnosticsStep = IncrementalStepRunReason.Unchanged,
29-
BuildMediatorStep = IncrementalStepRunReason.Modified,
29+
BuildRefitStep = IncrementalStepRunReason.Modified,
3030
};
3131
}

0 commit comments

Comments
 (0)