| 1 | using System; |
| 2 | using System.IO; |
| 3 | using System.Reflection; |
| 4 | |
| 5 | using NUnit.Core; |
| 6 | using NUnit.Core.Extensibility; |
| 7 | |
| 8 | using Mono.Cecil.Cil; |
| 9 | |
| 10 | namespace Mono.Cecil.Tests { |
| 11 | |
| 12 | public abstract class TestCecilAttribute : Attribute { |
| 13 | |
| 14 | bool verify = true; |
| 15 | Type symbol_reader_provider; |
| 16 | Type symbol_writer_provider; |
| 17 | |
| 18 | public bool Verify { |
| 19 | get { return verify; } |
| 20 | set { verify = value; } |
| 21 | } |
| 22 | |
| 23 | public Type SymbolReaderProvider { |
| 24 | get { return symbol_reader_provider; } |
| 25 | set { symbol_reader_provider = value; } |
| 26 | } |
| 27 | |
| 28 | public Type SymbolWriterProvider { |
| 29 | get { return symbol_writer_provider; } |
| 30 | set { symbol_writer_provider = value; } |
| 31 | } |
| 32 | |
| 33 | public abstract string GetModuleLocation (Assembly assembly); |
| 34 | } |
| 35 | |
| 36 | [AttributeUsage (AttributeTargets.Method, AllowMultiple = false)] |
| 37 | public sealed class TestModuleAttribute : TestCecilAttribute { |
| 38 | |
| 39 | readonly string module; |
| 40 | |
| 41 | public string Module { |
| 42 | get { return module; } |
| 43 | } |
| 44 | |
| 45 | public TestModuleAttribute (string assembly) |
| 46 | { |
| 47 | this.module = assembly; |
| 48 | } |
| 49 | |
| 50 | public override string GetModuleLocation (Assembly assembly) |
| 51 | { |
| 52 | return BaseTestFixture.GetAssemblyResourcePath (module, assembly); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | [AttributeUsage (AttributeTargets.Method, AllowMultiple = false)] |
| 57 | public sealed class TestCSharpAttribute : TestCecilAttribute { |
| 58 | |
| 59 | readonly string file; |
| 60 | |
| 61 | public string File { |
| 62 | get { return file; } |
| 63 | } |
| 64 | |
| 65 | public TestCSharpAttribute (string file) |
| 66 | { |
| 67 | this.file = file; |
| 68 | } |
| 69 | |
| 70 | public override string GetModuleLocation (Assembly assembly) |
| 71 | { |
| 72 | return CompilationService.CompileResource ( |
| 73 | BaseTestFixture.GetCSharpResourcePath (file, assembly)); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | [AttributeUsage (AttributeTargets.Method, AllowMultiple = false)] |
| 78 | public sealed class TestILAttribute : TestCecilAttribute { |
| 79 | |
| 80 | readonly string file; |
| 81 | |
| 82 | public string File { |
| 83 | get { return file; } |
| 84 | } |
| 85 | |
| 86 | public TestILAttribute (string file) |
| 87 | { |
| 88 | this.file = file; |
| 89 | } |
| 90 | |
| 91 | public override string GetModuleLocation (Assembly assembly) |
| 92 | { |
| 93 | return CompilationService.CompileResource ( |
| 94 | BaseTestFixture.GetILResourcePath (file, assembly)); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | class CecilTestCase : NUnitTestMethod { |
| 99 | |
| 100 | readonly TestCecilAttribute attribute; |
| 101 | readonly TestCaseType type; |
| 102 | |
| 103 | public CecilTestCase (MethodInfo method, TestCecilAttribute attribute, TestCaseType type) |
| 104 | : base (method) |
| 105 | { |
| 106 | this.TestName.Name = type.ToString (); |
| 107 | this.TestName.FullName = method.DeclaringType.FullName + "." + method.Name + "." + type; |
| 108 | this.attribute = attribute; |
| 109 | this.type = type; |
| 110 | } |
| 111 | |
| 112 | ModuleDefinition GetModule () |
| 113 | { |
| 114 | var location = attribute.GetModuleLocation (this.Method.DeclaringType.Assembly); |
| 115 | |
| 116 | var parameters = new ReaderParameters { |
| 117 | SymbolReaderProvider = GetSymbolReaderProvider (attribute), |
| 118 | }; |
| 119 | |
| 120 | switch (type) { |
| 121 | case TestCaseType.ReadImmediate: |
| 122 | parameters.ReadingMode = ReadingMode.Immediate; |
| 123 | return ModuleDefinition.ReadModule (location, parameters); |
| 124 | case TestCaseType.ReadDeferred: |
| 125 | parameters.ReadingMode = ReadingMode.Deferred; |
| 126 | return ModuleDefinition.ReadModule (location, parameters); |
| 127 | case TestCaseType.WriteFromImmediate: |
| 128 | parameters.ReadingMode = ReadingMode.Immediate; |
| 129 | return RoundTrip (location, parameters, "cecil-irt"); |
| 130 | case TestCaseType.WriteFromDeferred: |
| 131 | parameters.ReadingMode = ReadingMode.Deferred; |
| 132 | return RoundTrip (location, parameters, "cecil-drt"); |
| 133 | default: |
| 134 | return null; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static ISymbolReaderProvider GetSymbolReaderProvider (TestCecilAttribute attribute) |
| 139 | { |
| 140 | if (attribute.SymbolReaderProvider == null) |
| 141 | return null; |
| 142 | |
| 143 | return (ISymbolReaderProvider) Activator.CreateInstance (attribute.SymbolReaderProvider); |
| 144 | } |
| 145 | |
| 146 | static ISymbolWriterProvider GetSymbolWriterProvider (TestCecilAttribute attribute) |
| 147 | { |
| 148 | if (attribute.SymbolReaderProvider == null) |
| 149 | return null; |
| 150 | |
| 151 | return (ISymbolWriterProvider) Activator.CreateInstance (attribute.SymbolWriterProvider); |
| 152 | } |
| 153 | |
| 154 | ModuleDefinition RoundTrip (string location, ReaderParameters reader_parameters, string folder) |
| 155 | { |
| 156 | var module = ModuleDefinition.ReadModule (location, reader_parameters); |
| 157 | var rt_folder = Path.Combine (Path.GetTempPath (), folder); |
| 158 | if (!Directory.Exists (rt_folder)) |
| 159 | Directory.CreateDirectory (rt_folder); |
| 160 | var rt_module = Path.Combine (rt_folder, Path.GetFileName (location)); |
| 161 | |
| 162 | var writer_parameters = new WriterParameters { |
| 163 | SymbolWriterProvider = GetSymbolWriterProvider (attribute), |
| 164 | }; |
| 165 | |
| 166 | Reflect.InvokeMethod (Method, Fixture, new object [] { module }); |
| 167 | |
| 168 | module.Write (rt_module, writer_parameters); |
| 169 | |
| 170 | if (attribute.Verify) |
| 171 | CompilationService.Verify (rt_module); |
| 172 | |
| 173 | return ModuleDefinition.ReadModule (rt_module, reader_parameters); |
| 174 | } |
| 175 | |
| 176 | public override TestResult RunTest () |
| 177 | { |
| 178 | var result = new TestResult (TestName); |
| 179 | var module = GetModule (); |
| 180 | if (module == null) |
| 181 | return result; |
| 182 | |
| 183 | Reflect.InvokeMethod (Method, Fixture, new object [] { module }); |
| 184 | |
| 185 | result.Success (); |
| 186 | return result; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | class CecilTestSuite : TestSuite { |
| 191 | |
| 192 | public CecilTestSuite (MethodInfo method) |
| 193 | : base (method.DeclaringType.FullName, method.Name) |
| 194 | { |
| 195 | } |
| 196 | |
| 197 | public override TestResult Run (EventListener listener, ITestFilter filter) |
| 198 | { |
| 199 | if (this.Parent != null) |
| 200 | this.Fixture = this.Parent.Fixture; |
| 201 | |
| 202 | return base.Run (listener, filter); |
| 203 | } |
| 204 | |
| 205 | protected override void DoOneTimeSetUp (TestResult suiteResult) |
| 206 | { |
| 207 | } |
| 208 | |
| 209 | protected override void DoOneTimeTearDown (TestResult suiteResult) |
| 210 | { |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | enum TestCaseType { |
| 215 | ReadImmediate, |
| 216 | ReadDeferred, |
| 217 | WriteFromImmediate, |
| 218 | WriteFromDeferred, |
| 219 | } |
| 220 | |
| 221 | static class CecilTestFactory { |
| 222 | |
| 223 | public static CecilTestSuite CreateTestSuite (MethodInfo method) |
| 224 | { |
| 225 | if (method == null) |
| 226 | throw new ArgumentNullException ("method"); |
| 227 | |
| 228 | var suite = new CecilTestSuite (method); |
| 229 | |
| 230 | NUnitFramework.ApplyCommonAttributes (method, suite); |
| 231 | PopulateTestSuite (method, suite); |
| 232 | |
| 233 | return suite; |
| 234 | } |
| 235 | |
| 236 | static void PopulateTestSuite (MethodInfo method, CecilTestSuite suite) |
| 237 | { |
| 238 | var attribute = GetTestCecilAttribute (method); |
| 239 | if (attribute == null) |
| 240 | throw new ArgumentException (); |
| 241 | |
| 242 | foreach (var value in Enum.GetValues (typeof (TestCaseType))) { |
| 243 | var test = CreateTestCase (method, attribute, (TestCaseType) value); |
| 244 | if (test != null) |
| 245 | suite.Add (test); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | static CecilTestCase CreateTestCase (MethodInfo method, TestCecilAttribute attribute, TestCaseType type) |
| 250 | { |
| 251 | return new CecilTestCase (method, attribute, type); |
| 252 | } |
| 253 | |
| 254 | static TestCecilAttribute GetTestCecilAttribute (MethodInfo method) |
| 255 | { |
| 256 | foreach (var attribute in method.GetCustomAttributes (false)) { |
| 257 | var test = attribute as TestCecilAttribute; |
| 258 | if (test != null) |
| 259 | return test; |
| 260 | } |
| 261 | |
| 262 | return null; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | [NUnitAddin] |
| 267 | public class CecilTestAddin : IAddin, ITestCaseBuilder { |
| 268 | |
| 269 | public bool Install (IExtensionHost host) |
| 270 | { |
| 271 | if (host == null) |
| 272 | throw new ArgumentNullException ("host"); |
| 273 | |
| 274 | var builders = host.GetExtensionPoint ("TestCaseBuilders"); |
| 275 | if (builders == null) |
| 276 | return false; |
| 277 | |
| 278 | builders.Install (this); |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | public Test BuildFrom (MethodInfo method) |
| 283 | { |
| 284 | if (method == null) |
| 285 | throw new ArgumentNullException ("method"); |
| 286 | |
| 287 | return CecilTestFactory.CreateTestSuite (method); |
| 288 | } |
| 289 | |
| 290 | public bool CanBuildFrom (MethodInfo method) |
| 291 | { |
| 292 | if (method == null) |
| 293 | return false; |
| 294 | |
| 295 | return IsCecilTestMethod (method); |
| 296 | } |
| 297 | |
| 298 | static bool IsCecilTestMethod (MethodInfo method) |
| 299 | { |
| 300 | return Reflect.HasAttribute (method, typeof (TestModuleAttribute).FullName, false) |
| 301 | || Reflect.HasAttribute (method, typeof (TestILAttribute).FullName, false) |
| 302 | || Reflect.HasAttribute (method, typeof (TestCSharpAttribute).FullName, false); |
| 303 | } |
| 304 | } |
| 305 | } |