MoveDeclarationNearReferenceTests.cs 11.6 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.Threading.Tasks;
C
CyrusNajmabadi 已提交
4
using Microsoft.CodeAnalysis.CodeRefactorings;
5
using Microsoft.CodeAnalysis.CSharp.CodeRefactorings.MoveDeclarationNearReference;
6
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
7 8 9 10 11 12 13
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings.MoveDeclarationNearReference
{
    public class MoveDeclarationNearReferenceTests : AbstractCSharpCodeActionTest
    {
C
CyrusNajmabadi 已提交
14
        protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspace workspace)
15 16 17 18
        {
            return new MoveDeclarationNearReferenceCodeRefactoringProvider();
        }

19
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
20
        public async Task TestMove1()
21
        {
22
            await TestAsync(
23 24 25 26 27
@"class C { void M() { int [||]x; { Console.WriteLine(x); } } }",
@"class C { void M() { { int x; Console.WriteLine(x); } } }",
index: 0);
        }

28
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
29
        public async Task TestMove2()
30
        {
31
            await TestAsync(
32 33 34 35 36
@"class C { void M() { int [||]x; Console.WriteLine(); Console.WriteLine(x); } }",
@"class C { void M() { Console.WriteLine(); int x; Console.WriteLine(x); } }",
index: 0);
        }

37
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
38
        public async Task TestMove3()
39
        {
40
            await TestAsync(
41 42 43 44 45
@"class C { void M() { int [||]x; Console.WriteLine(); { Console.WriteLine(x); } { Console.WriteLine(x); } }",
@"class C { void M() { Console.WriteLine(); int x; { Console.WriteLine(x); } { Console.WriteLine(x); } }",
index: 0);
        }

46
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
47
        public async Task TestMove4()
48
        {
49
            await TestAsync(
50 51 52 53 54
@"class C { void M() { int [||]x; Console.WriteLine(); { Console.WriteLine(x); } }",
@"class C { void M() { Console.WriteLine(); { int x; Console.WriteLine(x); } }",
index: 0);
        }

55
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
56
        public async Task TestAssign1()
57
        {
58
            await TestAsync(
59 60 61 62 63
@"class C { void M() { int [||]x; { x = 5; Console.WriteLine(x); } } }",
@"class C { void M() { { int x = 5; Console.WriteLine(x); } } }",
index: 0);
        }

64
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
65
        public async Task TestAssign2()
66
        {
67
            await TestAsync(
68 69 70 71 72
@"class C { void M() { int [||]x = 0; { x = 5; Console.WriteLine(x); } } }",
@"class C { void M() { { int x = 5; Console.WriteLine(x); } } }",
index: 0);
        }

73
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
74
        public async Task TestAssign3()
75
        {
76
            await TestAsync(
77 78 79 80 81
@"class C { void M() { var [||]x = (short)0; { x = 5; Console.WriteLine(x); } } }",
@"class C { void M() { { var x = (short)0; x = 5; Console.WriteLine(x); } } }",
index: 0);
        }

82
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
83
        public async Task TestMissing1()
84
        {
85
            await TestMissingAsync(
86 87 88
@"class C { void M() { int [||]x; Console.WriteLine(x); } }");
        }

J
Jared Parsons 已提交
89
        [WorkItem(538424, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/538424")]
90
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
91
        public async Task TestMissingWhenReferencedInDeclaration()
92
        {
93
            await TestMissingAsync(
94 95 96
@"class Program { static void Main ( ) { object [ ] [||]x = { x = null } ; x . ToString ( ) ; } } ");
        }

97
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
98
        public async Task TestMissingWhenInDeclarationGroup()
99
        {
100
            await TestMissingAsync(
101 102 103
@"class Program { static void Main ( ) { int [||]i = 5; int j = 10; Console.WriteLine(i); } } ");
        }

104
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
J
Jared Parsons 已提交
105
        [WorkItem(541475, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/541475")]
106
        public async Task Regression8190()
107
        {
108
            await TestMissingAsync(
109 110 111
@"class Program { void M() { { object x; [|object|] } } }");
        }

112
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
113
        public async Task TestFormatting()
114
        {
115
            await TestAsync(
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
@"class Program
{
    static void Main(string[] args)
    {
        int [||]i = 5; Console.WriteLine();
        Console.Write(i);
    }
}",
@"class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine();
        int i = 5; Console.Write(i);
    }
}",
index: 0,
compareTokens: false);
        }

136
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
137
        public async Task TestMissingInHiddenBlock1()
138
        {
139
            await TestMissingAsync(
140 141 142 143 144 145 146 147 148 149 150 151 152
@"class Program
{
    void Main()
    {
        int [|x|] = 0;
        Foo();
#line hidden
        Bar(x);
    }
#line default
}");
        }

153
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
154
        public async Task TestMissingInHiddenBlock2()
155
        {
156
            await TestMissingAsync(
157 158 159 160 161 162 163 164 165 166 167 168 169 170
@"class Program
{
    void Main()
    {
        int [|x|] = 0;
        Foo();
#line hidden
        Foo();
#line default
        Bar(x);
    }
}");
        }

171
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
172
        public async Task TestAvailableInNonHiddenBlock1()
173
        {
174
            await TestAsync(
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
@"#line default
class Program
{
    void Main()
    {
        int [||]x = 0;
        Foo();
        Bar(x);
#line hidden
    }
#line default
}",
@"#line default
class Program
{
    void Main()
    {
        Foo();
        int x = 0;
        Bar(x);
#line hidden
    }
#line default
}",
compareTokens: false);
        }

202
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
203
        public async Task TestAvailableInNonHiddenBlock2()
204
        {
205
            await TestAsync(
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
@"class Program
{
    void Main()
    {
        int [||]x = 0;
        Foo();
#line hidden
        Foo();
#line default
        Foo();
        Bar(x);
    }
}",
@"class Program
{
    void Main()
    {
        Foo();
#line hidden
        Foo();
#line default
        Foo();
        int x = 0;
        Bar(x);
    }
}",
compareTokens: false);
        }

J
Jared Parsons 已提交
235
        [WorkItem(545435, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545435")]
236
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
237
        public async Task TestWarnOnChangingScopes1()
238
        {
239
            await TestAsync(
240 241 242 243
@"using System . Linq ; class Program { void Main ( ) { var [||]@lock = new object ( ) ; new [ ] { 1 } . AsParallel ( ) . ForAll ( ( i ) => { lock ( @lock ) { } } ) ; } } ",
@"using System . Linq ; class Program { void Main ( ) { new [ ] { 1 } . AsParallel ( ) . ForAll ( ( i ) => { {|Warning:var @lock = new object ( ) ;|} lock ( @lock ) { } } ) ; } } ");
        }

J
Jared Parsons 已提交
244
        [WorkItem(545435, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545435")]
245
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
246
        public async Task TestWarnOnChangingScopes2()
247
        {
248
            await TestAsync(
249 250 251 252
@"using System ; using System . Linq ; class Program { void Main ( ) { var [||]i = 0 ; foreach ( var v in new [ ] { 1 } ) { Console . Write ( i ) ; i ++ ; } } } ",
@"using System ; using System . Linq ; class Program { void Main ( ) { foreach ( var v in new [ ] { 1 } ) { {|Warning:var i = 0 ;|} Console . Write ( i ) ; i ++ ; } } } ");
        }

J
Jared Parsons 已提交
253
        [WorkItem(545840, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545840")]
254
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
255
        public async Task InsertCastIfNecessary1()
256
        {
257
            await TestAsync(
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
@"using System;

static class C
{
    static int Outer(Action<int> x, object y) { return 1; }
    static int Outer(Action<string> x, string y) { return 2; }

    static void Inner(int x, int[] y) { }
    unsafe static void Inner(string x, int*[] y) { }

    static void Main()
    {
        var [||]a = Outer(x => Inner(x, null), null);
        unsafe
        {
            Console.WriteLine(a);
        }
    }
}",

@"using System;

static class C
{
    static int Outer(Action<int> x, object y) { return 1; }
    static int Outer(Action<string> x, string y) { return 2; }

    static void Inner(int x, int[] y) { }
    unsafe static void Inner(string x, int*[] y) { }

    static void Main()
    {
        unsafe
        {
            var a = Outer(x => Inner(x, null), (object)null);
            Console.WriteLine(a);
        }
    }
}",
compareTokens: false);
        }

J
Jared Parsons 已提交
300
        [WorkItem(545835, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545835")]
301
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
302
        public async Task InsertCastIfNecessary2()
303
        {
304
            await TestAsync(
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
@"using System;

class X
{
    static int Foo(Func<int?, byte> x, object y) { return 1; }
    static int Foo(Func<X, byte> x, string y) { return 2; }

    const int Value = 1000;
    static void Main()
    {
        var [||]a = Foo(X => (byte)X.Value, null);
        unchecked
        {
            Console.WriteLine(a);
        }
    }
}",

@"using System;

class X
{
    static int Foo(Func<int?, byte> x, object y) { return 1; }
    static int Foo(Func<X, byte> x, string y) { return 2; }

    const int Value = 1000;
    static void Main()
    {
        unchecked
        {
            var a = Foo(X => (byte)X.Value, (object)null);
            Console.WriteLine(a);
        }
    }
}",
compareTokens: false);
        }

J
Jared Parsons 已提交
343
        [WorkItem(546267, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/546267")]
344
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
345
        public async Task MissingIfNotInDeclarationSpan()
346
        {
347
            await TestMissingAsync(
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
@"using System;
using System.Collections.Generic;
using System.Linq;
 
class Program
{
    static void Main(string[] args)
    {
        // Comment [||]about foo!
        // Comment about foo!
        // Comment about foo!
        // Comment about foo!
        // Comment about foo!
        // Comment about foo!
        // Comment about foo!
        int foo;
 
        Console.WriteLine();
        Console.WriteLine(foo);
    }
}");
        }
370 371 372 373 374 375 376 377

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
        public async Task Tuple()
        {
            await TestAsync(
@"class C { void M() { (int, string) [||]x; { Console.WriteLine(x); } } }",
@"class C { void M() { { (int, string) x; Console.WriteLine(x); } } }",
index: 0,
J
Jared Parsons 已提交
378
parseOptions: TestOptions.Regular,
379 380 381 382 383 384 385 386 387 388
withScriptOption: true);
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveDeclarationNearReference)]
        public async Task TupleWithNames()
        {
            await TestAsync(
@"class C { void M() { (int a, string b) [||]x; { Console.WriteLine(x); } } }",
@"class C { void M() { { (int a, string b) x; Console.WriteLine(x); } } }",
index: 0,
J
Jared Parsons 已提交
389
parseOptions: TestOptions.Regular,
390 391
withScriptOption: true);
        }
392 393
    }
}