|
| 1 | +using System.Threading.Tasks; |
| 2 | +using AnalyzerTesting.CSharp.Extensions; |
| 3 | +using DarkPatterns.Refactoring.Verifiers; |
| 4 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | + |
| 6 | +namespace DarkPatterns.Refactoring; |
| 7 | + |
| 8 | +[TestClass] |
| 9 | +public class MustRemovePlannedForRemovalAnalyzerShould |
| 10 | +{ |
| 11 | + public TestContext TestContext { get; set; } |
| 12 | + |
| 13 | + [TestMethod] |
| 14 | + public async Task Produce_no_diagnostics_by_default() |
| 15 | + { |
| 16 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 17 | + .AddSources(@"") |
| 18 | + .RunAsync(TestContext.CancellationToken); |
| 19 | + } |
| 20 | + |
| 21 | + #region fields |
| 22 | + [TestMethod] |
| 23 | + public async Task Produce_no_diagnostic_when_using_classes_being_removed_as_a_private_field_when_marked_for_refactor() |
| 24 | + { |
| 25 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 26 | + .AddSources(""" |
| 27 | + [PlannedRemoval("1", "Remove this class")] |
| 28 | + class OutdatedClass { } |
| 29 | +
|
| 30 | + [PlannedRefactor("1", "Needs rework")] |
| 31 | + class SomeClass |
| 32 | + { |
| 33 | + private OutdatedClass shouldBeRefactored; |
| 34 | + } |
| 35 | + """) |
| 36 | + .RunAsync(TestContext.CancellationToken); |
| 37 | + } |
| 38 | + |
| 39 | + [TestMethod] |
| 40 | + public async Task Warn_when_using_classes_being_removed_as_a_nonprivate_field_when_marked_for_refactor() |
| 41 | + { |
| 42 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 43 | + .AddSources(""" |
| 44 | + [PlannedRemoval("1", "Use NewClass instead")] |
| 45 | + class OutdatedClass { } |
| 46 | +
|
| 47 | + [PlannedRefactor("1", "Needs rework")] |
| 48 | + class SomeClass |
| 49 | + { |
| 50 | + protected {|#0:OutdatedClass|} shouldBeRefactored; |
| 51 | + } |
| 52 | + """) |
| 53 | + .AddExpectedDiagnostics(MustRemovePlannedForRemovalAnalyzer.Rule.AsResult().WithLocation(0).WithArguments("OutdatedClass", "shouldBeRefactored", "1", "Use NewClass instead")) |
| 54 | + .RunAsync(TestContext.CancellationToken); |
| 55 | + } |
| 56 | + |
| 57 | + // TODO: test field initialization |
| 58 | + #endregion fields |
| 59 | + |
| 60 | + #region properties |
| 61 | + [TestMethod] |
| 62 | + public async Task Warn_when_using_classes_being_removed_as_a_nonprivate_property_when_marked_for_refactor() |
| 63 | + { |
| 64 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 65 | + .AddSources(""" |
| 66 | + [PlannedRemoval("1", "Use NewClass instead")] |
| 67 | + class OutdatedClass { } |
| 68 | +
|
| 69 | + [PlannedRefactor("1", "Needs rework")] |
| 70 | + class SomeClass |
| 71 | + { |
| 72 | + protected {|#0:OutdatedClass|} ShouldBeRefactored { get; set; } |
| 73 | + } |
| 74 | + """) |
| 75 | + .AddExpectedDiagnostics(MustRemovePlannedForRemovalAnalyzer.Rule.AsResult().WithLocation(0).WithArguments("OutdatedClass", "ShouldBeRefactored", "1", "Use NewClass instead")) |
| 76 | + .RunAsync(TestContext.CancellationToken); |
| 77 | + } |
| 78 | + |
| 79 | + [TestMethod] |
| 80 | + public async Task Produce_no_diagnostic_when_using_classes_being_removed_as_a_nonprivate_property_when_marked_for_refactor() |
| 81 | + { |
| 82 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 83 | + .AddSources(""" |
| 84 | + [PlannedRemoval("1", "Remove this class")] |
| 85 | + class OutdatedClass { } |
| 86 | +
|
| 87 | + [PlannedRemoval("1", "Remove this class too")] |
| 88 | + class SomeClass |
| 89 | + { |
| 90 | + protected OutdatedClass ShouldBeRefactored { get; set; } |
| 91 | + } |
| 92 | + """) |
| 93 | + .RunAsync(TestContext.CancellationToken); |
| 94 | + } |
| 95 | + |
| 96 | + // TODO: test property initialization |
| 97 | + #endregion properties |
| 98 | + |
| 99 | + #region method return type |
| 100 | + [TestMethod] |
| 101 | + public async Task Warn_when_using_classes_being_removed_as_a_nonprivate_method_return_type_when_marked_for_refactor() |
| 102 | + { |
| 103 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 104 | + .AddSources(""" |
| 105 | + [PlannedRemoval("1", "Use NewClass instead")] |
| 106 | + class OutdatedClass { } |
| 107 | +
|
| 108 | + [PlannedRefactor("1", "Needs rework")] |
| 109 | + class SomeClass |
| 110 | + { |
| 111 | + protected {|#0:OutdatedClass|} ShouldBeRefactored() => null; |
| 112 | + } |
| 113 | + """) |
| 114 | + .AddExpectedDiagnostics(MustRemovePlannedForRemovalAnalyzer.Rule.AsResult().WithLocation(0).WithArguments("OutdatedClass", "ShouldBeRefactored", "1", "Use NewClass instead")) |
| 115 | + .RunAsync(TestContext.CancellationToken); |
| 116 | + } |
| 117 | + |
| 118 | + [TestMethod] |
| 119 | + public async Task Produce_no_diagnostic_when_using_classes_being_removed_as_a_nonprivate_method_return_type_when_marked_for_removal() |
| 120 | + { |
| 121 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 122 | + .AddSources(""" |
| 123 | + [PlannedRemoval("1", "Remove this class")] |
| 124 | + class OutdatedClass { } |
| 125 | +
|
| 126 | + [PlannedRemoval("1", "Remove this too")] |
| 127 | + class SomeClass |
| 128 | + { |
| 129 | + protected {|#0:OutdatedClass|} ShouldBeRefactored() => null; |
| 130 | + } |
| 131 | + """) |
| 132 | + .RunAsync(TestContext.CancellationToken); |
| 133 | + } |
| 134 | + #endregion method return type |
| 135 | + |
| 136 | + #region method parameter type |
| 137 | + [TestMethod] |
| 138 | + public async Task Warn_when_using_classes_being_removed_as_a_nonprivate_method_parameter_type_when_marked_for_refactor() |
| 139 | + { |
| 140 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 141 | + .AddSources(""" |
| 142 | + [PlannedRemoval("1", "Use NewClass instead")] |
| 143 | + class OutdatedClass { } |
| 144 | +
|
| 145 | + [PlannedRefactor("1", "Needs rework")] |
| 146 | + class SomeClass |
| 147 | + { |
| 148 | + protected void ShouldBeRefactored({|#0:OutdatedClass|} outdatedClass) { } |
| 149 | + } |
| 150 | + """) |
| 151 | + .AddExpectedDiagnostics(MustRemovePlannedForRemovalAnalyzer.Rule.AsResult().WithLocation(0).WithArguments("OutdatedClass", "ShouldBeRefactored", "1", "Use NewClass instead")) |
| 152 | + .RunAsync(TestContext.CancellationToken); |
| 153 | + } |
| 154 | + |
| 155 | + [TestMethod] |
| 156 | + public async Task Produce_no_diagnostic_when_using_classes_being_removed_as_a_nonprivate_method_parameter_type_when_marked_for_removal() |
| 157 | + { |
| 158 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 159 | + .AddSources(""" |
| 160 | + [PlannedRemoval("1", "Remove this class")] |
| 161 | + class OutdatedClass { } |
| 162 | +
|
| 163 | + class SomeClass |
| 164 | + { |
| 165 | + [PlannedRemoval("1", "Needs rework")] |
| 166 | + protected void ShouldBeRefactored({|#0:OutdatedClass|} outdatedClass) { } |
| 167 | + } |
| 168 | + """) |
| 169 | + .RunAsync(TestContext.CancellationToken); |
| 170 | + } |
| 171 | + #endregion method parameter type |
| 172 | + |
| 173 | + #region method type parameter constraint |
| 174 | + [TestMethod] |
| 175 | + public async Task Warn_when_using_classes_being_removed_as_a_nonprivate_method_type_parameter_constraint_when_marked_for_refactor() |
| 176 | + { |
| 177 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 178 | + .AddSources(""" |
| 179 | + [PlannedRemoval("1", "Use NewClass instead")] |
| 180 | + class OutdatedClass { } |
| 181 | +
|
| 182 | + [PlannedRefactor("1", "Needs rework")] |
| 183 | + class SomeClass |
| 184 | + { |
| 185 | + protected void ShouldBeRefactored<T>() |
| 186 | + where T : {|#0:OutdatedClass|} |
| 187 | + { |
| 188 | + } |
| 189 | + } |
| 190 | + """) |
| 191 | + .AddExpectedDiagnostics(MustRemovePlannedForRemovalAnalyzer.Rule.AsResult().WithLocation(0).WithArguments("OutdatedClass", "ShouldBeRefactored", "1", "Use NewClass instead")) |
| 192 | + .RunAsync(TestContext.CancellationToken); |
| 193 | + } |
| 194 | + |
| 195 | + [TestMethod] |
| 196 | + public async Task Produce_no_diagnostic_when_using_classes_being_removed_as_a_nonprivate_method_type_parameter_constraint_when_marked_for_removal() |
| 197 | + { |
| 198 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 199 | + .AddSources(""" |
| 200 | + [PlannedRemoval("1", "Remove this class")] |
| 201 | + class OutdatedClass { } |
| 202 | +
|
| 203 | + class SomeClass |
| 204 | + { |
| 205 | + [PlannedRemoval("1", "Needs rework")] |
| 206 | + protected void ShouldBeRefactored<T>() |
| 207 | + where T : {|#0:OutdatedClass|} |
| 208 | + { |
| 209 | + } |
| 210 | + } |
| 211 | + """) |
| 212 | + .RunAsync(TestContext.CancellationToken); |
| 213 | + } |
| 214 | + #endregion method type parameter constraint |
| 215 | + |
| 216 | + #region class type parameter constraint |
| 217 | + [TestMethod] |
| 218 | + public async Task Warn_when_using_classes_being_removed_as_a_nonprivate_class_type_parameter_constraint_when_marked_for_refactor() |
| 219 | + { |
| 220 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 221 | + .AddSources(""" |
| 222 | + [PlannedRemoval("1", "Use NewClass instead")] |
| 223 | + public class OutdatedClass { } |
| 224 | +
|
| 225 | + [PlannedRefactor("1", "Needs rework")] |
| 226 | + public class ShouldBeRefactored<T> |
| 227 | + where T : {|#0:OutdatedClass|} |
| 228 | + { |
| 229 | + } |
| 230 | + """) |
| 231 | + .AddExpectedDiagnostics(MustRemovePlannedForRemovalAnalyzer.Rule.AsResult().WithLocation(0).WithArguments("OutdatedClass", "ShouldBeRefactored", "1", "Use NewClass instead")) |
| 232 | + .RunAsync(TestContext.CancellationToken); |
| 233 | + } |
| 234 | + |
| 235 | + [TestMethod] |
| 236 | + public async Task Produce_no_diagnostic_when_using_classes_being_removed_as_a_nonprivate_class_type_parameter_constraint_when_marked_for_removal() |
| 237 | + { |
| 238 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 239 | + .AddSources(""" |
| 240 | + [PlannedRemoval("1", "Remove this class")] |
| 241 | + public class OutdatedClass { } |
| 242 | +
|
| 243 | + [PlannedRemoval("1", "Needs rework")] |
| 244 | + public class ShouldBeRefactored<T> |
| 245 | + where T : {|#0:OutdatedClass|} |
| 246 | + { |
| 247 | + } |
| 248 | + """) |
| 249 | + .RunAsync(TestContext.CancellationToken); |
| 250 | + } |
| 251 | + #endregion class type parameter constraint |
| 252 | + |
| 253 | + #region delegate |
| 254 | + [TestMethod] |
| 255 | + public async Task Warn_when_using_classes_being_removed_as_a_public_delegate() |
| 256 | + { |
| 257 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 258 | + .AddSources(""" |
| 259 | + [PlannedRemoval("1", "Use NewClass instead")] |
| 260 | + public class OutdatedClass { } |
| 261 | +
|
| 262 | + delegate {|#0:OutdatedClass|} ShouldBeRefactored({|#1:OutdatedClass|} data); |
| 263 | + """) |
| 264 | + .AddExpectedDiagnostics(MustRemovePlannedForRemovalAnalyzer.Rule.AsResult().WithLocation(0).WithArguments("OutdatedClass", "ShouldBeRefactored", "1", "Use NewClass instead")) |
| 265 | + .AddExpectedDiagnostics(MustRemovePlannedForRemovalAnalyzer.Rule.AsResult().WithLocation(1).WithArguments("OutdatedClass", "ShouldBeRefactored", "1", "Use NewClass instead")) |
| 266 | + .RunAsync(TestContext.CancellationToken); |
| 267 | + } |
| 268 | + |
| 269 | + [TestMethod] |
| 270 | + public async Task Produce_no_diagnostic_when_using_classes_being_removed_as_a_nonprivate_delegate_when_marked_for_removal() |
| 271 | + { |
| 272 | + await TestBuilder.TestCSharp<MustRemovePlannedForRemovalAnalyzer>() |
| 273 | + .AddSources(""" |
| 274 | + [PlannedRemoval("1", "Remove this class")] |
| 275 | + public class OutdatedClass { } |
| 276 | +
|
| 277 | + [PlannedRemoval("1", "Needs rework")] |
| 278 | + delegate {|#0:OutdatedClass|} ShouldBeRefactored({|#1:OutdatedClass|} data); |
| 279 | + """) |
| 280 | + .RunAsync(TestContext.CancellationToken); |
| 281 | + } |
| 282 | + #endregion delegate |
| 283 | +} |
0 commit comments