| 1 | using System; |
| 2 | using System.IO; |
| 3 | using System.Reflection; |
| 4 | |
| 5 | using NUnit.Framework; |
| 6 | |
| 7 | using Mono.Cecil.PE; |
| 8 | |
| 9 | namespace Mono.Cecil.Tests { |
| 10 | |
| 11 | public abstract class BaseTestFixture { |
| 12 | |
| 13 | public static string GetResourcePath (string name, Assembly assembly) |
| 14 | { |
| 15 | return Path.Combine (FindResourcesDirectory (assembly), name); |
| 16 | } |
| 17 | |
| 18 | public static string GetAssemblyResourcePath (string name, Assembly assembly) |
| 19 | { |
| 20 | return GetResourcePath (Path.Combine ("assemblies", name), assembly); |
| 21 | } |
| 22 | |
| 23 | public static string GetCSharpResourcePath (string name, Assembly assembly) |
| 24 | { |
| 25 | return GetResourcePath (Path.Combine ("cs", name), assembly); |
| 26 | } |
| 27 | |
| 28 | public static string GetILResourcePath (string name, Assembly assembly) |
| 29 | { |
| 30 | return GetResourcePath (Path.Combine ("il", name), assembly); |
| 31 | } |
| 32 | |
| 33 | public static ModuleDefinition GetResourceModule (string name) |
| 34 | { |
| 35 | return ModuleDefinition.ReadModule (GetAssemblyResourcePath (name, typeof (BaseTestFixture).Assembly)); |
| 36 | } |
| 37 | |
| 38 | public static ModuleDefinition GetResourceModule (string name, ReaderParameters parameters) |
| 39 | { |
| 40 | return ModuleDefinition.ReadModule (GetAssemblyResourcePath (name, typeof (BaseTestFixture).Assembly), parameters); |
| 41 | } |
| 42 | |
| 43 | public static ModuleDefinition GetResourceModule (string name, ReadingMode mode) |
| 44 | { |
| 45 | return ModuleDefinition.ReadModule (GetAssemblyResourcePath (name, typeof (BaseTestFixture).Assembly), new ReaderParameters (mode)); |
| 46 | } |
| 47 | |
| 48 | internal static Image GetResourceImage (string name) |
| 49 | { |
| 50 | using (var fs = new FileStream (GetAssemblyResourcePath (name, typeof (BaseTestFixture).Assembly), FileMode.Open, FileAccess.Read)) |
| 51 | return ImageReader.ReadImageFrom (fs); |
| 52 | } |
| 53 | |
| 54 | public static ModuleDefinition GetCurrentModule () |
| 55 | { |
| 56 | return ModuleDefinition.ReadModule (typeof (BaseTestFixture).Module.FullyQualifiedName); |
| 57 | } |
| 58 | |
| 59 | public static ModuleDefinition GetCurrentModule (ReaderParameters parameters) |
| 60 | { |
| 61 | return ModuleDefinition.ReadModule (typeof (BaseTestFixture).Module.FullyQualifiedName, parameters); |
| 62 | } |
| 63 | |
| 64 | public static string FindResourcesDirectory (Assembly assembly) |
| 65 | { |
| 66 | var path = Path.GetDirectoryName (new Uri (assembly.CodeBase).LocalPath); |
| 67 | while (!Directory.Exists (Path.Combine (path, "Resources"))) { |
| 68 | var old = path; |
| 69 | path = Path.GetDirectoryName (path); |
| 70 | Assert.AreNotEqual (old, path); |
| 71 | } |
| 72 | |
| 73 | return Path.Combine (path, "Resources"); |
| 74 | } |
| 75 | } |
| 76 | } |