Skip to content

Commit bea72f9

Browse files
committed
Add incrementality tests
1 parent 4d085d3 commit bea72f9

File tree

2 files changed

+142
-4
lines changed

2 files changed

+142
-4
lines changed

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Helpers/CSharpGeneratorTest{TGenerator}.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static void VerifyDiagnostics(string source, params string[] diagnosticsI
5555
/// <param name="source">The input source to process.</param>
5656
/// <param name="result">The expected source to be generated.</param>
5757
/// <param name="languageVersion">The language version to use to run the test.</param>
58-
public static void VerifySources(string source, (string Filename, string Source) result, LanguageVersion languageVersion = LanguageVersion.CSharp12)
58+
public static void VerifySources(string source, (string Filename, string Source) result, LanguageVersion languageVersion = LanguageVersion.CSharp13)
5959
{
6060
RunGenerator(source, out Compilation compilation, out ImmutableArray<Diagnostic> diagnostics, languageVersion);
6161

@@ -89,7 +89,7 @@ public static void VerifyIncrementalSteps(
8989
IncrementalStepRunReason outputReason,
9090
IncrementalStepRunReason? diagnosticsSourceReason,
9191
IncrementalStepRunReason sourceReason,
92-
LanguageVersion languageVersion = LanguageVersion.CSharp12)
92+
LanguageVersion languageVersion = LanguageVersion.CSharp13)
9393
{
9494
Compilation compilation = CreateCompilation(source, languageVersion);
9595

@@ -173,7 +173,7 @@ public static void VerifyIncrementalSteps(
173173
/// <param name="source">The input source to process.</param>
174174
/// <param name="languageVersion">The language version to use to run the test.</param>
175175
/// <returns>The resulting <see cref="Compilation"/> object.</returns>
176-
private static CSharpCompilation CreateCompilation(string source, LanguageVersion languageVersion = LanguageVersion.CSharp12)
176+
private static CSharpCompilation CreateCompilation(string source, LanguageVersion languageVersion = LanguageVersion.CSharp13)
177177
{
178178
// Get all assembly references for the .NET TFM and ComputeSharp
179179
IEnumerable<MetadataReference> metadataReferences =
@@ -209,7 +209,7 @@ private static void RunGenerator(
209209
string source,
210210
out Compilation compilation,
211211
out ImmutableArray<Diagnostic> diagnostics,
212-
LanguageVersion languageVersion = LanguageVersion.CSharp12)
212+
LanguageVersion languageVersion = LanguageVersion.CSharp13)
213213
{
214214
Compilation originalCompilation = CreateCompilation(source, languageVersion);
215215

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using CommunityToolkit.GeneratedDependencyProperty.Tests.Helpers;
6+
using Microsoft.CodeAnalysis;
7+
using Microsoft.CodeAnalysis.CSharp;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
namespace CommunityToolkit.GeneratedDependencyProperty.Tests;
11+
12+
[TestClass]
13+
public class Test_DependencyPropertyGenerator_Incrementality
14+
{
15+
[TestMethod]
16+
public void ModifiedOptions_ModifiesOutput()
17+
{
18+
const string source = """"
19+
using Windows.UI.Xaml;
20+
using CommunityToolkit.WinUI;
21+
22+
namespace MyNamespace;
23+
24+
public partial class MyControl : DependencyObject
25+
{
26+
[GeneratedDependencyProperty]
27+
public partial int Number { get; set; }
28+
}
29+
"""";
30+
31+
const string updatedSource = """"
32+
using Windows.UI.Xaml;
33+
using CommunityToolkit.WinUI;
34+
35+
namespace MyNamespace;
36+
37+
public partial class MyControl : DependencyObject
38+
{
39+
[GeneratedDependencyProperty(DefaultValue = 42)]
40+
public partial int Number { get; set; }
41+
}
42+
"""";
43+
44+
CSharpGeneratorTest<DependencyPropertyGenerator>.VerifyIncrementalSteps(
45+
source,
46+
updatedSource,
47+
executeReason: IncrementalStepRunReason.Modified,
48+
diagnosticsReason: null,
49+
outputReason: IncrementalStepRunReason.Modified,
50+
diagnosticsSourceReason: null,
51+
sourceReason: IncrementalStepRunReason.Modified);
52+
}
53+
54+
[TestMethod]
55+
public void AddedLeadingTrivia_DoesNotModifyOutput()
56+
{
57+
const string source = """"
58+
using Windows.UI.Xaml;
59+
using CommunityToolkit.WinUI;
60+
61+
namespace MyNamespace;
62+
63+
public partial class MyControl : DependencyObject
64+
{
65+
[GeneratedDependencyProperty]
66+
public partial int Number { get; set; }
67+
}
68+
"""";
69+
70+
const string updatedSource = """"
71+
using Windows.UI.Xaml;
72+
using CommunityToolkit.WinUI;
73+
74+
namespace MyNamespace;
75+
76+
public partial class MyControl : DependencyObject
77+
{
78+
/// <summary>
79+
/// This is some property.
80+
/// </summary>
81+
[GeneratedDependencyProperty]
82+
public partial int Number { get; set; }
83+
}
84+
"""";
85+
86+
CSharpGeneratorTest<DependencyPropertyGenerator>.VerifyIncrementalSteps(
87+
source,
88+
updatedSource,
89+
executeReason: IncrementalStepRunReason.Unchanged,
90+
diagnosticsReason: null,
91+
outputReason: IncrementalStepRunReason.Cached,
92+
diagnosticsSourceReason: null,
93+
sourceReason: IncrementalStepRunReason.Cached);
94+
}
95+
96+
[TestMethod]
97+
public void AddedOtherMember_DoesNotModifyOutput()
98+
{
99+
const string source = """"
100+
using Windows.UI.Xaml;
101+
using CommunityToolkit.WinUI;
102+
103+
namespace MyNamespace;
104+
105+
public partial class MyControl : DependencyObject
106+
{
107+
[GeneratedDependencyProperty]
108+
public partial int Number { get; set; }
109+
}
110+
"""";
111+
112+
const string updatedSource = """"
113+
using Windows.UI.Xaml;
114+
using CommunityToolkit.WinUI;
115+
116+
namespace MyNamespace;
117+
118+
public partial class MyControl : DependencyObject
119+
{
120+
public void Foo()
121+
{
122+
}
123+
124+
[GeneratedDependencyProperty]
125+
public partial int Number { get; set; }
126+
}
127+
"""";
128+
129+
CSharpGeneratorTest<DependencyPropertyGenerator>.VerifyIncrementalSteps(
130+
source,
131+
updatedSource,
132+
executeReason: IncrementalStepRunReason.Unchanged,
133+
diagnosticsReason: null,
134+
outputReason: IncrementalStepRunReason.Cached,
135+
diagnosticsSourceReason: null,
136+
sourceReason: IncrementalStepRunReason.Cached);
137+
}
138+
}

0 commit comments

Comments
 (0)