|
| 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.CodeStyle; |
| 7 | +using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions; |
| 8 | +using Microsoft.CodeAnalysis.Features.Intents; |
| 9 | +using Microsoft.CodeAnalysis.Test.Utilities; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Intents; |
| 13 | + |
| 14 | +[UseExportProvider] |
| 15 | +public sealed class AddConstructorParameterIntentTests : IntentTestsBase |
| 16 | +{ |
| 17 | + [Fact] |
| 18 | + public async Task AddConstructorParameterWithField() |
| 19 | + { |
| 20 | + var initialText = |
| 21 | + """ |
| 22 | + class C |
| 23 | + { |
| 24 | + private readonly int _someInt;{|priorSelection:|} |
| 25 | +
|
| 26 | + public C() |
| 27 | + { |
| 28 | + } |
| 29 | + } |
| 30 | + """; |
| 31 | + |
| 32 | + var currentText = |
| 33 | + """ |
| 34 | + class C |
| 35 | + { |
| 36 | + private readonly int _someInt; |
| 37 | +
|
| 38 | + public C(int som) |
| 39 | + { |
| 40 | + } |
| 41 | + } |
| 42 | + """; |
| 43 | + var expectedText = |
| 44 | + """ |
| 45 | + class C |
| 46 | + { |
| 47 | + private readonly int _someInt; |
| 48 | +
|
| 49 | + public C(int someInt) |
| 50 | + { |
| 51 | + _someInt = someInt; |
| 52 | + } |
| 53 | + } |
| 54 | + """; |
| 55 | + |
| 56 | + await VerifyExpectedTextAsync(WellKnownIntents.AddConstructorParameter, initialText, currentText, expectedText).ConfigureAwait(false); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public async Task AddConstructorParameterWithProperty() |
| 61 | + { |
| 62 | + var initialText = |
| 63 | + """ |
| 64 | + class C |
| 65 | + { |
| 66 | + public int SomeInt { get; }{|priorSelection:|} |
| 67 | +
|
| 68 | + public C() |
| 69 | + { |
| 70 | + } |
| 71 | + } |
| 72 | + """; |
| 73 | + var currentText = |
| 74 | + """ |
| 75 | + class C |
| 76 | + { |
| 77 | + public int SomeInt { get; } |
| 78 | +
|
| 79 | + public C(int som) |
| 80 | + { |
| 81 | + } |
| 82 | + } |
| 83 | + """; |
| 84 | + var expectedText = |
| 85 | + """ |
| 86 | + class C |
| 87 | + { |
| 88 | + public int SomeInt { get; } |
| 89 | +
|
| 90 | + public C(int someInt) |
| 91 | + { |
| 92 | + SomeInt = someInt; |
| 93 | + } |
| 94 | + } |
| 95 | + """; |
| 96 | + |
| 97 | + await VerifyExpectedTextAsync(WellKnownIntents.AddConstructorParameter, initialText, currentText, expectedText).ConfigureAwait(false); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public async Task AddMultipleConstructorParameters() |
| 102 | + { |
| 103 | + var initialText = |
| 104 | + """ |
| 105 | + class C |
| 106 | + { |
| 107 | + {|priorSelection:private readonly int _someInt; |
| 108 | + private readonly string _someString;|} |
| 109 | +
|
| 110 | + public C() |
| 111 | + { |
| 112 | + } |
| 113 | + } |
| 114 | + """; |
| 115 | + var currentText = |
| 116 | + """ |
| 117 | + class C |
| 118 | + { |
| 119 | + {|priorSelection:private readonly int _someInt; |
| 120 | + private readonly string _someString;|} |
| 121 | +
|
| 122 | + public C(int som) |
| 123 | + { |
| 124 | + } |
| 125 | + } |
| 126 | + """; |
| 127 | + var expectedText = |
| 128 | + """ |
| 129 | + class C |
| 130 | + { |
| 131 | + private readonly int _someInt; |
| 132 | + private readonly string _someString; |
| 133 | +
|
| 134 | + public C(int someInt, string someString) |
| 135 | + { |
| 136 | + _someInt = someInt; |
| 137 | + _someString = someString; |
| 138 | + } |
| 139 | + } |
| 140 | + """; |
| 141 | + |
| 142 | + await VerifyExpectedTextAsync(WellKnownIntents.AddConstructorParameter, initialText, currentText, expectedText).ConfigureAwait(false); |
| 143 | + } |
| 144 | + |
| 145 | + [Fact] |
| 146 | + public async Task AddConstructorParameterOnlyAddsSelected() |
| 147 | + { |
| 148 | + var initialText = |
| 149 | + """ |
| 150 | + class C |
| 151 | + { |
| 152 | + private readonly int _someInt;{|priorSelection:|} |
| 153 | + private readonly string _someString; |
| 154 | +
|
| 155 | + public C() |
| 156 | + { |
| 157 | + } |
| 158 | + } |
| 159 | + """; |
| 160 | + var currentText = |
| 161 | + """ |
| 162 | + class C |
| 163 | + { |
| 164 | + private readonly int _someInt;{|priorSelection:|} |
| 165 | + private readonly string _someString; |
| 166 | +
|
| 167 | + public C(int som) |
| 168 | + { |
| 169 | + } |
| 170 | + } |
| 171 | + """; |
| 172 | + var expectedText = |
| 173 | + """ |
| 174 | + class C |
| 175 | + { |
| 176 | + private readonly int _someInt; |
| 177 | + private readonly string _someString; |
| 178 | +
|
| 179 | + public C(int someInt) |
| 180 | + { |
| 181 | + _someInt = someInt; |
| 182 | + } |
| 183 | + } |
| 184 | + """; |
| 185 | + |
| 186 | + await VerifyExpectedTextAsync(WellKnownIntents.AddConstructorParameter, initialText, currentText, expectedText).ConfigureAwait(false); |
| 187 | + } |
| 188 | + |
| 189 | + [Fact] |
| 190 | + public async Task AddConstructorParameterUsesCodeStyleOption() |
| 191 | + { |
| 192 | + var initialText = |
| 193 | + """ |
| 194 | + class C |
| 195 | + { |
| 196 | + private readonly int _someInt;{|priorSelection:|} |
| 197 | +
|
| 198 | + public C() |
| 199 | + { |
| 200 | + } |
| 201 | + } |
| 202 | + """; |
| 203 | + var currentText = |
| 204 | + """ |
| 205 | + class C |
| 206 | + { |
| 207 | + private readonly int _someInt;{|priorSelection:|} |
| 208 | +
|
| 209 | + public C(int som) |
| 210 | + { |
| 211 | + } |
| 212 | + } |
| 213 | + """; |
| 214 | + var expectedText = |
| 215 | + """ |
| 216 | + class C |
| 217 | + { |
| 218 | + private readonly int _someInt; |
| 219 | +
|
| 220 | + public C(int someInt) |
| 221 | + { |
| 222 | + this._someInt = someInt; |
| 223 | + } |
| 224 | + } |
| 225 | + """; |
| 226 | + await VerifyExpectedTextAsync(WellKnownIntents.AddConstructorParameter, initialText, currentText, expectedText, |
| 227 | + options: new OptionsCollection(LanguageNames.CSharp) |
| 228 | + { |
| 229 | + { CodeStyleOptions2.QualifyFieldAccess, true } |
| 230 | + }).ConfigureAwait(false); |
| 231 | + } |
| 232 | + |
| 233 | + [Fact] |
| 234 | + public async Task AddConstructorParameterUsesExistingAccessibility() |
| 235 | + { |
| 236 | + var initialText = |
| 237 | + """ |
| 238 | + class C |
| 239 | + { |
| 240 | + private readonly int _someInt;{|priorSelection:|} |
| 241 | +
|
| 242 | + protected C() |
| 243 | + { |
| 244 | + } |
| 245 | + } |
| 246 | + """; |
| 247 | + var currentText = |
| 248 | + """ |
| 249 | + class C |
| 250 | + { |
| 251 | + private readonly int _someInt;{|priorSelection:|} |
| 252 | +
|
| 253 | + protected C(int som) |
| 254 | + { |
| 255 | + } |
| 256 | + } |
| 257 | + """; |
| 258 | + var expectedText = |
| 259 | + """ |
| 260 | + class C |
| 261 | + { |
| 262 | + private readonly int _someInt; |
| 263 | +
|
| 264 | + protected C(int someInt) |
| 265 | + { |
| 266 | + _someInt = someInt; |
| 267 | + } |
| 268 | + } |
| 269 | + """; |
| 270 | + |
| 271 | + await VerifyExpectedTextAsync(WellKnownIntents.AddConstructorParameter, initialText, currentText, expectedText).ConfigureAwait(false); |
| 272 | + } |
| 273 | +} |
0 commit comments