InvertIfTests.cs 15.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;
4 5 6 7 8 9 10 11 12 13 14
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.CSharp.CodeRefactorings.InvertIf;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings.InvertIf
{
    public class InvertIfTests : AbstractCSharpCodeActionTest
    {
15
        private async Task TestFixOneAsync(
16 17 18
            string initial,
            string expected)
        {
19
            await TestAsync(CreateTreeText(initial), CreateTreeText(expected), index: 0);
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
        }

        protected override object CreateCodeRefactoringProvider(Workspace workspace)
        {
            return new InvertIfCodeRefactoringProvider();
        }

        private string CreateTreeText(string initial)
        {
            return
@"class A
{
  void Foo()
  {
" + initial + @"
  }
}";
        }

39
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
40
        public async Task TestIdentifier()
41
        {
42
            await TestFixOneAsync(
43 44 45 46
@"[||]if (a) { a(); } else { b(); }",
@"if (!a) { b(); } else { a(); }");
        }

47
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
48
        public async Task TestNotIdentifier()
49
        {
50
            await TestFixOneAsync(
51 52 53 54
@"[||]if (!a) { a(); } else { b(); }",
@"if (a) { b(); } else { a(); }");
        }

55
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
56
        public async Task TestEqualsEquals()
57
        {
58
            await TestFixOneAsync(
59 60 61 62
@"[||]if (a == b) { a(); } else { b(); }",
@"if (a != b) { b(); } else { a(); }");
        }

63
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
64
        public async Task TestNotEquals()
65
        {
66
            await TestFixOneAsync(
67 68 69 70
@"[||]if (a != b) { a(); } else { b(); }",
@"if (a == b) { b(); } else { a(); }");
        }

71
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
72
        public async Task TestGreaterThan()
73
        {
74
            await TestFixOneAsync(
75 76 77 78
@"[||]if (a > b) { a(); } else { b(); }",
@"if (a <= b) { b(); } else { a(); }");
        }

79
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
80
        public async Task TestGreaterThanEquals()
81
        {
82
            await TestFixOneAsync(
83 84 85 86
@"[||]if (a >= b) { a(); } else { b(); }",
@"if (a < b) { b(); } else { a(); }");
        }

87
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
88
        public async Task TestLessThan()
89
        {
90
            await TestFixOneAsync(
91 92 93 94
@"[||]if (a < b) { a(); } else { b(); }",
@"if (a >= b) { b(); } else { a(); }");
        }

95
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
96
        public async Task TestLessThanEquals()
97
        {
98
            await TestFixOneAsync(
99 100 101 102
@"[||]if (a <= b) { a(); } else { b(); }",
@"if (a > b) { b(); } else { a(); }");
        }

103
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
104
        public async Task TestParens()
105
        {
106
            await TestFixOneAsync(
107 108 109 110
@"[||]if ((a)) { a(); } else { b(); }",
@"if (!a) { b(); } else { a(); }");
        }

111
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
112
        public async Task TestIs()
113
        {
114
            await TestFixOneAsync(
115 116 117 118
@"[||]if (a is Foo) { a(); } else { b(); }",
@"if (!(a is Foo)) { b(); } else { a(); }");
        }

119
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
120
        public async Task TestCall()
121
        {
122
            await TestFixOneAsync(
123 124 125 126
@"[||]if (a.Foo()) { a(); } else { b(); }",
@"if (!a.Foo()) { b(); } else { a(); }");
        }

127
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
128
        public async Task TestOr()
129
        {
130
            await TestFixOneAsync(
131 132 133 134
@"[||]if (a || b) { a(); } else { b(); }",
@"if (!a && !b) { b(); } else { a(); }");
        }

135
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
136
        public async Task TestOr2()
137
        {
138
            await TestFixOneAsync(
139 140 141 142
@"[||]if (!a || !b) { a(); } else { b(); }",
@"if (a && b) { b(); } else { a(); }");
        }

143
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
144
        public async Task TestAnd()
145
        {
146
            await TestFixOneAsync(
147 148 149 150
@"[||]if (a && b) { a(); } else { b(); }",
@"if (!a || !b) { b(); } else { a(); }");
        }

151
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
152
        public async Task TestAnd2()
153
        {
154
            await TestFixOneAsync(
155 156 157 158
@"[||]if (!a && !b) { a(); } else { b(); }",
@"if (a || b) { b(); } else { a(); }");
        }

159
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
160
        public async Task TestParenthesizeAndForPrecedence()
161
        {
162
            await TestFixOneAsync(
163 164 165 166
@"[||]if (a && b || c) { a(); } else { b(); }",
@"if ((!a || !b) && !c) { b(); } else { a(); }");
        }

167
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
168
        public async Task TestPlus()
169
        {
170
            await TestFixOneAsync(
171 172 173 174
@"[||]if (a + b) { a(); } else { b(); }",
@"if (!(a + b)) { b(); } else { a(); }");
        }

175
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
176
        public async Task TestTrue()
177
        {
178
            await TestFixOneAsync(
179 180 181 182
@"[||]if (true) { a(); } else { b(); }",
@"if (false) { b(); } else { a(); }");
        }

183
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
184
        public async Task TestFalse()
185
        {
186
            await TestFixOneAsync(
187 188 189 190
@"[||]if (false) { a(); } else { b(); }",
@"if (true) { b(); } else { a(); }");
        }

191
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
192
        public async Task TestTrueAndFalse()
193
        {
194
            await TestFixOneAsync(
195 196 197 198
@"[||]if (true && false) { a(); } else { b(); }",
@"if (false || true) { b(); } else { a(); }");
        }

199
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
200
        public async Task TestCurlies1()
201
        {
202
            await TestFixOneAsync(
203 204 205 206
@"[||]if (a) a(); else b();",
@"if (!a) b(); else a();");
        }

207
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
208
        public async Task TestCurlies2()
209
        {
210
            await TestFixOneAsync(
211 212 213 214
@"[||]if (a) { a(); } else b();",
@"if (!a) b(); else { a(); }");
        }

215
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
216
        public async Task TestCurlies3()
217
        {
218
            await TestFixOneAsync(
219 220 221 222
@"[||]if (a) a(); else { b(); }",
@"if (!a) { b(); } else a();");
        }

223
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
224
        public async Task TestIfElseIf()
225
        {
226
            await TestFixOneAsync(
227 228 229 230
@"[||]if (a) { a(); } else if (b) { b(); }",
@"if (!a) { if (b) { b(); } } else { a(); }");
        }

231
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
232
        public async Task TestIfElseIf2()
233
        {
234
            await TestFixOneAsync(
235 236 237 238
@"[||]if (a) { a(); } else if (b) { b(); } else { c(); }",
@"if (!a) { if (b) { b(); } else { c(); } } else { a(); }");
        }

239
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
240
        public async Task TestNested()
241
        {
242
            await TestFixOneAsync(
243 244 245 246
@"[||]if (((a == b) && (c != d)) || ((e < f) && (!g))) { a(); } else { b(); }",
@"if ((a != b || c == d) && (e >= f || g)) { b(); } else { a(); }");
        }

247
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
248
        public async Task TestKeepTriviaWithinExpression()
249
        {
250
            await TestFixOneAsync(
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
@"[||]if (a ||
    b &&
    c < // comment
    d)
{
    a();
}
else
{
    b();
}",
@"if (!a &&
    (!b ||
    c >= // comment
    d))
{
    b();
}
else
{
    a();
}");
        }

275
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
276
        public async Task TestMissingOnNonEmptySpan()
277
        {
278
            await TestMissingAsync(
279 280 281
@"class C { void F() { [|if (a) { a(); } else { b(); }|] } }");
        }

282
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
283
        public async Task TestOverlapsHiddenPosition1()
284
        {
285
            await TestMissingAsync(
286 287 288 289 290 291 292 293 294 295 296 297
@"
class C 
{
    void F()
    {
#line hidden
        [||]if (a) { a(); } else { b(); }
#line default
    }
}");
        }

298
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
299
        public async Task TestOverlapsHiddenPosition2()
300
        {
301
            await TestMissingAsync(
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
@"
class C 
{
    void F()
    {
        [||]if (a)
        {
#line hidden
            a();
#line default
        }
        else
        { 
            b();
        }
    }
}");
        }

321
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
322
        public async Task TestOverlapsHiddenPosition3()
323
        {
324
            await TestMissingAsync(
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
@"
class C 
{
    void F()
    {
        [||]if (a)
        {
            a();
        }
        else
        { 
#line hidden
            b();
#line default
        }
    }
}");
        }

344
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
345
        public async Task TestOverlapsHiddenPosition4()
346
        {
347
            await TestMissingAsync(
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
@"
class C 
{
    void F()
    {
        [||]if (a)
        {
#line hidden
            a();
        }
        else
        { 
            b();
#line default
        }
    }
}");
        }

367
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
368
        public async Task TestOverlapsHiddenPosition5()
369
        {
370
            await TestMissingAsync(
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
@"
class C 
{
    void F()
    {
        [||]if (a)
        {
            a();
#line hidden
        }
        else
        { 
#line default
            b();
        }
    }
}");
        }

390
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
391
        public async Task TestOverlapsHiddenPosition6()
392
        {
393
            await TestAsync(
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
@"
#line hidden
class C 
{
    void F()
    {
#line default
        [||]if (a)
        {
            a();
        }
        else
        { 
            b();
        }
    }
}",

@"
#line hidden
class C 
{
    void F()
    {
#line default
        if (!a)
        {
            b();
        }
        else
        {
            a();
        }
    }
}", compareTokens: false);
        }

431
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
432
        public async Task TestOverlapsHiddenPosition7()
433
        {
434
            await TestAsync(
435 436 437 438 439 440 441 442 443 444 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
@"
#line hidden
class C 
{
    void F()
    {
#line default
        [||]if (a)
        {
            a();
        }
        else
        { 
            b();
        }
#line hidden
    }
}
#line default",

@"
#line hidden
class C 
{
    void F()
    {
#line default
        if (!a)
        {
            b();
        }
        else
        {
            a();
        }
#line hidden
    }
}
#line default", compareTokens: false);
        }

476
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
477
        public async Task TestSimplifyToLengthEqualsZero()
478
        {
479
            await TestFixOneAsync(
480 481 482 483
@"string x; [||]if (x.Length > 0) { GreaterThanZero(); } else { EqualsZero(); } } } ",
@"string x; if (x.Length == 0) { EqualsZero(); } else { GreaterThanZero(); } } } ");
        }

484
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
485
        public async Task TestSimplifyToLengthEqualsZero2()
486
        {
487
            await TestFixOneAsync(
488 489 490 491
@"string[] x; [||]if (x.Length > 0) { GreaterThanZero(); } else { EqualsZero(); } } } ",
@"string[] x; if (x.Length == 0) { EqualsZero(); } else { GreaterThanZero(); } } } ");
        }

492
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
493
        public async Task TestSimplifyToLengthEqualsZero3()
494
        {
495
            await TestFixOneAsync(
496 497 498 499
@"string x; [||]if (x.Length > 0x0) { a(); } else { b(); } } } ",
@"string x; if (x.Length == 0x0) { b(); } else { a(); } } } ");
        }

500
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
501
        public async Task TestSimplifyToLengthEqualsZero4()
502
        {
503
            await TestFixOneAsync(
504 505 506 507 508
@"string x; [||]if (0 < x.Length) { a(); } else { b(); } } } ",
@"string x; if (0 == x.Length) { b(); } else { a(); } } } ");
        }

        [WorkItem(545986)]
509
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
510
        public async Task TestSimplifyToLengthEqualsZero5()
511
        {
512
            await TestFixOneAsync(
513 514 515 516 517
@"byte x = 1; [||]if (0 < x) { a(); } else { b(); } } } ",
@"byte x = 1; if (0 == x) { b(); } else { a(); } } } ");
        }

        [WorkItem(545986)]
518
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
519
        public async Task TestSimplifyToLengthEqualsZero6()
520
        {
521
            await TestFixOneAsync(
522 523 524 525 526
@"ushort x = 1; [||]if (0 < x) { a(); } else { b(); } } } ",
@"ushort x = 1; if (0 == x) { b(); } else { a(); } } } ");
        }

        [WorkItem(545986)]
527
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
528
        public async Task TestSimplifyToLengthEqualsZero7()
529
        {
530
            await TestFixOneAsync(
531 532 533 534 535
@"uint x = 1; [||]if (0 < x) { a(); } else { b(); } } } ",
@"uint x = 1; if (0 == x) { b(); } else { a(); } } } ");
        }

        [WorkItem(545986)]
536
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
537
        public async Task TestSimplifyToLengthEqualsZero8()
538
        {
539
            await TestFixOneAsync(
540 541 542 543 544
@"ulong x = 1; [||]if (0 < x) { a(); } else { b(); } } } ",
@"ulong x = 1; if (0 == x) { b(); } else { a(); } } } ");
        }

        [WorkItem(545986)]
545
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
546
        public async Task TestSimplifyToLengthEqualsZero9()
547
        {
548
            await TestFixOneAsync(
549 550 551 552 553
@"ulong x = 1; [||]if (0 == x) { a(); } else { b(); } } } ",
@"ulong x = 1; if (0 < x) { b(); } else { a(); } } } ");
        }

        [WorkItem(545986)]
554
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
555
        public async Task TestSimplifyToLengthEqualsZero10()
556
        {
557
            await TestFixOneAsync(
558 559 560 561 562
@"ulong x = 1; [||]if (x == 0) { a(); } else { b(); } } } ",
@"ulong x = 1; if (x > 0) { b(); } else { a(); } } } ");
        }

        [WorkItem(530505)]
563
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
564
        public async Task TestSimplifyToLengthEqualsZero11()
565
        {
566
            await TestFixOneAsync(
567 568 569 570
@"string[] x; [||]if (x.LongLength > 0) { GreaterThanZero(); } else { EqualsZero(); } } } ",
@"string[] x; if (x.LongLength == 0) { EqualsZero(); } else { GreaterThanZero(); } } } ");
        }

571
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
572
        public async Task TestDoesNotSimplifyToLengthEqualsZero()
573
        {
574
            await TestFixOneAsync(
575 576 577 578
@"string x; [||]if (x.Length >= 0) { a(); } else { b(); } } } ",
@"string x; if (x.Length < 0) { b(); } else { a(); } } } ");
        }

579
        [WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)]
580
        public async Task TestDoesNotSimplifyToLengthEqualsZero2()
581
        {
582
            await TestFixOneAsync(
583 584 585 586 587
@"string x; [||]if (x.Length > 0.0f) { GreaterThanZero(); } else { EqualsZero(); } } } ",
@"string x; if (x.Length <= 0.0f) { EqualsZero(); } else { GreaterThanZero(); } } } ");
        }
    }
}