Browse Source

[using] Add more test cases.

pull/877/head
Daniel Grunwald 8 years ago
parent
commit
666ff3b5f3
  1. 65
      ICSharpCode.Decompiler.Tests/TestCases/Correctness/Using.cs
  2. 14
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.cs

65
ICSharpCode.Decompiler.Tests/TestCases/Correctness/Using.cs

@ -40,9 +40,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
static void Main() static void Main()
{ {
SimpleUsingNullStatement(); SimpleUsingNullStatement();
NoUsing(); NoUsingDueToAssignment();
NoUsing2(); NoUsingDueToAssignment2();
ThisIsNotAUsingBlock(); NoUsingDueToByRefCall();
NoUsingDueToContinuedDisposableUse();
ContinuedObjectUse();
UsingObject(); UsingObject();
} }
@ -58,7 +60,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
Console.WriteLine("after using"); Console.WriteLine("after using");
} }
public static void NoUsing() public static void NoUsingDueToAssignment()
{ {
PrintOnDispose printOnDispose = new PrintOnDispose("Wrong"); PrintOnDispose printOnDispose = new PrintOnDispose("Wrong");
try { 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"); PrintOnDispose printOnDispose = new PrintOnDispose("NoUsing(): Wrong");
try { try {
@ -81,12 +83,31 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
} }
} }
public static void ThisIsNotAUsingBlock() static void Clear<T>(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(); var obj = new System.IO.StringWriter();
IDisposable disposable; IDisposable disposable;
try { try {
obj.WriteLine("ThisIsNotAUsingBlock"); obj.WriteLine("NoUsingDueToContinuedDisposableUse");
} finally { } finally {
disposable = (object)obj as IDisposable; disposable = (object)obj as IDisposable;
if (disposable != null) { if (disposable != null) {
@ -96,6 +117,36 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Correctness
Console.WriteLine(disposable); 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() public static void UsingObject()
{ {
object obj = new object(); object obj = new object();

14
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 t) where T : IDisposable public void GenericUsing<T>(T t) where T : IDisposable
{ {
using (t) { using (t) {
@ -106,5 +113,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
Console.WriteLine(t); Console.WriteLine(t);
} }
} }
public void GenericNullableUsing<T>(T? t) where T : struct, IDisposable
{
using (t) {
Console.WriteLine(t);
}
}
} }
} }

Loading…
Cancel
Save