diff options
Diffstat (limited to 'Test')
-rw-r--r-- | Test/Mono.Cecil.Tests/Addin.cs | 33 | ||||
-rw-r--r-- | Test/Mono.Cecil.Tests/CustomAttributesTests.cs | 2 | ||||
-rw-r--r-- | Test/Mono.Cecil.Tests/Extensions.cs | 3 | ||||
-rw-r--r-- | Test/Mono.Cecil.Tests/ImageReadTests.cs | 120 | ||||
-rw-r--r-- | Test/Mono.Cecil.Tests/ImportCecilTests.cs | 9 | ||||
-rw-r--r-- | Test/Mono.Cecil.Tests/ImportReflectionTests.cs | 82 | ||||
-rw-r--r-- | Test/Mono.Cecil.Tests/ModuleTests.cs | 40 | ||||
-rw-r--r-- | Test/Mono.Cecil.Tests/ResolveTests.cs | 11 |
8 files changed, 148 insertions, 152 deletions
diff --git a/Test/Mono.Cecil.Tests/Addin.cs b/Test/Mono.Cecil.Tests/Addin.cs index 38b02ce..469d158 100644 --- a/Test/Mono.Cecil.Tests/Addin.cs +++ b/Test/Mono.Cecil.Tests/Addin.cs @@ -153,19 +153,20 @@ namespace Mono.Cecil.Tests { ModuleDefinition RoundTrip (string location, ReaderParameters reader_parameters, string folder) { - var module = ModuleDefinition.ReadModule (location, reader_parameters); - var rt_folder = Path.Combine (Path.GetTempPath (), folder); - if (!Directory.Exists (rt_folder)) - Directory.CreateDirectory (rt_folder); - var rt_module = Path.Combine (rt_folder, Path.GetFileName (location)); - - var writer_parameters = new WriterParameters { - SymbolWriterProvider = GetSymbolWriterProvider (attribute), - }; + var rt_folder = Path.Combine(Path.GetTempPath(), folder); + if (!Directory.Exists(rt_folder)) + Directory.CreateDirectory(rt_folder); + var rt_module = Path.Combine(rt_folder, Path.GetFileName(location)); + + using (var module = ModuleDefinition.ReadModule (location, reader_parameters)) { + var writer_parameters = new WriterParameters { + SymbolWriterProvider = GetSymbolWriterProvider (attribute), + }; - Reflect.InvokeMethod (Method, Fixture, new object [] { module }); + Reflect.InvokeMethod (Method, Fixture, new object [] { module }); - module.Write (rt_module, writer_parameters); + module.Write (rt_module, writer_parameters); + } if (attribute.Verify) CompilationService.Verify (rt_module); @@ -176,11 +177,13 @@ namespace Mono.Cecil.Tests { public override TestResult RunTest () { var result = new TestResult (TestName); - var module = GetModule (); - if (module == null) - return result; - Reflect.InvokeMethod (Method, Fixture, new object [] { module }); + using (var module = GetModule ()) { + if (module == null) + return result; + + Reflect.InvokeMethod (Method, Fixture, new object [] { module }); + } result.Success (); return result; diff --git a/Test/Mono.Cecil.Tests/CustomAttributesTests.cs b/Test/Mono.Cecil.Tests/CustomAttributesTests.cs index 6565ba6..ab8897d 100644 --- a/Test/Mono.Cecil.Tests/CustomAttributesTests.cs +++ b/Test/Mono.Cecil.Tests/CustomAttributesTests.cs @@ -376,6 +376,8 @@ namespace Mono.Cecil.Tests { Assert.IsNotNull (attribute); Assert.AreEqual ("CaBlob", (string) attribute.ConstructorArguments [0].Value); + + module.Dispose (); } static void AssertCustomAttribute (string expected, CustomAttribute attribute) diff --git a/Test/Mono.Cecil.Tests/Extensions.cs b/Test/Mono.Cecil.Tests/Extensions.cs index 0788d21..ab27e9b 100644 --- a/Test/Mono.Cecil.Tests/Extensions.cs +++ b/Test/Mono.Cecil.Tests/Extensions.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using SR = System.Reflection; @@ -20,7 +21,7 @@ namespace Mono.Cecil.Tests { public static TypeDefinition ToDefinition (this Type self) { - var module = ModuleDefinition.ReadModule (self.Module.FullyQualifiedName); + var module = ModuleDefinition.ReadModule (new MemoryStream (File.ReadAllBytes (self.Module.FullyQualifiedName))); return (TypeDefinition) module.LookupToken (self.MetadataToken); } diff --git a/Test/Mono.Cecil.Tests/ImageReadTests.cs b/Test/Mono.Cecil.Tests/ImageReadTests.cs index c601fc9..f2db8b1 100644 --- a/Test/Mono.Cecil.Tests/ImageReadTests.cs +++ b/Test/Mono.Cecil.Tests/ImageReadTests.cs @@ -15,113 +15,115 @@ namespace Mono.Cecil.Tests { [Test] public void ImageSections () { - var image = GetResourceImage ("hello.exe"); - - Assert.AreEqual (3, image.Sections.Length); - Assert.AreEqual (".text", image.Sections [0].Name); - Assert.AreEqual (".rsrc", image.Sections [1].Name); - Assert.AreEqual (".reloc", image.Sections [2].Name); + using (var image = GetResourceImage ("hello.exe")) { + Assert.AreEqual (3, image.Sections.Length); + Assert.AreEqual (".text", image.Sections [0].Name); + Assert.AreEqual (".rsrc", image.Sections [1].Name); + Assert.AreEqual (".reloc", image.Sections [2].Name); + } } [Test] public void ImageMetadataVersion () { - var image = GetResourceImage ("hello.exe"); - Assert.AreEqual (TargetRuntime.Net_2_0, image.Runtime); + using (var image = GetResourceImage ("hello.exe")) + Assert.AreEqual (TargetRuntime.Net_2_0, image.Runtime); - image = GetResourceImage ("hello1.exe"); - Assert.AreEqual (TargetRuntime.Net_1_1, image.Runtime); + using (var image = GetResourceImage ("hello1.exe")) + Assert.AreEqual (TargetRuntime.Net_1_1, image.Runtime); } [Test] public void ImageModuleKind () { - var image = GetResourceImage ("hello.exe"); - Assert.AreEqual (ModuleKind.Console, image.Kind); + using (var image = GetResourceImage ("hello.exe")) + Assert.AreEqual (ModuleKind.Console, image.Kind); - image = GetResourceImage ("libhello.dll"); - Assert.AreEqual (ModuleKind.Dll, image.Kind); + using (var image = GetResourceImage ("libhello.dll")) + Assert.AreEqual (ModuleKind.Dll, image.Kind); - image = GetResourceImage ("hellow.exe"); - Assert.AreEqual (ModuleKind.Windows, image.Kind); + using (var image = GetResourceImage ("hellow.exe")) + Assert.AreEqual (ModuleKind.Windows, image.Kind); } [Test] public void MetadataHeaps () { - var image = GetResourceImage ("hello.exe"); - - Assert.IsNotNull (image.TableHeap); + using (var image = GetResourceImage ("hello.exe")) { + Assert.IsNotNull (image.TableHeap); - Assert.IsNotNull (image.StringHeap); - Assert.AreEqual (string.Empty, image.StringHeap.Read (0)); - Assert.AreEqual ("<Module>", image.StringHeap.Read (1)); + Assert.IsNotNull (image.StringHeap); + Assert.AreEqual (string.Empty, image.StringHeap.Read (0)); + Assert.AreEqual ("<Module>", image.StringHeap.Read (1)); - Assert.IsNotNull (image.UserStringHeap); - Assert.AreEqual (string.Empty, image.UserStringHeap.Read (0)); - Assert.AreEqual ("Hello Cecil World !", image.UserStringHeap.Read (1)); + Assert.IsNotNull (image.UserStringHeap); + Assert.AreEqual (string.Empty, image.UserStringHeap.Read (0)); + Assert.AreEqual ("Hello Cecil World !", image.UserStringHeap.Read (1)); - Assert.IsNotNull (image.GuidHeap); - Assert.AreEqual (new Guid (), image.GuidHeap.Read (0)); - Assert.AreEqual (new Guid ("C3BC2BD3-2576-4D00-A80E-465B5632415F"), image.GuidHeap.Read (1)); + Assert.IsNotNull (image.GuidHeap); + Assert.AreEqual (new Guid (), image.GuidHeap.Read (0)); + Assert.AreEqual (new Guid ("C3BC2BD3-2576-4D00-A80E-465B5632415F"), image.GuidHeap.Read (1)); - Assert.IsNotNull (image.BlobHeap); - Assert.AreEqual (new byte [0], image.BlobHeap.Read (0)); + Assert.IsNotNull (image.BlobHeap); + Assert.AreEqual (new byte [0], image.BlobHeap.Read (0)); + } } [Test] public void TablesHeap () { - var image = GetResourceImage ("hello.exe"); - var heap = image.TableHeap; - - Assert.IsNotNull (heap); - - Assert.AreEqual (1, heap [Table.Module].Length); - Assert.AreEqual (4, heap [Table.TypeRef].Length); - Assert.AreEqual (2, heap [Table.TypeDef].Length); - Assert.AreEqual (0, heap [Table.Field].Length); - Assert.AreEqual (2, heap [Table.Method].Length); - Assert.AreEqual (4, heap [Table.MemberRef].Length); - Assert.AreEqual (2, heap [Table.CustomAttribute].Length); - Assert.AreEqual (1, heap [Table.Assembly].Length); - Assert.AreEqual (1, heap [Table.AssemblyRef].Length); + using (var image = GetResourceImage ("hello.exe")) { + var heap = image.TableHeap; + + Assert.IsNotNull (heap); + + Assert.AreEqual (1, heap [Table.Module].Length); + Assert.AreEqual (4, heap [Table.TypeRef].Length); + Assert.AreEqual (2, heap [Table.TypeDef].Length); + Assert.AreEqual (0, heap [Table.Field].Length); + Assert.AreEqual (2, heap [Table.Method].Length); + Assert.AreEqual (4, heap [Table.MemberRef].Length); + Assert.AreEqual (2, heap [Table.CustomAttribute].Length); + Assert.AreEqual (1, heap [Table.Assembly].Length); + Assert.AreEqual (1, heap [Table.AssemblyRef].Length); + } } [Test] public void ReadX64Image () { - var image = GetResourceImage ("hello.x64.exe"); + using (var image = GetResourceImage ("hello.x64.exe")) { - Assert.AreEqual (TargetArchitecture.AMD64, image.Architecture); - Assert.AreEqual (ModuleAttributes.ILOnly, image.Attributes); + Assert.AreEqual (TargetArchitecture.AMD64, image.Architecture); + Assert.AreEqual (ModuleAttributes.ILOnly, image.Attributes); + } } [Test] public void ReadIA64Image () { - var image = GetResourceImage ("hello.ia64.exe"); - - Assert.AreEqual (TargetArchitecture.IA64, image.Architecture); - Assert.AreEqual (ModuleAttributes.ILOnly, image.Attributes); + using (var image = GetResourceImage ("hello.ia64.exe")) { + Assert.AreEqual (TargetArchitecture.IA64, image.Architecture); + Assert.AreEqual (ModuleAttributes.ILOnly, image.Attributes); + } } [Test] public void ReadX86Image () { - var image = GetResourceImage ("hello.x86.exe"); - - Assert.AreEqual (TargetArchitecture.I386, image.Architecture); - Assert.AreEqual (ModuleAttributes.ILOnly | ModuleAttributes.Required32Bit, image.Attributes); + using (var image = GetResourceImage ("hello.x86.exe")) { + Assert.AreEqual (TargetArchitecture.I386, image.Architecture); + Assert.AreEqual (ModuleAttributes.ILOnly | ModuleAttributes.Required32Bit, image.Attributes); + } } [Test] public void ReadAnyCpuImage () { - var image = GetResourceImage ("hello.anycpu.exe"); - - Assert.AreEqual (TargetArchitecture.I386, image.Architecture); - Assert.AreEqual (ModuleAttributes.ILOnly, image.Attributes); + using (var image = GetResourceImage ("hello.anycpu.exe")) { + Assert.AreEqual (TargetArchitecture.I386, image.Architecture); + Assert.AreEqual (ModuleAttributes.ILOnly, image.Attributes); + } } } } diff --git a/Test/Mono.Cecil.Tests/ImportCecilTests.cs b/Test/Mono.Cecil.Tests/ImportCecilTests.cs index 3fa0406..7554863 100644 --- a/Test/Mono.Cecil.Tests/ImportCecilTests.cs +++ b/Test/Mono.Cecil.Tests/ImportCecilTests.cs @@ -213,11 +213,10 @@ namespace Mono.Cecil.Tests { public void ImportMethodOnOpenGeneric () { var generic = typeof (Generic<>).ToDefinition (); - var module = ModuleDefinition.CreateModule ("foo", ModuleKind.Dll); - - var method = module.Import (generic.GetMethod ("Method")); - - Assert.AreEqual ("T Mono.Cecil.Tests.ImportCecilTests/Generic`1::Method(T)", method.FullName); + using (var module = ModuleDefinition.CreateModule ("foo", ModuleKind.Dll)) { + var method = module.Import (generic.GetMethod ("Method")); + Assert.AreEqual ("T Mono.Cecil.Tests.ImportCecilTests/Generic`1::Method(T)", method.FullName); + } } delegate void Emitter (ModuleDefinition module, MethodBody body); diff --git a/Test/Mono.Cecil.Tests/ImportReflectionTests.cs b/Test/Mono.Cecil.Tests/ImportReflectionTests.cs index dd1d28a..ffb2619 100644 --- a/Test/Mono.Cecil.Tests/ImportReflectionTests.cs +++ b/Test/Mono.Cecil.Tests/ImportReflectionTests.cs @@ -209,13 +209,13 @@ namespace Mono.Cecil.Tests { [Test] public void ImportGenericTypeDefOrOpen () { - var module = typeof (Foo<>).ToDefinition ().Module; + using (var module = typeof (Foo<>).ToDefinition ().Module) { + var foo_def = module.Import (typeof (Foo<>)); + var foo_open = module.Import (typeof (Foo<>), foo_def); - var foo_def = module.Import (typeof (Foo<>)); - var foo_open = module.Import (typeof (Foo<>), foo_def); - - Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1", foo_def.FullName); - Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>", foo_open.FullName); + Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1", foo_def.FullName); + Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>", foo_open.FullName); + } } [Test] @@ -225,12 +225,12 @@ namespace Mono.Cecil.Tests { var generic_list_foo_open = typeof (Generic<>).MakeGenericType (list_foo); var foo_def = typeof (Foo<>).ToDefinition (); - var module = foo_def.Module; + using (var module = foo_def.Module) { + var generic_foo = module.Import (generic_list_foo_open, foo_def); - var generic_foo = module.Import (generic_list_foo_open, foo_def); - - Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>", - generic_foo.FullName); + Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>", + generic_foo.FullName); + } } [Test] @@ -240,12 +240,12 @@ namespace Mono.Cecil.Tests { var generic_foo_open = typeof (Generic<>).MakeGenericType (foo_open); var foo_def = typeof (Foo<>).ToDefinition (); - var module = foo_def.Module; + using (var module = foo_def.Module) { + var generic_foo = module.Import (generic_foo_open, foo_def); - var generic_foo = module.Import (generic_foo_open, foo_def); - - Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>>", - generic_foo.FullName); + Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>>", + generic_foo.FullName); + } } [Test] @@ -255,12 +255,12 @@ namespace Mono.Cecil.Tests { var foo_open_array = foo_open.MakeArrayType (); var foo_def = typeof (Foo<>).ToDefinition (); - var module = foo_def.Module; + using (var module = foo_def.Module) { + var array_foo = module.Import (foo_open_array, foo_def); - var array_foo = module.Import (foo_open_array, foo_def); - - Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>[]", - array_foo.FullName); + Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>[]", + array_foo.FullName); + } } [Test] @@ -271,12 +271,12 @@ namespace Mono.Cecil.Tests { var generic_list_foo_open_field = generic_list_foo_open.GetField ("Field"); var foo_def = typeof (Foo<>).ToDefinition (); - var module = foo_def.Module; + using (var module = foo_def.Module) { + var generic_field = module.Import (generic_list_foo_open_field, foo_def); - var generic_field = module.Import (generic_list_foo_open_field, foo_def); - - Assert.AreEqual ("TFoo Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Field", - generic_field.FullName); + Assert.AreEqual ("TFoo Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Field", + generic_field.FullName); + } } [Test] @@ -287,36 +287,36 @@ namespace Mono.Cecil.Tests { var generic_list_foo_open_method = generic_list_foo_open.GetMethod ("Method"); var foo_def = typeof (Foo<>).ToDefinition (); - var module = foo_def.Module; + using (var module = foo_def.Module) { + var generic_method = module.Import (generic_list_foo_open_method, foo_def); - var generic_method = module.Import (generic_list_foo_open_method, foo_def); - - Assert.AreEqual ("TFoo Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Method(TFoo)", - generic_method.FullName); + Assert.AreEqual ("TFoo Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Method(TFoo)", + generic_method.FullName); + } } [Test] public void ImportMethodOnOpenGenericType () { - var module = typeof (Generic<>).ToDefinition ().Module; + using (var module = typeof (Generic<>).ToDefinition ().Module) { + var method = module.Import (typeof (Generic<>).GetMethod ("Method")); - var method = module.Import (typeof (Generic<>).GetMethod ("Method")); - - Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::Method(T)", method.FullName); + Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::Method(T)", method.FullName); + } } [Test] public void ImportGenericMethodOnOpenGenericType () { - var module = typeof (Generic<>).ToDefinition ().Module; + using (var module = typeof (Generic<>).ToDefinition ().Module) { + var generic_method = module.Import (typeof (Generic<>).GetMethod ("GenericMethod")); - var generic_method = module.Import (typeof (Generic<>).GetMethod ("GenericMethod")); + Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod(T,TS)", generic_method.FullName); - Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod(T,TS)", generic_method.FullName); + generic_method = module.Import (typeof (Generic<>).GetMethod ("GenericMethod"), generic_method); - generic_method = module.Import (typeof (Generic<>).GetMethod ("GenericMethod"), generic_method); - - Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod<TS>(T,TS)", generic_method.FullName); + Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod<TS>(T,TS)", generic_method.FullName); + } } delegate void Emitter (ModuleDefinition module, MethodBody body); diff --git a/Test/Mono.Cecil.Tests/ModuleTests.cs b/Test/Mono.Cecil.Tests/ModuleTests.cs index 00f9e1a..ee0b3d1 100644 --- a/Test/Mono.Cecil.Tests/ModuleTests.cs +++ b/Test/Mono.Cecil.Tests/ModuleTests.cs @@ -197,10 +197,10 @@ namespace Mono.Cecil.Tests { [Test] public void MixedModeModule () { - var module = GetResourceModule ("cppcli.dll"); - - Assert.AreEqual (1, module.ModuleReferences.Count); - Assert.AreEqual (string.Empty, module.ModuleReferences [0].Name); + using (var module = GetResourceModule ("cppcli.dll")) { + Assert.AreEqual (1, module.ModuleReferences.Count); + Assert.AreEqual (string.Empty, module.ModuleReferences [0].Name); + } } [Test] @@ -211,40 +211,28 @@ namespace Mono.Cecil.Tests { } [Test] - public void WriteModuleTwice () - { - var module = GetResourceModule ("iterator.exe"); - - var path = Path.Combine (Path.GetTempPath (), "cecil"); - var file = Path.Combine (path, "iteratorrt.exe"); - - module.Write (file); - module.Write (file); - } - - [Test] public void GetTypeNamespacePlusName () { - var module = GetResourceModule ("moda.netmodule"); - - var type = module.GetType ("Module.A", "Foo"); - Assert.IsNotNull (type); + using (var module = GetResourceModule ("moda.netmodule")) { + var type = module.GetType ("Module.A", "Foo"); + Assert.IsNotNull (type); + } } [Test] public void OpenModuleImmediate () { - var module = GetResourceModule ("hello.exe", ReadingMode.Immediate); - - Assert.AreEqual (ReadingMode.Immediate, module.ReadingMode); + using (var module = GetResourceModule ("hello.exe", ReadingMode.Immediate)) { + Assert.AreEqual (ReadingMode.Immediate, module.ReadingMode); + } } [Test] public void OpenModuleDeferred () { - var module = GetResourceModule ("hello.exe", ReadingMode.Deferred); - - Assert.AreEqual (ReadingMode.Deferred, module.ReadingMode); + using (var module = GetResourceModule ("hello.exe", ReadingMode.Deferred)) { + Assert.AreEqual (ReadingMode.Deferred, module.ReadingMode); + } } } } diff --git a/Test/Mono.Cecil.Tests/ResolveTests.cs b/Test/Mono.Cecil.Tests/ResolveTests.cs index 9ec1be8..ca0cd56 100644 --- a/Test/Mono.Cecil.Tests/ResolveTests.cs +++ b/Test/Mono.Cecil.Tests/ResolveTests.cs @@ -113,12 +113,13 @@ namespace Mono.Cecil.Tests { resolver.Register (mma.Assembly); - var current_module = GetCurrentModule (parameters); - var reference = new TypeReference ("Module.A", "Foo", current_module, AssemblyNameReference.Parse (mma.Assembly.FullName), false); + using (var current_module = GetCurrentModule (parameters)) { + var reference = new TypeReference ("Module.A", "Foo", current_module, AssemblyNameReference.Parse (mma.Assembly.FullName), false); - var definition = reference.Resolve (); - Assert.IsNotNull (definition); - Assert.AreEqual ("Module.A.Foo", definition.FullName); + var definition = reference.Resolve (); + Assert.IsNotNull (definition); + Assert.AreEqual ("Module.A.Foo", definition.FullName); + } } [Test] |