From 666ff3b5f3fbdd01fb5b03c597493ebeb64cba89 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 24 Sep 2017 23:18:10 +0200 Subject: [PATCH] [using] Add more test cases. --- .../TestCases/Correctness/Using.cs | 65 +++++++++++++++++-- .../TestCases/Pretty/Using.cs | 14 ++++ 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Correctness/Using.cs b/ICSharpCode.Decompiler.Tests/TestCases/Correctness/Using.cs index 537cf6aa1..bbc286dd6 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Correctness/Using.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Correctness/Using.cs @@ -40,9 +40,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness static void Main() { SimpleUsingNullStatement(); - NoUsing(); - NoUsing2(); - ThisIsNotAUsingBlock(); + NoUsingDueToAssignment(); + NoUsingDueToAssignment2(); + NoUsingDueToByRefCall(); + NoUsingDueToContinuedDisposableUse(); + ContinuedObjectUse(); UsingObject(); } @@ -58,7 +60,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness Console.WriteLine("after using"); } - public static void NoUsing() + public static void NoUsingDueToAssignment() { PrintOnDispose printOnDispose = new PrintOnDispose("Wrong"); try { @@ -68,7 +70,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness } } - public static void NoUsing2() + public static void NoUsingDueToAssignment2() { PrintOnDispose printOnDispose = new PrintOnDispose("NoUsing(): Wrong"); try { @@ -81,12 +83,31 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness } } - public static void ThisIsNotAUsingBlock() + static void Clear(ref T t) + { + t = default(T); + } + + public static void NoUsingDueToByRefCall() + { + PrintOnDispose printOnDispose = new PrintOnDispose("NoUsingDueToByRefCall(): Wrong"); + try { + Console.WriteLine("NoUsingDueToByRefCall"); + Clear(ref printOnDispose); + } finally { + IDisposable disposable = (object)printOnDispose as IDisposable; + if (disposable != null) { + disposable.Dispose(); + } + } + } + + public static void NoUsingDueToContinuedDisposableUse() { var obj = new System.IO.StringWriter(); IDisposable disposable; try { - obj.WriteLine("ThisIsNotAUsingBlock"); + obj.WriteLine("NoUsingDueToContinuedDisposableUse"); } finally { disposable = (object)obj as IDisposable; if (disposable != null) { @@ -96,6 +117,36 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness Console.WriteLine(disposable); } + public static void ContinuedObjectUse() + { + var obj = new System.IO.StringWriter(); + try { + obj.WriteLine("ContinuedObjectUse"); + } finally { + IDisposable disposable = (object)obj as IDisposable; + if (disposable != null) { + disposable.Dispose(); + } + } + Console.WriteLine(obj); + } + + public static void VariableAlreadyUsedBefore() + { + System.IO.StringWriter obj = new System.IO.StringWriter(); + obj.Write("VariableAlreadyUsedBefore - 1"); + Console.WriteLine(obj); + obj = new System.IO.StringWriter(); + try { + obj.WriteLine("VariableAlreadyUsedBefore - 2"); + } finally { + IDisposable disposable = (object)obj as IDisposable; + if (disposable != null) { + disposable.Dispose(); + } + } + } + public static void UsingObject() { object obj = new object(); diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.cs index d8f1a79b8..3f615c56b 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.cs @@ -86,6 +86,13 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty } } + private void UsingStatementOnNullableStruct(UsingStruct? us) + { + using (us) { + Console.WriteLine("using-body: " + us); + } + } + public void GenericUsing(T t) where T : IDisposable { using (t) { @@ -106,5 +113,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty Console.WriteLine(t); } } + + public void GenericNullableUsing(T? t) where T : struct, IDisposable + { + using (t) { + Console.WriteLine(t); + } + } } }