|
| 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