Skip to content

Commit 7f57e6c

Browse files
committed
Add basic code fixer test
1 parent df50c2f commit 7f57e6c

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.CodeFixers/UseGeneratedDependencyPropertyOnManualPropertyCodeFixer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
using static CommunityToolkit.GeneratedDependencyProperty.Diagnostics.DiagnosticDescriptors;
2121
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
2222

23-
namespace CommunityToolkit.Mvvm.CodeFixers;
23+
namespace CommunityToolkit.GeneratedDependencyProperty;
2424

2525
/// <summary>
2626
/// A code fixer that converts manual properties into partial properties using <c>[GeneratedDependencytProperty]</c>.

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/CommunityToolkit.DependencyPropertyGenerator.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
</ItemGroup>
2525

2626
<ItemGroup>
27+
<ProjectReference Include="..\CommunityToolkit.DependencyPropertyGenerator.CodeFixers\CommunityToolkit.DependencyPropertyGenerator.CodeFixers.csproj" />
2728
<ProjectReference Include="..\CommunityToolkit.DependencyPropertyGenerator.SourceGenerators\CommunityToolkit.DependencyPropertyGenerator.SourceGenerators.csproj" />
2829
<ProjectReference Include="..\src\CommunityToolkit.WinUI.DependencyPropertyGenerator.csproj" />
2930
</ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 System.Threading.Tasks;
6+
using CommunityToolkit.WinUI;
7+
using Microsoft.CodeAnalysis;
8+
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.Testing;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
using Windows.UI.ViewManagement;
12+
using Windows.UI.Xaml;
13+
using CSharpCodeFixTest = CommunityToolkit.GeneratedDependencyProperty.Tests.Helpers.CSharpCodeFixTest<
14+
CommunityToolkit.GeneratedDependencyProperty.UseGeneratedDependencyPropertyOnManualPropertyAnalyzer,
15+
CommunityToolkit.GeneratedDependencyProperty.UseGeneratedDependencyPropertyOnManualPropertyCodeFixer>;
16+
using VerifyCS = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixVerifier<
17+
CommunityToolkit.GeneratedDependencyProperty.UseGeneratedDependencyPropertyOnManualPropertyAnalyzer,
18+
CommunityToolkit.GeneratedDependencyProperty.UseGeneratedDependencyPropertyOnManualPropertyCodeFixer,
19+
Microsoft.CodeAnalysis.Testing.DefaultVerifier>;
20+
21+
namespace CommunityToolkit.GeneratedDependencyProperty.Tests;
22+
23+
[TestClass]
24+
public class Test_UseGeneratedDependencyPropertyOnManualPropertyCodeFixer
25+
{
26+
[TestMethod]
27+
[DataRow("string", "string")]
28+
[DataRow("string", "string?")]
29+
[DataRow("object", "object")]
30+
[DataRow("object", "object?")]
31+
[DataRow("int", "int")]
32+
[DataRow("int?", "int?")]
33+
public async Task SimpleProperty(string underlyingType, string propertyType)
34+
{
35+
string original = $$"""
36+
using Windows.UI.Xaml;
37+
using Windows.UI.Xaml.Controls;
38+
39+
namespace MyApp;
40+
41+
public class MyControl : Control
42+
{
43+
public static readonly DependencyProperty NameProperty = DependencyProperty.Register(
44+
name: nameof(Name),
45+
propertyType: typeof({{underlyingType}}),
46+
ownerType: typeof(MyControl),
47+
typeMetadata: null);
48+
49+
public {{propertyType}} [|Name|]
50+
{
51+
get => ({{propertyType}})GetValue(NameProperty);
52+
set => SetValue(NameProperty, value);
53+
}
54+
}
55+
""";
56+
57+
string @fixed = $$"""
58+
using CommunityToolkit.WinUI;
59+
using Windows.UI.Xaml;
60+
using Windows.UI.Xaml.Controls;
61+
62+
namespace MyApp;
63+
64+
public partial class MyControl : Control
65+
{
66+
[GeneratedDependencyProperty]
67+
public partial {{propertyType}} {|CS9248:Name|} { get; set; }
68+
}
69+
""";
70+
71+
CSharpCodeFixTest test = new(LanguageVersion.Preview)
72+
{
73+
TestCode = original,
74+
FixedCode = @fixed,
75+
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
76+
TestState = { AdditionalReferences =
77+
{
78+
MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location),
79+
MetadataReference.CreateFromFile(typeof(DependencyProperty).Assembly.Location),
80+
MetadataReference.CreateFromFile(typeof(GeneratedDependencyPropertyAttribute).Assembly.Location)
81+
}}
82+
};
83+
84+
await test.RunAsync();
85+
}
86+
}

0 commit comments

Comments
 (0)