|
| 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.Mvvm.ComponentModel; |
| 7 | +using Microsoft.CodeAnalysis.Testing; |
| 8 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 9 | +using CSharpCodeFixTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixTest< |
| 10 | + CommunityToolkit.Mvvm.SourceGenerators.ClassUsingAttributeInsteadOfInheritanceAnalyzer, |
| 11 | + CommunityToolkit.Mvvm.CodeFixers.ClassUsingAttributeInsteadOfInheritanceCodeFixer, |
| 12 | + Microsoft.CodeAnalysis.Testing.Verifiers.MSTestVerifier>; |
| 13 | +using CSharpCodeFixVerifier = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixVerifier< |
| 14 | + CommunityToolkit.Mvvm.SourceGenerators.ClassUsingAttributeInsteadOfInheritanceAnalyzer, |
| 15 | + CommunityToolkit.Mvvm.CodeFixers.ClassUsingAttributeInsteadOfInheritanceCodeFixer, |
| 16 | + Microsoft.CodeAnalysis.Testing.Verifiers.MSTestVerifier>; |
| 17 | + |
| 18 | +namespace CommunityToolkit.Mvvm.SourceGenerators.Roslyn401.UnitTests; |
| 19 | + |
| 20 | +[TestClass] |
| 21 | +public class ClassUsingAttributeInsteadOfInheritanceCodeFixer |
| 22 | +{ |
| 23 | + [TestMethod] |
| 24 | + [DataRow("INotifyPropertyChanged", "MVVMTK0032")] |
| 25 | + [DataRow("ObservableObject", "MVVMTK0033")] |
| 26 | + public async Task SingleAttributeList(string attributeTypeName, string diagnosticId) |
| 27 | + { |
| 28 | + string original = $$""" |
| 29 | + using CommunityToolkit.Mvvm.ComponentModel; |
| 30 | +
|
| 31 | + // This is some trivia |
| 32 | + [{{attributeTypeName}}] |
| 33 | + class C |
| 34 | + { |
| 35 | + } |
| 36 | + """; |
| 37 | + |
| 38 | + string @fixed = """ |
| 39 | + using CommunityToolkit.Mvvm.ComponentModel; |
| 40 | +
|
| 41 | + // This is some trivia |
| 42 | + class C : ObservableObject |
| 43 | + { |
| 44 | + } |
| 45 | + """; |
| 46 | + |
| 47 | + CSharpCodeFixTest test = new() |
| 48 | + { |
| 49 | + TestCode = original, |
| 50 | + FixedCode = @fixed, |
| 51 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net60 |
| 52 | + }; |
| 53 | + |
| 54 | + test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly); |
| 55 | + test.ExpectedDiagnostics.AddRange(new[] |
| 56 | + { |
| 57 | + // /0/Test0.cs(5,15): warning <DIAGNOSTIC_ID>: The type C is using the <ATTRIBUTE_TYPE> attribute while having no base type, and it should instead inherit from ObservableObject |
| 58 | + CSharpCodeFixVerifier.Diagnostic(diagnosticId).WithSpan(5, 7, 5, 8).WithArguments("C") |
| 59 | + }); |
| 60 | + |
| 61 | + await test.RunAsync(); |
| 62 | + } |
| 63 | + |
| 64 | + [TestMethod] |
| 65 | + [DataRow("INotifyPropertyChanged", "MVVMTK0032")] |
| 66 | + [DataRow("ObservableObject", "MVVMTK0033")] |
| 67 | + public async Task SingleAttributeList_WithOtherInterface(string attributeTypeName, string diagnosticId) |
| 68 | + { |
| 69 | + string original = $$""" |
| 70 | + using System; |
| 71 | + using CommunityToolkit.Mvvm.ComponentModel; |
| 72 | +
|
| 73 | + // This is some trivia |
| 74 | + [{{attributeTypeName}}] |
| 75 | + class C : IDisposable |
| 76 | + { |
| 77 | + public void Dispose() |
| 78 | + { |
| 79 | + } |
| 80 | + } |
| 81 | + """; |
| 82 | + |
| 83 | + string @fixed = """ |
| 84 | + using System; |
| 85 | + using CommunityToolkit.Mvvm.ComponentModel; |
| 86 | +
|
| 87 | + // This is some trivia |
| 88 | + class C : ObservableObject, IDisposable |
| 89 | + { |
| 90 | + public void Dispose() |
| 91 | + { |
| 92 | + } |
| 93 | + } |
| 94 | + """; |
| 95 | + |
| 96 | + CSharpCodeFixTest test = new() |
| 97 | + { |
| 98 | + TestCode = original, |
| 99 | + FixedCode = @fixed, |
| 100 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net60 |
| 101 | + }; |
| 102 | + |
| 103 | + test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly); |
| 104 | + test.ExpectedDiagnostics.AddRange(new[] |
| 105 | + { |
| 106 | + // /0/Test0.cs(5,15): warning <DIAGNOSTIC_ID>: The type C is using the <ATTRIBUTE_TYPE> attribute while having no base type, and it should instead inherit from ObservableObject |
| 107 | + CSharpCodeFixVerifier.Diagnostic(diagnosticId).WithSpan(6, 7, 6, 8).WithArguments("C") |
| 108 | + }); |
| 109 | + |
| 110 | + await test.RunAsync(); |
| 111 | + } |
| 112 | + |
| 113 | + [TestMethod] |
| 114 | + [DataRow("INotifyPropertyChanged", "MVVMTK0032")] |
| 115 | + [DataRow("ObservableObject", "MVVMTK0033")] |
| 116 | + public async Task MultipleAttributeLists_OneBeforeTarget(string attributeTypeName, string diagnosticId) |
| 117 | + { |
| 118 | + string original = $$""" |
| 119 | + using System; |
| 120 | + using CommunityToolkit.Mvvm.ComponentModel; |
| 121 | +
|
| 122 | + // This is some trivia |
| 123 | + [Test] |
| 124 | + [{{attributeTypeName}}] |
| 125 | + class C |
| 126 | + { |
| 127 | + } |
| 128 | +
|
| 129 | + class TestAttribute : Attribute |
| 130 | + { |
| 131 | + } |
| 132 | + """; |
| 133 | + |
| 134 | + string @fixed = """ |
| 135 | + using System; |
| 136 | + using CommunityToolkit.Mvvm.ComponentModel; |
| 137 | +
|
| 138 | + // This is some trivia |
| 139 | + [Test] |
| 140 | + class C : ObservableObject |
| 141 | + { |
| 142 | + } |
| 143 | +
|
| 144 | + class TestAttribute : Attribute |
| 145 | + { |
| 146 | + } |
| 147 | + """; |
| 148 | + |
| 149 | + CSharpCodeFixTest test = new() |
| 150 | + { |
| 151 | + TestCode = original, |
| 152 | + FixedCode = @fixed, |
| 153 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net60 |
| 154 | + }; |
| 155 | + |
| 156 | + test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly); |
| 157 | + test.ExpectedDiagnostics.AddRange(new[] |
| 158 | + { |
| 159 | + // /0/Test0.cs(5,15): warning <DIAGNOSTIC_ID>: The type C is using the <ATTRIBUTE_TYPE> attribute while having no base type, and it should instead inherit from ObservableObject |
| 160 | + CSharpCodeFixVerifier.Diagnostic(diagnosticId).WithSpan(7, 7, 7, 8).WithArguments("C") |
| 161 | + }); |
| 162 | + |
| 163 | + await test.RunAsync(); |
| 164 | + } |
| 165 | + |
| 166 | + [TestMethod] |
| 167 | + [DataRow("INotifyPropertyChanged", "MVVMTK0032")] |
| 168 | + [DataRow("ObservableObject", "MVVMTK0033")] |
| 169 | + public async Task MultipleAttributeLists_OneAfterTarget(string attributeTypeName, string diagnosticId) |
| 170 | + { |
| 171 | + string original = $$""" |
| 172 | + using System; |
| 173 | + using CommunityToolkit.Mvvm.ComponentModel; |
| 174 | +
|
| 175 | + // This is some trivia |
| 176 | + [{{attributeTypeName}}] |
| 177 | + [Test] |
| 178 | + class C |
| 179 | + { |
| 180 | + } |
| 181 | +
|
| 182 | + class TestAttribute : Attribute |
| 183 | + { |
| 184 | + } |
| 185 | + """; |
| 186 | + |
| 187 | + string @fixed = """ |
| 188 | + using System; |
| 189 | + using CommunityToolkit.Mvvm.ComponentModel; |
| 190 | +
|
| 191 | + // This is some trivia |
| 192 | + [Test] |
| 193 | + class C : ObservableObject |
| 194 | + { |
| 195 | + } |
| 196 | +
|
| 197 | + class TestAttribute : Attribute |
| 198 | + { |
| 199 | + } |
| 200 | + """; |
| 201 | + |
| 202 | + CSharpCodeFixTest test = new() |
| 203 | + { |
| 204 | + TestCode = original, |
| 205 | + FixedCode = @fixed, |
| 206 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net60 |
| 207 | + }; |
| 208 | + |
| 209 | + test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly); |
| 210 | + test.ExpectedDiagnostics.AddRange(new[] |
| 211 | + { |
| 212 | + // /0/Test0.cs(5,15): warning <DIAGNOSTIC_ID>: The type C is using the <ATTRIBUTE_TYPE> attribute while having no base type, and it should instead inherit from ObservableObject |
| 213 | + CSharpCodeFixVerifier.Diagnostic(diagnosticId).WithSpan(7, 7, 7, 8).WithArguments("C") |
| 214 | + }); |
| 215 | + |
| 216 | + await test.RunAsync(); |
| 217 | + } |
| 218 | + |
| 219 | + [TestMethod] |
| 220 | + [DataRow("INotifyPropertyChanged", "MVVMTK0032")] |
| 221 | + [DataRow("ObservableObject", "MVVMTK0033")] |
| 222 | + public async Task MultipleAttributeLists_OneBeforeAndOneAfterTarget(string attributeTypeName, string diagnosticId) |
| 223 | + { |
| 224 | + string original = $$""" |
| 225 | + using System; |
| 226 | + using CommunityToolkit.Mvvm.ComponentModel; |
| 227 | +
|
| 228 | + // This is some trivia |
| 229 | + [Test] |
| 230 | + [{{attributeTypeName}}] |
| 231 | + [Test] |
| 232 | + class C |
| 233 | + { |
| 234 | + } |
| 235 | +
|
| 236 | + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] |
| 237 | + class TestAttribute : Attribute |
| 238 | + { |
| 239 | + } |
| 240 | + """; |
| 241 | + |
| 242 | + string @fixed = """ |
| 243 | + using System; |
| 244 | + using CommunityToolkit.Mvvm.ComponentModel; |
| 245 | +
|
| 246 | + // This is some trivia |
| 247 | + [Test] |
| 248 | + [Test] |
| 249 | + class C : ObservableObject |
| 250 | + { |
| 251 | + } |
| 252 | +
|
| 253 | + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] |
| 254 | + class TestAttribute : Attribute |
| 255 | + { |
| 256 | + } |
| 257 | + """; |
| 258 | + |
| 259 | + CSharpCodeFixTest test = new() |
| 260 | + { |
| 261 | + TestCode = original, |
| 262 | + FixedCode = @fixed, |
| 263 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net60 |
| 264 | + }; |
| 265 | + |
| 266 | + test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly); |
| 267 | + test.ExpectedDiagnostics.AddRange(new[] |
| 268 | + { |
| 269 | + // /0/Test0.cs(5,15): warning <DIAGNOSTIC_ID>: The type C is using the <ATTRIBUTE_TYPE> attribute while having no base type, and it should instead inherit from ObservableObject |
| 270 | + CSharpCodeFixVerifier.Diagnostic(diagnosticId).WithSpan(8, 7, 8, 8).WithArguments("C") |
| 271 | + }); |
| 272 | + |
| 273 | + await test.RunAsync(); |
| 274 | + } |
| 275 | + |
| 276 | + [TestMethod] |
| 277 | + [DataRow("INotifyPropertyChanged", "MVVMTK0032")] |
| 278 | + [DataRow("ObservableObject", "MVVMTK0033")] |
| 279 | + public async Task MultipleAttributesInAttributeList(string attributeTypeName, string diagnosticId) |
| 280 | + { |
| 281 | + string original = $$""" |
| 282 | + using System; |
| 283 | + using CommunityToolkit.Mvvm.ComponentModel; |
| 284 | +
|
| 285 | + // This is some trivia |
| 286 | + [Test, {{attributeTypeName}}] |
| 287 | + class C |
| 288 | + { |
| 289 | + } |
| 290 | +
|
| 291 | + class TestAttribute : Attribute |
| 292 | + { |
| 293 | + } |
| 294 | + """; |
| 295 | + |
| 296 | + string @fixed = """ |
| 297 | + using System; |
| 298 | + using CommunityToolkit.Mvvm.ComponentModel; |
| 299 | +
|
| 300 | + // This is some trivia |
| 301 | + [Test] |
| 302 | + class C : ObservableObject |
| 303 | + { |
| 304 | + } |
| 305 | +
|
| 306 | + class TestAttribute : Attribute |
| 307 | + { |
| 308 | + } |
| 309 | + """; |
| 310 | + |
| 311 | + CSharpCodeFixTest test = new() |
| 312 | + { |
| 313 | + TestCode = original, |
| 314 | + FixedCode = @fixed, |
| 315 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net60 |
| 316 | + }; |
| 317 | + |
| 318 | + test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly); |
| 319 | + test.ExpectedDiagnostics.AddRange(new[] |
| 320 | + { |
| 321 | + // /0/Test0.cs(5,15): warning <DIAGNOSTIC_ID>: The type C is using the <ATTRIBUTE_TYPE> attribute while having no base type, and it should instead inherit from ObservableObject |
| 322 | + CSharpCodeFixVerifier.Diagnostic(diagnosticId).WithSpan(6, 7, 6, 8).WithArguments("C") |
| 323 | + }); |
| 324 | + |
| 325 | + await test.RunAsync(); |
| 326 | + } |
| 327 | +} |
0 commit comments