|
1 | 1 | namespace IDisposableAnalyzers.Test.Helpers |
2 | 2 | { |
3 | 3 | using System.Threading; |
| 4 | + using Gu.Roslyn.AnalyzerExtensions; |
4 | 5 | using Gu.Roslyn.Asserts; |
5 | 6 | using Microsoft.CodeAnalysis.CSharp; |
6 | 7 | using NUnit.Framework; |
@@ -41,6 +42,97 @@ public void Dispose() |
41 | 42 | var value = syntaxTree.FindArgument("File.OpenRead(string.Empty)"); |
42 | 43 | Assert.AreEqual(true, Disposable.DisposedByReturnValue(value, semanticModel, CancellationToken.None, out _)); |
43 | 44 | } |
| 45 | + |
| 46 | + [TestCase("new BinaryReader(stream)", true)] |
| 47 | + [TestCase("new BinaryReader(stream, new UTF8Encoding(), true)", false)] |
| 48 | + [TestCase("new BinaryReader(stream, new UTF8Encoding(), leaveOpen: true)", false)] |
| 49 | + [TestCase("new BinaryReader(stream, encoding: new UTF8Encoding(), leaveOpen: true)", false)] |
| 50 | + [TestCase("new BinaryReader(stream, leaveOpen: true, encoding: new UTF8Encoding())", false)] |
| 51 | + [TestCase("new BinaryReader(stream, new UTF8Encoding(), false)", true)] |
| 52 | + [TestCase("new BinaryReader(stream, leaveOpen: false, encoding: new UTF8Encoding())", true)] |
| 53 | + [TestCase("new BinaryWriter(stream, new UTF8Encoding(), leaveOpen: false)", true)] |
| 54 | + [TestCase("new BinaryWriter(stream, new UTF8Encoding(), leaveOpen: true)", false)] |
| 55 | + [TestCase("new StreamReader(stream, new UTF8Encoding(), true, 1024, leaveOpen: false)", true)] |
| 56 | + [TestCase("new StreamReader(stream, new UTF8Encoding(), true, 1024, leaveOpen: true)", false)] |
| 57 | + [TestCase("new StreamWriter(stream, new UTF8Encoding(), 1024, leaveOpen: false)", true)] |
| 58 | + [TestCase("new StreamWriter(stream, new UTF8Encoding(), 1024, leaveOpen: true)", false)] |
| 59 | + [TestCase("new CryptoStream(stream, new FromBase64Transform(), CryptoStreamMode.Read, leaveOpen: true)", false)] |
| 60 | + [TestCase("new CryptoStream(stream, new FromBase64Transform(), CryptoStreamMode.Read, leaveOpen: false)", true)] |
| 61 | + [TestCase("new DeflateStream(stream, CompressionLevel.Fastest)", true)] |
| 62 | + [TestCase("new DeflateStream(stream, CompressionLevel.Fastest, leaveOpen: true)", false)] |
| 63 | + [TestCase("new DeflateStream(stream, CompressionLevel.Fastest, leaveOpen: false)", true)] |
| 64 | + [TestCase("new GZipStream(stream, CompressionLevel.Fastest)", true)] |
| 65 | + [TestCase("new GZipStream(stream, CompressionLevel.Fastest, leaveOpen: true)", false)] |
| 66 | + [TestCase("new GZipStream(stream, CompressionLevel.Fastest, leaveOpen: false)", true)] |
| 67 | + [TestCase("new System.Net.Mail.Attachment(stream, string.Empty)", true)] |
| 68 | + public static void InLeaveOpen(string expression, bool stores) |
| 69 | + { |
| 70 | + var code = @" |
| 71 | +namespace N |
| 72 | +{ |
| 73 | + using System; |
| 74 | + using System.IO; |
| 75 | + using System.IO.Compression; |
| 76 | + using System.Security.Cryptography; |
| 77 | + using System.Text; |
| 78 | +
|
| 79 | + public class C |
| 80 | + { |
| 81 | + private readonly IDisposable disposable; |
| 82 | +
|
| 83 | + public C(Stream stream) |
| 84 | + { |
| 85 | + this.disposable = new BinaryReader(stream); |
| 86 | + } |
| 87 | + } |
| 88 | +}".AssertReplace("new BinaryReader(stream)", expression); |
| 89 | + var syntaxTree = CSharpSyntaxTree.ParseText(code); |
| 90 | + var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); |
| 91 | + var semanticModel = compilation.GetSemanticModel(syntaxTree); |
| 92 | + var value = syntaxTree.FindParameter("stream"); |
| 93 | + Assert.AreEqual(true, semanticModel.TryGetSymbol(value, CancellationToken.None, out var symbol)); |
| 94 | + Assert.AreEqual(true, LocalOrParameter.TryCreate(symbol, out var localOrParameter)); |
| 95 | + Assert.AreEqual(stores, Disposable.Stores(localOrParameter, semanticModel, CancellationToken.None, out var container)); |
| 96 | + Assert.AreEqual(stores, Disposable.DisposedByReturnValue(syntaxTree.FindArgument("stream"), semanticModel, CancellationToken.None, out _)); |
| 97 | + if (stores) |
| 98 | + { |
| 99 | + Assert.AreEqual("N.C.disposable", container.ToString()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [TestCase("new HttpClient(handler)", true)] |
| 104 | + [TestCase("new HttpClient(handler, disposeHandler: true)", true)] |
| 105 | + [TestCase("new HttpClient(handler, disposeHandler: false)", false)] |
| 106 | + public static void InHttpClient(string expression, bool stores) |
| 107 | + { |
| 108 | + var code = @" |
| 109 | +namespace N |
| 110 | +{ |
| 111 | + using System.Net.Http; |
| 112 | +
|
| 113 | + public class C |
| 114 | + { |
| 115 | + private readonly IDisposable disposable; |
| 116 | +
|
| 117 | + public C(HttpClientHandler handler) |
| 118 | + { |
| 119 | + this.disposable = new HttpClient(handler); |
| 120 | + } |
| 121 | + } |
| 122 | +}".AssertReplace("new HttpClient(handler)", expression); |
| 123 | + var syntaxTree = CSharpSyntaxTree.ParseText(code); |
| 124 | + var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); |
| 125 | + var semanticModel = compilation.GetSemanticModel(syntaxTree); |
| 126 | + var value = syntaxTree.FindParameter("handler"); |
| 127 | + Assert.AreEqual(true, semanticModel.TryGetSymbol(value, CancellationToken.None, out var symbol)); |
| 128 | + Assert.AreEqual(true, LocalOrParameter.TryCreate(symbol, out var localOrParameter)); |
| 129 | + Assert.AreEqual(stores, Disposable.Stores(localOrParameter, semanticModel, CancellationToken.None, out var container)); |
| 130 | + Assert.AreEqual(stores, Disposable.DisposedByReturnValue(syntaxTree.FindArgument("handler"), semanticModel, CancellationToken.None, out _)); |
| 131 | + if (stores) |
| 132 | + { |
| 133 | + Assert.AreEqual("N.C.disposable", container.ToString()); |
| 134 | + } |
| 135 | + } |
44 | 136 | } |
45 | 137 | } |
46 | 138 | } |
0 commit comments