|
1 | | -namespace IDisposableAnalyzers.Test.IDISP004DoNotIgnoreCreatedTests |
| 1 | +namespace IDisposableAnalyzers.Test.IDISP004DoNotIgnoreCreatedTests |
2 | 2 | { |
3 | 3 | using Gu.Roslyn.Asserts; |
4 | 4 | using NUnit.Framework; |
@@ -47,8 +47,65 @@ public void M() |
47 | 47 | RoslynAssert.Valid(Analyzer, code); |
48 | 48 | } |
49 | 49 |
|
| 50 | + [TestCase("await Task.FromResult(new Disposable())")] |
| 51 | + [TestCase("await Task.FromResult(new Disposable()).ConfigureAwait(false)")] |
| 52 | + [TestCase("await Task.Run(() => new Disposable())")] |
| 53 | + [TestCase("await Task.Run(() => new Disposable()).ConfigureAwait(false)")] |
| 54 | + [TestCase("await Task.Run(() => new Disposable()).Result")] |
| 55 | + [TestCase("await Task.Run(() => new Disposable()).GetAwaiter().GetResult()")] |
| 56 | + public static void AwaitSimple(string expression) |
| 57 | + { |
| 58 | + var code = @" |
| 59 | +namespace N |
| 60 | +{ |
| 61 | + using System; |
| 62 | + using System.Threading; |
| 63 | + using System.Threading.Tasks; |
| 64 | +
|
| 65 | + class C |
| 66 | + { |
| 67 | + public async Task M() |
| 68 | + { |
| 69 | + using (await Task.FromResult(new Disposable())) |
| 70 | + { |
| 71 | + } |
| 72 | + |
| 73 | + await Task.Delay(10); |
| 74 | + } |
| 75 | + } |
| 76 | +}".AssertReplace("await Task.FromResult(new Disposable())", expression); |
| 77 | + RoslynAssert.Valid(Analyzer, DisposableCode, code); |
| 78 | + } |
| 79 | + |
| 80 | + [TestCase("await Task.FromResult(new Disposable())")] |
| 81 | + [TestCase("await Task.FromResult(new Disposable()).ConfigureAwait(false)")] |
| 82 | + [TestCase("await Task.Run(() => new Disposable())")] |
| 83 | + [TestCase("await Task.Run(() => new Disposable()).ConfigureAwait(false)")] |
| 84 | + [TestCase("await Task.Run(() => new Disposable()).Result")] |
| 85 | + [TestCase("await Task.Run(() => new Disposable()).GetAwaiter().GetResult()")] |
| 86 | + public static void AwaitSimpleUsingDeclaration(string expression) |
| 87 | + { |
| 88 | + var code = @" |
| 89 | +namespace N |
| 90 | +{ |
| 91 | + using System; |
| 92 | + using System.Threading; |
| 93 | + using System.Threading.Tasks; |
| 94 | +
|
| 95 | + class C |
| 96 | + { |
| 97 | + public async Task M() |
| 98 | + { |
| 99 | + using var disposable = await Task.FromResult(new Disposable()); |
| 100 | + await Task.Delay(10); |
| 101 | + } |
| 102 | + } |
| 103 | +}".AssertReplace("await Task.FromResult(new Disposable())", expression); |
| 104 | + RoslynAssert.Valid(Analyzer, DisposableCode, code); |
| 105 | + } |
| 106 | + |
50 | 107 | [Test] |
51 | | - public static void SampleWithAwait() |
| 108 | + public static void AwaitWeirdCase() |
52 | 109 | { |
53 | 110 | var code = @" |
54 | 111 | namespace N |
|
0 commit comments