GenerateConstructorTests.cs 45.7 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
using Microsoft.CodeAnalysis.CodeFixes;
6
using Microsoft.CodeAnalysis.CodeStyle;
7
using Microsoft.CodeAnalysis.CSharp.CodeFixes.GenerateConstructor;
8
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
9
using Microsoft.CodeAnalysis.CSharp.Diagnostics;
10
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
11 12 13 14 15 16 17 18 19 20 21 22 23 24
using Microsoft.CodeAnalysis.Diagnostics;
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
        [Fact, 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 34 35 36 37 38
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        public async Task TestWithSimpleArgument_UseExpressionBody1()
        {
            await TestAsync(
@"class C { void M() { new [|C|](1); } }",
@"class C { private int v; public C(int v) => this.v = v; void M() { new C(1); } }",
C
CyrusNajmabadi 已提交
39
options: Option(CSharpCodeStyleOptions.PreferExpressionBodiedConstructors, CodeStyleOptions.TrueWithNoneEnforcement));
40 41
        }

J
Jared Parsons 已提交
42
        [Fact, WorkItem(910589, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/910589"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
43
        public async Task TestWithNoArgs()
44
        {
45
            await TestAsync(
46 47 48 49
@"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 已提交
50
        [Fact, WorkItem(910589, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/910589"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
51
        public async Task TestWithNamedArg()
52
        {
53
            await TestAsync(
54 55 56 57
@"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 已提交
58
        [Fact, WorkItem(910589, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/910589"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
59
        public async Task TestWithExistingField1()
60
        {
61
            await TestAsync(
62 63 64 65
@"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; } }");
        }

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

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

82
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
83
        public async Task TestWithExistingField4()
84
        {
85
            await TestAsync(
86 87 88 89
@"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 已提交
90
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
91
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
92
        public async Task TestWithExistingField5()
93
        {
94
            await TestAsync(
95 96 97 98
@"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 已提交
99
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
100
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
101
        public async Task TestWithExistingField5WithQualification()
102
        {
103
            await TestAsync(
104 105
@"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; } }",
106
                options: Option(CodeStyleOptions.QualifyFieldAccess, true, NotificationOption.Error));
107 108
        }

J
Jared Parsons 已提交
109
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
110
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
111
        public async Task TestWithExistingField6()
112
        {
113
            await TestAsync(
114 115 116 117
@"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 已提交
118
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
119
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
120
        public async Task TestWithExistingField7()
121
        {
122
            await TestAsync(
123 124 125 126
@"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 已提交
127
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
128
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
129
        public async Task TestWithExistingField7WithQualification()
130
        {
131
            await TestAsync(
132 133
@"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; } }",
134
                options: Option(CodeStyleOptions.QualifyFieldAccess, true, NotificationOption.Error));
135 136
        }

J
Jared Parsons 已提交
137
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
138
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
139
        public async Task TestWithExistingField8()
140
        {
141
            await TestAsync(
142 143 144 145
@"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 已提交
146
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
147
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
148
        public async Task TestWithExistingField9()
149
        {
150
            await TestAsync(
151 152 153 154
@"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 已提交
155
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
156
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
157
        public async Task TestWithExistingProperty1()
158
        {
159
            await TestAsync(
160 161 162 163
@"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 已提交
164
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
165
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
166
        public async Task TestWithExistingProperty1WithQualification()
167
        {
168
            await TestAsync(
169 170
@"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; } }",
171
                options: Option(CodeStyleOptions.QualifyPropertyAccess, true, NotificationOption.Error));
172 173
        }

J
Jared Parsons 已提交
174
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
175
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
176
        public async Task TestWithExistingProperty2()
177
        {
178
            await TestAsync(
179 180 181 182
@"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 已提交
183
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
184
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
185
        public async Task TestWithExistingProperty3()
186
        {
187
            await TestAsync(
188 189 190 191
@"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 已提交
192
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
193
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
194
        public async Task TestWithExistingProperty3WithQualification()
195
        {
196
            await TestAsync(
197 198
@"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; } }",
199
                options: Option(CodeStyleOptions.QualifyPropertyAccess, true, NotificationOption.Error));
200 201
        }

J
Jared Parsons 已提交
202
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
203
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
204
        public async Task TestWithExistingProperty4()
205
        {
206
            await TestAsync(
207 208 209 210
@"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 已提交
211
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
212
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
213
        public async Task TestWithExistingProperty4WithQualification()
214
        {
215
            await TestAsync(
216 217
@"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; } }",
218
                options: Option(CodeStyleOptions.QualifyPropertyAccess, true, NotificationOption.Error));
219 220
        }

J
Jared Parsons 已提交
221
        [WorkItem(539444, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539444")]
222
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
223
        public async Task TestWithExistingProperty5()
224
        {
225
            await TestAsync(
226 227 228 229
@"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; } }");
        }

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

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

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

254
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
255
        public async Task TestStructInLocalInitializerWithSystemType()
256
        {
257
            await TestAsync(
258 259 260 261
@"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 已提交
262
        [WorkItem(539489, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539489")]
263
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
264
        public async Task TestEscapedName()
265
        {
266
            await TestAsync(
267 268 269 270
@"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 已提交
271
        [WorkItem(539489, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539489")]
272
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
273
        public async Task TestEscapedKeyword()
274
        {
275
            await TestAsync(
276 277 278 279
@"class @int { void M() { new [|@int|](1); } }",
@"class @int { private int v; public @int(int v) { this.v = v; } void M() { new @int(1); } }");
        }

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

J
Jared Parsons 已提交
325
        [WorkItem(539787, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539787")]
326
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
327
        public async Task TestGenerateIntoCorrectPart()
328
        {
329
            await TestAsync(
330 331 332 333
@"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"" ) ; } } ");
        }

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

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

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

358
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
359
        public async Task TestDelegateToSmallerConstructor4()
360
        {
361
            await TestAsync(
362 363 364 365
@"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 ; } } ");
        }

366
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
367
        public async Task TestGenerateFromThisInitializer1()
368
        {
369
            await TestAsync(
370 371 372 373
@"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 已提交
374
        [Fact, WorkItem(910589, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/910589"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
375
        public async Task TestGenerateFromThisInitializer2()
376
        {
377
            await TestAsync(
378 379 380 381
@"class C { public C ( int i ) [|: this ( )|] { } } ",
@"class C { public C ( ) { } public C ( int i ) : this ( ) { } } ");
        }

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

390
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
391
        public async Task TestGenerateFromBaseInitializer2()
392
        {
393
            await TestAsync(
394 395 396 397
@"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 已提交
398
        [WorkItem(539969, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539969")]
399
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
400
        public async Task TestNotOnExistingConstructor()
401
        {
402
            await TestMissingAsync(
403 404 405
@"class C { private class D { } } class A { void M ( ) { C . D d = new C . [|D|] ( ) ; } } ");
        }

J
Jared Parsons 已提交
406
        [WorkItem(539972, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/539972")]
407
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
408
        public async Task TestUnavailableTypeParameters()
409
        {
410
            await TestAsync(
411 412 413 414
@"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 已提交
415
        [WorkItem(541020, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/541020")]
416
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
417
        public async Task TestGenerateCallToDefaultConstructorInStruct()
418
        {
419
            await TestAsync(
420 421 422 423
@"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 已提交
424
        [WorkItem(541121, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/541121")]
425
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
426
        public async Task TestReadonlyFieldDelegation()
427
        {
428
            await TestAsync(
429 430 431 432
@"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 ) ; } } ");
        }

433
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
434
        public async Task TestNoGenerationIntoEntirelyHiddenType()
435
        {
436
            await TestMissingAsync(
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
@"
class C
{
    void Foo()
    {
        new [|D|](1, 2, 3);
    }
}

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

454
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
455
        public async Task TestNestedConstructorCall()
456
        {
457
            await TestAsync(
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 494 495 496 497 498 499 500 501
@"
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 已提交
502
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
503
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
504
        public async Task TestAttributesWithArgument()
505
        {
506
            await TestAsync(
507 508 509 510
@"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 已提交
511
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
512
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
513
        public async Task TestAttributesWithMultipleArguments()
514
        {
515
            await TestAsync(
516 517 518 519
@"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 已提交
520
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
521
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
522
        public async Task TestAttributesWithNamedArguments()
523
        {
524
            await TestAsync(
525 526 527 528
@"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 已提交
529
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
530
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
531
        public async Task TestAttributesWithAdditionalConstructors()
532
        {
533
            await TestAsync(
534 535 536 537
@"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 已提交
538
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
539
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
540
        public async Task TestAttributesWithOverloading()
541
        {
542
            await TestAsync(
543 544 545 546
@"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 已提交
547
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
548
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
549
        public async Task TestAttributesWithOverloadingMultipleParameters()
550
        {
551
            await TestAsync(
552 553 554 555
@"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 已提交
556
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
557
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
558
        public async Task TestAttributesWithAllValidParameters()
559
        {
560
            await TestAsync(
561 562 563 564
@"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 已提交
565
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
566
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
567
        public async Task TestAttributesWithDelegation()
568
        {
569
            await TestMissingAsync(
570 571 572
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttrAttribute : Attribute { } [|[MyAttrAttribute(()=>{return;})]|] class D { } ");
        }

J
Jared Parsons 已提交
573
        [WorkItem(530003, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530003")]
574
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
575
        public async Task TestAttributesWithLambda()
576
        {
577
            await TestMissingAsync(
578 579 580
@"using System; [AttributeUsage(AttributeTargets.Class)] class MyAttrAttribute : Attribute { } [|[MyAttrAttribute(()=>5)]|] class D { } ");
        }

J
Jared Parsons 已提交
581
        [WorkItem(889349, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/889349")]
582
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
583
        public async Task TestConstructorGenerationForDifferentNamedParameter()
584
        {
585
            await TestAsync(
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 615 616 617 618 619 620 621 622
@"
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 已提交
623
        [WorkItem(528257, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/528257")]
624
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
625
        public async Task TestGenerateInInaccessibleType()
626
        {
627
            await TestAsync(
628 629 630
@"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); } }");
        }
631 632 633 634 635 636 637 638 639 640

        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")]
641
            [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
642
            public async Task TestGenerateConstructorInIncompleteLambda()
643
            {
644
                await TestAsync(
645 646 647 648
    @"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 ) } ) ; } } ");
            }
        }
649 650

        [WorkItem(5274, "https://github.com/dotnet/roslyn/issues/5274")]
651
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
652
        public async Task TestGenerateIntoDerivedClassWithAbstractBase()
653
        {
654
            await TestAsync(
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 695 696 697 698 699 700 701 702 703
@"
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;
        }
    }
704 705 706 707 708
}");
        }

        [WorkItem(6541, "https://github.com/dotnet/Roslyn/issues/6541")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
709
        public async Task TestGenerateFromDerivedClass()
710
        {
711
            await TestAsync(
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
@"
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)]
741
        public async Task TestGenerateFromDerivedClass2()
742
        {
743
            await TestAsync(
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
@"
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)
    {
    }
768 769
}");
        }
770 771

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
C
Cyrus Najmabadi 已提交
772
        public async Task TestGenerateWithIncorrectConstructorArguments_Crash()
773
        {
C
Cyrus Najmabadi 已提交
774
            await TestAsync(
775 776 777
@"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 ( ) ) ; } } } ");
        }
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796

        [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);
    }
}");
        }
797 798 799 800 801 802 803

        [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); } }",
J
Jared Parsons 已提交
804
parseOptions: TestOptions.Regular,
805 806 807 808 809 810 811 812 813
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"")); } }",
J
Jared Parsons 已提交
814
parseOptions: TestOptions.Regular,
815 816 817 818 819 820
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        public async Task TupleWithOneName()
        {
J
Julien 已提交
821 822 823
            await TestAsync(
@"class C { void M() { new [|C|]((a: 1, ""hello"")); } }",
@"class C { private (int a, string) p; public C((int a, string) p) { this.p = p; } void M() { new C((a: 1, ""hello"")); } }",
J
Jared Parsons 已提交
824
parseOptions: TestOptions.Regular,
825 826 827 828 829 830 831 832 833
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; } }",
J
Jared Parsons 已提交
834
parseOptions: TestOptions.Regular,
835 836 837 838 839 840 841 842 843
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; } }",
J
Jared Parsons 已提交
844
parseOptions: TestOptions.Regular,
845 846 847 848 849 850 851 852 853
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; } }",
J
Jared Parsons 已提交
854
parseOptions: TestOptions.Regular,
855 856 857 858 859 860 861 862 863
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) { } }",
J
Jared Parsons 已提交
864
parseOptions: TestOptions.Regular,
865 866 867 868 869 870 871 872 873
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) { } }",
J
Jared Parsons 已提交
874
parseOptions: TestOptions.Regular,
875 876 877 878 879 880 881 882 883
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) { } }",
J
Jared Parsons 已提交
884
parseOptions: TestOptions.Regular,
885 886 887
withScriptOption: true);
        }

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        [WorkItem(11563, "https://github.com/dotnet/roslyn/issues/11563")]
        public async Task StripUnderscoresFromParameterNames()
        {
            await TestAsync(
@"class C { 
    int _i;
    string _s;
    void M() {
        new [|D|](_i, _s);
    }
}

class D {
}",
@"class C { 
    int _i;
    string _s;

    void M() {
        new D(_i, _s);
    }
}

class D {
    private int _i;
    private string _s;

    public D(int i, string s) {
        _i = i;
        _s = s;
    }
C
CyrusNajmabadi 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
}");
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        [WorkItem(11563, "https://github.com/dotnet/roslyn/issues/11563")]
        public async Task DoNotStripSingleUnderscore()
        {
            await TestAsync(
@"class C { 
    int _;
    void M() {
        new [|D|](_);
    }
}

class D {
}",
@"class C { 
    int _;

    void M() {
        new D(_);
    }
}

class D {
    private int _;

    public D(int _) {
        this._ = _;
    }
951 952
}");
        }
953

954 955 956 957 958 959 960
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        [WorkItem(12147, "https://github.com/dotnet/roslyn/issues/12147")]
        public async Task TestOutVariableDeclaration_ImplicitlyTyped()
        {
            await TestAsync(
@"class C { void M() { new [|C|](out var a); } }",
@"class C { public C(out object a) { a = null; } void M() { new C(out var a); } }",
A
AlekseyTs 已提交
961
parseOptions: TestOptions.Regular,
962 963 964 965 966 967 968 969 970 971
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        [WorkItem(12147, "https://github.com/dotnet/roslyn/issues/12147")]
        public async Task TestOutVariableDeclaration_ImplicitlyTyped_NamedArgument()
        {
            await TestAsync(
@"class C { void M() { new C([|b|]: out var a); } }",
@"class C { public C(out object b) { b = null; } void M() { new C(b: out var a); } }",
A
AlekseyTs 已提交
972
parseOptions: TestOptions.Regular,
973 974 975 976 977 978 979 980 981 982
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        [WorkItem(12147, "https://github.com/dotnet/roslyn/issues/12147")]
        public async Task TestOutVariableDeclaration_ExplicitlyTyped()
        {
            await TestAsync(
@"class C { void M() { new [|C|](out int a); } }",
@"class C { public C(out int a) { a = 0; } void M() { new C(out int a); } }",
A
AlekseyTs 已提交
983
parseOptions: TestOptions.Regular,
984 985 986 987 988 989 990 991 992 993
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        [WorkItem(12147, "https://github.com/dotnet/roslyn/issues/12147")]
        public async Task TestOutVariableDeclaration_ExplicitlyTyped_NamedArgument()
        {
            await TestAsync(
@"class C { void M() { new C([|b|]: out int a); } }",
@"class C { public C(out int b) { b = 0; } void M() { new C(b: out int a); } }",
A
AlekseyTs 已提交
994
parseOptions: TestOptions.Regular,
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
withScriptOption: true);
        }

        [Fact(Skip = "https://github.com/dotnet/roslyn/issues/12182"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        [WorkItem(12182, "https://github.com/dotnet/roslyn/issues/12182")]
        public async Task TestOutVariableDeclaration_ImplicitlyTyped_CSharp6()
        {
            await TestAsync(
@"class C { void M() { new [|C|](out var a); } }",
@"class C { public C(out object a) { a = null; } void M() { new C(out var a); } }",
parseOptions: TestOptions.Regular.WithLanguageVersion(CodeAnalysis.CSharp.LanguageVersion.CSharp6),
withScriptOption: true);
        }

        [Fact(Skip = "https://github.com/dotnet/roslyn/issues/12182"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        [WorkItem(12182, "https://github.com/dotnet/roslyn/issues/12182")]
        public async Task TestOutVariableDeclaration_ImplicitlyTyped_NamedArgument_CSharp6()
        {
            await TestAsync(
@"class C { void M() { new C([|b|]: out var a); } }",
@"class C { public C(out object b) { b = null; } void M() { new C(b: out var a); } }",
parseOptions: TestOptions.Regular.WithLanguageVersion(CodeAnalysis.CSharp.LanguageVersion.CSharp6),
withScriptOption: true);
        }

        [Fact(Skip = "https://github.com/dotnet/roslyn/issues/12182"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        [WorkItem(12182, "https://github.com/dotnet/roslyn/issues/12182")]
        public async Task TestOutVariableDeclaration_ExplicitlyTyped_CSharp6()
        {
            await TestAsync(
@"class C { void M() { new [|C|](out int a); } }",
@"class C { public C(out int a) { a = 0; } void M() { new C(out int a); } }",
parseOptions: TestOptions.Regular.WithLanguageVersion(CodeAnalysis.CSharp.LanguageVersion.CSharp6),
withScriptOption: true);
        }

        [Fact(Skip = "https://github.com/dotnet/roslyn/issues/12182"), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
        [WorkItem(12182, "https://github.com/dotnet/roslyn/issues/12182")]
        public async Task TestOutVariableDeclaration_ExplicitlyTyped_NamedArgument_CSharp6()
        {
            await TestAsync(
@"class C { void M() { new C([|b|]: out int a); } }",
@"class C { public C(out int b) { b = 0; } void M() { new C(b: out int a); } }",
parseOptions: TestOptions.Regular.WithLanguageVersion(CodeAnalysis.CSharp.LanguageVersion.CSharp6),
1039 1040
withScriptOption: true);
        }
1041 1042
    }
}