GenerateConstructorTests.cs 33.0 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
2 3 4

using System;
using System.Collections.Generic;
5
using System.Threading.Tasks;
6 7
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.CodeFixes.GenerateConstructor;
8
using Microsoft.CodeAnalysis.CSharp.Diagnostics;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Simplification;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.GenerateConstructor
{
    public class GenerateConstructorTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
    {
        internal override Tuple<DiagnosticAnalyzer, CodeFixProvider> CreateDiagnosticProviderAndFixer(Workspace workspace)
        {
            return new Tuple<DiagnosticAnalyzer, CodeFixProvider>(
                null, new GenerateConstructorCodeFixProvider());
        }

25
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
26
        public async Task TestWithSimpleArgument()
27
        {
28
            await TestAsync(
29 30 31 32
@"class C { void M() { new [|C|](1); } }",
@"class C { private int v; public C(int v) { this.v = v; } void M() { new C(1); } }");
        }

33
        [WpfFact, WorkItem(910589), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
34
        public async Task TestWithNoArgs()
35
        {
36
            await TestAsync(
37 38 39 40
@"class C { public C(int v) { } void M() { new [|C|](); } }",
@"class C { public C() { } public C(int v) { } void M() { new C(); } }");
        }

41
        [WpfFact, WorkItem(910589), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
42
        public async Task TestWithNamedArg()
43
        {
44
            await TestAsync(
45 46 47 48
@"class C { void M() { new [|C(foo: 1)|]; } }",
@"class C { private int foo; public C(int foo) { this.foo = foo; } void M() { new C(foo: 1); } }");
        }

49
        [WpfFact, WorkItem(910589), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
50
        public async Task TestWithExistingField1()
51
        {
52
            await TestAsync(
53 54 55 56
@"class C { void M() { new [|D(foo: 1)|]; } } class D { private int foo; }",
@"class C { void M() { new D(foo: 1); } } class D { private int foo; public D(int foo) { this.foo = foo; } }");
        }

57
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
58
        public async Task TestWithExistingField2()
59
        {
60
            await TestAsync(
61 62 63 64
@"class C { void M() { new [|D|](1); } } class D { private string v; }",
@"class C { void M() { new D(1); } } class D { private string v; private int v1; public D(int v1) { this.v1 = v1; } }");
        }

65
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
66
        public async Task TestWithExistingField3()
67
        {
68
            await TestAsync(
69 70 71 72
@"class C { void M() { new [|D|](1); } } class B { protected int v; } class D : B { }",
@"class C { void M() { new D(1); } } class B { protected int v; } class D : B { public D(int v) { this.v = v; } }");
        }

73
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
74
        public async Task TestWithExistingField4()
75
        {
76
            await TestAsync(
77 78 79 80 81
@"class C { void M() { new [|D|](1); } } class B { private int v; } class D : B { }",
@"class C { void M() { new D(1); } } class B { private int v; } class D : B { private int v; public D(int v) { this.v = v; } }");
        }

        [WorkItem(539444)]
82
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
83
        public async Task TestWithExistingField5()
84
        {
85
            await TestAsync(
86 87 88 89 90
@"class C { void M(int X) { new [|D|](X); } } class D { int X; }",
@"class C { void M(int X) { new D(X); } } class D { int X; public D(int x) { X = x; } }");
        }

        [WorkItem(539444)]
91
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
92
        public async Task TestWithExistingField5WithQualification()
93
        {
94
            await TestAsync(
95 96 97 98 99 100
@"class C { void M(int X) { new [|D|](X); } } class D { int X; }",
@"class C { void M(int X) { new D(X); } } class D { int X; public D(int x) { this.X = x; } }",
                options: new Dictionary<OptionKey, object> { { new OptionKey(SimplificationOptions.QualifyMemberAccessWithThisOrMe, "C#"), true } });
        }

        [WorkItem(539444)]
101
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
102
        public async Task TestWithExistingField6()
103
        {
104
            await TestAsync(
105 106 107 108 109
@"class C { void M(int X) { new [|D|](X); } } class B { private int X; } class D : B { }",
@"class C { void M(int X) { new D(X); } } class B { private int X; } class D : B { private int x; public D(int x) { this.x = x; } }");
        }

        [WorkItem(539444)]
110
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
111
        public async Task TestWithExistingField7()
112
        {
113
            await TestAsync(
114 115 116 117 118
@"class C { void M(int X) { new [|D|](X); } } class B { protected int X; } class D : B { }",
@"class C { void M(int X) { new D(X); } } class B { protected int X; } class D : B { public D(int x) { X = x; } }");
        }

        [WorkItem(539444)]
119
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
120
        public async Task TestWithExistingField7WithQualification()
121
        {
122
            await TestAsync(
123 124 125 126 127 128
@"class C { void M(int X) { new [|D|](X); } } class B { protected int X; } class D : B { }",
@"class C { void M(int X) { new D(X); } } class B { protected int X; } class D : B { public D(int x) { this.X = x; } }",
                options: new Dictionary<OptionKey, object> { { new OptionKey(SimplificationOptions.QualifyMemberAccessWithThisOrMe, "C#"), true } });
        }

        [WorkItem(539444)]
129
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
130
        public async Task TestWithExistingField8()
131
        {
132
            await TestAsync(
133 134 135 136 137
@"class C { void M(int X) { new [|D|](X); } } class B { protected static int x; } class D : B { }",
@"class C { void M(int X) { new D(X); } } class B { protected static int x; } class D : B { private int x1; public D(int x1) { this.x1 = x1; } }");
        }

        [WorkItem(539444)]
138
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
139
        public async Task TestWithExistingField9()
140
        {
141
            await TestAsync(
142 143 144 145 146
@"class C { void M(int X) { new [|D|](X); } } class B { protected int x; } class D : B { int X; }",
@"class C { void M(int X) { new D(X); } } class B { protected int x; } class D : B { int X; public D(int x) { this.x = x; } }");
        }

        [WorkItem(539444)]
147
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
148
        public async Task TestWithExistingProperty1()
149
        {
150
            await TestAsync(
151 152 153 154 155
@"class C { void M(int X) { new [|D|](X); } } class D { public int X { get; private set; } }",
@"class C { void M(int X) { new D(X); } } class D { public D(int x) { X = x; } public int X { get; private set; } }");
        }

        [WorkItem(539444)]
156
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
157
        public async Task TestWithExistingProperty1WithQualification()
158
        {
159
            await TestAsync(
160 161 162 163 164 165
@"class C { void M(int X) { new [|D|](X); } } class D { public int X { get; private set; } }",
@"class C { void M(int X) { new D(X); } } class D { public D(int x) { this.X = x; } public int X { get; private set; } }",
                options: new Dictionary<OptionKey, object> { { new OptionKey(SimplificationOptions.QualifyMemberAccessWithThisOrMe, "C#"), true } });
        }

        [WorkItem(539444)]
166
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
167
        public async Task TestWithExistingProperty2()
168
        {
169
            await TestAsync(
170 171 172 173 174
@"class C { void M(int X) { new [|D|](X); } } class B { public int X { get; private set; } } class D : B { }",
@"class C { void M(int X) { new D(X); } } class B { public int X { get; private set; } } class D : B { private int x; public D(int x) { this.x = x; } }");
        }

        [WorkItem(539444)]
175
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
176
        public async Task TestWithExistingProperty3()
177
        {
178
            await TestAsync(
179 180 181 182 183
@"class C { void M(int X) { new [|D|](X); } } class B { public int X { get; protected set; } } class D : B { }",
@"class C { void M(int X) { new D(X); } } class B { public int X { get; protected set; } } class D : B { public D(int x) { X = x; } }");
        }

        [WorkItem(539444)]
184
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
185
        public async Task TestWithExistingProperty3WithQualification()
186
        {
187
            await TestAsync(
188 189 190 191 192 193
@"class C { void M(int X) { new [|D|](X); } } class B { public int X { get; protected set; } } class D : B { }",
@"class C { void M(int X) { new D(X); } } class B { public int X { get; protected set; } } class D : B { public D(int x) { this.X = x; } }",
                options: new Dictionary<OptionKey, object> { { new OptionKey(SimplificationOptions.QualifyMemberAccessWithThisOrMe, "C#"), true } });
        }

        [WorkItem(539444)]
194
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
195
        public async Task TestWithExistingProperty4()
196
        {
197
            await TestAsync(
198 199 200 201 202
@"class C { void M(int X) { new [|D|](X); } } class B { protected int X { get; set; } } class D : B { }",
@"class C { void M(int X) { new D(X); } } class B { protected int X { get; set; } } class D : B { public D(int x) { X = x; } }");
        }

        [WorkItem(539444)]
203
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
204
        public async Task TestWithExistingProperty4WithQualification()
205
        {
206
            await TestAsync(
207 208 209 210 211 212
@"class C { void M(int X) { new [|D|](X); } } class B { protected int X { get; set; } } class D : B { }",
@"class C { void M(int X) { new D(X); } } class B { protected int X { get; set; } } class D : B { public D(int x) { this.X = x; } }",
                options: new Dictionary<OptionKey, object> { { new OptionKey(SimplificationOptions.QualifyMemberAccessWithThisOrMe, "C#"), true } });
        }

        [WorkItem(539444)]
213
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
214
        public async Task TestWithExistingProperty5()
215
        {
216
            await TestAsync(
217 218 219 220
@"class C { void M(int X) { new [|D|](X); } } class B { protected int X { get; } } class D : B { }",
@"class C { void M(int X) { new D(X); } } class B { protected int X { get; } } class D : B { private int x; public D(int x) { this.x = x; } }");
        }

221
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
222
        public async Task TestWithOutParam()
223
        {
224
            await TestAsync(
225 226 227 228
@"class C { void M(int i) { new [|D|](out i); } } class D { }",
@"class C { void M(int i) { new D(out i); } } class D { public D(out int i) { i = 0; } }");
        }

229
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
230
        public async Task TestWithBaseDelegatingConstructor1()
231
        {
232
            await TestAsync(
233 234 235 236
@"class C { void M() { new [|D|](1); } } class B { protected B(int x) { } } class D : B { }",
@"class C { void M() { new D(1); } } class B { protected B(int x) { } } class D : B { public D(int x) : base(x) { } }");
        }

237
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
238
        public async Task TestWithBaseDelegatingConstructor2()
239
        {
240
            await TestAsync(
241 242 243 244
@"class C { void M() { new [|D|](1); } } class B { private B(int x) { } } class D : B { }",
@"class C { void M() { new D(1); } } class B { private B(int x) { } } class D : B { private int v; public D(int v) { this.v = v; } }");
        }

245
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
246
        public async Task TestStructInLocalInitializerWithSystemType()
247
        {
248
            await TestAsync(
249 250 251 252 253
@"struct S { void M() { S s = new [|S|](System.DateTime.Now); } }",
@"using System; struct S { private DateTime now; public S(DateTime now) { this.now = now; } void M() { S s = new S(System.DateTime.Now); } }");
        }

        [WorkItem(539489)]
254
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
255
        public async Task TestEscapedName()
256
        {
257
            await TestAsync(
258 259 260 261 262
@"class C { void M() { new [|@C|](1); } }",
@"class C { private int v; public C(int v) { this.v = v; } void M() { new @C(1); } }");
        }

        [WorkItem(539489)]
263
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
264
        public async Task TestEscapedKeyword()
265
        {
266
            await TestAsync(
267 268 269 270
@"class @int { void M() { new [|@int|](1); } }",
@"class @int { private int v; public @int(int v) { this.v = v; } void M() { new @int(1); } }");
        }

271
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
272
        public async Task TestIsSymbolAccessibleWithInternalField()
273
        {
274
            await TestAsync(
275 276 277 278 279
@"class Base { internal long field ; void Main ( ) { int field = 5 ; new [|Derived|] ( field ) ; } } class Derived : Base { } ",
@"class Base { internal long field ; void Main ( ) { int field = 5 ; new Derived ( field ) ; } } class Derived : Base { public Derived ( int field ) { this . field = field ; } } ");
        }

        [WorkItem(539548)]
280
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
281
        public async Task TestFormatting()
282
        {
283
            await TestAsync(
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
@"class C
{
    void M()
    {
        new [|C|](1);
    }
}",
@"class C
{
    private int v;

    public C(int v)
    {
        this.v = v;
    }

    void M()
    {
        new C(1);
    }
}",
compareTokens: false);
        }

        [WorkItem(5864, "DevDiv_Projects/Roslyn")]
309
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
310
        public async Task TestNotOnStructConstructor()
311
        {
312
            await TestMissingAsync(
313 314 315 316
@"struct Struct { void Main ( ) { Struct s = new [|Struct|] ( ) ; } } ");
        }

        [WorkItem(539787)]
317
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
318
        public async Task TestGenerateIntoCorrectPart()
319
        {
320
            await TestAsync(
321 322 323 324
@"partial class C { } partial class C { void Method ( ) { C c = new [|C|] ( ""a"" ) ; } } ",
@"partial class C { } partial class C { private string v ; public C ( string v ) { this . v = v ; } void Method ( ) { C c = new C ( ""a"" ) ; } } ");
        }

325
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
326
        public async Task TestDelegateToSmallerConstructor1()
327
        {
328
            await TestAsync(
329 330 331 332
@"class A { void M ( ) { Delta d1 = new Delta ( ""ss"" , 3 ) ; Delta d2 = new [|Delta|] ( ""ss"" , 5 , true ) ; } } class Delta { private string v1 ; private int v2 ; public Delta ( string v1 , int v2 ) { this . v1 = v1 ; this . v2 = v2 ; } } ",
@"class A { void M ( ) { Delta d1 = new Delta ( ""ss"" , 3 ) ; Delta d2 = new Delta ( ""ss"" , 5 , true ) ; } } class Delta { private bool v ; private string v1 ; private int v2 ; public Delta ( string v1 , int v2 ) { this . v1 = v1 ; this . v2 = v2 ; } public Delta ( string v1 , int v2 , bool v ) : this ( v1 , v2 ) { this . v = v ; } } ");
        }

333
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
334
        public async Task TestDelegateToSmallerConstructor2()
335
        {
336
            await TestAsync(
337 338 339 340
@"class A { void M ( ) { Delta d1 = new Delta ( ""ss"" , 3 ) ; Delta d2 = new [|Delta|] ( ""ss"" , 5 , true ) ; } } class Delta { private string a ; private int b ; public Delta ( string a , int b ) { this . a = a ; this . b = b ; } } ",
@"class A { void M ( ) { Delta d1 = new Delta ( ""ss"" , 3 ) ; Delta d2 = new Delta ( ""ss"" , 5 , true ) ; } } class Delta { private string a ; private int b ; private bool v ; public Delta ( string a , int b ) { this . a = a ; this . b = b ; } public Delta ( string a , int b , bool v) : this ( a , b ) { this . v = v ; } } ");
        }

341
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
342
        public async Task TestDelegateToSmallerConstructor3()
343
        {
344
            await TestAsync(
345 346 347 348
@"class A { void M ( ) { var d1 = new Base ( ""ss"" , 3 ) ; var d2 = new [|Delta|] ( ""ss"" , 5 , true ) ; } } class Base { private string v1 ; private int v2 ; public Base ( string v1 , int v2 ) { this . v1 = v1 ; this . v2 = v2 ; } } class Delta : Base { } ",
@"class A { void M ( ) { var d1 = new Base ( ""ss"" , 3 ) ; var d2 = new Delta ( ""ss"" , 5 , true ) ; } } class Base { private string v1 ; private int v2 ; public Base ( string v1 , int v2 ) { this . v1 = v1 ; this . v2 = v2 ; } } class Delta : Base { private bool v ; public Delta ( string v1 , int v2 , bool v ) : base ( v1 , v2 ) { this . v = v ; } } ");
        }

349
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
350
        public async Task TestDelegateToSmallerConstructor4()
351
        {
352
            await TestAsync(
353 354 355 356
@"class A { void M ( ) { Delta d1 = new Delta ( ""ss"" , 3 ) ; Delta d2 = new [|Delta|] ( ""ss"" , 5 , true ) ; } } class Delta { private string v1 ; private int v2 ; public Delta ( string v1 , int v2 ) { this . v1 = v1 ; this . v2 = v2 ; } } ",
@"class A { void M ( ) { Delta d1 = new Delta ( ""ss"" , 3 ) ; Delta d2 = new Delta ( ""ss"" , 5 , true ) ; } } class Delta { private bool v ; private string v1 ; private int v2 ;  public Delta ( string v1 , int v2 ) { this . v1 = v1 ; this . v2 = v2 ; } public Delta ( string v1 , int v2 , bool v ) : this ( v1 , v2 ) { this . v = v ; } } ");
        }

357
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
358
        public async Task TestGenerateFromThisInitializer1()
359
        {
360
            await TestAsync(
361 362 363 364
@"class C { public C ( ) [|: this ( 4 )|] { } } ",
@"class C { private int v ; public C ( ) : this ( 4 ) { } public C ( int v ) { this . v = v ; } } ");
        }

365
        [WpfFact, WorkItem(910589), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
366
        public async Task TestGenerateFromThisInitializer2()
367
        {
368
            await TestAsync(
369 370 371 372
@"class C { public C ( int i ) [|: this ( )|] { } } ",
@"class C { public C ( ) { } public C ( int i ) : this ( ) { } } ");
        }

373
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
374
        public async Task TestGenerateFromBaseInitializer1()
375
        {
376
            await TestAsync(
377 378 379 380
@"class C : B { public C ( int i ) [|: base ( i )|] { } } class B { } ",
@"class C : B { public C ( int i ) : base ( i ) { } } class B { private int i ; public B ( int i ) { this . i = i ; } } ");
        }

381
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
382
        public async Task TestGenerateFromBaseInitializer2()
383
        {
384
            await TestAsync(
385 386 387 388 389
@"class C : B { public C ( int i ) [|: base ( i )|] { } } class B { int i ; } ",
@"class C : B { public C ( int i ) : base ( i ) { } } class B { int i ; public B ( int i ) { this . i = i ; } } ");
        }

        [WorkItem(539969)]
390
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
391
        public async Task TestNotOnExistingConstructor()
392
        {
393
            await TestMissingAsync(
394 395 396 397
@"class C { private class D { } } class A { void M ( ) { C . D d = new C . [|D|] ( ) ; } } ");
        }

        [WorkItem(539972)]
398
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
399
        public async Task TestUnavailableTypeParameters()
400
        {
401
            await TestAsync(
402 403 404 405 406
@"class C < T1 , T2 > { public void Foo ( T1 t1 , T2 t2 ) { A a = new [|A|] ( t1 , t2 ) ; } } internal class A { } ",
@"class C < T1 , T2 > { public void Foo ( T1 t1 , T2 t2 ) { A a = new A ( t1 , t2 ) ; } } internal class A { private object t1 ; private object t2 ; public A ( object t1 , object t2 ) { this . t1 = t1 ; this . t2 = t2 ; } } ");
        }

        [WorkItem(541020)]
407
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
408
        public async Task TestGenerateCallToDefaultConstructorInStruct()
409
        {
410
            await TestAsync(
411 412 413 414 415
@"class Program { void Main ( ) { Apartment Metropolitan = new Apartment ( [|""Pine""|] ) ; } } struct Apartment { private int v1 ; public Apartment ( int v1 ) { this . v1 = v1 ; } } ",
@"class Program { void Main ( ) { Apartment Metropolitan = new Apartment ( ""Pine"" ) ; } } struct Apartment { private string v ; private int v1 ; public Apartment ( string v ) : this ( ) { this . v = v ; } public Apartment ( int v1 ) { this . v1 = v1 ; } }");
        }

        [WorkItem(541121)]
416
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
417
        public async Task TestReadonlyFieldDelegation()
418
        {
419
            await TestAsync(
420 421 422 423
@"class C { private readonly int x ; void Test ( ) { int x = 10 ; C c = new [|C|] ( x ) ; } } ",
@"class C { private readonly int x ; public C ( int x ) { this . x = x ; } void Test ( ) { int x = 10 ; C c = new C ( x ) ; } } ");
        }

424
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
425
        public async Task TestNoGenerationIntoEntirelyHiddenType()
426
        {
427
            await TestMissingAsync(
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
@"
class C
{
    void Foo()
    {
        new [|D|](1, 2, 3);
    }
}

#line hidden
class D
{
}
#line default
");
        }

445
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
446
        public async Task TestNestedConstructorCall()
447
        {
448
            await TestAsync(
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
@"
class C
{
    void Foo()
    {
        var d = new D([|v|]: new D(u: 1));
    }
}

class D
{
    private int u;

    public D(int u)
    {
    }
}
",
@"
class C
{
    void Foo()
    {
        var d = new D(v: new D(u: 1));
    }
}

class D
{
    private int u;
    private D v;

    public D(D v)
    {
        this.v = v;
    }

    public D(int u)
    {
    }
}
");
        }

        [WorkItem(530003)]
494
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
495
        public async Task TestAttributesWithArgument()
496
        {
497
            await TestAsync(
498 499 500 501 502
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttribute : Attribute {} [[|MyAttribute(123)|]] class D {} ",
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttribute : Attribute { private int v; public MyAttribute(int v) { this.v = v; } } [MyAttribute(123)] class D {} ");
        }

        [WorkItem(530003)]
503
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
504
        public async Task TestAttributesWithMultipleArguments()
505
        {
506
            await TestAsync(
507 508 509 510 511
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttribute : Attribute {} [[|MyAttribute(true, 1, ""hello"")|]] class D {} ",
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttribute : Attribute { private bool v1; private int v2; private string v3; public MyAttribute(bool v1, int v2, string v3) { this.v1 = v1; this.v2 = v2; this.v3 = v3; } } [MyAttribute(true, 1, ""hello"")] class D {} ");
        }

        [WorkItem(530003)]
512
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
513
        public async Task TestAttributesWithNamedArguments()
514
        {
515
            await TestAsync(
516 517 518 519 520
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttribute : Attribute {} [[|MyAttribute(true, 1, topic = ""hello"")|]] class D {} ",
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttribute : Attribute { private string topic; private bool v1; private int v2; public MyAttribute(bool v1, int v2, string topic) { this.v1 = v1; this.v2 = v2; this.topic = topic; } } [MyAttribute(true, 1, topic = ""hello"")] class D {} ");
        }

        [WorkItem(530003)]
521
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
522
        public async Task TestAttributesWithAdditionalConstructors()
523
        {
524
            await TestAsync(
525 526 527 528 529
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttribute : Attribute { private int v; public MyAttribute(int v) { this.v = v; } } [[|MyAttribute(true, 1)|]] class D {} ",
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttribute : Attribute { private int v; private bool v1; private int v2; public MyAttribute(int v) { this.v = v; } public MyAttribute(bool v1, int v2) { this.v1 = v1; this.v2 = v2; } } [MyAttribute(true, 1)] class D {} ");
        }

        [WorkItem(530003)]
530
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
531
        public async Task TestAttributesWithOverloading()
532
        {
533
            await TestAsync(
534 535 536 537 538
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttribute : Attribute { private int v; public MyAttribute(int v) { this.v = v; } } [[|MyAttribute(true)|]] class D {} ",
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttribute : Attribute { private int v; private bool v1; public MyAttribute(bool v1) { this.v1 = v1; } public MyAttribute(int v) { this.v = v; } } [MyAttribute(true)] class D {} ");
        }

        [WorkItem(530003)]
539
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
540
        public async Task TestAttributesWithOverloadingMultipleParameters()
541
        {
542
            await TestAsync(
543 544 545 546 547
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttrAttribute : Attribute { private bool v1; private int v2; public MyAttrAttribute(bool v1, int v2) { this.v1 = v1; this.v2 = v2; } } [|[MyAttrAttribute(1,true)]|] class D { } ",
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttrAttribute : Attribute { private int v; private bool v1; private int v2; private bool v3; public MyAttrAttribute(int v, bool v3) { this.v = v; this.v3 = v3; } public MyAttrAttribute(bool v1, int v2) { this.v1 = v1; this.v2 = v2; } } [MyAttrAttribute(1,true)] class D { } ");
        }

        [WorkItem(530003)]
548
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
549
        public async Task TestAttributesWithAllValidParameters()
550
        {
551
            await TestAsync(
552 553 554 555 556
@"using System; enum A { A1 } [AttributeUsage(AttributeTargets.Class)] class MyAttrAttribute : Attribute { } [|[MyAttrAttribute(new int[] { 1, 2, 3}, A.A1, true, (byte)1, 'a', (short)12, (int) 1, (long) 5L, 5D, 3.5F, ""hello"")]|] class D { } ",
@"using System; enum A { A1 } [AttributeUsage(AttributeTargets.Class)] class MyAttrAttribute : Attribute { private A a1; private int[] v1; private string v10; private bool v2; private byte v3; private char v4; private short v5; private int v6; private long v7; private double v8; private float v9; public MyAttrAttribute(int[] v1, A a1, bool v2, byte v3, char v4, short v5, int v6, long v7, double v8, float v9, string v10) { this.v1 = v1; this.a1 = a1; this.v2 = v2; this.v3 = v3; this.v4 = v4; this.v5 = v5; this.v6 = v6; this.v7 = v7; this.v8 = v8; this.v9 = v9; this.v10 = v10; } } [MyAttrAttribute(new int[] { 1, 2, 3 }, A.A1, true, (byte)1, 'a', (short)12, (int)1, (long)5L, 5D, 3.5F, ""hello"")] class D { } ");
        }

        [WorkItem(530003)]
557
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
558
        public async Task TestAttributesWithDelegation()
559
        {
560
            await TestMissingAsync(
561 562 563 564
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttrAttribute : Attribute { } [|[MyAttrAttribute(()=>{return;})]|] class D { } ");
        }

        [WorkItem(530003)]
565
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
566
        public async Task TestAttributesWithLambda()
567
        {
568
            await TestMissingAsync(
569 570 571 572
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttrAttribute : Attribute { } [|[MyAttrAttribute(()=>5)]|] class D { } ");
        }

        [WorkItem(889349)]
573
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
574
        public async Task TestConstructorGenerationForDifferentNamedParameter()
575
        {
576
            await TestAsync(
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
@"
class Program
{
    static void Main(string[] args)
    {
        var ss = new [|Program(wde: 1)|];
    }

    Program(int s)
    {

    }
}
",
@"
class Program
{
    private int wde;

    static void Main(string[] args)
    {
        var ss = new Program(wde: 1);
    }

    Program(int s)
    {

    }

    public Program(int wde)
    {
        this.wde = wde;
    }
}
", compareTokens: false);
        }

        [WorkItem(528257)]
615
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
616
        public async Task TestGenerateInInaccessibleType()
617
        {
618
            await TestAsync(
619 620 621
@"class Foo { class Bar { } } class A { static void Main(string[] args) { var s = new [|Foo.Bar(5)|]; } }",
@"class Foo { class Bar { private int v; public Bar(int v) { this.v = v; } } } class A { static void Main(string[] args) { var s = new Foo.Bar(5); } }");
        }
622 623 624 625 626 627 628 629 630 631

        public partial class GenerateConstructorTestsWithFindMissingIdentifiersAnalyzer : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
        {
            internal override Tuple<DiagnosticAnalyzer, CodeFixProvider> CreateDiagnosticProviderAndFixer(Workspace workspace)
            {
                return new Tuple<DiagnosticAnalyzer, CodeFixProvider>(
                new CSharpUnboundIdentifiersDiagnosticAnalyzer(), new GenerateConstructorCodeFixProvider());
            }

            [WorkItem(1241, @"https://github.com/dotnet/roslyn/issues/1241")]
632
            [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
633
            public async Task TestGenerateConstructorInIncompleteLambda()
634
            {
635
                await TestAsync(
636 637 638 639
    @"using System . Threading . Tasks ; class C { C ( ) { Task . Run ( ( ) => { new [|C|] ( 0 ) } ) ; } } ",
    @"using System . Threading . Tasks ; class C { private int v ; public C ( int v ) { this . v = v ; } C ( ) { Task . Run ( ( ) => { new C ( 0 ) } ) ; } } ");
            }
        }
640 641

        [WorkItem(5274, "https://github.com/dotnet/roslyn/issues/5274")]
642
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
643
        public async Task TestGenerateIntoDerivedClassWithAbstractBase()
644
        {
645
            await TestAsync(
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
@"
class Class1
{
    private void Foo(string value)
    {
        var rewriter = new [|Derived|](value);
    }

    private class Derived : Base
    {
    }

    public abstract partial class Base
    {
        private readonly bool _val;

        public Base(bool val = false)
        {
            _val = val;
        }
    }
}",
@"
class Class1
{
    private void Foo(string value)
    {
        var rewriter = new Derived(value);
    }

    private class Derived : Base
    {
        private string value;

        public Derived(string value)
        {
            this.value = value;
        }
    }

    public abstract partial class Base
    {
        private readonly bool _val;

        public Base(bool val = false)
        {
            _val = val;
        }
    }
695 696 697 698 699
}");
        }

        [WorkItem(6541, "https://github.com/dotnet/Roslyn/issues/6541")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
700
        public async Task TestGenerateFromDerivedClass()
701
        {
702
            await TestAsync(
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
@"
class Base
{
    public Base(string value)
    {
    }
}

class [||]Derived : Base
{
}",
@"
class Base
{
    public Base(string value)
    {
    }
} 

class Derived : Base
{
    public Derived(string value) : base(value)
    {
    }
}");
        }

        [WorkItem(6541, "https://github.com/dotnet/Roslyn/issues/6541")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
732
        public async Task TestGenerateFromDerivedClass2()
733
        {
734
            await TestAsync(
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
@"
class Base
{
    public Base(int a, string value = null)
    {
    }
}

class [||]Derived : Base
{
}",
@"
class Base
{
    public Base(int a, string value = null)
    {
    }
} 

class Derived : Base
{
    public Derived(int a, string value = null) : base(a, value)
    {
    }
759 760
}");
        }
761 762

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
C
Cyrus Najmabadi 已提交
763
        public async Task TestGenerateWithIncorrectConstructorArguments_Crash()
764
        {
C
Cyrus Najmabadi 已提交
765
            await TestAsync(
766 767 768
@"using System ; using System . Collections . Generic ; using System . Linq ; using System . Threading . Tasks ; abstract class Y { class X : Y { void M ( ) { new X ( new [|string|] ( ) ) ; } } } ",
@"using System ; using System . Collections . Generic ; using System . Linq ; using System . Threading . Tasks ; abstract class Y { class X : Y { private string v ; public X ( string v ) { this . v = v ; } void M ( ) { new X ( new string ( ) ) ; } } } ");
        }
769 770
    }
}