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

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

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

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

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

40
        [WpfFact, WorkItem(910589), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
41 42 43 44 45 46 47
        public void TestWithNamedArg()
        {
            Test(
@"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); } }");
        }

48
        [WpfFact, WorkItem(910589), Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
49 50 51 52 53 54 55
        public void TestWithExistingField1()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
57 58 59 60 61 62 63
        public void TestWithExistingField2()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
65 66 67 68 69 70 71
        public void TestWithExistingField3()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
73 74 75 76 77 78 79 80
        public void TestWithExistingField4()
        {
            Test(
@"class C { void M() { new [|D|](1); } } class B { private int v; } class D : B { }",
@"class C { void M() { new D(1); } } class B { private int v; } class D : B { private int v; public D(int v) { this.v = v; } }");
        }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        [WorkItem(539444)]
212
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
213 214 215 216 217 218 219
        public void TestWithExistingProperty5()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
221 222 223 224 225 226 227
        public void TestWithOutParam()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
229 230 231 232 233 234 235
        public void TestWithBaseDelegatingConstructor1()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
237 238 239 240 241 242 243
        public void TestWithBaseDelegatingConstructor2()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
245 246 247 248 249 250 251 252
        public void TestStructInLocalInitializerWithSystemType()
        {
            Test(
@"struct S { void M() { S s = new [|S|](System.DateTime.Now); } }",
@"using System; struct S { private DateTime now; public S(DateTime now) { this.now = now; } void M() { S s = new S(System.DateTime.Now); } }");
        }

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

        [WorkItem(539489)]
262
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
263 264 265 266 267 268 269
        public void TestEscapedKeyword()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
271 272 273 274 275 276 277 278
        public void TestIsSymbolAccessibleWithInternalField()
        {
            Test(
@"class Base { internal long field ; void Main ( ) { int field = 5 ; new [|Derived|] ( field ) ; } } class Derived : Base { } ",
@"class Base { internal long field ; void Main ( ) { int field = 5 ; new Derived ( field ) ; } } class Derived : Base { public Derived ( int field ) { this . field = field ; } } ");
        }

        [WorkItem(539548)]
279
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
280 281 282 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
        public void TestFormatting()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
309 310 311 312 313 314 315
        public void TestNotOnStructConstructor()
        {
            TestMissing(
@"struct Struct { void Main ( ) { Struct s = new [|Struct|] ( ) ; } } ");
        }

        [WorkItem(539787)]
316
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
317 318 319 320 321 322 323
        public void TestGenerateIntoCorrectPart()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
325 326 327 328 329 330 331
        public void TestDelegateToSmallerConstructor1()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
333 334 335 336 337 338 339
        public void TestDelegateToSmallerConstructor2()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
341 342 343 344 345 346 347
        public void TestDelegateToSmallerConstructor3()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
349 350 351 352 353 354 355
        public void TestDelegateToSmallerConstructor4()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
357 358 359 360 361 362 363
        public void TestGenerateFromThisInitializer1()
        {
            Test(
@"class C { public C ( ) [|: this ( 4 )|] { } } ",
@"class C { private int v ; public C ( ) : this ( 4 ) { } public C ( int v ) { this . v = v ; } } ");
        }

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

372
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
373 374 375 376 377 378 379
        public void TestGenerateFromBaseInitializer1()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
381 382 383 384 385 386 387 388
        public void TestGenerateFromBaseInitializer2()
        {
            Test(
@"class C : B { public C ( int i ) [|: base ( i )|] { } } class B { int i ; } ",
@"class C : B { public C ( int i ) : base ( i ) { } } class B { int i ; public B ( int i ) { this . i = i ; } } ");
        }

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

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

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

        [WorkItem(541121)]
415
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)]
416 417 418 419 420 421 422
        public void TestReadonlyFieldDelegation()
        {
            Test(
@"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
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
        public void TestNoGenerationIntoEntirelyHiddenType()
        {
            TestMissing(
@"
class C
{
    void Foo()
    {
        new [|D|](1, 2, 3);
    }
}

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

444
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
445 446 447 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 492
        public void TestNestedConstructorCall()
        {
            Test(
@"
class C
{
    void Foo()
    {
        var d = new D([|v|]: new D(u: 1));
    }
}

class D
{
    private int u;

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

class D
{
    private int u;
    private D v;

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

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

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

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

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

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

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

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

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

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

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

        [WorkItem(889349)]
572
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
573 574 575 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 613
        public void TestConstructorGenerationForDifferentNamedParameter()
        {
            Test(
@"
class Program
{
    static void Main(string[] args)
    {
        var ss = new [|Program(wde: 1)|];
    }

    Program(int s)
    {

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

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

    Program(int s)
    {

    }

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

        [WorkItem(528257)]
614
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
615 616 617 618 619 620
        public void TestGenerateInInaccessibleType()
        {
            Test(
@"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
            [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
632 633 634 635 636 637 638
            public void TestGenerateConstructorInIncompleteLambda()
            {
                Test(
    @"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 641 642 643 644 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 694 695

        [WorkItem(5274, "https://github.com/dotnet/roslyn/issues/5274")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
        public void TestGenerateIntoDerivedClassWithAbstractBase()
        {
            Test(
@"
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;
        }
    }
}");
        }
696 697
    }
}