|
| 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 Microsoft.CodeAnalysis.CSharp; |
| 7 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 8 | +using CSharpCodeFixTest = CommunityToolkit.GeneratedDependencyProperty.Tests.Helpers.CSharpCodeFixTest< |
| 9 | + CommunityToolkit.GeneratedDependencyProperty.UseFieldDeclarationCorrectlyAnalyzer, |
| 10 | + CommunityToolkit.GeneratedDependencyProperty.UseFieldDeclarationCorrectlyCodeFixer>; |
| 11 | + |
| 12 | +namespace CommunityToolkit.GeneratedDependencyProperty.Tests; |
| 13 | + |
| 14 | +[TestClass] |
| 15 | +public class Test_UseFieldDeclarationCorrectlyCodeFixer |
| 16 | +{ |
| 17 | + [TestMethod] |
| 18 | + [DataRow("private static readonly DependencyProperty")] |
| 19 | + [DataRow("public readonly DependencyProperty")] |
| 20 | + [DataRow("public static DependencyProperty")] |
| 21 | + [DataRow("public static volatile DependencyProperty")] |
| 22 | + [DataRow("public static readonly DependencyProperty?")] |
| 23 | + public async Task SingleField(string fieldDeclaration) |
| 24 | + { |
| 25 | + string original = $$""" |
| 26 | + using Windows.UI.Xaml; |
| 27 | +
|
| 28 | + #nullable enable |
| 29 | +
|
| 30 | + namespace MyApp; |
| 31 | +
|
| 32 | + public class MyObject : DependencyObject |
| 33 | + { |
| 34 | + {{fieldDeclaration}} [|TestProperty|]; |
| 35 | + } |
| 36 | + """; |
| 37 | + |
| 38 | + const string @fixed = """ |
| 39 | + using Windows.UI.Xaml; |
| 40 | +
|
| 41 | + #nullable enable |
| 42 | +
|
| 43 | + namespace MyApp; |
| 44 | +
|
| 45 | + public class MyObject : DependencyObject |
| 46 | + { |
| 47 | + public static readonly DependencyProperty TestProperty; |
| 48 | + } |
| 49 | + """; |
| 50 | + |
| 51 | + CSharpCodeFixTest test = new(LanguageVersion.Preview) |
| 52 | + { |
| 53 | + TestCode = original, |
| 54 | + FixedCode = @fixed |
| 55 | + }; |
| 56 | + |
| 57 | + await test.RunAsync(); |
| 58 | + } |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + [DataRow("private static readonly DependencyProperty")] |
| 62 | + [DataRow("public readonly DependencyProperty")] |
| 63 | + [DataRow("public static DependencyProperty")] |
| 64 | + [DataRow("public static volatile DependencyProperty")] |
| 65 | + [DataRow("public static readonly DependencyProperty?")] |
| 66 | + public async Task SingleField_WithInitializer(string fieldDeclaration) |
| 67 | + { |
| 68 | + string original = $$""" |
| 69 | + using Windows.UI.Xaml; |
| 70 | +
|
| 71 | + #nullable enable |
| 72 | +
|
| 73 | + namespace MyApp; |
| 74 | +
|
| 75 | + public class MyObject : DependencyObject |
| 76 | + { |
| 77 | + {{fieldDeclaration}} [|TestProperty|] = DependencyProperty.Register( |
| 78 | + "Test", |
| 79 | + typeof(string), |
| 80 | + typeof(MyObject), |
| 81 | + null); |
| 82 | + } |
| 83 | + """; |
| 84 | + |
| 85 | + const string @fixed = """ |
| 86 | + using Windows.UI.Xaml; |
| 87 | +
|
| 88 | + #nullable enable |
| 89 | +
|
| 90 | + namespace MyApp; |
| 91 | +
|
| 92 | + public class MyObject : DependencyObject |
| 93 | + { |
| 94 | + public static readonly DependencyProperty TestProperty = DependencyProperty.Register( |
| 95 | + "Test", |
| 96 | + typeof(string), |
| 97 | + typeof(MyObject), |
| 98 | + null); |
| 99 | + } |
| 100 | + """; |
| 101 | + |
| 102 | + CSharpCodeFixTest test = new(LanguageVersion.Preview) |
| 103 | + { |
| 104 | + TestCode = original, |
| 105 | + FixedCode = @fixed |
| 106 | + }; |
| 107 | + |
| 108 | + await test.RunAsync(); |
| 109 | + } |
| 110 | + |
| 111 | + [TestMethod] |
| 112 | + public async Task MultipleFields() |
| 113 | + { |
| 114 | + string original = $$""" |
| 115 | + using Windows.UI.Xaml; |
| 116 | +
|
| 117 | + #nullable enable |
| 118 | +
|
| 119 | + namespace MyApp; |
| 120 | +
|
| 121 | + public class MyObject : DependencyObject |
| 122 | + { |
| 123 | + private static readonly DependencyProperty [|Test1Property|]; |
| 124 | + public readonly DependencyProperty [|Test2Property|]; |
| 125 | + public static DependencyProperty [|Test3Property|]; |
| 126 | + public static readonly DependencyProperty Test4Property; |
| 127 | + public static volatile DependencyProperty [|Test5Property|]; |
| 128 | + public static readonly DependencyProperty? [|Test6Property|]; |
| 129 | + public static readonly DependencyProperty Test7Property; |
| 130 | + public static readonly DependencyProperty Test8Property; |
| 131 | + } |
| 132 | + """; |
| 133 | + |
| 134 | + const string @fixed = """ |
| 135 | + using Windows.UI.Xaml; |
| 136 | +
|
| 137 | + #nullable enable |
| 138 | +
|
| 139 | + namespace MyApp; |
| 140 | +
|
| 141 | + public class MyObject : DependencyObject |
| 142 | + { |
| 143 | + public static readonly DependencyProperty Test1Property; |
| 144 | + public static readonly DependencyProperty Test2Property; |
| 145 | + public static readonly DependencyProperty Test3Property; |
| 146 | + public static readonly DependencyProperty Test4Property; |
| 147 | + public static readonly DependencyProperty Test5Property; |
| 148 | + public static readonly DependencyProperty Test6Property; |
| 149 | + public static readonly DependencyProperty Test7Property; |
| 150 | + public static readonly DependencyProperty Test8Property; |
| 151 | + } |
| 152 | + """; |
| 153 | + |
| 154 | + CSharpCodeFixTest test = new(LanguageVersion.Preview) |
| 155 | + { |
| 156 | + TestCode = original, |
| 157 | + FixedCode = @fixed |
| 158 | + }; |
| 159 | + |
| 160 | + await test.RunAsync(); |
| 161 | + } |
| 162 | + |
| 163 | + [TestMethod] |
| 164 | + public async Task MultipleFields_WithInitializers() |
| 165 | + { |
| 166 | + string original = $$""" |
| 167 | + using Windows.UI.Xaml; |
| 168 | +
|
| 169 | + #nullable enable |
| 170 | +
|
| 171 | + namespace MyApp; |
| 172 | +
|
| 173 | + public class MyObject : DependencyObject |
| 174 | + { |
| 175 | + private static readonly DependencyProperty [|Test1Property|] = DependencyProperty.Register( |
| 176 | + "Test1", |
| 177 | + typeof(string), |
| 178 | + typeof(MyObject), |
| 179 | + null); |
| 180 | +
|
| 181 | + public readonly DependencyProperty [|Test2Property|] = DependencyProperty.Register( |
| 182 | + "Test2", |
| 183 | + typeof(string), |
| 184 | + typeof(MyObject), |
| 185 | + null); |
| 186 | +
|
| 187 | + public static DependencyProperty [|Test3Property|]; |
| 188 | + public static readonly DependencyProperty Test4Property = DependencyProperty.Register("Test4", typeof(string), typeof(MyObject), null); |
| 189 | + public static volatile DependencyProperty [|Test5Property|]; |
| 190 | + public static readonly DependencyProperty? [|Test6Property|] = DependencyProperty.Register("Test6", typeof(string), typeof(MyObject), null); |
| 191 | + public static readonly DependencyProperty Test7Property; |
| 192 | + public static readonly DependencyProperty Test8Property = DependencyProperty.Register( |
| 193 | + "Test8", |
| 194 | + typeof(string), |
| 195 | + typeof(MyObject), |
| 196 | + null); |
| 197 | + } |
| 198 | + """; |
| 199 | + |
| 200 | + const string @fixed = """ |
| 201 | + using Windows.UI.Xaml; |
| 202 | +
|
| 203 | + #nullable enable |
| 204 | +
|
| 205 | + namespace MyApp; |
| 206 | +
|
| 207 | + public class MyObject : DependencyObject |
| 208 | + { |
| 209 | + public static readonly DependencyProperty Test1Property = DependencyProperty.Register( |
| 210 | + "Test1", |
| 211 | + typeof(string), |
| 212 | + typeof(MyObject), |
| 213 | + null); |
| 214 | +
|
| 215 | + public static readonly DependencyProperty Test2Property = DependencyProperty.Register( |
| 216 | + "Test2", |
| 217 | + typeof(string), |
| 218 | + typeof(MyObject), |
| 219 | + null); |
| 220 | +
|
| 221 | + public static readonly DependencyProperty Test3Property; |
| 222 | + public static readonly DependencyProperty Test4Property = DependencyProperty.Register("Test4", typeof(string), typeof(MyObject), null); |
| 223 | + public static readonly DependencyProperty Test5Property; |
| 224 | + public static readonly DependencyProperty Test6Property = DependencyProperty.Register("Test6", typeof(string), typeof(MyObject), null); |
| 225 | + public static readonly DependencyProperty Test7Property; |
| 226 | + public static readonly DependencyProperty Test8Property = DependencyProperty.Register( |
| 227 | + "Test8", |
| 228 | + typeof(string), |
| 229 | + typeof(MyObject), |
| 230 | + null); |
| 231 | + } |
| 232 | + """; |
| 233 | + |
| 234 | + CSharpCodeFixTest test = new(LanguageVersion.Preview) |
| 235 | + { |
| 236 | + TestCode = original, |
| 237 | + FixedCode = @fixed |
| 238 | + }; |
| 239 | + |
| 240 | + await test.RunAsync(); |
| 241 | + } |
| 242 | +} |
0 commit comments