GenerateConstructorTests.cs 39.8 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

using System;
4
using System.Threading.Tasks;
5 6
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.CodeFixes.GenerateConstructor;
7
using Microsoft.CodeAnalysis.CSharp.Diagnostics;
8
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
using Microsoft.CodeAnalysis.Diagnostics;
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());
        }

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

J
Jared Parsons 已提交
32
        [Fact, WorkItem(910589, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/910589"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
33
        public async Task TestWithNoArgs()
34
        {
35
            await TestAsync(
36 37 38 39
@"class C { public C(int v) { } void M() { new [|C|](); } }",
@"class C { public C() { } public C(int v) { } void M() { new C(); } }");
        }

J
Jared Parsons 已提交
40
        [Fact, WorkItem(910589, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/910589"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
41
        public async Task TestWithNamedArg()
42
        {
43
            await TestAsync(
44 45 46 47
@"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); } }");
        }

J
Jared Parsons 已提交
48
        [Fact, WorkItem(910589, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/910589"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
49
        public async Task TestWithExistingField1()
50
        {
51
            await TestAsync(
52 53 54 55
@"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; } }");
        }

56
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
57
        public async Task TestWithExistingField2()
58
        {
59
            await TestAsync(
60 61 62 63
@"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; } }");
        }

64
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
65
        public async Task TestWithExistingField3()
66
        {
67
            await TestAsync(
68 69 70 71
@"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; } }");
        }

72
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
73
        public async Task TestWithExistingField4()
74
        {
75
            await TestAsync(
76 77 78 79
@"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; } }");
        }

J
Jared Parsons 已提交
80
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
81
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
82
        public async Task TestWithExistingField5()
83
        {
84
            await TestAsync(
85 86 87 88
@"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; } }");
        }

J
Jared Parsons 已提交
89
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
90
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
91
        public async Task TestWithExistingField5WithQualification()
92
        {
93
            await TestAsync(
94 95
@"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; } }",
96
                options: Option(SimplificationOptions.QualifyFieldAccess, true));
97 98
        }

J
Jared Parsons 已提交
99
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
100
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
101
        public async Task TestWithExistingField6()
102
        {
103
            await TestAsync(
104 105 106 107
@"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; } }");
        }

J
Jared Parsons 已提交
108
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
109
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
110
        public async Task TestWithExistingField7()
111
        {
112
            await TestAsync(
113 114 115 116
@"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; } }");
        }

J
Jared Parsons 已提交
117
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
118
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
119
        public async Task TestWithExistingField7WithQualification()
120
        {
121
            await TestAsync(
122 123
@"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; } }",
124
                options: Option(SimplificationOptions.QualifyFieldAccess, true));
125 126
        }

J
Jared Parsons 已提交
127
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
128
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
129
        public async Task TestWithExistingField8()
130
        {
131
            await TestAsync(
132 133 134 135
@"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; } }");
        }

J
Jared Parsons 已提交
136
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
137
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
138
        public async Task TestWithExistingField9()
139
        {
140
            await TestAsync(
141 142 143 144
@"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; } }");
        }

J
Jared Parsons 已提交
145
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
146
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
147
        public async Task TestWithExistingProperty1()
148
        {
149
            await TestAsync(
150 151 152 153
@"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; } }");
        }

J
Jared Parsons 已提交
154
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
155
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
156
        public async Task TestWithExistingProperty1WithQualification()
157
        {
158
            await TestAsync(
159 160
@"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; } }",
161
                options: Option(SimplificationOptions.QualifyPropertyAccess, true));
162 163
        }

J
Jared Parsons 已提交
164
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
165
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
166
        public async Task TestWithExistingProperty2()
167
        {
168
            await TestAsync(
169 170 171 172
@"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; } }");
        }

J
Jared Parsons 已提交
173
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
174
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
175
        public async Task TestWithExistingProperty3()
176
        {
177
            await TestAsync(
178 179 180 181
@"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; } }");
        }

J
Jared Parsons 已提交
182
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
183
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
184
        public async Task TestWithExistingProperty3WithQualification()
185
        {
186
            await TestAsync(
187 188
@"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; } }",
189
                options: Option(SimplificationOptions.QualifyPropertyAccess, true));
190 191
        }

J
Jared Parsons 已提交
192
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
193
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
194
        public async Task TestWithExistingProperty4()
195
        {
196
            await TestAsync(
197 198 199 200
@"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; } }");
        }

J
Jared Parsons 已提交
201
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
202
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
203
        public async Task TestWithExistingProperty4WithQualification()
204
        {
205
            await TestAsync(
206 207
@"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; } }",
208
                options: Option(SimplificationOptions.QualifyPropertyAccess, true));
209 210
        }

J
Jared Parsons 已提交
211
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
212
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
213
        public async Task TestWithExistingProperty5()
214
        {
215
            await TestAsync(
216 217 218 219
@"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; } }");
        }

220
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
221
        public async Task TestWithOutParam()
222
        {
223
            await TestAsync(
224 225 226 227
@"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; } }");
        }

228
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
229
        public async Task TestWithBaseDelegatingConstructor1()
230
        {
231
            await TestAsync(
232 233 234 235
@"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) { } }");
        }

236
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
237
        public async Task TestWithBaseDelegatingConstructor2()
238
        {
239
            await TestAsync(
240 241 242 243
@"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; } }");
        }

244
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
245
        public async Task TestStructInLocalInitializerWithSystemType()
246
        {
247
            await TestAsync(
248 249 250 251
@"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); } }");
        }

J
Jared Parsons 已提交
252
        [WorkItem(539489, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539489")]
253
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
254
        public async Task TestEscapedName()
255
        {
256
            await TestAsync(
257 258 259 260
@"class C { void M() { new [|@C|](1); } }",
@"class C { private int v; public C(int v) { this.v = v; } void M() { new @C(1); } }");
        }

J
Jared Parsons 已提交
261
        [WorkItem(539489, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539489")]
262
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
263
        public async Task TestEscapedKeyword()
264
        {
265
            await TestAsync(
266 267 268 269
@"class @int { void M() { new [|@int|](1); } }",
@"class @int { private int v; public @int(int v) { this.v = v; } void M() { new @int(1); } }");
        }

270
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
271
        public async Task TestIsSymbolAccessibleWithInternalField()
272
        {
273
            await TestAsync(
274 275 276 277
@"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 ; } } ");
        }

J
Jared Parsons 已提交
278
        [WorkItem(539548, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539548")]
279
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
280
        public async Task TestFormatting()
281
        {
282
            await TestAsync(
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
@"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")]
308
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
309
        public async Task TestNotOnStructConstructor()
310
        {
311
            await TestMissingAsync(
312 313 314
@"struct Struct { void Main ( ) { Struct s = new [|Struct|] ( ) ; } } ");
        }

J
Jared Parsons 已提交
315
        [WorkItem(539787, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539787")]
316
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
317
        public async Task TestGenerateIntoCorrectPart()
318
        {
319
            await TestAsync(
320 321 322 323
@"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"" ) ; } } ");
        }

324
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
325
        public async Task TestDelegateToSmallerConstructor1()
326
        {
327
            await TestAsync(
328 329 330 331
@"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 ; } } ");
        }

332
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
333
        public async Task TestDelegateToSmallerConstructor2()
334
        {
335
            await TestAsync(
336 337 338 339
@"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 ; } } ");
        }

340
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
341
        public async Task TestDelegateToSmallerConstructor3()
342
        {
343
            await TestAsync(
344 345 346 347
@"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 ; } } ");
        }

348
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
349
        public async Task TestDelegateToSmallerConstructor4()
350
        {
351
            await TestAsync(
352 353 354 355
@"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 ; } } ");
        }

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

J
Jared Parsons 已提交
364
        [Fact, WorkItem(910589, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/910589"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
365
        public async Task TestGenerateFromThisInitializer2()
366
        {
367
            await TestAsync(
368 369 370 371
@"class C { public C ( int i ) [|: this ( )|] { } } ",
@"class C { public C ( ) { } public C ( int i ) : this ( ) { } } ");
        }

372
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
373
        public async Task TestGenerateFromBaseInitializer1()
374
        {
375
            await TestAsync(
376 377 378 379
@"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 ; } } ");
        }

380
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
381
        public async Task TestGenerateFromBaseInitializer2()
382
        {
383
            await TestAsync(
384 385 386 387
@"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 ; } } ");
        }

J
Jared Parsons 已提交
388
        [WorkItem(539969, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539969")]
389
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
390
        public async Task TestNotOnExistingConstructor()
391
        {
392
            await TestMissingAsync(
393 394 395
@"class C { private class D { } } class A { void M ( ) { C . D d = new C . [|D|] ( ) ; } } ");
        }

J
Jared Parsons 已提交
396
        [WorkItem(539972, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539972")]
397
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
398
        public async Task TestUnavailableTypeParameters()
399
        {
400
            await TestAsync(
401 402 403 404
@"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 ; } } ");
        }

J
Jared Parsons 已提交
405
        [WorkItem(541020, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/541020")]
406
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
407
        public async Task TestGenerateCallToDefaultConstructorInStruct()
408
        {
409
            await TestAsync(
410 411 412 413
@"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 ; } }");
        }

J
Jared Parsons 已提交
414
        [WorkItem(541121, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/541121")]
415
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
416
        public async Task TestReadonlyFieldDelegation()
417
        {
418
            await TestAsync(
419 420 421 422
@"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 ) ; } } ");
        }

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

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

444
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
445
        public async Task TestNestedConstructorCall()
446
        {
447
            await TestAsync(
448 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
@"
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)
    {
    }
}
");
        }

J
Jared Parsons 已提交
492
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
493
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
494
        public async Task TestAttributesWithArgument()
495
        {
496
            await TestAsync(
497 498 499 500
@"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 {} ");
        }

J
Jared Parsons 已提交
501
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
502
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
503
        public async Task TestAttributesWithMultipleArguments()
504
        {
505
            await TestAsync(
506 507 508 509
@"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 {} ");
        }

J
Jared Parsons 已提交
510
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
511
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
512
        public async Task TestAttributesWithNamedArguments()
513
        {
514
            await TestAsync(
515 516 517 518
@"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 {} ");
        }

J
Jared Parsons 已提交
519
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
520
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
521
        public async Task TestAttributesWithAdditionalConstructors()
522
        {
523
            await TestAsync(
524 525 526 527
@"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 {} ");
        }

J
Jared Parsons 已提交
528
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
529
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
530
        public async Task TestAttributesWithOverloading()
531
        {
532
            await TestAsync(
533 534 535 536
@"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 {} ");
        }

J
Jared Parsons 已提交
537
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
538
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
539
        public async Task TestAttributesWithOverloadingMultipleParameters()
540
        {
541
            await TestAsync(
542 543 544 545
@"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 { } ");
        }

J
Jared Parsons 已提交
546
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
547
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
548
        public async Task TestAttributesWithAllValidParameters()
549
        {
550
            await TestAsync(
551 552 553 554
@"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 { } ");
        }

J
Jared Parsons 已提交
555
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
556
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
557
        public async Task TestAttributesWithDelegation()
558
        {
559
            await TestMissingAsync(
560 561 562
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttrAttribute : Attribute { } [|[MyAttrAttribute(()=>{return;})]|] class D { } ");
        }

J
Jared Parsons 已提交
563
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
564
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
565
        public async Task TestAttributesWithLambda()
566
        {
567
            await TestMissingAsync(
568 569 570
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttrAttribute : Attribute { } [|[MyAttrAttribute(()=>5)]|] class D { } ");
        }

J
Jared Parsons 已提交
571
        [WorkItem(889349, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/889349")]
572
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
573
        public async Task TestConstructorGenerationForDifferentNamedParameter()
574
        {
575
            await TestAsync(
576 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
@"
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);
        }

J
Jared Parsons 已提交
613
        [WorkItem(528257, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/528257")]
614
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
615
        public async Task TestGenerateInInaccessibleType()
616
        {
617
            await TestAsync(
618 619 620
@"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); } }");
        }
621 622 623 624 625 626 627 628 629 630

        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")]
631
            [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
632
            public async Task TestGenerateConstructorInIncompleteLambda()
633
            {
634
                await TestAsync(
635 636 637 638
    @"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 ) } ) ; } } ");
            }
        }
639 640

        [WorkItem(5274, "https://github.com/dotnet/roslyn/issues/5274")]
641
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
642
        public async Task TestGenerateIntoDerivedClassWithAbstractBase()
643
        {
644
            await TestAsync(
645 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
@"
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;
        }
    }
694 695 696 697 698
}");
        }

        [WorkItem(6541, "https://github.com/dotnet/Roslyn/issues/6541")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
699
        public async Task TestGenerateFromDerivedClass()
700
        {
701
            await TestAsync(
702 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
@"
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)]
731
        public async Task TestGenerateFromDerivedClass2()
732
        {
733
            await TestAsync(
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
@"
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)
    {
    }
758 759
}");
        }
760 761

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
C
Cyrus Najmabadi 已提交
762
        public async Task TestGenerateWithIncorrectConstructorArguments_Crash()
763
        {
C
Cyrus Najmabadi 已提交
764
            await TestAsync(
765 766 767
@"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 ( ) ) ; } } } ");
        }
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786

        [WorkItem(9575, "https://github.com/dotnet/roslyn/issues/9575")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        public async Task TestMissingOnMethodCall()
        {
            await TestMissingAsync(
@"
class C
{
    public C(int arg)
    {
    }

    public bool M(string s, int i, bool b)
    {
        return [|M|](i, b);
    }
}");
        }
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        public async Task Tuple()
        {
            await TestAsync(
@"class C { void M() { new [|C|]((1, ""hello""), true); } }",
@"class C { private (int, string) p; private bool v; public C((int, string) p, bool v) { this.p = p; this.v = v; } void M() { new C((1, ""hello""), true); } }",
parseOptions: TestOptions.Regular.WithTuplesFeature(),
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        public async Task TupleWithNames()
        {
            await TestAsync(
@"class C { void M() { new [|C|]((a: 1, b: ""hello"")); } }",
@"class C { private (int a, string b) p; public C((int a, string b) p) { this.p = p; } void M() { new C((a: 1, b: ""hello"")); } }",
parseOptions: TestOptions.Regular.WithTuplesFeature(),
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        public async Task TupleWithOneName()
        {
            await TestMissingAsync(@"class C { void M() { new [|C|]((a: 1, ""hello"")); } }",
parseOptions: TestOptions.Regular.WithTuplesFeature(),
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        public async Task TupleAndExistingField()
        {
            await TestAsync(
@"class C { void M() { new [|D(existing: (1, ""hello""))|]; } } class D { private (int, string) existing; }",
@"class C { void M() { new D(existing: (1, ""hello"")); } } class D { private (int, string) existing; public D((int, string) existing) { this.existing = existing; } }",
parseOptions: TestOptions.Regular.WithTuplesFeature(),
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        public async Task TupleWithNamesAndExistingField()
        {
            await TestAsync(
@"class C { void M() { new [|D(existing: (a: 1, b: ""hello""))|]; } } class D { private (int a, string b) existing; }",
@"class C { void M() { new D(existing: (a: 1, b: ""hello"")); } } class D { private (int a, string b) existing; public D((int a, string b) existing) { this.existing = existing; } }",
parseOptions: TestOptions.Regular.WithTuplesFeature(),
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        public async Task TupleWithDifferentNamesAndExistingField()
        {
            await TestAsync(
@"class C { void M() { new [|D(existing: (a: 1, b: ""hello""))|]; } } class D { private (int c, string d) existing; }",
@"class C { void M() { new D(existing: (a: 1, b: ""hello"")); } } class D { private (int c, string d) existing; public D((int a, string b) existing) { this.existing = existing; } }",
parseOptions: TestOptions.Regular.WithTuplesFeature(),
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        public async Task TupleAndDelegatingConstructor()
        {
            await TestAsync(
@"class C { void M() { new [|D|]((1, ""hello"")); } } class B { protected B((int, string) x) { } } class D : B { }",
@"class C { void M() { new D((1, ""hello"")); } } class B { protected B((int, string) x) { } } class D : B { public D((int, string) x) : base(x) { } }",
parseOptions: TestOptions.Regular.WithTuplesFeature(),
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        public async Task TupleWithNamesAndDelegatingConstructor()
        {
            await TestAsync(
@"class C { void M() { new [|D|]((a: 1, b: ""hello"")); } } class B { protected B((int a, string b) x) { } } class D : B { }",
@"class C { void M() { new D((a: 1, b: ""hello"")); } } class B { protected B((int a, string b) x) { } } class D : B { public D((int a, string b) x) : base(x) { } }",
parseOptions: TestOptions.Regular.WithTuplesFeature(),
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        public async Task TupleWithDifferentNamesAndDelegatingConstructor()
        {
            await TestAsync(
@"class C { void M() { new [|D|]((a: 1, b: ""hello"")); } } class B { protected B((int c, string d) x) { } } class D : B { }",
@"class C { void M() { new D((a: 1, b: ""hello"")); } } class B { protected B((int c, string d) x) { } } class D : B { public D((int c, string d) x) : base(x) { } }",
parseOptions: TestOptions.Regular.WithTuplesFeature(),
withScriptOption: true);
        }
875 876
    }
}