You've already forked linux-packaging-mono
Imported Upstream version 5.0.0.42
Former-commit-id: fd56571888259555122d8a0f58c68838229cea2b
This commit is contained in:
parent
1190d13a04
commit
6bdd276d05
6
external/linker/cecil/Test/Mono.Cecil.Tests/AssemblyInfo.cs
vendored
Normal file
6
external/linker/cecil/Test/Mono.Cecil.Tests/AssemblyInfo.cs
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
[assembly: AssemblyTitle ("Mono.Cecil.Tests")]
|
||||
|
||||
[assembly: Guid ("da96c202-696a-457e-89af-5fa74e6bda0d")]
|
||||
87
external/linker/cecil/Test/Mono.Cecil.Tests/AssemblyTests.cs
vendored
Normal file
87
external/linker/cecil/Test/Mono.Cecil.Tests/AssemblyTests.cs
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
|
||||
using Mono.Cecil;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class AssemblyTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void Name ()
|
||||
{
|
||||
TestModule ("hello.exe", module => {
|
||||
var name = module.Assembly.Name;
|
||||
|
||||
Assert.IsNotNull (name);
|
||||
|
||||
Assert.AreEqual ("hello", name.Name);
|
||||
Assert.AreEqual ("", name.Culture);
|
||||
Assert.AreEqual (new Version (0, 0, 0, 0), name.Version);
|
||||
Assert.AreEqual (AssemblyHashAlgorithm.SHA1, name.HashAlgorithm);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParseLowerCaseNameParts ()
|
||||
{
|
||||
var name = AssemblyNameReference.Parse ("Foo, version=2.0.0.0, culture=fr-FR");
|
||||
Assert.AreEqual ("Foo", name.Name);
|
||||
Assert.AreEqual (2, name.Version.Major);
|
||||
Assert.AreEqual (0, name.Version.Minor);
|
||||
Assert.AreEqual ("fr-FR", name.Culture);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ZeroVersion ()
|
||||
{
|
||||
var name = new AssemblyNameReference ("Foo", null);
|
||||
Assert.AreEqual ("0.0.0.0", name.Version.ToString (fieldCount: 4));
|
||||
Assert.AreEqual ("Foo, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", name.FullName);
|
||||
|
||||
name = new AssemblyNameReference ("Foo", new Version (0, 0, 0, 0));
|
||||
Assert.AreEqual ("0.0.0.0", name.Version.ToString (fieldCount: 4));
|
||||
Assert.AreEqual ("Foo, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", name.FullName);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NoBuildOrMajor ()
|
||||
{
|
||||
var name = new AssemblyNameReference ("Foo", new Version (0, 0));
|
||||
Assert.AreEqual ("Foo, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", name.FullName);
|
||||
|
||||
name = new AssemblyNameReference ("Foo", new Version (0, 0, 0));
|
||||
Assert.AreEqual ("Foo, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", name.FullName);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Retargetable ()
|
||||
{
|
||||
TestModule ("RetargetableExample.dll", module => {
|
||||
var type = module.Types [1];
|
||||
var property = type.Properties [0];
|
||||
var attribute = property.CustomAttributes [0];
|
||||
|
||||
var argumentType = ((CustomAttributeArgument) attribute.ConstructorArguments [0].Value).Type;
|
||||
var reference = (AssemblyNameReference) argumentType.Scope;
|
||||
|
||||
Assert.AreEqual (
|
||||
"System.Data, Version=3.5.0.0, Culture=neutral, PublicKeyToken=969db8053d3322ac, Retargetable=Yes",
|
||||
reference.FullName);
|
||||
}, verify: !Platform.OnMono);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SystemRuntime ()
|
||||
{
|
||||
TestModule ("System.Runtime.dll", module => {
|
||||
Assert.AreEqual ("System.Runtime", module.Assembly.Name.Name);
|
||||
Assert.AreEqual (1, module.AssemblyReferences.Count);
|
||||
Assert.AreNotEqual (module, module.TypeSystem.CoreLibrary);
|
||||
Assert.AreEqual (module.AssemblyReferences [0], module.TypeSystem.CoreLibrary);
|
||||
}, verify: !Platform.OnMono);
|
||||
}
|
||||
}
|
||||
}
|
||||
327
external/linker/cecil/Test/Mono.Cecil.Tests/BaseTestFixture.cs
vendored
Normal file
327
external/linker/cecil/Test/Mono.Cecil.Tests/BaseTestFixture.cs
vendored
Normal file
@@ -0,0 +1,327 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using Mono.Cecil.Cil;
|
||||
using NUnit.Framework;
|
||||
|
||||
using Mono.Cecil.PE;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
public abstract class BaseTestFixture {
|
||||
|
||||
protected static void IgnoreOnMono ()
|
||||
{
|
||||
if (Platform.OnMono)
|
||||
Assert.Ignore ();
|
||||
}
|
||||
|
||||
public static string GetResourcePath (string name, Assembly assembly)
|
||||
{
|
||||
return Path.Combine (FindResourcesDirectory (assembly), name);
|
||||
}
|
||||
|
||||
public static string GetAssemblyResourcePath (string name, Assembly assembly)
|
||||
{
|
||||
return GetResourcePath (Path.Combine ("assemblies", name), assembly);
|
||||
}
|
||||
|
||||
public static string GetCSharpResourcePath (string name, Assembly assembly)
|
||||
{
|
||||
return GetResourcePath (Path.Combine ("cs", name), assembly);
|
||||
}
|
||||
|
||||
public static string GetILResourcePath (string name, Assembly assembly)
|
||||
{
|
||||
return GetResourcePath (Path.Combine ("il", name), assembly);
|
||||
}
|
||||
|
||||
public ModuleDefinition GetResourceModule (string name)
|
||||
{
|
||||
return ModuleDefinition.ReadModule (GetAssemblyResourcePath (name, GetType ().Assembly));
|
||||
}
|
||||
|
||||
public ModuleDefinition GetResourceModule (string name, ReaderParameters parameters)
|
||||
{
|
||||
return ModuleDefinition.ReadModule (GetAssemblyResourcePath (name, GetType ().Assembly), parameters);
|
||||
}
|
||||
|
||||
public ModuleDefinition GetResourceModule (string name, ReadingMode mode)
|
||||
{
|
||||
return ModuleDefinition.ReadModule (GetAssemblyResourcePath (name, GetType ().Assembly), new ReaderParameters (mode));
|
||||
}
|
||||
|
||||
internal Image GetResourceImage (string name)
|
||||
{
|
||||
var file = new FileStream (GetAssemblyResourcePath (name, GetType ().Assembly), FileMode.Open, FileAccess.Read);
|
||||
return ImageReader.ReadImage (Disposable.Owned (file as Stream), file.Name);
|
||||
}
|
||||
|
||||
public ModuleDefinition GetCurrentModule ()
|
||||
{
|
||||
return ModuleDefinition.ReadModule (GetType ().Module.FullyQualifiedName);
|
||||
}
|
||||
|
||||
public ModuleDefinition GetCurrentModule (ReaderParameters parameters)
|
||||
{
|
||||
return ModuleDefinition.ReadModule (GetType ().Module.FullyQualifiedName, parameters);
|
||||
}
|
||||
|
||||
public static string FindResourcesDirectory (Assembly assembly)
|
||||
{
|
||||
var path = Path.GetDirectoryName (new Uri (assembly.CodeBase).LocalPath);
|
||||
while (!Directory.Exists (Path.Combine (path, "Resources"))) {
|
||||
var old = path;
|
||||
path = Path.GetDirectoryName (path);
|
||||
Assert.AreNotEqual (old, path);
|
||||
}
|
||||
|
||||
return Path.Combine (path, "Resources");
|
||||
}
|
||||
|
||||
public static void AssertCode (string expected, MethodDefinition method)
|
||||
{
|
||||
Assert.IsTrue (method.HasBody);
|
||||
Assert.IsNotNull (method.Body);
|
||||
|
||||
Assert.AreEqual (Normalize (expected), Normalize (Formatter.FormatMethodBody (method)));
|
||||
}
|
||||
|
||||
static string Normalize (string str)
|
||||
{
|
||||
return str.Trim ().Replace ("\r\n", "\n");
|
||||
}
|
||||
|
||||
public static void TestModule (string file, Action<ModuleDefinition> test, bool verify = true, bool readOnly = false, Type symbolReaderProvider = null, Type symbolWriterProvider = null, IAssemblyResolver assemblyResolver = null, bool applyWindowsRuntimeProjections = false)
|
||||
{
|
||||
Run (new ModuleTestCase (file, test, verify, readOnly, symbolReaderProvider, symbolWriterProvider, assemblyResolver, applyWindowsRuntimeProjections));
|
||||
}
|
||||
|
||||
public static void TestCSharp (string file, Action<ModuleDefinition> test, bool verify = true, bool readOnly = false, Type symbolReaderProvider = null, Type symbolWriterProvider = null, IAssemblyResolver assemblyResolver = null, bool applyWindowsRuntimeProjections = false)
|
||||
{
|
||||
Run (new CSharpTestCase (file, test, verify, readOnly, symbolReaderProvider, symbolWriterProvider, assemblyResolver, applyWindowsRuntimeProjections));
|
||||
}
|
||||
|
||||
public static void TestIL (string file, Action<ModuleDefinition> test, bool verify = true, bool readOnly = false, Type symbolReaderProvider = null, Type symbolWriterProvider = null, IAssemblyResolver assemblyResolver = null, bool applyWindowsRuntimeProjections = false)
|
||||
{
|
||||
Run (new ILTestCase (file, test, verify, readOnly, symbolReaderProvider, symbolWriterProvider, assemblyResolver, applyWindowsRuntimeProjections));
|
||||
}
|
||||
|
||||
static void Run (TestCase testCase)
|
||||
{
|
||||
using (var runner = new TestRunner (testCase, TestCaseType.ReadDeferred))
|
||||
runner.RunTest ();
|
||||
|
||||
using (var runner = new TestRunner (testCase, TestCaseType.ReadImmediate))
|
||||
runner.RunTest ();
|
||||
|
||||
if (testCase.ReadOnly)
|
||||
return;
|
||||
|
||||
using (var runner = new TestRunner (testCase, TestCaseType.WriteFromDeferred))
|
||||
runner.RunTest ();
|
||||
|
||||
using (var runner = new TestRunner (testCase, TestCaseType.WriteFromImmediate))
|
||||
runner.RunTest ();
|
||||
}
|
||||
}
|
||||
|
||||
abstract class TestCase {
|
||||
|
||||
public readonly bool Verify;
|
||||
public readonly bool ReadOnly;
|
||||
public readonly Type SymbolReaderProvider;
|
||||
public readonly Type SymbolWriterProvider;
|
||||
public readonly IAssemblyResolver AssemblyResolver;
|
||||
public readonly Action<ModuleDefinition> Test;
|
||||
public readonly bool ApplyWindowsRuntimeProjections;
|
||||
|
||||
public abstract string ModuleLocation { get; }
|
||||
|
||||
protected Assembly Assembly { get { return Test.Method.Module.Assembly; } }
|
||||
|
||||
protected TestCase (Action<ModuleDefinition> test, bool verify, bool readOnly, Type symbolReaderProvider, Type symbolWriterProvider, IAssemblyResolver assemblyResolver, bool applyWindowsRuntimeProjections)
|
||||
{
|
||||
Test = test;
|
||||
Verify = verify;
|
||||
ReadOnly = readOnly;
|
||||
SymbolReaderProvider = symbolReaderProvider;
|
||||
SymbolWriterProvider = symbolWriterProvider;
|
||||
AssemblyResolver = assemblyResolver;
|
||||
ApplyWindowsRuntimeProjections = applyWindowsRuntimeProjections;
|
||||
}
|
||||
}
|
||||
|
||||
class ModuleTestCase : TestCase {
|
||||
|
||||
public readonly string Module;
|
||||
|
||||
public ModuleTestCase (string module, Action<ModuleDefinition> test, bool verify, bool readOnly, Type symbolReaderProvider, Type symbolWriterProvider, IAssemblyResolver assemblyResolver, bool applyWindowsRuntimeProjections)
|
||||
: base (test, verify, readOnly, symbolReaderProvider, symbolWriterProvider, assemblyResolver, applyWindowsRuntimeProjections)
|
||||
{
|
||||
Module = module;
|
||||
}
|
||||
|
||||
public override string ModuleLocation
|
||||
{
|
||||
get { return BaseTestFixture.GetAssemblyResourcePath (Module, Assembly); }
|
||||
}
|
||||
}
|
||||
|
||||
class CSharpTestCase : TestCase {
|
||||
|
||||
public readonly string File;
|
||||
|
||||
public CSharpTestCase (string file, Action<ModuleDefinition> test, bool verify, bool readOnly, Type symbolReaderProvider, Type symbolWriterProvider, IAssemblyResolver assemblyResolver, bool applyWindowsRuntimeProjections)
|
||||
: base (test, verify, readOnly, symbolReaderProvider, symbolWriterProvider, assemblyResolver, applyWindowsRuntimeProjections)
|
||||
{
|
||||
File = file;
|
||||
}
|
||||
|
||||
public override string ModuleLocation
|
||||
{
|
||||
get
|
||||
{
|
||||
return CompilationService.CompileResource (BaseTestFixture.GetCSharpResourcePath (File, Assembly));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ILTestCase : TestCase {
|
||||
|
||||
public readonly string File;
|
||||
|
||||
public ILTestCase (string file, Action<ModuleDefinition> test, bool verify, bool readOnly, Type symbolReaderProvider, Type symbolWriterProvider, IAssemblyResolver assemblyResolver, bool applyWindowsRuntimeProjections)
|
||||
: base (test, verify, readOnly, symbolReaderProvider, symbolWriterProvider, assemblyResolver, applyWindowsRuntimeProjections)
|
||||
{
|
||||
File = file;
|
||||
}
|
||||
|
||||
public override string ModuleLocation
|
||||
{
|
||||
get
|
||||
{
|
||||
return CompilationService.CompileResource (BaseTestFixture.GetILResourcePath (File, Assembly)); ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestRunner : IDisposable {
|
||||
|
||||
readonly TestCase test_case;
|
||||
readonly TestCaseType type;
|
||||
|
||||
ModuleDefinition test_module;
|
||||
DefaultAssemblyResolver test_resolver;
|
||||
|
||||
public TestRunner (TestCase testCase, TestCaseType type)
|
||||
{
|
||||
this.test_case = testCase;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
ModuleDefinition GetModule ()
|
||||
{
|
||||
var location = test_case.ModuleLocation;
|
||||
|
||||
var parameters = new ReaderParameters {
|
||||
SymbolReaderProvider = GetSymbolReaderProvider (),
|
||||
AssemblyResolver = GetAssemblyResolver (),
|
||||
ApplyWindowsRuntimeProjections = test_case.ApplyWindowsRuntimeProjections
|
||||
};
|
||||
|
||||
switch (type) {
|
||||
case TestCaseType.ReadImmediate:
|
||||
parameters.ReadingMode = ReadingMode.Immediate;
|
||||
return ModuleDefinition.ReadModule (location, parameters);
|
||||
case TestCaseType.ReadDeferred:
|
||||
parameters.ReadingMode = ReadingMode.Deferred;
|
||||
return ModuleDefinition.ReadModule (location, parameters);
|
||||
case TestCaseType.WriteFromImmediate:
|
||||
parameters.ReadingMode = ReadingMode.Immediate;
|
||||
return RoundTrip (location, parameters, "cecil-irt");
|
||||
case TestCaseType.WriteFromDeferred:
|
||||
parameters.ReadingMode = ReadingMode.Deferred;
|
||||
return RoundTrip (location, parameters, "cecil-drt");
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
ISymbolReaderProvider GetSymbolReaderProvider ()
|
||||
{
|
||||
if (test_case.SymbolReaderProvider == null)
|
||||
return null;
|
||||
|
||||
return (ISymbolReaderProvider) Activator.CreateInstance (test_case.SymbolReaderProvider);
|
||||
}
|
||||
|
||||
ISymbolWriterProvider GetSymbolWriterProvider ()
|
||||
{
|
||||
if (test_case.SymbolReaderProvider == null)
|
||||
return null;
|
||||
|
||||
return (ISymbolWriterProvider) Activator.CreateInstance (test_case.SymbolWriterProvider);
|
||||
}
|
||||
|
||||
IAssemblyResolver GetAssemblyResolver ()
|
||||
{
|
||||
if (test_case.AssemblyResolver != null)
|
||||
return test_case.AssemblyResolver;
|
||||
|
||||
test_resolver = new DefaultAssemblyResolver ();
|
||||
var directory = Path.GetDirectoryName (test_case.ModuleLocation);
|
||||
test_resolver.AddSearchDirectory (directory);
|
||||
return test_resolver;
|
||||
}
|
||||
|
||||
ModuleDefinition RoundTrip (string location, ReaderParameters reader_parameters, string folder)
|
||||
{
|
||||
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 (),
|
||||
};
|
||||
|
||||
test_case.Test (module);
|
||||
|
||||
module.Write (rt_module, writer_parameters);
|
||||
}
|
||||
|
||||
if (test_case.Verify)
|
||||
CompilationService.Verify (rt_module);
|
||||
|
||||
return ModuleDefinition.ReadModule (rt_module, reader_parameters);
|
||||
}
|
||||
|
||||
public void RunTest ()
|
||||
{
|
||||
var module = GetModule ();
|
||||
if (module == null)
|
||||
return;
|
||||
|
||||
test_module = module;
|
||||
test_case.Test (module);
|
||||
}
|
||||
|
||||
public void Dispose ()
|
||||
{
|
||||
if (test_module != null)
|
||||
test_module.Dispose ();
|
||||
|
||||
if (test_resolver != null)
|
||||
test_resolver.Dispose ();
|
||||
}
|
||||
}
|
||||
|
||||
enum TestCaseType {
|
||||
ReadImmediate,
|
||||
ReadDeferred,
|
||||
WriteFromImmediate,
|
||||
WriteFromDeferred,
|
||||
}
|
||||
}
|
||||
275
external/linker/cecil/Test/Mono.Cecil.Tests/CompilationService.cs
vendored
Normal file
275
external/linker/cecil/Test/Mono.Cecil.Tests/CompilationService.cs
vendored
Normal file
@@ -0,0 +1,275 @@
|
||||
using System;
|
||||
using System.CodeDom.Compiler;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
struct CompilationResult {
|
||||
internal DateTime source_write_time;
|
||||
internal string result_file;
|
||||
|
||||
public CompilationResult (DateTime write_time, string result_file)
|
||||
{
|
||||
this.source_write_time = write_time;
|
||||
this.result_file = result_file;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Platform {
|
||||
|
||||
public static bool OnMono { get { return typeof (object).Assembly.GetType ("Mono.Runtime") != null; } }
|
||||
}
|
||||
|
||||
abstract class CompilationService {
|
||||
|
||||
Dictionary<string, CompilationResult> files = new Dictionary<string, CompilationResult> ();
|
||||
|
||||
bool TryGetResult (string name, out string file_result)
|
||||
{
|
||||
file_result = null;
|
||||
CompilationResult result;
|
||||
if (!files.TryGetValue (name, out result))
|
||||
return false;
|
||||
|
||||
if (result.source_write_time != File.GetLastWriteTime (name))
|
||||
return false;
|
||||
|
||||
file_result = result.result_file;
|
||||
return true;
|
||||
}
|
||||
|
||||
public string Compile (string name)
|
||||
{
|
||||
string result_file;
|
||||
if (TryGetResult (name, out result_file))
|
||||
return result_file;
|
||||
|
||||
result_file = CompileFile (name);
|
||||
RegisterFile (name, result_file);
|
||||
return result_file;
|
||||
}
|
||||
|
||||
void RegisterFile (string name, string result_file)
|
||||
{
|
||||
files [name] = new CompilationResult (File.GetLastWriteTime (name), result_file);
|
||||
}
|
||||
|
||||
protected abstract string CompileFile (string name);
|
||||
|
||||
public static string CompileResource (string name)
|
||||
{
|
||||
var extension = Path.GetExtension (name);
|
||||
if (extension == ".il")
|
||||
return IlasmCompilationService.Instance.Compile (name);
|
||||
|
||||
if (extension == ".cs" || extension == ".vb")
|
||||
return CodeDomCompilationService.Instance.Compile (name);
|
||||
|
||||
throw new NotSupportedException (extension);
|
||||
}
|
||||
|
||||
protected static string GetCompiledFilePath (string file_name)
|
||||
{
|
||||
var tmp_cecil = Path.Combine (Path.GetTempPath (), "cecil");
|
||||
if (!Directory.Exists (tmp_cecil))
|
||||
Directory.CreateDirectory (tmp_cecil);
|
||||
|
||||
return Path.Combine (tmp_cecil, Path.GetFileName (file_name) + ".dll");
|
||||
}
|
||||
|
||||
public static void Verify (string name)
|
||||
{
|
||||
var output = Platform.OnMono ? ShellService.PEDump (name) : ShellService.PEVerify (name);
|
||||
if (output.ExitCode != 0)
|
||||
Assert.Fail (output.ToString ());
|
||||
}
|
||||
}
|
||||
|
||||
class IlasmCompilationService : CompilationService {
|
||||
|
||||
public static readonly IlasmCompilationService Instance = new IlasmCompilationService ();
|
||||
|
||||
protected override string CompileFile (string name)
|
||||
{
|
||||
string file = GetCompiledFilePath (name);
|
||||
|
||||
var output = ShellService.ILAsm (name, file);
|
||||
|
||||
AssertAssemblerResult (output);
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
static void AssertAssemblerResult (ShellService.ProcessOutput output)
|
||||
{
|
||||
if (output.ExitCode != 0)
|
||||
Assert.Fail (output.ToString ());
|
||||
}
|
||||
}
|
||||
|
||||
class CodeDomCompilationService : CompilationService {
|
||||
|
||||
public static readonly CodeDomCompilationService Instance = new CodeDomCompilationService ();
|
||||
|
||||
protected override string CompileFile (string name)
|
||||
{
|
||||
string file = GetCompiledFilePath (name);
|
||||
|
||||
using (var provider = GetProvider (name)) {
|
||||
var parameters = GetDefaultParameters (name);
|
||||
parameters.IncludeDebugInformation = false;
|
||||
parameters.GenerateExecutable = false;
|
||||
parameters.OutputAssembly = file;
|
||||
|
||||
var results = provider.CompileAssemblyFromFile (parameters, name);
|
||||
AssertCompilerResults (results);
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
static void AssertCompilerResults (CompilerResults results)
|
||||
{
|
||||
Assert.IsFalse (results.Errors.HasErrors, GetErrorMessage (results));
|
||||
}
|
||||
|
||||
static string GetErrorMessage (CompilerResults results)
|
||||
{
|
||||
if (!results.Errors.HasErrors)
|
||||
return string.Empty;
|
||||
|
||||
var builder = new StringBuilder ();
|
||||
foreach (CompilerError error in results.Errors)
|
||||
builder.AppendLine (error.ToString ());
|
||||
return builder.ToString ();
|
||||
}
|
||||
|
||||
static CompilerParameters GetDefaultParameters (string name)
|
||||
{
|
||||
return GetCompilerInfo (name).CreateDefaultCompilerParameters ();
|
||||
}
|
||||
|
||||
static CodeDomProvider GetProvider (string name)
|
||||
{
|
||||
return GetCompilerInfo (name).CreateProvider ();
|
||||
}
|
||||
|
||||
static CompilerInfo GetCompilerInfo (string name)
|
||||
{
|
||||
return CodeDomProvider.GetCompilerInfo (
|
||||
CodeDomProvider.GetLanguageFromExtension (Path.GetExtension (name)));
|
||||
}
|
||||
}
|
||||
|
||||
class ShellService {
|
||||
|
||||
public class ProcessOutput {
|
||||
|
||||
public int ExitCode;
|
||||
public string StdOut;
|
||||
public string StdErr;
|
||||
|
||||
public ProcessOutput (int exitCode, string stdout, string stderr)
|
||||
{
|
||||
ExitCode = exitCode;
|
||||
StdOut = stdout;
|
||||
StdErr = stderr;
|
||||
}
|
||||
|
||||
public override string ToString ()
|
||||
{
|
||||
return StdOut + StdErr;
|
||||
}
|
||||
}
|
||||
|
||||
static ProcessOutput RunProcess (string target, params string [] arguments)
|
||||
{
|
||||
var stdout = new StringWriter ();
|
||||
var stderr = new StringWriter ();
|
||||
|
||||
var process = new Process {
|
||||
StartInfo = new ProcessStartInfo {
|
||||
FileName = target,
|
||||
Arguments = string.Join (" ", arguments),
|
||||
CreateNoWindow = true,
|
||||
UseShellExecute = false,
|
||||
RedirectStandardError = true,
|
||||
RedirectStandardInput = true,
|
||||
RedirectStandardOutput = true,
|
||||
},
|
||||
};
|
||||
|
||||
process.Start ();
|
||||
|
||||
process.OutputDataReceived += (_, args) => stdout.Write (args.Data);
|
||||
process.ErrorDataReceived += (_, args) => stderr.Write (args.Data);
|
||||
|
||||
process.BeginOutputReadLine ();
|
||||
process.BeginErrorReadLine ();
|
||||
|
||||
process.WaitForExit ();
|
||||
|
||||
return new ProcessOutput (process.ExitCode, stdout.ToString (), stderr.ToString ());
|
||||
}
|
||||
|
||||
public static ProcessOutput ILAsm (string source, string output)
|
||||
{
|
||||
var ilasm = "ilasm";
|
||||
if (!Platform.OnMono)
|
||||
ilasm = NetFrameworkTool ("ilasm");
|
||||
|
||||
return RunProcess (ilasm, "/nologo", "/dll", "/out:" + Quote (output), Quote (source));
|
||||
}
|
||||
|
||||
static string Quote (string file)
|
||||
{
|
||||
return "\"" + file + "\"";
|
||||
}
|
||||
|
||||
public static ProcessOutput PEVerify (string source)
|
||||
{
|
||||
return RunProcess (WinSdkTool ("peverify"), "/nologo", Quote (source));
|
||||
}
|
||||
|
||||
public static ProcessOutput PEDump (string source)
|
||||
{
|
||||
return RunProcess ("pedump", "--verify code,metadata", Quote (source));
|
||||
}
|
||||
|
||||
static string NetFrameworkTool (string tool)
|
||||
{
|
||||
return Path.Combine (
|
||||
Path.GetDirectoryName (typeof (object).Assembly.Location),
|
||||
tool + ".exe");
|
||||
}
|
||||
|
||||
static string WinSdkTool (string tool)
|
||||
{
|
||||
var sdks = new [] {
|
||||
@"Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools",
|
||||
@"Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools",
|
||||
@"Microsoft SDKs\Windows\v7.0A\Bin",
|
||||
};
|
||||
|
||||
foreach (var sdk in sdks) {
|
||||
var pgf = IntPtr.Size == 8
|
||||
? Environment.GetEnvironmentVariable("ProgramFiles(x86)")
|
||||
: Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
|
||||
|
||||
var exe = Path.Combine (
|
||||
Path.Combine (pgf, sdk),
|
||||
tool + ".exe");
|
||||
|
||||
if (File.Exists(exe))
|
||||
return exe;
|
||||
}
|
||||
|
||||
return tool;
|
||||
}
|
||||
}
|
||||
}
|
||||
620
external/linker/cecil/Test/Mono.Cecil.Tests/CustomAttributesTests.cs
vendored
Normal file
620
external/linker/cecil/Test/Mono.Cecil.Tests/CustomAttributesTests.cs
vendored
Normal file
File diff suppressed because it is too large
Load Diff
79
external/linker/cecil/Test/Mono.Cecil.Tests/EventTests.cs
vendored
Normal file
79
external/linker/cecil/Test/Mono.Cecil.Tests/EventTests.cs
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
|
||||
using Mono.Cecil;
|
||||
using Mono.Cecil.Metadata;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class EventTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void AbstractMethod ()
|
||||
{
|
||||
TestCSharp ("Events.cs", module => {
|
||||
var type = module.GetType ("Foo");
|
||||
|
||||
Assert.IsTrue (type.HasEvents);
|
||||
|
||||
var events = type.Events;
|
||||
|
||||
Assert.AreEqual (1, events.Count);
|
||||
|
||||
var @event = events [0];
|
||||
|
||||
Assert.IsNotNull (@event);
|
||||
Assert.AreEqual ("Bar", @event.Name);
|
||||
Assert.IsNotNull (@event.EventType);
|
||||
Assert.AreEqual ("Pan", @event.EventType.FullName);
|
||||
|
||||
Assert.IsNotNull (@event.AddMethod);
|
||||
Assert.AreEqual (MethodSemanticsAttributes.AddOn, @event.AddMethod.SemanticsAttributes);
|
||||
Assert.IsNotNull (@event.RemoveMethod);
|
||||
Assert.AreEqual (MethodSemanticsAttributes.RemoveOn, @event.RemoveMethod.SemanticsAttributes);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OtherMethod ()
|
||||
{
|
||||
TestIL ("others.il", module => {
|
||||
var type = module.GetType ("Others");
|
||||
|
||||
Assert.IsTrue (type.HasEvents);
|
||||
|
||||
var events = type.Events;
|
||||
|
||||
Assert.AreEqual (1, events.Count);
|
||||
|
||||
var @event = events [0];
|
||||
|
||||
Assert.IsNotNull (@event);
|
||||
Assert.AreEqual ("Handler", @event.Name);
|
||||
Assert.IsNotNull (@event.EventType);
|
||||
Assert.AreEqual ("System.EventHandler", @event.EventType.FullName);
|
||||
|
||||
Assert.IsTrue (@event.HasOtherMethods);
|
||||
|
||||
Assert.AreEqual (2, @event.OtherMethods.Count);
|
||||
|
||||
var other = @event.OtherMethods [0];
|
||||
Assert.AreEqual ("dang_Handler", other.Name);
|
||||
|
||||
other = @event.OtherMethods [1];
|
||||
Assert.AreEqual ("fang_Handler", other.Name);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UnattachedEvent ()
|
||||
{
|
||||
var int32 = typeof (int).ToDefinition ();
|
||||
var evt = new EventDefinition ("Event", EventAttributes.None, int32);
|
||||
|
||||
Assert.AreEqual (null, evt.AddMethod);
|
||||
}
|
||||
}
|
||||
}
|
||||
93
external/linker/cecil/Test/Mono.Cecil.Tests/Extensions.cs
vendored
Normal file
93
external/linker/cecil/Test/Mono.Cecil.Tests/Extensions.cs
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using SR = System.Reflection;
|
||||
|
||||
using Mono.Cecil;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
public static class Extensions {
|
||||
|
||||
public static MethodDefinition GetMethod (this TypeDefinition self, string name)
|
||||
{
|
||||
return self.Methods.Where (m => m.Name == name).First ();
|
||||
}
|
||||
|
||||
public static FieldDefinition GetField (this TypeDefinition self, string name)
|
||||
{
|
||||
return self.Fields.Where (f => f.Name == name).First ();
|
||||
}
|
||||
|
||||
public static TypeDefinition ToDefinition (this Type self)
|
||||
{
|
||||
var module = ModuleDefinition.ReadModule (new MemoryStream (File.ReadAllBytes (self.Module.FullyQualifiedName)));
|
||||
return (TypeDefinition) module.LookupToken (self.MetadataToken);
|
||||
}
|
||||
|
||||
public static MethodDefinition ToDefinition (this SR.MethodBase method)
|
||||
{
|
||||
var declaring_type = method.DeclaringType.ToDefinition ();
|
||||
return (MethodDefinition) declaring_type.Module.LookupToken (method.MetadataToken);
|
||||
}
|
||||
|
||||
public static FieldDefinition ToDefinition (this SR.FieldInfo field)
|
||||
{
|
||||
var declaring_type = field.DeclaringType.ToDefinition ();
|
||||
return (FieldDefinition) declaring_type.Module.LookupToken (field.MetadataToken);
|
||||
}
|
||||
|
||||
public static TypeReference MakeGenericType (this TypeReference self, params TypeReference [] arguments)
|
||||
{
|
||||
if (self.GenericParameters.Count != arguments.Length)
|
||||
throw new ArgumentException ();
|
||||
|
||||
var instance = new GenericInstanceType (self);
|
||||
foreach (var argument in arguments)
|
||||
instance.GenericArguments.Add (argument);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static MethodReference MakeGenericMethod (this MethodReference self, params TypeReference [] arguments)
|
||||
{
|
||||
if (self.GenericParameters.Count != arguments.Length)
|
||||
throw new ArgumentException ();
|
||||
|
||||
var instance = new GenericInstanceMethod (self);
|
||||
foreach (var argument in arguments)
|
||||
instance.GenericArguments.Add (argument);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static MethodReference MakeGeneric (this MethodReference self, params TypeReference [] arguments)
|
||||
{
|
||||
var reference = new MethodReference {
|
||||
Name = self.Name,
|
||||
DeclaringType = self.DeclaringType.MakeGenericType (arguments),
|
||||
HasThis = self.HasThis,
|
||||
ExplicitThis = self.ExplicitThis,
|
||||
ReturnType = self.ReturnType,
|
||||
CallingConvention = self.CallingConvention,
|
||||
};
|
||||
|
||||
foreach (var parameter in self.Parameters)
|
||||
reference.Parameters.Add (new ParameterDefinition (parameter.ParameterType));
|
||||
|
||||
foreach (var generic_parameter in self.GenericParameters)
|
||||
reference.GenericParameters.Add (new GenericParameter (generic_parameter.Name, reference));
|
||||
|
||||
return reference;
|
||||
}
|
||||
|
||||
public static FieldReference MakeGeneric (this FieldReference self, params TypeReference [] arguments)
|
||||
{
|
||||
return new FieldReference {
|
||||
Name = self.Name,
|
||||
DeclaringType = self.DeclaringType.MakeGenericType (arguments),
|
||||
FieldType = self.FieldType,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
372
external/linker/cecil/Test/Mono.Cecil.Tests/FieldTests.cs
vendored
Normal file
372
external/linker/cecil/Test/Mono.Cecil.Tests/FieldTests.cs
vendored
Normal file
@@ -0,0 +1,372 @@
|
||||
using System;
|
||||
|
||||
using Mono.Cecil.PE;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class FieldTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void TypeDefField ()
|
||||
{
|
||||
TestCSharp ("Fields.cs", module => {
|
||||
var type = module.Types [1];
|
||||
Assert.AreEqual ("Foo", type.Name);
|
||||
Assert.AreEqual (1, type.Fields.Count);
|
||||
|
||||
var field = type.Fields [0];
|
||||
Assert.AreEqual ("bar", field.Name);
|
||||
Assert.AreEqual (1, field.MetadataToken.RID);
|
||||
Assert.IsNotNull (field.FieldType);
|
||||
Assert.AreEqual ("Bar", field.FieldType.FullName);
|
||||
Assert.AreEqual (TokenType.Field, field.MetadataToken.TokenType);
|
||||
Assert.IsFalse (field.HasConstant);
|
||||
Assert.IsNull (field.Constant);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PrimitiveTypes ()
|
||||
{
|
||||
TestCSharp ("Fields.cs", module => {
|
||||
var type = module.GetType ("Baz");
|
||||
|
||||
AssertField (type, "char", typeof (char));
|
||||
AssertField (type, "bool", typeof (bool));
|
||||
AssertField (type, "sbyte", typeof (sbyte));
|
||||
AssertField (type, "byte", typeof (byte));
|
||||
AssertField (type, "int16", typeof (short));
|
||||
AssertField (type, "uint16", typeof (ushort));
|
||||
AssertField (type, "int32", typeof (int));
|
||||
AssertField (type, "uint32", typeof (uint));
|
||||
AssertField (type, "int64", typeof (long));
|
||||
AssertField (type, "uint64", typeof (ulong));
|
||||
AssertField (type, "single", typeof (float));
|
||||
AssertField (type, "double", typeof (double));
|
||||
AssertField (type, "string", typeof (string));
|
||||
AssertField (type, "object", typeof (object));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void VolatileField ()
|
||||
{
|
||||
TestCSharp ("Fields.cs", module => {
|
||||
var type = module.GetType ("Bar");
|
||||
|
||||
Assert.IsTrue (type.HasFields);
|
||||
Assert.AreEqual (1, type.Fields.Count);
|
||||
|
||||
var field = type.Fields [0];
|
||||
|
||||
Assert.AreEqual ("oiseau", field.Name);
|
||||
Assert.AreEqual ("System.Int32 modreq(System.Runtime.CompilerServices.IsVolatile)", field.FieldType.FullName);
|
||||
|
||||
Assert.IsFalse (field.HasConstant);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FieldLayout ()
|
||||
{
|
||||
TestCSharp ("Layouts.cs", module => {
|
||||
var foo = module.GetType ("Foo");
|
||||
Assert.IsNotNull (foo);
|
||||
|
||||
Assert.IsTrue (foo.HasFields);
|
||||
|
||||
var fields = foo.Fields;
|
||||
|
||||
var field = fields [0];
|
||||
|
||||
Assert.AreEqual ("Bar", field.Name);
|
||||
Assert.IsTrue (field.HasLayoutInfo);
|
||||
Assert.AreEqual (0, field.Offset);
|
||||
|
||||
field = fields [1];
|
||||
|
||||
Assert.AreEqual ("Baz", field.Name);
|
||||
Assert.IsTrue (field.HasLayoutInfo);
|
||||
Assert.AreEqual (2, field.Offset);
|
||||
|
||||
field = fields [2];
|
||||
|
||||
Assert.AreEqual ("Gazonk", field.Name);
|
||||
Assert.IsTrue (field.HasLayoutInfo);
|
||||
Assert.AreEqual (4, field.Offset);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FieldRVA ()
|
||||
{
|
||||
TestCSharp ("Layouts.cs", module => {
|
||||
var priv_impl = GetPrivateImplementationType (module);
|
||||
Assert.IsNotNull (priv_impl);
|
||||
|
||||
Assert.AreEqual (1, priv_impl.Fields.Count);
|
||||
|
||||
var field = priv_impl.Fields [0];
|
||||
|
||||
Assert.IsNotNull (field);
|
||||
Assert.AreNotEqual (0, field.RVA);
|
||||
Assert.IsNotNull (field.InitialValue);
|
||||
Assert.AreEqual (16, field.InitialValue.Length);
|
||||
|
||||
var buffer = new ByteBuffer (field.InitialValue);
|
||||
|
||||
Assert.AreEqual (1, buffer.ReadUInt32 ());
|
||||
Assert.AreEqual (2, buffer.ReadUInt32 ());
|
||||
Assert.AreEqual (3, buffer.ReadUInt32 ());
|
||||
Assert.AreEqual (4, buffer.ReadUInt32 ());
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericFieldDefinition ()
|
||||
{
|
||||
TestCSharp ("Generics.cs", module => {
|
||||
var bar = module.GetType ("Bar`1");
|
||||
Assert.IsNotNull (bar);
|
||||
|
||||
Assert.IsTrue (bar.HasGenericParameters);
|
||||
var t = bar.GenericParameters [0];
|
||||
|
||||
Assert.AreEqual ("T", t.Name);
|
||||
Assert.AreEqual (t.Owner, bar);
|
||||
|
||||
var bang = bar.GetField ("bang");
|
||||
|
||||
Assert.IsNotNull (bang);
|
||||
|
||||
Assert.AreEqual (t, bang.FieldType);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ArrayFields ()
|
||||
{
|
||||
TestIL ("types.il", module => {
|
||||
var types = module.GetType ("Types");
|
||||
Assert.IsNotNull (types);
|
||||
|
||||
var rank_two = types.GetField ("rank_two");
|
||||
|
||||
var array = rank_two.FieldType as ArrayType;
|
||||
Assert.IsNotNull (array);
|
||||
|
||||
Assert.AreEqual (2, array.Rank);
|
||||
Assert.IsFalse (array.Dimensions [0].IsSized);
|
||||
Assert.IsFalse (array.Dimensions [1].IsSized);
|
||||
|
||||
var rank_two_low_bound_zero = types.GetField ("rank_two_low_bound_zero");
|
||||
|
||||
array = rank_two_low_bound_zero.FieldType as ArrayType;
|
||||
Assert.IsNotNull (array);
|
||||
|
||||
Assert.AreEqual (2, array.Rank);
|
||||
Assert.IsTrue (array.Dimensions [0].IsSized);
|
||||
Assert.AreEqual (0, array.Dimensions [0].LowerBound);
|
||||
Assert.AreEqual (null, array.Dimensions [0].UpperBound);
|
||||
Assert.IsTrue (array.Dimensions [1].IsSized);
|
||||
Assert.AreEqual (0, array.Dimensions [1].LowerBound);
|
||||
Assert.AreEqual (null, array.Dimensions [1].UpperBound);
|
||||
|
||||
var rank_one_low_bound_m1 = types.GetField ("rank_one_low_bound_m1");
|
||||
array = rank_one_low_bound_m1.FieldType as ArrayType;
|
||||
Assert.IsNotNull (array);
|
||||
|
||||
Assert.AreEqual (1, array.Rank);
|
||||
Assert.IsTrue (array.Dimensions [0].IsSized);
|
||||
Assert.AreEqual (-1, array.Dimensions [0].LowerBound);
|
||||
Assert.AreEqual (4, array.Dimensions [0].UpperBound);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EnumFieldsConstant ()
|
||||
{
|
||||
TestCSharp ("Fields.cs", module => {
|
||||
var pim = module.GetType ("Pim");
|
||||
Assert.IsNotNull (pim);
|
||||
|
||||
var field = pim.GetField ("Pam");
|
||||
Assert.IsTrue (field.HasConstant);
|
||||
Assert.AreEqual (1, (int) field.Constant);
|
||||
|
||||
field = pim.GetField ("Poum");
|
||||
Assert.AreEqual (2, (int) field.Constant);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StringAndClassConstant ()
|
||||
{
|
||||
TestCSharp ("Fields.cs", module => {
|
||||
var panpan = module.GetType ("PanPan");
|
||||
Assert.IsNotNull (panpan);
|
||||
|
||||
var field = panpan.GetField ("Peter");
|
||||
Assert.IsTrue (field.HasConstant);
|
||||
Assert.IsNull (field.Constant);
|
||||
|
||||
field = panpan.GetField ("QQ");
|
||||
Assert.AreEqual ("qq", (string) field.Constant);
|
||||
|
||||
field = panpan.GetField ("nil");
|
||||
Assert.AreEqual (null, (string) field.Constant);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ObjectConstant ()
|
||||
{
|
||||
TestCSharp ("Fields.cs", module => {
|
||||
var panpan = module.GetType ("PanPan");
|
||||
Assert.IsNotNull (panpan);
|
||||
|
||||
var field = panpan.GetField ("obj");
|
||||
Assert.IsTrue (field.HasConstant);
|
||||
Assert.IsNull (field.Constant);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NullPrimitiveConstant ()
|
||||
{
|
||||
TestIL ("types.il", module => {
|
||||
var fields = module.GetType ("Fields");
|
||||
|
||||
var field = fields.GetField ("int32_nullref");
|
||||
Assert.IsTrue (field.HasConstant);
|
||||
Assert.AreEqual (null, field.Constant);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ArrayConstant ()
|
||||
{
|
||||
TestCSharp ("Fields.cs", module => {
|
||||
var panpan = module.GetType ("PanPan");
|
||||
Assert.IsNotNull (panpan);
|
||||
|
||||
var field = panpan.GetField ("ints");
|
||||
Assert.IsTrue (field.HasConstant);
|
||||
Assert.IsNull (field.Constant);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConstantCoalescing ()
|
||||
{
|
||||
TestIL ("types.il", module => {
|
||||
var fields = module.GetType ("Fields");
|
||||
|
||||
var field = fields.GetField ("int32_int16");
|
||||
Assert.AreEqual ("System.Int32", field.FieldType.FullName);
|
||||
Assert.IsTrue (field.HasConstant);
|
||||
Assert.IsInstanceOf (typeof (short), field.Constant);
|
||||
Assert.AreEqual ((short) 1, field.Constant);
|
||||
|
||||
field = fields.GetField ("int16_int32");
|
||||
Assert.AreEqual ("System.Int16", field.FieldType.FullName);
|
||||
Assert.IsTrue (field.HasConstant);
|
||||
Assert.IsInstanceOf (typeof (int), field.Constant);
|
||||
Assert.AreEqual (1, field.Constant);
|
||||
|
||||
field = fields.GetField ("char_int16");
|
||||
Assert.AreEqual ("System.Char", field.FieldType.FullName);
|
||||
Assert.IsTrue (field.HasConstant);
|
||||
Assert.IsInstanceOf (typeof (short), field.Constant);
|
||||
Assert.AreEqual ((short) 1, field.Constant);
|
||||
|
||||
field = fields.GetField ("int16_char");
|
||||
Assert.AreEqual ("System.Int16", field.FieldType.FullName);
|
||||
Assert.IsTrue (field.HasConstant);
|
||||
Assert.IsInstanceOf (typeof (char), field.Constant);
|
||||
Assert.AreEqual ('s', field.Constant);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NestedEnumOfGenericTypeDefinition ()
|
||||
{
|
||||
TestCSharp ("Generics.cs", module => {
|
||||
var dang = module.GetType ("Bongo`1/Dang");
|
||||
Assert.IsNotNull (dang);
|
||||
|
||||
var field = dang.GetField ("Ding");
|
||||
Assert.IsNotNull (field);
|
||||
Assert.AreEqual (2, field.Constant);
|
||||
|
||||
field = dang.GetField ("Dong");
|
||||
Assert.IsNotNull (field);
|
||||
Assert.AreEqual (12, field.Constant);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MarshalAsFixedStr ()
|
||||
{
|
||||
TestModule ("marshal.dll", module => {
|
||||
var boc = module.GetType ("Boc");
|
||||
var field = boc.GetField ("a");
|
||||
|
||||
Assert.IsNotNull (field);
|
||||
|
||||
Assert.IsTrue (field.HasMarshalInfo);
|
||||
|
||||
var info = (FixedSysStringMarshalInfo) field.MarshalInfo;
|
||||
|
||||
Assert.AreEqual (42, info.Size);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MarshalAsFixedArray ()
|
||||
{
|
||||
TestModule ("marshal.dll", module => {
|
||||
var boc = module.GetType ("Boc");
|
||||
var field = boc.GetField ("b");
|
||||
|
||||
Assert.IsNotNull (field);
|
||||
|
||||
Assert.IsTrue (field.HasMarshalInfo);
|
||||
|
||||
var info = (FixedArrayMarshalInfo) field.MarshalInfo;
|
||||
|
||||
Assert.AreEqual (12, info.Size);
|
||||
Assert.AreEqual (NativeType.Boolean, info.ElementType);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UnattachedField ()
|
||||
{
|
||||
var field = new FieldDefinition ("Field", FieldAttributes.Public, typeof (int).ToDefinition ());
|
||||
|
||||
Assert.IsFalse (field.HasConstant);
|
||||
Assert.IsNull (field.Constant);
|
||||
}
|
||||
|
||||
static TypeDefinition GetPrivateImplementationType (ModuleDefinition module)
|
||||
{
|
||||
foreach (var type in module.Types)
|
||||
if (type.FullName.Contains ("<PrivateImplementationDetails>"))
|
||||
return type;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static void AssertField (TypeDefinition type, string name, Type expected)
|
||||
{
|
||||
var field = type.GetField (name);
|
||||
Assert.IsNotNull (field, name);
|
||||
|
||||
Assert.AreEqual (expected.FullName, field.FieldType.FullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
188
external/linker/cecil/Test/Mono.Cecil.Tests/Formatter.cs
vendored
Normal file
188
external/linker/cecil/Test/Mono.Cecil.Tests/Formatter.cs
vendored
Normal file
@@ -0,0 +1,188 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Mono.Cecil;
|
||||
using Mono.Cecil.Cil;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
public static class Formatter {
|
||||
|
||||
public static string FormatInstruction (Instruction instruction)
|
||||
{
|
||||
var writer = new StringWriter ();
|
||||
WriteInstruction (writer, instruction);
|
||||
return writer.ToString ();
|
||||
}
|
||||
|
||||
public static string FormatMethodBody (MethodDefinition method)
|
||||
{
|
||||
var writer = new StringWriter ();
|
||||
WriteMethodBody (writer, method);
|
||||
return writer.ToString ();
|
||||
}
|
||||
|
||||
public static void WriteMethodBody (TextWriter writer, MethodDefinition method)
|
||||
{
|
||||
var body = method.Body;
|
||||
|
||||
WriteVariables (writer, body);
|
||||
|
||||
foreach (Instruction instruction in body.Instructions) {
|
||||
var sequence_point = body.Method.DebugInformation.GetSequencePoint (instruction);
|
||||
if (sequence_point != null) {
|
||||
writer.Write ('\t');
|
||||
WriteSequencePoint (writer, sequence_point);
|
||||
writer.WriteLine ();
|
||||
}
|
||||
|
||||
writer.Write ('\t');
|
||||
WriteInstruction (writer, instruction);
|
||||
writer.WriteLine ();
|
||||
}
|
||||
|
||||
WriteExceptionHandlers (writer, body);
|
||||
}
|
||||
|
||||
static void WriteVariables (TextWriter writer, MethodBody body)
|
||||
{
|
||||
var variables = body.Variables;
|
||||
|
||||
writer.Write ('\t');
|
||||
writer.Write (".locals {0}(", body.InitLocals ? "init " : string.Empty);
|
||||
|
||||
for (int i = 0; i < variables.Count; i++) {
|
||||
if (i > 0)
|
||||
writer.Write (", ");
|
||||
|
||||
var variable = variables [i];
|
||||
|
||||
writer.Write ("{0} {1}", variable.VariableType, GetVariableName (variable, body));
|
||||
}
|
||||
writer.WriteLine (")");
|
||||
}
|
||||
|
||||
static string GetVariableName (VariableDefinition variable, MethodBody body)
|
||||
{
|
||||
string name;
|
||||
if (body.Method.DebugInformation.TryGetName (variable, out name))
|
||||
return name;
|
||||
|
||||
return variable.ToString ();
|
||||
}
|
||||
|
||||
static void WriteInstruction (TextWriter writer, Instruction instruction)
|
||||
{
|
||||
writer.Write (FormatLabel (instruction.Offset));
|
||||
writer.Write (": ");
|
||||
writer.Write (instruction.OpCode.Name);
|
||||
if (null != instruction.Operand) {
|
||||
writer.Write (' ');
|
||||
WriteOperand (writer, instruction.Operand);
|
||||
}
|
||||
}
|
||||
|
||||
static void WriteSequencePoint (TextWriter writer, SequencePoint sequence_point)
|
||||
{
|
||||
writer.Write (".line {0},{1}:{2},{3} '{4}'",
|
||||
sequence_point.StartLine,
|
||||
sequence_point.EndLine,
|
||||
sequence_point.StartColumn,
|
||||
sequence_point.EndColumn,
|
||||
sequence_point.Document.Url);
|
||||
}
|
||||
|
||||
static string FormatLabel (int offset)
|
||||
{
|
||||
string label = "000" + offset.ToString ("x");
|
||||
return "IL_" + label.Substring (label.Length - 4);
|
||||
}
|
||||
|
||||
static string FormatLabel (Instruction instruction)
|
||||
{
|
||||
return FormatLabel (instruction.Offset);
|
||||
}
|
||||
|
||||
static void WriteOperand (TextWriter writer, object operand)
|
||||
{
|
||||
if (null == operand) throw new ArgumentNullException ("operand");
|
||||
|
||||
var target = operand as Instruction;
|
||||
if (null != target) {
|
||||
writer.Write (FormatLabel (target.Offset));
|
||||
return;
|
||||
}
|
||||
|
||||
var targets = operand as Instruction [];
|
||||
if (null != targets) {
|
||||
WriteLabelList (writer, targets);
|
||||
return;
|
||||
}
|
||||
|
||||
string s = operand as string;
|
||||
if (null != s) {
|
||||
writer.Write ("\"" + s + "\"");
|
||||
return;
|
||||
}
|
||||
|
||||
var parameter = operand as ParameterDefinition;
|
||||
if (parameter != null) {
|
||||
writer.Write (ToInvariantCultureString (parameter.Sequence));
|
||||
return;
|
||||
}
|
||||
|
||||
s = ToInvariantCultureString (operand);
|
||||
writer.Write (s);
|
||||
}
|
||||
|
||||
static void WriteLabelList (TextWriter writer, Instruction [] instructions)
|
||||
{
|
||||
writer.Write ("(");
|
||||
|
||||
for (int i = 0; i < instructions.Length; i++) {
|
||||
if (i != 0) writer.Write (", ");
|
||||
writer.Write (FormatLabel (instructions [i].Offset));
|
||||
}
|
||||
|
||||
writer.Write (")");
|
||||
}
|
||||
|
||||
static void WriteExceptionHandlers (TextWriter writer, MethodBody body)
|
||||
{
|
||||
if (!body.HasExceptionHandlers)
|
||||
return;
|
||||
|
||||
foreach (var handler in body.ExceptionHandlers) {
|
||||
writer.Write ("\t");
|
||||
writer.WriteLine (".try {0} to {1} {2} handler {3} to {4}",
|
||||
FormatLabel (handler.TryStart),
|
||||
FormatLabel (handler.TryEnd),
|
||||
FormatHandlerType (handler),
|
||||
FormatLabel (handler.HandlerStart),
|
||||
FormatLabel (handler.HandlerEnd));
|
||||
}
|
||||
}
|
||||
|
||||
static string FormatHandlerType (ExceptionHandler handler)
|
||||
{
|
||||
var handler_type = handler.HandlerType;
|
||||
var type = handler_type.ToString ().ToLowerInvariant ();
|
||||
|
||||
switch (handler_type) {
|
||||
case ExceptionHandlerType.Catch:
|
||||
return string.Format ("{0} {1}", type, handler.CatchType.FullName);
|
||||
case ExceptionHandlerType.Filter:
|
||||
throw new NotImplementedException ();
|
||||
default:
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
public static string ToInvariantCultureString (object value)
|
||||
{
|
||||
var convertible = value as IConvertible;
|
||||
return (null != convertible)
|
||||
? convertible.ToString (System.Globalization.CultureInfo.InvariantCulture)
|
||||
: value.ToString ();
|
||||
}
|
||||
}
|
||||
}
|
||||
79
external/linker/cecil/Test/Mono.Cecil.Tests/ILProcessorTests.cs
vendored
Normal file
79
external/linker/cecil/Test/Mono.Cecil.Tests/ILProcessorTests.cs
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
using Mono.Cecil;
|
||||
using Mono.Cecil.Cil;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class ILProcessorTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void Append ()
|
||||
{
|
||||
var method = CreateTestMethod ();
|
||||
var il = method.GetILProcessor ();
|
||||
|
||||
var ret = il.Create (OpCodes.Ret);
|
||||
il.Append (ret);
|
||||
|
||||
AssertOpCodeSequence (new [] { OpCodes.Ret }, method);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertBefore ()
|
||||
{
|
||||
var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
|
||||
var il = method.GetILProcessor ();
|
||||
|
||||
var ldloc_2 = method.Instructions.Where (i => i.OpCode == OpCodes.Ldloc_2).First ();
|
||||
|
||||
il.InsertBefore (
|
||||
ldloc_2,
|
||||
il.Create (OpCodes.Ldloc_1));
|
||||
|
||||
AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertAfter ()
|
||||
{
|
||||
var method = CreateTestMethod (OpCodes.Ldloc_0, OpCodes.Ldloc_2, OpCodes.Ldloc_3);
|
||||
var il = method.GetILProcessor ();
|
||||
|
||||
var ldloc_0 = method.Instructions.First ();
|
||||
|
||||
il.InsertAfter (
|
||||
ldloc_0,
|
||||
il.Create (OpCodes.Ldloc_1));
|
||||
|
||||
AssertOpCodeSequence (new [] { OpCodes.Ldloc_0, OpCodes.Ldloc_1, OpCodes.Ldloc_2, OpCodes.Ldloc_3 }, method);
|
||||
}
|
||||
|
||||
static void AssertOpCodeSequence (OpCode [] expected, MethodBody body)
|
||||
{
|
||||
var opcodes = body.Instructions.Select (i => i.OpCode).ToArray ();
|
||||
Assert.AreEqual (expected.Length, opcodes.Length);
|
||||
|
||||
for (int i = 0; i < opcodes.Length; i++)
|
||||
Assert.AreEqual (expected [i], opcodes [i]);
|
||||
}
|
||||
|
||||
static MethodBody CreateTestMethod (params OpCode [] opcodes)
|
||||
{
|
||||
var method = new MethodDefinition {
|
||||
Name = "function",
|
||||
};
|
||||
|
||||
var il = method.Body.GetILProcessor ();
|
||||
|
||||
foreach (var opcode in opcodes)
|
||||
il.Emit (opcode);
|
||||
|
||||
return method.Body;
|
||||
}
|
||||
}
|
||||
}
|
||||
183
external/linker/cecil/Test/Mono.Cecil.Tests/ImageReadTests.cs
vendored
Normal file
183
external/linker/cecil/Test/Mono.Cecil.Tests/ImageReadTests.cs
vendored
Normal file
@@ -0,0 +1,183 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using Mono.Cecil;
|
||||
using Mono.Cecil.PE;
|
||||
using Mono.Cecil.Metadata;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class ImageReadTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void ImageSections ()
|
||||
{
|
||||
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 ()
|
||||
{
|
||||
using (var image = GetResourceImage ("hello.exe"))
|
||||
Assert.AreEqual (TargetRuntime.Net_2_0, image.RuntimeVersion.ParseRuntime ());
|
||||
|
||||
using (var image = GetResourceImage ("hello1.exe"))
|
||||
Assert.AreEqual (TargetRuntime.Net_1_1, image.RuntimeVersion.ParseRuntime ());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImageModuleKind ()
|
||||
{
|
||||
using (var image = GetResourceImage ("hello.exe"))
|
||||
Assert.AreEqual (ModuleKind.Console, image.Kind);
|
||||
|
||||
using (var image = GetResourceImage ("libhello.dll"))
|
||||
Assert.AreEqual (ModuleKind.Dll, image.Kind);
|
||||
|
||||
using (var image = GetResourceImage ("hellow.exe"))
|
||||
Assert.AreEqual (ModuleKind.Windows, image.Kind);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MetadataHeaps ()
|
||||
{
|
||||
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.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.BlobHeap);
|
||||
Assert.AreEqual (new byte [0], image.BlobHeap.Read (0));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TablesHeap ()
|
||||
{
|
||||
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 X64Module ()
|
||||
{
|
||||
TestModule ("hello.x64.exe", module => {
|
||||
Assert.AreEqual (TargetArchitecture.AMD64, module.Image.Architecture);
|
||||
Assert.AreEqual (ModuleAttributes.ILOnly, module.Image.Attributes);
|
||||
}, verify: !Platform.OnMono);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void X64ModuleTextOnlySection ()
|
||||
{
|
||||
TestModule ("hello.textonly.x64.exe", module => {
|
||||
Assert.AreEqual (TargetArchitecture.AMD64, module.Image.Architecture);
|
||||
Assert.AreEqual (ModuleAttributes.ILOnly, module.Image.Attributes);
|
||||
}, verify: !Platform.OnMono);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IA64Module ()
|
||||
{
|
||||
TestModule ("hello.ia64.exe", module => {
|
||||
Assert.AreEqual (TargetArchitecture.IA64, module.Image.Architecture);
|
||||
Assert.AreEqual (ModuleAttributes.ILOnly, module.Image.Attributes);
|
||||
}, verify: !Platform.OnMono);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void X86Module ()
|
||||
{
|
||||
TestModule ("hello.x86.exe", module => {
|
||||
Assert.AreEqual (TargetArchitecture.I386, module.Image.Architecture);
|
||||
Assert.AreEqual (ModuleAttributes.ILOnly | ModuleAttributes.Required32Bit, module.Image.Attributes);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnyCpuModule ()
|
||||
{
|
||||
TestModule ("hello.anycpu.exe", module => {
|
||||
Assert.AreEqual (TargetArchitecture.I386, module.Image.Architecture);
|
||||
Assert.AreEqual (ModuleAttributes.ILOnly, module.Image.Attributes);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DelaySignedAssembly ()
|
||||
{
|
||||
TestModule ("delay-signed.dll", module => {
|
||||
Assert.IsNotNull (module.Assembly.Name.PublicKey);
|
||||
Assert.AreNotEqual (0, module.Assembly.Name.PublicKey.Length);
|
||||
Assert.AreNotEqual (ModuleAttributes.StrongNameSigned, module.Attributes & ModuleAttributes.StrongNameSigned);
|
||||
Assert.AreNotEqual (0, module.Image.StrongName.VirtualAddress);
|
||||
Assert.AreNotEqual (0, module.Image.StrongName.Size);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WindowsPhoneNonSignedAssembly ()
|
||||
{
|
||||
TestModule ("wp7.dll", module => {
|
||||
Assert.AreEqual (0, module.Assembly.Name.PublicKey.Length);
|
||||
Assert.AreNotEqual (ModuleAttributes.StrongNameSigned, module.Attributes & ModuleAttributes.StrongNameSigned);
|
||||
Assert.AreEqual (0, module.Image.StrongName.VirtualAddress);
|
||||
Assert.AreEqual (0, module.Image.StrongName.Size);
|
||||
}, verify: false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MetroAssembly ()
|
||||
{
|
||||
if (Platform.OnMono)
|
||||
return;
|
||||
|
||||
TestModule ("metro.exe", module => {
|
||||
Assert.AreEqual (ModuleCharacteristics.AppContainer, module.Characteristics & ModuleCharacteristics.AppContainer);
|
||||
}, verify: false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WindowsRuntimeComponentAssembly ()
|
||||
{
|
||||
var resolver = WindowsRuntimeAssemblyResolver.CreateInstance ();
|
||||
if (resolver == null)
|
||||
return;
|
||||
|
||||
TestModule("winrtcomp.winmd", module => {
|
||||
Assert.IsTrue (module.Assembly.Name.IsWindowsRuntime);
|
||||
}, verify: false, assemblyResolver: resolver);
|
||||
}
|
||||
}
|
||||
}
|
||||
374
external/linker/cecil/Test/Mono.Cecil.Tests/ImportCecilTests.cs
vendored
Normal file
374
external/linker/cecil/Test/Mono.Cecil.Tests/ImportCecilTests.cs
vendored
Normal file
@@ -0,0 +1,374 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using SR = System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
using Mono.Cecil.Cil;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class ImportCecilTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void ImportStringByRef ()
|
||||
{
|
||||
var get_string = Compile<Func<string, string>> ((module, body) => {
|
||||
var type = module.Types [1];
|
||||
|
||||
var method_by_ref = new MethodDefinition {
|
||||
Name = "ModifyString",
|
||||
IsPrivate = true,
|
||||
IsStatic = true,
|
||||
};
|
||||
|
||||
type.Methods.Add (method_by_ref);
|
||||
|
||||
method_by_ref.MethodReturnType.ReturnType = module.ImportReference (typeof (void).ToDefinition ());
|
||||
|
||||
method_by_ref.Parameters.Add (new ParameterDefinition (module.ImportReference (typeof (string).ToDefinition ())));
|
||||
method_by_ref.Parameters.Add (new ParameterDefinition (module.ImportReference (new ByReferenceType (typeof (string).ToDefinition ()))));
|
||||
|
||||
var m_il = method_by_ref.Body.GetILProcessor ();
|
||||
m_il.Emit (OpCodes.Ldarg_1);
|
||||
m_il.Emit (OpCodes.Ldarg_0);
|
||||
m_il.Emit (OpCodes.Stind_Ref);
|
||||
m_il.Emit (OpCodes.Ret);
|
||||
|
||||
var v_0 = new VariableDefinition (module.ImportReference (typeof (string).ToDefinition ()));
|
||||
body.Variables.Add (v_0);
|
||||
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldnull);
|
||||
il.Emit (OpCodes.Stloc, v_0);
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldloca, v_0);
|
||||
il.Emit (OpCodes.Call, method_by_ref);
|
||||
il.Emit (OpCodes.Ldloc_0);
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual ("foo", get_string ("foo"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportStringArray ()
|
||||
{
|
||||
var identity = Compile<Func<string [,], string [,]>> ((module, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
var array = new string [2, 2];
|
||||
|
||||
Assert.AreEqual (array, identity (array));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportFieldStringEmpty ()
|
||||
{
|
||||
var get_empty = Compile<Func<string>> ((module, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldsfld, module.ImportReference (typeof (string).GetField ("Empty").ToDefinition ()));
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual ("", get_empty ());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportStringConcat ()
|
||||
{
|
||||
var concat = Compile<Func<string, string, string>> ((module, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldarg_1);
|
||||
il.Emit (OpCodes.Call, module.ImportReference (typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }).ToDefinition ()));
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual ("FooBar", concat ("Foo", "Bar"));
|
||||
}
|
||||
|
||||
public class Generic<T> {
|
||||
public T Field;
|
||||
|
||||
public T Method (T t)
|
||||
{
|
||||
return t;
|
||||
}
|
||||
|
||||
public TS GenericMethod<TS> (T t, TS s)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
public Generic<TS> ComplexGenericMethod<TS> (T t, TS s)
|
||||
{
|
||||
return new Generic<TS> { Field = s };
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportGenericField ()
|
||||
{
|
||||
var get_field = Compile<Func<Generic<string>, string>> ((module, body) => {
|
||||
var generic_def = module.ImportReference (typeof (Generic<>)).Resolve ();
|
||||
var field_def = generic_def.Fields.Where (f => f.Name == "Field").First ();
|
||||
|
||||
var field_string = field_def.MakeGeneric (module.ImportReference (typeof (string)));
|
||||
|
||||
var field_ref = module.ImportReference (field_string);
|
||||
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldfld, field_ref);
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
var generic = new Generic<string> {
|
||||
Field = "foo",
|
||||
};
|
||||
|
||||
Assert.AreEqual ("foo", get_field (generic));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportGenericMethod ()
|
||||
{
|
||||
var generic_identity = Compile<Func<Generic<int>, int, int>> ((module, body) => {
|
||||
var generic_def = module.ImportReference (typeof (Generic<>)).Resolve ();
|
||||
var method_def = generic_def.Methods.Where (m => m.Name == "Method").First ();
|
||||
|
||||
var method_int = method_def.MakeGeneric (module.ImportReference (typeof (int)));
|
||||
var method_ref = module.ImportReference (method_int);
|
||||
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldarg_1);
|
||||
il.Emit (OpCodes.Callvirt, method_ref);
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual (42, generic_identity (new Generic<int> (), 42));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportGenericMethodSpec ()
|
||||
{
|
||||
var gen_spec_id = Compile<Func<Generic<string>, int, int>> ((module, body) => {
|
||||
var generic_def = module.ImportReference (typeof (Generic<>)).Resolve ();
|
||||
var method_def = generic_def.Methods.Where (m => m.Name == "GenericMethod").First ();
|
||||
|
||||
var method_string = method_def.MakeGeneric (module.ImportReference (typeof (string)));
|
||||
|
||||
var method_instance = method_string.MakeGenericMethod (module.ImportReference (typeof (int)));
|
||||
|
||||
var method_ref = module.ImportReference (method_instance);
|
||||
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldnull);
|
||||
il.Emit (OpCodes.Ldarg_1);
|
||||
il.Emit (OpCodes.Callvirt, method_ref);
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual (42, gen_spec_id (new Generic<string> (), 42));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportComplexGenericMethodSpec ()
|
||||
{
|
||||
var gen_spec_id = Compile<Func<Generic<string>, int, int>> ((module, body) => {
|
||||
var generic_def = module.ImportReference (typeof (Generic<>)).Resolve ();
|
||||
var method_def = generic_def.Methods.Where (m => m.Name == "ComplexGenericMethod").First ();
|
||||
|
||||
var method_string = method_def.MakeGeneric (module.ImportReference (typeof (string)));
|
||||
var method_instance = method_string.MakeGenericMethod (module.ImportReference (typeof (int)));
|
||||
var method_ref = module.ImportReference (method_instance);
|
||||
|
||||
var field_def = generic_def.Fields.Where (f => f.Name == "Field").First ();
|
||||
var field_int = field_def.MakeGeneric (module.ImportReference (typeof (int)));
|
||||
var field_ref = module.ImportReference (field_int);
|
||||
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldnull);
|
||||
il.Emit (OpCodes.Ldarg_1);
|
||||
il.Emit (OpCodes.Callvirt, method_ref);
|
||||
il.Emit (OpCodes.Ldfld, field_ref);
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual (42, gen_spec_id (new Generic<string> (), 42));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportMethodOnOpenGeneric ()
|
||||
{
|
||||
var generic = typeof (Generic<>).ToDefinition ();
|
||||
|
||||
using (var module = ModuleDefinition.CreateModule ("foo", ModuleKind.Dll)) {
|
||||
var method = module.ImportReference (generic.GetMethod ("Method"));
|
||||
Assert.AreEqual ("T Mono.Cecil.Tests.ImportCecilTests/Generic`1::Method(T)", method.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
public class ContextGeneric1Method2<G1>
|
||||
{
|
||||
public G1 GenericMethod<R1, S1> (R1 r, S1 s)
|
||||
{
|
||||
return default (G1);
|
||||
}
|
||||
}
|
||||
|
||||
public class ContextGeneric2Method1<G2, H2>
|
||||
{
|
||||
public R2 GenericMethod<R2> (G2 g, H2 h)
|
||||
{
|
||||
return default (R2);
|
||||
}
|
||||
}
|
||||
|
||||
public class NestedGenericsA<A>
|
||||
{
|
||||
public class NestedGenericsB<B>
|
||||
{
|
||||
public class NestedGenericsC<C>
|
||||
{
|
||||
public A GenericMethod (B b, C c)
|
||||
{
|
||||
return default (A);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ContextGenericTest ()
|
||||
{
|
||||
var module = ModuleDefinition.ReadModule (typeof (ContextGeneric1Method2<>).Module.FullyQualifiedName);
|
||||
// by mixing open generics with 2 & 1 parameters, we make sure the right context is used (because otherwise, an exception will be thrown)
|
||||
var type = typeof (ContextGeneric1Method2<>).MakeGenericType (typeof (ContextGeneric2Method1<,>));
|
||||
var meth = type.GetMethod ("GenericMethod");
|
||||
var imported_type = module.ImportReference (type);
|
||||
var method = module.ImportReference (meth, imported_type);
|
||||
Assert.AreEqual ("G1 Mono.Cecil.Tests.ImportCecilTests/ContextGeneric1Method2`1<Mono.Cecil.Tests.ImportCecilTests/ContextGeneric2Method1`2<G2,H2>>::GenericMethod<R1,S1>(R1,S1)", method.FullName);
|
||||
|
||||
// and the other way around
|
||||
type = typeof (ContextGeneric2Method1<,>).MakeGenericType (typeof (ContextGeneric1Method2<>), typeof (IList<>));
|
||||
meth = type.GetMethod ("GenericMethod");
|
||||
imported_type = module.ImportReference (type);
|
||||
method = module.ImportReference (meth, imported_type);
|
||||
Assert.AreEqual ("R2 Mono.Cecil.Tests.ImportCecilTests/ContextGeneric2Method1`2<Mono.Cecil.Tests.ImportCecilTests/ContextGeneric1Method2`1<G1>,System.Collections.Generic.IList`1<T>>::GenericMethod<R2>(G2,H2)", method.FullName);
|
||||
|
||||
// not sure about this one
|
||||
type = typeof (NestedGenericsA<string>.NestedGenericsB<int>.NestedGenericsC<float>);
|
||||
meth = type.GetMethod ("GenericMethod");
|
||||
imported_type = module.ImportReference (type);
|
||||
method = module.ImportReference (meth, imported_type);
|
||||
Assert.AreEqual ("A Mono.Cecil.Tests.ImportCecilTests/NestedGenericsA`1/NestedGenericsB`1/NestedGenericsC`1<System.String,System.Int32,System.Single>::GenericMethod(B,C)", method.FullName);
|
||||
|
||||
// We need both the method & type !
|
||||
type = typeof (Generic<>).MakeGenericType (typeof (string));
|
||||
meth = type.GetMethod ("ComplexGenericMethod");
|
||||
imported_type = module.ImportReference (type);
|
||||
method = module.ImportReference (meth, imported_type);
|
||||
Assert.AreEqual ("Mono.Cecil.Tests.ImportCecilTests/Generic`1<TS> Mono.Cecil.Tests.ImportCecilTests/Generic`1<System.String>::ComplexGenericMethod<TS>(T,TS)", method.FullName);
|
||||
}
|
||||
|
||||
delegate void Emitter (ModuleDefinition module, MethodBody body);
|
||||
|
||||
[MethodImpl (MethodImplOptions.NoInlining)]
|
||||
static TDelegate Compile<TDelegate> (Emitter emitter)
|
||||
where TDelegate : class
|
||||
{
|
||||
var name = GetTestCaseName ();
|
||||
|
||||
var module = CreateTestModule<TDelegate> (name, emitter);
|
||||
var assembly = LoadTestModule (module);
|
||||
|
||||
return CreateRunDelegate<TDelegate> (GetTestCase (name, assembly));
|
||||
}
|
||||
|
||||
static TDelegate CreateRunDelegate<TDelegate> (Type type)
|
||||
where TDelegate : class
|
||||
{
|
||||
return (TDelegate) (object) Delegate.CreateDelegate (typeof (TDelegate), type.GetMethod ("Run"));
|
||||
}
|
||||
|
||||
static Type GetTestCase (string name, SR.Assembly assembly)
|
||||
{
|
||||
return assembly.GetType (name);
|
||||
}
|
||||
|
||||
static SR.Assembly LoadTestModule (ModuleDefinition module)
|
||||
{
|
||||
using (var stream = new MemoryStream ()) {
|
||||
module.Write (stream);
|
||||
File.WriteAllBytes (Path.Combine (Path.Combine (Path.GetTempPath (), "cecil"), module.Name + ".dll"), stream.ToArray ());
|
||||
return SR.Assembly.Load (stream.ToArray ());
|
||||
}
|
||||
}
|
||||
|
||||
static ModuleDefinition CreateTestModule<TDelegate> (string name, Emitter emitter)
|
||||
{
|
||||
var module = CreateModule (name);
|
||||
|
||||
var type = new TypeDefinition (
|
||||
"",
|
||||
name,
|
||||
TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Abstract,
|
||||
module.ImportReference (typeof (object)));
|
||||
|
||||
module.Types.Add (type);
|
||||
|
||||
var method = CreateMethod (type, typeof (TDelegate).GetMethod ("Invoke"));
|
||||
|
||||
emitter (module, method.Body);
|
||||
|
||||
return module;
|
||||
}
|
||||
|
||||
static MethodDefinition CreateMethod (TypeDefinition type, SR.MethodInfo pattern)
|
||||
{
|
||||
var module = type.Module;
|
||||
|
||||
var method = new MethodDefinition {
|
||||
Name = "Run",
|
||||
IsPublic = true,
|
||||
IsStatic = true,
|
||||
};
|
||||
|
||||
type.Methods.Add (method);
|
||||
|
||||
method.MethodReturnType.ReturnType = module.ImportReference (pattern.ReturnType);
|
||||
|
||||
foreach (var parameter_pattern in pattern.GetParameters ())
|
||||
method.Parameters.Add (new ParameterDefinition (module.ImportReference (parameter_pattern.ParameterType)));
|
||||
|
||||
return method;
|
||||
}
|
||||
|
||||
static ModuleDefinition CreateModule (string name)
|
||||
{
|
||||
return ModuleDefinition.CreateModule (name, ModuleKind.Dll);
|
||||
}
|
||||
|
||||
[MethodImpl (MethodImplOptions.NoInlining)]
|
||||
static string GetTestCaseName ()
|
||||
{
|
||||
var stack_trace = new StackTrace ();
|
||||
var stack_frame = stack_trace.GetFrame (2);
|
||||
|
||||
return "ImportCecil_" + stack_frame.GetMethod ().Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
421
external/linker/cecil/Test/Mono.Cecil.Tests/ImportReflectionTests.cs
vendored
Normal file
421
external/linker/cecil/Test/Mono.Cecil.Tests/ImportReflectionTests.cs
vendored
Normal file
@@ -0,0 +1,421 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using SR = System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
using Mono.Cecil.Cil;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class ImportReflectionTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void ImportString ()
|
||||
{
|
||||
var get_string = Compile<Func<string>> ((_, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldstr, "yo dawg!");
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual ("yo dawg!", get_string ());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportInt ()
|
||||
{
|
||||
var add = Compile<Func<int, int, int>> ((_, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldarg_1);
|
||||
il.Emit (OpCodes.Add);
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual (42, add (40, 2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportStringByRef ()
|
||||
{
|
||||
var get_string = Compile<Func<string, string>> ((module, body) => {
|
||||
var type = module.Types [1];
|
||||
|
||||
var method_by_ref = new MethodDefinition {
|
||||
Name = "ModifyString",
|
||||
IsPrivate = true,
|
||||
IsStatic = true,
|
||||
};
|
||||
|
||||
type.Methods.Add (method_by_ref);
|
||||
|
||||
method_by_ref.MethodReturnType.ReturnType = module.ImportReference (typeof (void));
|
||||
|
||||
method_by_ref.Parameters.Add (new ParameterDefinition (module.ImportReference (typeof (string))));
|
||||
method_by_ref.Parameters.Add (new ParameterDefinition (module.ImportReference (typeof (string).MakeByRefType ())));
|
||||
|
||||
var m_il = method_by_ref.Body.GetILProcessor ();
|
||||
m_il.Emit (OpCodes.Ldarg_1);
|
||||
m_il.Emit (OpCodes.Ldarg_0);
|
||||
m_il.Emit (OpCodes.Stind_Ref);
|
||||
m_il.Emit (OpCodes.Ret);
|
||||
|
||||
var v_0 = new VariableDefinition (module.ImportReference (typeof (string)));
|
||||
body.Variables.Add (v_0);
|
||||
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldnull);
|
||||
il.Emit (OpCodes.Stloc, v_0);
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldloca, v_0);
|
||||
il.Emit (OpCodes.Call, method_by_ref);
|
||||
il.Emit (OpCodes.Ldloc_0);
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual ("foo", get_string ("foo"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportStringArray ()
|
||||
{
|
||||
var identity = Compile<Func<string [,], string [,]>> ((module, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
var array = new string [2, 2];
|
||||
|
||||
Assert.AreEqual (array, identity (array));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportFieldStringEmpty ()
|
||||
{
|
||||
var get_empty = Compile<Func<string>> ((module, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldsfld, module.ImportReference (typeof (string).GetField ("Empty")));
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual ("", get_empty ());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportStringConcat ()
|
||||
{
|
||||
var concat = Compile<Func<string, string, string>> ((module, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldarg_1);
|
||||
il.Emit (OpCodes.Call, module.ImportReference (typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) })));
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual ("FooBar", concat ("Foo", "Bar"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GeneratedAssemblyCulture ()
|
||||
{
|
||||
var id = Compile<Func<int, int>> ((module, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual ("", id.Method.DeclaringType.Assembly.GetName ().CultureInfo.Name);
|
||||
}
|
||||
|
||||
public class Generic<T> {
|
||||
public T Field;
|
||||
|
||||
public T Method (T t)
|
||||
{
|
||||
return t;
|
||||
}
|
||||
|
||||
public TS GenericMethod<TS> (T t, TS s)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
public Generic<TS> ComplexGenericMethod<TS> (T t, TS s)
|
||||
{
|
||||
return new Generic<TS> { Field = s };
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportGenericField ()
|
||||
{
|
||||
var get_field = Compile<Func<Generic<string>, string>> ((module, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldfld, module.ImportReference (typeof (Generic<string>).GetField ("Field")));
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
var generic = new Generic<string> {
|
||||
Field = "foo",
|
||||
};
|
||||
|
||||
Assert.AreEqual ("foo", get_field (generic));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportGenericMethod ()
|
||||
{
|
||||
var generic_identity = Compile<Func<Generic<int>, int, int>> ((module, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldarg_1);
|
||||
il.Emit (OpCodes.Callvirt, module.ImportReference (typeof (Generic<int>).GetMethod ("Method")));
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual (42, generic_identity (new Generic<int> (), 42));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportGenericMethodSpec ()
|
||||
{
|
||||
var gen_spec_id = Compile<Func<Generic<string>, int, int>> ((module, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldnull);
|
||||
il.Emit (OpCodes.Ldarg_1);
|
||||
il.Emit (OpCodes.Callvirt, module.ImportReference (typeof (Generic<string>).GetMethod ("GenericMethod").MakeGenericMethod (typeof (int))));
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual (42, gen_spec_id (new Generic<string> (), 42));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportComplexGenericMethodSpec ()
|
||||
{
|
||||
var gen_spec_id = Compile<Func<Generic<string>, int, int>> ((module, body) => {
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ldarg_0);
|
||||
il.Emit (OpCodes.Ldnull);
|
||||
il.Emit (OpCodes.Ldarg_1);
|
||||
il.Emit (OpCodes.Callvirt, module.ImportReference (typeof (Generic<string>).GetMethod ("ComplexGenericMethod").MakeGenericMethod (typeof (int))));
|
||||
il.Emit (OpCodes.Ldfld, module.ImportReference (typeof (Generic<int>).GetField ("Field")));
|
||||
il.Emit (OpCodes.Ret);
|
||||
});
|
||||
|
||||
Assert.AreEqual (42, gen_spec_id (new Generic<string> (), 42));
|
||||
}
|
||||
|
||||
public class Foo<TFoo> {
|
||||
public List<TFoo> list;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportGenericTypeDefOrOpen ()
|
||||
{
|
||||
using (var module = typeof (Foo<>).ToDefinition ().Module) {
|
||||
var foo_def = module.ImportReference (typeof (Foo<>));
|
||||
var foo_open = module.ImportReference (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);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportGenericTypeFromContext ()
|
||||
{
|
||||
var list_foo = typeof (Foo<>).GetField ("list").FieldType;
|
||||
var generic_list_foo_open = typeof (Generic<>).MakeGenericType (list_foo);
|
||||
|
||||
var foo_def = typeof (Foo<>).ToDefinition ();
|
||||
using (var module = foo_def.Module) {
|
||||
var generic_foo = module.ImportReference (generic_list_foo_open, foo_def);
|
||||
|
||||
Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>",
|
||||
generic_foo.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportGenericTypeDefFromContext ()
|
||||
{
|
||||
var foo_open = typeof (Foo<>).MakeGenericType (typeof (Foo<>).GetGenericArguments () [0]);
|
||||
var generic_foo_open = typeof (Generic<>).MakeGenericType (foo_open);
|
||||
|
||||
var foo_def = typeof (Foo<>).ToDefinition ();
|
||||
using (var module = foo_def.Module) {
|
||||
var generic_foo = module.ImportReference (generic_foo_open, foo_def);
|
||||
|
||||
Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>>",
|
||||
generic_foo.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportArrayTypeDefFromContext ()
|
||||
{
|
||||
var foo_open = typeof (Foo<>).MakeGenericType (typeof (Foo<>).GetGenericArguments () [0]);
|
||||
var foo_open_array = foo_open.MakeArrayType ();
|
||||
|
||||
var foo_def = typeof (Foo<>).ToDefinition ();
|
||||
using (var module = foo_def.Module) {
|
||||
var array_foo = module.ImportReference (foo_open_array, foo_def);
|
||||
|
||||
Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>[]",
|
||||
array_foo.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportGenericFieldFromContext ()
|
||||
{
|
||||
var list_foo = typeof (Foo<>).GetField ("list").FieldType;
|
||||
var generic_list_foo_open = typeof (Generic<>).MakeGenericType (list_foo);
|
||||
var generic_list_foo_open_field = generic_list_foo_open.GetField ("Field");
|
||||
|
||||
var foo_def = typeof (Foo<>).ToDefinition ();
|
||||
using (var module = foo_def.Module) {
|
||||
var generic_field = module.ImportReference (generic_list_foo_open_field, foo_def);
|
||||
|
||||
Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Field",
|
||||
generic_field.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportGenericMethodFromContext ()
|
||||
{
|
||||
var list_foo = typeof (Foo<>).GetField ("list").FieldType;
|
||||
var generic_list_foo_open = typeof (Generic<>).MakeGenericType (list_foo);
|
||||
var generic_list_foo_open_method = generic_list_foo_open.GetMethod ("Method");
|
||||
|
||||
var foo_def = typeof (Foo<>).ToDefinition ();
|
||||
using (var module = foo_def.Module) {
|
||||
var generic_method = module.ImportReference (generic_list_foo_open_method, foo_def);
|
||||
|
||||
Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Method(T)",
|
||||
generic_method.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportMethodOnOpenGenericType ()
|
||||
{
|
||||
using (var module = typeof (Generic<>).ToDefinition ().Module) {
|
||||
var method = module.ImportReference (typeof (Generic<>).GetMethod ("Method"));
|
||||
|
||||
Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::Method(T)", method.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportGenericMethodOnOpenGenericType ()
|
||||
{
|
||||
using (var module = typeof (Generic<>).ToDefinition ().Module) {
|
||||
var generic_method = module.ImportReference (typeof (Generic<>).GetMethod ("GenericMethod"));
|
||||
|
||||
Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod(T,TS)", generic_method.FullName);
|
||||
|
||||
generic_method = module.ImportReference (typeof (Generic<>).GetMethod ("GenericMethod"), generic_method);
|
||||
|
||||
Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod<TS>(T,TS)", generic_method.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
delegate void Emitter (ModuleDefinition module, MethodBody body);
|
||||
|
||||
[MethodImpl (MethodImplOptions.NoInlining)]
|
||||
static TDelegate Compile<TDelegate> (Emitter emitter)
|
||||
where TDelegate : class
|
||||
{
|
||||
var name = GetTestCaseName ();
|
||||
|
||||
var module = CreateTestModule<TDelegate> (name, emitter);
|
||||
var assembly = LoadTestModule (module);
|
||||
|
||||
return CreateRunDelegate<TDelegate> (GetTestCase (name, assembly));
|
||||
}
|
||||
|
||||
static TDelegate CreateRunDelegate<TDelegate> (Type type)
|
||||
where TDelegate : class
|
||||
{
|
||||
return (TDelegate) (object) Delegate.CreateDelegate (typeof (TDelegate), type.GetMethod ("Run"));
|
||||
}
|
||||
|
||||
static Type GetTestCase (string name, SR.Assembly assembly)
|
||||
{
|
||||
return assembly.GetType (name);
|
||||
}
|
||||
|
||||
static SR.Assembly LoadTestModule (ModuleDefinition module)
|
||||
{
|
||||
using (var stream = new MemoryStream ()) {
|
||||
module.Write (stream);
|
||||
File.WriteAllBytes (Path.Combine (Path.Combine (Path.GetTempPath (), "cecil"), module.Name + ".dll"), stream.ToArray ());
|
||||
return SR.Assembly.Load (stream.ToArray ());
|
||||
}
|
||||
}
|
||||
|
||||
static ModuleDefinition CreateTestModule<TDelegate> (string name, Emitter emitter)
|
||||
{
|
||||
var module = CreateModule (name);
|
||||
|
||||
var type = new TypeDefinition (
|
||||
"",
|
||||
name,
|
||||
TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Abstract,
|
||||
module.ImportReference (typeof (object)));
|
||||
|
||||
module.Types.Add (type);
|
||||
|
||||
var method = CreateMethod (type, typeof (TDelegate).GetMethod ("Invoke"));
|
||||
|
||||
emitter (module, method.Body);
|
||||
|
||||
return module;
|
||||
}
|
||||
|
||||
static MethodDefinition CreateMethod (TypeDefinition type, SR.MethodInfo pattern)
|
||||
{
|
||||
var module = type.Module;
|
||||
|
||||
var method = new MethodDefinition {
|
||||
Name = "Run",
|
||||
IsPublic = true,
|
||||
IsStatic = true,
|
||||
};
|
||||
|
||||
type.Methods.Add (method);
|
||||
|
||||
method.MethodReturnType.ReturnType = module.ImportReference (pattern.ReturnType);
|
||||
|
||||
foreach (var parameter_pattern in pattern.GetParameters ())
|
||||
method.Parameters.Add (new ParameterDefinition (module.ImportReference (parameter_pattern.ParameterType)));
|
||||
|
||||
return method;
|
||||
}
|
||||
|
||||
static ModuleDefinition CreateModule (string name)
|
||||
{
|
||||
return ModuleDefinition.CreateModule (name, ModuleKind.Dll);
|
||||
}
|
||||
|
||||
[MethodImpl (MethodImplOptions.NoInlining)]
|
||||
static string GetTestCaseName ()
|
||||
{
|
||||
var stack_trace = new StackTrace ();
|
||||
var stack_frame = stack_trace.GetFrame (2);
|
||||
|
||||
return "ImportReflection_" + stack_frame.GetMethod ().Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
434
external/linker/cecil/Test/Mono.Cecil.Tests/MethodBodyTests.cs
vendored
Normal file
434
external/linker/cecil/Test/Mono.Cecil.Tests/MethodBodyTests.cs
vendored
Normal file
@@ -0,0 +1,434 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
using Mono.Cecil;
|
||||
using Mono.Cecil.Cil;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class MethodBodyTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void MultiplyMethod ()
|
||||
{
|
||||
TestIL ("hello.il", module => {
|
||||
var foo = module.GetType ("Foo");
|
||||
Assert.IsNotNull (foo);
|
||||
|
||||
var bar = foo.GetMethod ("Bar");
|
||||
Assert.IsNotNull (bar);
|
||||
Assert.IsTrue (bar.IsIL);
|
||||
|
||||
AssertCode (@"
|
||||
.locals init (System.Int32 V_0)
|
||||
IL_0000: ldarg.0
|
||||
IL_0001: ldarg.1
|
||||
IL_0002: mul
|
||||
IL_0003: stloc.0
|
||||
IL_0004: ldloc.0
|
||||
IL_0005: call System.Void Foo::Baz(System.Int32)
|
||||
IL_000a: ret
|
||||
", bar);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PrintStringEmpty ()
|
||||
{
|
||||
TestIL ("hello.il", module => {
|
||||
var foo = module.GetType ("Foo");
|
||||
Assert.IsNotNull (foo);
|
||||
|
||||
var print_empty = foo.GetMethod ("PrintEmpty");
|
||||
Assert.IsNotNull (print_empty);
|
||||
|
||||
AssertCode (@"
|
||||
.locals ()
|
||||
IL_0000: ldsfld System.String System.String::Empty
|
||||
IL_0005: call System.Void System.Console::WriteLine(System.String)
|
||||
IL_000a: ret
|
||||
", print_empty);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Branch ()
|
||||
{
|
||||
TestModule ("libhello.dll", module => {
|
||||
var lib = module.GetType ("Library");
|
||||
Assert.IsNotNull (lib);
|
||||
|
||||
var method = lib.GetMethod ("GetHelloString");
|
||||
Assert.IsNotNull (method);
|
||||
|
||||
AssertCode (@"
|
||||
.locals init (System.String V_0)
|
||||
IL_0000: nop
|
||||
IL_0001: ldstr ""hello world of tomorrow""
|
||||
IL_0006: stloc.0
|
||||
IL_0007: br.s IL_0009
|
||||
IL_0009: ldloc.0
|
||||
IL_000a: ret
|
||||
", method);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Switch ()
|
||||
{
|
||||
TestModule ("switch.exe", module => {
|
||||
var program = module.GetType ("Program");
|
||||
Assert.IsNotNull (program);
|
||||
|
||||
var method = program.GetMethod ("Main");
|
||||
Assert.IsNotNull (method);
|
||||
|
||||
AssertCode (@"
|
||||
.locals init (System.Int32 V_0)
|
||||
IL_0000: ldarg.0
|
||||
IL_0001: ldlen
|
||||
IL_0002: conv.i4
|
||||
IL_0003: stloc.0
|
||||
IL_0004: ldloc.0
|
||||
IL_0005: ldc.i4.8
|
||||
IL_0006: bgt.s IL_0026
|
||||
IL_0008: ldloc.0
|
||||
IL_0009: ldc.i4.1
|
||||
IL_000a: sub
|
||||
IL_000b: switch (IL_0032, IL_0034, IL_0038, IL_0034)
|
||||
IL_0020: ldloc.0
|
||||
IL_0021: ldc.i4.8
|
||||
IL_0022: beq.s IL_0036
|
||||
IL_0024: br.s IL_0038
|
||||
IL_0026: ldloc.0
|
||||
IL_0027: ldc.i4.s 16
|
||||
IL_0029: beq.s IL_0036
|
||||
IL_002b: ldloc.0
|
||||
IL_002c: ldc.i4.s 32
|
||||
IL_002e: beq.s IL_0036
|
||||
IL_0030: br.s IL_0038
|
||||
IL_0032: ldc.i4.0
|
||||
IL_0033: ret
|
||||
IL_0034: ldc.i4.1
|
||||
IL_0035: ret
|
||||
IL_0036: ldc.i4.2
|
||||
IL_0037: ret
|
||||
IL_0038: ldc.i4.s 42
|
||||
IL_003a: ret
|
||||
", method);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MethodSpec ()
|
||||
{
|
||||
TestIL ("methodspecs.il", module => {
|
||||
var tamtam = module.GetType ("Tamtam");
|
||||
|
||||
var bar = tamtam.GetMethod ("Bar");
|
||||
Assert.IsNotNull (bar);
|
||||
|
||||
AssertCode (@"
|
||||
.locals ()
|
||||
IL_0000: ldc.i4.2
|
||||
IL_0001: call System.Void Tamtam::Foo<System.Int32>(TFoo)
|
||||
IL_0006: ret
|
||||
", bar);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NestedTryCatchFinally ()
|
||||
{
|
||||
TestModule ("catch.exe", module => {
|
||||
var program = module.GetType ("Program");
|
||||
var main = program.GetMethod ("Main");
|
||||
Assert.IsNotNull (main);
|
||||
|
||||
AssertCode (@"
|
||||
.locals ()
|
||||
IL_0000: call System.Void Program::Foo()
|
||||
IL_0005: leave.s IL_000d
|
||||
IL_0007: call System.Void Program::Baz()
|
||||
IL_000c: endfinally
|
||||
IL_000d: leave.s IL_001f
|
||||
IL_000f: pop
|
||||
IL_0010: call System.Void Program::Bar()
|
||||
IL_0015: leave.s IL_001f
|
||||
IL_0017: pop
|
||||
IL_0018: call System.Void Program::Bar()
|
||||
IL_001d: leave.s IL_001f
|
||||
IL_001f: leave.s IL_0027
|
||||
IL_0021: call System.Void Program::Baz()
|
||||
IL_0026: endfinally
|
||||
IL_0027: ret
|
||||
.try IL_0000 to IL_0007 finally handler IL_0007 to IL_000d
|
||||
.try IL_0000 to IL_000f catch System.ArgumentException handler IL_000f to IL_0017
|
||||
.try IL_0000 to IL_000f catch System.Exception handler IL_0017 to IL_001f
|
||||
.try IL_0000 to IL_0021 finally handler IL_0021 to IL_0027
|
||||
", main);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FunctionPointersAndCallSites ()
|
||||
{
|
||||
TestModule ("fptr.exe", module => {
|
||||
var type = module.Types [0];
|
||||
var start = type.GetMethod ("Start");
|
||||
Assert.IsNotNull (start);
|
||||
|
||||
AssertCode (@"
|
||||
.locals init ()
|
||||
IL_0000: ldc.i4.1
|
||||
IL_0001: call method System.Int32 *(System.Int32) MakeDecision::Decide()
|
||||
IL_0006: calli System.Int32(System.Int32)
|
||||
IL_000b: call System.Void System.Console::WriteLine(System.Int32)
|
||||
IL_0010: ldc.i4.1
|
||||
IL_0011: call method System.Int32 *(System.Int32) MakeDecision::Decide()
|
||||
IL_0016: calli System.Int32(System.Int32)
|
||||
IL_001b: call System.Void System.Console::WriteLine(System.Int32)
|
||||
IL_0020: ldc.i4.1
|
||||
IL_0021: call method System.Int32 *(System.Int32) MakeDecision::Decide()
|
||||
IL_0026: calli System.Int32(System.Int32)
|
||||
IL_002b: call System.Void System.Console::WriteLine(System.Int32)
|
||||
IL_0030: ret
|
||||
", start);
|
||||
}, verify: false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThisParameter ()
|
||||
{
|
||||
TestIL ("hello.il", module => {
|
||||
var type = module.GetType ("Foo");
|
||||
var method = type.GetMethod ("Gazonk");
|
||||
|
||||
Assert.IsNotNull (method);
|
||||
|
||||
AssertCode (@"
|
||||
.locals ()
|
||||
IL_0000: ldarg 0
|
||||
IL_0004: pop
|
||||
IL_0005: ret
|
||||
", method);
|
||||
|
||||
Assert.AreEqual (method.Body.ThisParameter.ParameterType, type);
|
||||
Assert.AreEqual (method.Body.ThisParameter, method.Body.Instructions [0].Operand);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThisParameterStaticMethod ()
|
||||
{
|
||||
var static_method = typeof (ModuleDefinition).ToDefinition ().Methods.Where (m => m.IsStatic).First ();
|
||||
Assert.IsNull (static_method.Body.ThisParameter);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThisParameterPrimitive ()
|
||||
{
|
||||
var int32 = typeof (int).ToDefinition ();
|
||||
var int_to_string = int32.Methods.Where (m => m.Name == "ToString" && m.Parameters.Count == 0).First();
|
||||
Assert.IsNotNull (int_to_string);
|
||||
|
||||
var this_parameter_type = int_to_string.Body.ThisParameter.ParameterType;
|
||||
Assert.IsTrue (this_parameter_type.IsByReference);
|
||||
|
||||
var element_type = ((ByReferenceType) this_parameter_type).ElementType;
|
||||
Assert.AreEqual (int32, element_type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThisParameterValueType ()
|
||||
{
|
||||
var token = typeof (MetadataToken).ToDefinition ();
|
||||
var token_to_string = token.Methods.Where (m => m.Name == "ToString" && m.Parameters.Count == 0).First ();
|
||||
Assert.IsNotNull (token_to_string);
|
||||
|
||||
var this_parameter_type = token_to_string.Body.ThisParameter.ParameterType;
|
||||
Assert.IsTrue (this_parameter_type.IsByReference);
|
||||
|
||||
var element_type = ((ByReferenceType) this_parameter_type).ElementType;
|
||||
Assert.AreEqual (token, element_type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThisParameterObject ()
|
||||
{
|
||||
var module = typeof (MethodBodyTests).ToDefinition ().Module;
|
||||
var @object = module.TypeSystem.Object.Resolve ();
|
||||
var method = @object.Methods.Where (m => m.HasBody).First ();
|
||||
|
||||
var type = method.Body.ThisParameter.ParameterType;
|
||||
Assert.IsFalse (type.IsValueType);
|
||||
Assert.IsFalse (type.IsPrimitive);
|
||||
Assert.IsFalse (type.IsPointer);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FilterMaxStack ()
|
||||
{
|
||||
TestIL ("hello.il", module => {
|
||||
var type = module.GetType ("Foo");
|
||||
var method = type.GetMethod ("TestFilter");
|
||||
|
||||
Assert.IsNotNull (method);
|
||||
Assert.AreEqual (2, method.Body.MaxStackSize);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Iterator ()
|
||||
{
|
||||
TestModule ("iterator.exe", module => {
|
||||
var method = module.GetType ("Program").GetMethod ("GetLittleArgs");
|
||||
Assert.IsNotNull (method.Body);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LoadString ()
|
||||
{
|
||||
TestCSharp ("CustomAttributes.cs", module => {
|
||||
var type = module.GetType ("FooAttribute");
|
||||
var get_fiou = type.GetMethod ("get_Fiou");
|
||||
Assert.IsNotNull (get_fiou);
|
||||
|
||||
var ldstr = get_fiou.Body.Instructions.Where (i => i.OpCode == OpCodes.Ldstr).First ();
|
||||
Assert.AreEqual ("fiou", ldstr.Operand);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UnattachedMethodBody ()
|
||||
{
|
||||
var system_void = typeof (void).ToDefinition ();
|
||||
var method = new MethodDefinition ("NewMethod", MethodAttributes.Assembly | MethodAttributes.Static, system_void);
|
||||
var body = new MethodBody (method);
|
||||
var il = body.GetILProcessor ();
|
||||
il.Emit (OpCodes.Ret);
|
||||
method.Body = body;
|
||||
|
||||
Assert.AreEqual (body, method.Body);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddInstruction ()
|
||||
{
|
||||
var object_ref = new TypeReference ("System", "Object", null, null, false);
|
||||
var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
|
||||
var body = new MethodBody (method);
|
||||
|
||||
var il = body.GetILProcessor ();
|
||||
|
||||
var first = il.Create (OpCodes.Nop);
|
||||
var second = il.Create (OpCodes.Nop);
|
||||
|
||||
body.Instructions.Add (first);
|
||||
body.Instructions.Add (second);
|
||||
|
||||
Assert.IsNull (first.Previous);
|
||||
Assert.AreEqual (second, first.Next);
|
||||
Assert.AreEqual (first, second.Previous);
|
||||
Assert.IsNull (second.Next);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertInstruction ()
|
||||
{
|
||||
var object_ref = new TypeReference ("System", "Object", null, null, false);
|
||||
var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
|
||||
var body = new MethodBody (method);
|
||||
|
||||
var il = body.GetILProcessor ();
|
||||
|
||||
var first = il.Create (OpCodes.Nop);
|
||||
var second = il.Create (OpCodes.Nop);
|
||||
var third = il.Create (OpCodes.Nop);
|
||||
|
||||
body.Instructions.Add (first);
|
||||
body.Instructions.Add (third);
|
||||
|
||||
Assert.IsNull (first.Previous);
|
||||
Assert.AreEqual (third, first.Next);
|
||||
Assert.AreEqual (first, third.Previous);
|
||||
Assert.IsNull (third.Next);
|
||||
|
||||
body.Instructions.Insert (1, second);
|
||||
|
||||
Assert.IsNull (first.Previous);
|
||||
Assert.AreEqual (second, first.Next);
|
||||
Assert.AreEqual (first, second.Previous);
|
||||
Assert.AreEqual (third, second.Next);
|
||||
Assert.AreEqual (second, third.Previous);
|
||||
Assert.IsNull (third.Next);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertAfterLastInstruction ()
|
||||
{
|
||||
var object_ref = new TypeReference ("System", "Object", null, null, false);
|
||||
var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
|
||||
var body = new MethodBody (method);
|
||||
|
||||
var il = body.GetILProcessor ();
|
||||
|
||||
var first = il.Create (OpCodes.Nop);
|
||||
var second = il.Create (OpCodes.Nop);
|
||||
var third = il.Create (OpCodes.Nop);
|
||||
|
||||
body.Instructions.Add (first);
|
||||
body.Instructions.Add (second);
|
||||
|
||||
Assert.IsNull (first.Previous);
|
||||
Assert.AreEqual (second, first.Next);
|
||||
Assert.AreEqual (first, second.Previous);
|
||||
Assert.IsNull (second.Next);
|
||||
|
||||
body.Instructions.Insert (2, third);
|
||||
|
||||
Assert.IsNull (first.Previous);
|
||||
Assert.AreEqual (second, first.Next);
|
||||
Assert.AreEqual (first, second.Previous);
|
||||
Assert.AreEqual (third, second.Next);
|
||||
Assert.AreEqual (second, third.Previous);
|
||||
Assert.IsNull (third.Next);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveInstruction ()
|
||||
{
|
||||
var object_ref = new TypeReference ("System", "Object", null, null, false);
|
||||
var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
|
||||
var body = new MethodBody (method);
|
||||
|
||||
var il = body.GetILProcessor ();
|
||||
|
||||
var first = il.Create (OpCodes.Nop);
|
||||
var second = il.Create (OpCodes.Nop);
|
||||
var third = il.Create (OpCodes.Nop);
|
||||
|
||||
body.Instructions.Add (first);
|
||||
body.Instructions.Add (second);
|
||||
body.Instructions.Add (third);
|
||||
|
||||
Assert.IsNull (first.Previous);
|
||||
Assert.AreEqual (second, first.Next);
|
||||
Assert.AreEqual (first, second.Previous);
|
||||
Assert.AreEqual (third, second.Next);
|
||||
Assert.AreEqual (second, third.Previous);
|
||||
Assert.IsNull (third.Next);
|
||||
|
||||
body.Instructions.Remove (second);
|
||||
|
||||
Assert.IsNull (first.Previous);
|
||||
Assert.AreEqual (third, first.Next);
|
||||
Assert.AreEqual (first, third.Previous);
|
||||
Assert.IsNull (third.Next);
|
||||
}
|
||||
}
|
||||
}
|
||||
223
external/linker/cecil/Test/Mono.Cecil.Tests/MethodTests.cs
vendored
Normal file
223
external/linker/cecil/Test/Mono.Cecil.Tests/MethodTests.cs
vendored
Normal file
@@ -0,0 +1,223 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
using Mono.Cecil;
|
||||
using Mono.Cecil.Metadata;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class MethodTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void AbstractMethod ()
|
||||
{
|
||||
TestCSharp ("Methods.cs", module => {
|
||||
var type = module.Types [1];
|
||||
Assert.AreEqual ("Foo", type.Name);
|
||||
Assert.AreEqual (2, type.Methods.Count);
|
||||
|
||||
var method = type.GetMethod ("Bar");
|
||||
Assert.AreEqual ("Bar", method.Name);
|
||||
Assert.IsTrue (method.IsAbstract);
|
||||
Assert.IsNotNull (method.ReturnType);
|
||||
|
||||
Assert.AreEqual (1, method.Parameters.Count);
|
||||
|
||||
var parameter = method.Parameters [0];
|
||||
|
||||
Assert.AreEqual ("a", parameter.Name);
|
||||
Assert.AreEqual ("System.Int32", parameter.ParameterType.FullName);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SimplePInvoke ()
|
||||
{
|
||||
TestCSharp ("Methods.cs", module => {
|
||||
var bar = module.GetType ("Bar");
|
||||
var pan = bar.GetMethod ("Pan");
|
||||
|
||||
Assert.IsTrue (pan.IsPInvokeImpl);
|
||||
Assert.IsNotNull (pan.PInvokeInfo);
|
||||
|
||||
Assert.AreEqual ("Pan", pan.PInvokeInfo.EntryPoint);
|
||||
Assert.IsNotNull (pan.PInvokeInfo.Module);
|
||||
Assert.AreEqual ("foo.dll", pan.PInvokeInfo.Module.Name);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericMethodDefinition ()
|
||||
{
|
||||
TestCSharp ("Generics.cs", module => {
|
||||
var baz = module.GetType ("Baz");
|
||||
|
||||
var gazonk = baz.GetMethod ("Gazonk");
|
||||
|
||||
Assert.IsNotNull (gazonk);
|
||||
|
||||
Assert.IsTrue (gazonk.HasGenericParameters);
|
||||
Assert.AreEqual (1, gazonk.GenericParameters.Count);
|
||||
Assert.AreEqual ("TBang", gazonk.GenericParameters [0].Name);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReturnGenericInstance ()
|
||||
{
|
||||
TestCSharp ("Generics.cs", module => {
|
||||
var bar = module.GetType ("Bar`1");
|
||||
|
||||
var self = bar.GetMethod ("Self");
|
||||
Assert.IsNotNull (self);
|
||||
|
||||
var bar_t = self.ReturnType;
|
||||
|
||||
Assert.IsTrue (bar_t.IsGenericInstance);
|
||||
|
||||
var bar_t_instance = (GenericInstanceType) bar_t;
|
||||
|
||||
Assert.AreEqual (bar.GenericParameters [0], bar_t_instance.GenericArguments [0]);
|
||||
|
||||
var self_str = bar.GetMethod ("SelfString");
|
||||
Assert.IsNotNull (self_str);
|
||||
|
||||
var bar_str = self_str.ReturnType;
|
||||
Assert.IsTrue (bar_str.IsGenericInstance);
|
||||
|
||||
var bar_str_instance = (GenericInstanceType) bar_str;
|
||||
|
||||
Assert.AreEqual ("System.String", bar_str_instance.GenericArguments [0].FullName);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReturnGenericInstanceWithMethodParameter ()
|
||||
{
|
||||
TestCSharp ("Generics.cs", module => {
|
||||
var baz = module.GetType ("Baz");
|
||||
|
||||
var gazoo = baz.GetMethod ("Gazoo");
|
||||
Assert.IsNotNull (gazoo);
|
||||
|
||||
var bar_bingo = gazoo.ReturnType;
|
||||
|
||||
Assert.IsTrue (bar_bingo.IsGenericInstance);
|
||||
|
||||
var bar_bingo_instance = (GenericInstanceType) bar_bingo;
|
||||
|
||||
Assert.AreEqual (gazoo.GenericParameters [0], bar_bingo_instance.GenericArguments [0]);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SimpleOverrides ()
|
||||
{
|
||||
TestCSharp ("Interfaces.cs", module => {
|
||||
var ibingo = module.GetType ("IBingo");
|
||||
var ibingo_foo = ibingo.GetMethod ("Foo");
|
||||
Assert.IsNotNull (ibingo_foo);
|
||||
|
||||
var ibingo_bar = ibingo.GetMethod ("Bar");
|
||||
Assert.IsNotNull (ibingo_bar);
|
||||
|
||||
var bingo = module.GetType ("Bingo");
|
||||
|
||||
var foo = bingo.GetMethod ("IBingo.Foo");
|
||||
Assert.IsNotNull (foo);
|
||||
|
||||
Assert.IsTrue (foo.HasOverrides);
|
||||
Assert.AreEqual (ibingo_foo, foo.Overrides [0]);
|
||||
|
||||
var bar = bingo.GetMethod ("IBingo.Bar");
|
||||
Assert.IsNotNull (bar);
|
||||
|
||||
Assert.IsTrue (bar.HasOverrides);
|
||||
Assert.AreEqual (ibingo_bar, bar.Overrides [0]);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void VarArgs ()
|
||||
{
|
||||
TestModule ("varargs.exe", module => {
|
||||
var module_type = module.Types [0];
|
||||
|
||||
Assert.AreEqual (3, module_type.Methods.Count);
|
||||
|
||||
var bar = module_type.GetMethod ("Bar");
|
||||
var baz = module_type.GetMethod ("Baz");
|
||||
var foo = module_type.GetMethod ("Foo");
|
||||
|
||||
Assert.IsTrue (bar.IsVarArg ());
|
||||
Assert.IsFalse (baz.IsVarArg ());
|
||||
|
||||
Assert.IsTrue (foo.IsVarArg ());
|
||||
|
||||
var foo_reference = (MethodReference) baz.Body.Instructions.First (i => i.Offset == 0x000a).Operand;
|
||||
|
||||
Assert.IsTrue (foo_reference.IsVarArg ());
|
||||
Assert.AreEqual (0, foo_reference.GetSentinelPosition ());
|
||||
|
||||
Assert.AreEqual (foo, foo_reference.Resolve ());
|
||||
|
||||
var bar_reference = (MethodReference) baz.Body.Instructions.First (i => i.Offset == 0x0023).Operand;
|
||||
|
||||
Assert.IsTrue (bar_reference.IsVarArg ());
|
||||
|
||||
Assert.AreEqual (1, bar_reference.GetSentinelPosition ());
|
||||
|
||||
Assert.AreEqual (bar, bar_reference.Resolve ());
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericInstanceMethod ()
|
||||
{
|
||||
TestCSharp ("Generics.cs", module => {
|
||||
var type = module.GetType ("It");
|
||||
var method = type.GetMethod ("ReadPwow");
|
||||
|
||||
GenericInstanceMethod instance = null;
|
||||
|
||||
foreach (var instruction in method.Body.Instructions) {
|
||||
instance = instruction.Operand as GenericInstanceMethod;
|
||||
if (instance != null)
|
||||
break;
|
||||
}
|
||||
|
||||
Assert.IsNotNull (instance);
|
||||
|
||||
Assert.AreEqual (TokenType.MethodSpec, instance.MetadataToken.TokenType);
|
||||
Assert.AreNotEqual (0, instance.MetadataToken.RID);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MethodRefDeclaredOnGenerics ()
|
||||
{
|
||||
TestCSharp ("Generics.cs", module => {
|
||||
var type = module.GetType ("Tamtam");
|
||||
var beta = type.GetMethod ("Beta");
|
||||
var charlie = type.GetMethod ("Charlie");
|
||||
|
||||
var new_list_beta = (MethodReference) beta.Body.Instructions [0].Operand;
|
||||
var new_list_charlie = (MethodReference) charlie.Body.Instructions [0].Operand;
|
||||
|
||||
Assert.AreEqual ("System.Collections.Generic.List`1<TBeta>", new_list_beta.DeclaringType.FullName);
|
||||
Assert.AreEqual ("System.Collections.Generic.List`1<TCharlie>", new_list_charlie.DeclaringType.FullName);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReturnParameterMethod ()
|
||||
{
|
||||
var method = typeof (MethodTests).ToDefinition ().GetMethod ("ReturnParameterMethod");
|
||||
Assert.IsNotNull (method);
|
||||
Assert.AreEqual (method, method.MethodReturnType.Parameter.Method);
|
||||
}
|
||||
}
|
||||
}
|
||||
284
external/linker/cecil/Test/Mono.Cecil.Tests/ModuleTests.cs
vendored
Normal file
284
external/linker/cecil/Test/Mono.Cecil.Tests/ModuleTests.cs
vendored
Normal file
@@ -0,0 +1,284 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
using Mono.Cecil;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class ModuleTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void CreateModuleEscapesAssemblyName ()
|
||||
{
|
||||
var module = ModuleDefinition.CreateModule ("Test.dll", ModuleKind.Dll);
|
||||
Assert.AreEqual ("Test", module.Assembly.Name.Name);
|
||||
|
||||
module = ModuleDefinition.CreateModule ("Test.exe", ModuleKind.Console);
|
||||
Assert.AreEqual ("Test", module.Assembly.Name.Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SingleModule ()
|
||||
{
|
||||
TestModule ("hello.exe", module => {
|
||||
var assembly = module.Assembly;
|
||||
|
||||
Assert.AreEqual (1, assembly.Modules.Count);
|
||||
Assert.IsNotNull (assembly.MainModule);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EntryPoint ()
|
||||
{
|
||||
TestModule ("hello.exe", module => {
|
||||
var entry_point = module.EntryPoint;
|
||||
Assert.IsNotNull (entry_point);
|
||||
|
||||
Assert.AreEqual ("System.Void Program::Main()", entry_point.ToString ());
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MultiModules ()
|
||||
{
|
||||
TestModule ("mma.exe", module => {
|
||||
var assembly = module.Assembly;
|
||||
|
||||
Assert.AreEqual (3, assembly.Modules.Count);
|
||||
|
||||
Assert.AreEqual ("mma.exe", assembly.Modules [0].Name);
|
||||
Assert.AreEqual (ModuleKind.Console, assembly.Modules [0].Kind);
|
||||
|
||||
Assert.AreEqual ("moda.netmodule", assembly.Modules [1].Name);
|
||||
Assert.AreEqual ("eedb4721-6c3e-4d9a-be30-49021121dd92", assembly.Modules [1].Mvid.ToString ());
|
||||
Assert.AreEqual (ModuleKind.NetModule, assembly.Modules [1].Kind);
|
||||
|
||||
Assert.AreEqual ("modb.netmodule", assembly.Modules [2].Name);
|
||||
Assert.AreEqual ("46c5c577-11b2-4ea0-bb3c-3c71f1331dd0", assembly.Modules [2].Mvid.ToString ());
|
||||
Assert.AreEqual (ModuleKind.NetModule, assembly.Modules [2].Kind);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ModuleInformation ()
|
||||
{
|
||||
TestModule ("hello.exe", module => {
|
||||
Assert.IsNotNull (module);
|
||||
|
||||
Assert.AreEqual ("hello.exe", module.Name);
|
||||
Assert.AreEqual (new Guid ("C3BC2BD3-2576-4D00-A80E-465B5632415F"), module.Mvid);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AssemblyReferences ()
|
||||
{
|
||||
TestModule ("hello.exe", module => {
|
||||
Assert.AreEqual (1, module.AssemblyReferences.Count);
|
||||
|
||||
var reference = module.AssemblyReferences [0];
|
||||
|
||||
Assert.AreEqual ("mscorlib", reference.Name);
|
||||
Assert.AreEqual (new Version (2, 0, 0, 0), reference.Version);
|
||||
Assert.AreEqual (new byte [] { 0xB7, 0x7A, 0x5C, 0x56, 0x19, 0x34, 0xE0, 0x89 }, reference.PublicKeyToken);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ModuleReferences ()
|
||||
{
|
||||
TestModule ("pinvoke.exe", module => {
|
||||
Assert.AreEqual (2, module.ModuleReferences.Count);
|
||||
Assert.AreEqual ("kernel32.dll", module.ModuleReferences [0].Name);
|
||||
Assert.AreEqual ("shell32.dll", module.ModuleReferences [1].Name);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Types ()
|
||||
{
|
||||
TestModule ("hello.exe", module => {
|
||||
Assert.AreEqual (2, module.Types.Count);
|
||||
Assert.AreEqual ("<Module>", module.Types [0].FullName);
|
||||
Assert.AreEqual ("<Module>", module.GetType ("<Module>").FullName);
|
||||
Assert.AreEqual ("Program", module.Types [1].FullName);
|
||||
Assert.AreEqual ("Program", module.GetType ("Program").FullName);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LinkedResource ()
|
||||
{
|
||||
TestModule ("libres.dll", module => {
|
||||
var resource = module.Resources.Where (res => res.Name == "linked.txt").First () as LinkedResource;
|
||||
Assert.IsNotNull (resource);
|
||||
|
||||
Assert.AreEqual ("linked.txt", resource.Name);
|
||||
Assert.AreEqual ("linked.txt", resource.File);
|
||||
Assert.AreEqual (ResourceType.Linked, resource.ResourceType);
|
||||
Assert.IsTrue (resource.IsPublic);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EmbeddedResource ()
|
||||
{
|
||||
TestModule ("libres.dll", module => {
|
||||
var resource = module.Resources.Where (res => res.Name == "embedded1.txt").First () as EmbeddedResource;
|
||||
Assert.IsNotNull (resource);
|
||||
|
||||
Assert.AreEqual ("embedded1.txt", resource.Name);
|
||||
Assert.AreEqual (ResourceType.Embedded, resource.ResourceType);
|
||||
Assert.IsTrue (resource.IsPublic);
|
||||
|
||||
using (var reader = new StreamReader (resource.GetResourceStream ()))
|
||||
Assert.AreEqual ("Hello", reader.ReadToEnd ());
|
||||
|
||||
resource = module.Resources.Where (res => res.Name == "embedded2.txt").First () as EmbeddedResource;
|
||||
Assert.IsNotNull (resource);
|
||||
|
||||
Assert.AreEqual ("embedded2.txt", resource.Name);
|
||||
Assert.AreEqual (ResourceType.Embedded, resource.ResourceType);
|
||||
Assert.IsTrue (resource.IsPublic);
|
||||
|
||||
using (var reader = new StreamReader (resource.GetResourceStream ()))
|
||||
Assert.AreEqual ("World", reader.ReadToEnd ());
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExportedTypeFromNetModule ()
|
||||
{
|
||||
TestModule ("mma.exe", module => {
|
||||
Assert.IsTrue (module.HasExportedTypes);
|
||||
Assert.AreEqual (2, module.ExportedTypes.Count);
|
||||
|
||||
var exported_type = module.ExportedTypes [0];
|
||||
|
||||
Assert.AreEqual ("Module.A.Foo", exported_type.FullName);
|
||||
Assert.AreEqual ("moda.netmodule", exported_type.Scope.Name);
|
||||
|
||||
exported_type = module.ExportedTypes [1];
|
||||
|
||||
Assert.AreEqual ("Module.B.Baz", exported_type.FullName);
|
||||
Assert.AreEqual ("modb.netmodule", exported_type.Scope.Name);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NestedTypeForwarder ()
|
||||
{
|
||||
TestCSharp ("CustomAttributes.cs", module => {
|
||||
Assert.IsTrue (module.HasExportedTypes);
|
||||
Assert.AreEqual (2, module.ExportedTypes.Count);
|
||||
|
||||
var exported_type = module.ExportedTypes [0];
|
||||
|
||||
Assert.AreEqual ("System.Diagnostics.DebuggableAttribute", exported_type.FullName);
|
||||
Assert.AreEqual ("mscorlib", exported_type.Scope.Name);
|
||||
Assert.IsTrue (exported_type.IsForwarder);
|
||||
|
||||
var nested_exported_type = module.ExportedTypes [1];
|
||||
|
||||
Assert.AreEqual ("System.Diagnostics.DebuggableAttribute/DebuggingModes", nested_exported_type.FullName);
|
||||
Assert.AreEqual (exported_type, nested_exported_type.DeclaringType);
|
||||
Assert.AreEqual ("mscorlib", nested_exported_type.Scope.Name);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void HasTypeReference ()
|
||||
{
|
||||
TestCSharp ("CustomAttributes.cs", module => {
|
||||
Assert.IsTrue (module.HasTypeReference ("System.Attribute"));
|
||||
Assert.IsTrue (module.HasTypeReference ("mscorlib", "System.Attribute"));
|
||||
|
||||
Assert.IsFalse (module.HasTypeReference ("System.Core", "System.Attribute"));
|
||||
Assert.IsFalse (module.HasTypeReference ("System.Linq.Enumerable"));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Win32FileVersion ()
|
||||
{
|
||||
TestModule ("libhello.dll", module => {
|
||||
var version = FileVersionInfo.GetVersionInfo (module.FileName);
|
||||
|
||||
Assert.AreEqual ("0.0.0.0", version.FileVersion);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ModuleWithoutBlob ()
|
||||
{
|
||||
TestModule ("noblob.dll", module => {
|
||||
Assert.IsNull (module.Image.BlobHeap);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MixedModeModule ()
|
||||
{
|
||||
using (var module = GetResourceModule ("cppcli.dll")) {
|
||||
Assert.AreEqual (1, module.ModuleReferences.Count);
|
||||
Assert.AreEqual (string.Empty, module.ModuleReferences [0].Name);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException (typeof (BadImageFormatException))]
|
||||
public void OpenIrrelevantFile ()
|
||||
{
|
||||
GetResourceModule ("text_file.txt");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetTypeNamespacePlusName ()
|
||||
{
|
||||
using (var module = GetResourceModule ("moda.netmodule")) {
|
||||
var type = module.GetType ("Module.A", "Foo");
|
||||
Assert.IsNotNull (type);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OpenModuleImmediate ()
|
||||
{
|
||||
using (var module = GetResourceModule ("hello.exe", ReadingMode.Immediate)) {
|
||||
Assert.AreEqual (ReadingMode.Immediate, module.ReadingMode);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OpenModuleDeferred ()
|
||||
{
|
||||
using (var module = GetResourceModule ("hello.exe", ReadingMode.Deferred)) {
|
||||
Assert.AreEqual (ReadingMode.Deferred, module.ReadingMode);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadAndWriteFile ()
|
||||
{
|
||||
var path = Path.GetTempFileName ();
|
||||
|
||||
var original = ModuleDefinition.CreateModule ("FooFoo", ModuleKind.Dll);
|
||||
var type = new TypeDefinition ("Foo", "Foo", TypeAttributes.Abstract | TypeAttributes.Sealed);
|
||||
original.Types.Add (type);
|
||||
original.Write (path);
|
||||
|
||||
using (var module = ModuleDefinition.ReadModule (path, new ReaderParameters { ReadWrite = true })) {
|
||||
module.Write ();
|
||||
}
|
||||
|
||||
using (var module = ModuleDefinition.ReadModule (path))
|
||||
Assert.AreEqual ("Foo.Foo", module.Types [1].FullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
91
external/linker/cecil/Test/Mono.Cecil.Tests/NestedTypesTests.cs
vendored
Normal file
91
external/linker/cecil/Test/Mono.Cecil.Tests/NestedTypesTests.cs
vendored
Normal file
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
|
||||
using Mono.Cecil;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class NestedTypesTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void NestedTypes ()
|
||||
{
|
||||
TestCSharp ("NestedTypes.cs", module => {
|
||||
var foo = module.GetType ("Foo");
|
||||
|
||||
Assert.AreEqual ("Foo", foo.Name);
|
||||
Assert.AreEqual ("Foo", foo.FullName);
|
||||
Assert.AreEqual (module, foo.Module);
|
||||
Assert.AreEqual (1, foo.NestedTypes.Count);
|
||||
|
||||
var bar = foo.NestedTypes [0];
|
||||
|
||||
Assert.AreEqual ("Bar", bar.Name);
|
||||
Assert.AreEqual ("Foo/Bar", bar.FullName);
|
||||
Assert.AreEqual (module, bar.Module);
|
||||
Assert.AreEqual (1, bar.NestedTypes.Count);
|
||||
|
||||
var baz = bar.NestedTypes [0];
|
||||
|
||||
Assert.AreEqual ("Baz", baz.Name);
|
||||
Assert.AreEqual ("Foo/Bar/Baz", baz.FullName);
|
||||
Assert.AreEqual (module, baz.Module);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DirectNestedType ()
|
||||
{
|
||||
TestCSharp ("NestedTypes.cs", module => {
|
||||
var bingo = module.GetType ("Bingo");
|
||||
var get_fuel = bingo.GetMethod ("GetFuel");
|
||||
|
||||
Assert.AreEqual ("Bingo/Fuel", get_fuel.ReturnType.FullName);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NestedTypeWithOwnNamespace ()
|
||||
{
|
||||
TestModule ("bug-185.dll", module => {
|
||||
var foo = module.GetType ("Foo");
|
||||
var foo_child = foo.NestedTypes [0];
|
||||
|
||||
Assert.AreEqual ("<IFoo<System.Byte[]>", foo_child.Namespace);
|
||||
Assert.AreEqual ("Do>d__0", foo_child.Name);
|
||||
|
||||
Assert.AreEqual ("Foo/<IFoo<System.Byte[]>.Do>d__0", foo_child.FullName);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NestedTypeFullName ()
|
||||
{
|
||||
var foo = new TypeDefinition (null, "Foo", TypeAttributes.Class);
|
||||
var bar = new TypeDefinition (null, "Bar", TypeAttributes.Class);
|
||||
var baz = new TypeDefinition (null, "Baz", TypeAttributes.Class);
|
||||
|
||||
foo.NestedTypes.Add (bar);
|
||||
bar.NestedTypes.Add (baz);
|
||||
|
||||
Assert.AreEqual ("Foo/Bar/Baz", baz.FullName);
|
||||
|
||||
foo.Namespace = "Change";
|
||||
|
||||
Assert.AreEqual ("Change.Foo/Bar", bar.FullName);
|
||||
Assert.AreEqual ("Change.Foo/Bar/Baz", baz.FullName);
|
||||
|
||||
bar.Namespace = "AnotherChange";
|
||||
|
||||
Assert.AreEqual ("Change.Foo/AnotherChange.Bar", bar.FullName);
|
||||
Assert.AreEqual ("Change.Foo/AnotherChange.Bar/Baz", baz.FullName);
|
||||
|
||||
foo.Name = "FooFoo";
|
||||
|
||||
Assert.AreEqual ("Change.FooFoo/AnotherChange.Bar", bar.FullName);
|
||||
Assert.AreEqual ("Change.FooFoo/AnotherChange.Bar/Baz", baz.FullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
274
external/linker/cecil/Test/Mono.Cecil.Tests/ParameterTests.cs
vendored
Normal file
274
external/linker/cecil/Test/Mono.Cecil.Tests/ParameterTests.cs
vendored
Normal file
@@ -0,0 +1,274 @@
|
||||
using System;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class ParameterTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void MarshalAsI4 ()
|
||||
{
|
||||
TestModule ("marshal.dll", module => {
|
||||
var bar = module.GetType ("Bar");
|
||||
var pan = bar.GetMethod ("Pan");
|
||||
|
||||
Assert.AreEqual (1, pan.Parameters.Count);
|
||||
|
||||
var parameter = pan.Parameters [0];
|
||||
|
||||
Assert.IsTrue (parameter.HasMarshalInfo);
|
||||
var info = parameter.MarshalInfo;
|
||||
|
||||
Assert.AreEqual (typeof (MarshalInfo), info.GetType ());
|
||||
Assert.AreEqual (NativeType.I4, info.NativeType);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CustomMarshaler ()
|
||||
{
|
||||
TestModule ("marshal.dll", module => {
|
||||
var bar = module.GetType ("Bar");
|
||||
var pan = bar.GetMethod ("PanPan");
|
||||
|
||||
var parameter = pan.Parameters [0];
|
||||
|
||||
Assert.IsTrue (parameter.HasMarshalInfo);
|
||||
|
||||
var info = (CustomMarshalInfo) parameter.MarshalInfo;
|
||||
|
||||
Assert.AreEqual (Guid.Empty, info.Guid);
|
||||
Assert.AreEqual (string.Empty, info.UnmanagedType);
|
||||
Assert.AreEqual (NativeType.CustomMarshaler, info.NativeType);
|
||||
Assert.AreEqual ("nomnom", info.Cookie);
|
||||
|
||||
Assert.AreEqual ("Boc", info.ManagedType.FullName);
|
||||
Assert.AreEqual (module, info.ManagedType.Scope);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SafeArrayMarshaler ()
|
||||
{
|
||||
TestModule ("marshal.dll", module => {
|
||||
var bar = module.GetType ("Bar");
|
||||
var pan = bar.GetMethod ("PanPan");
|
||||
|
||||
Assert.IsTrue (pan.MethodReturnType.HasMarshalInfo);
|
||||
|
||||
var info = (SafeArrayMarshalInfo) pan.MethodReturnType.MarshalInfo;
|
||||
|
||||
Assert.AreEqual (VariantType.Dispatch, info.ElementType);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ArrayMarshaler ()
|
||||
{
|
||||
TestModule ("marshal.dll", module => {
|
||||
var bar = module.GetType ("Bar");
|
||||
var pan = bar.GetMethod ("PanPan");
|
||||
|
||||
var parameter = pan.Parameters [1];
|
||||
|
||||
Assert.IsTrue (parameter.HasMarshalInfo);
|
||||
|
||||
var info = (ArrayMarshalInfo) parameter.MarshalInfo;
|
||||
|
||||
Assert.AreEqual (NativeType.I8, info.ElementType);
|
||||
Assert.AreEqual (66, info.Size);
|
||||
Assert.AreEqual (2, info.SizeParameterIndex);
|
||||
|
||||
parameter = pan.Parameters [3];
|
||||
|
||||
Assert.IsTrue (parameter.HasMarshalInfo);
|
||||
|
||||
info = (ArrayMarshalInfo) parameter.MarshalInfo;
|
||||
|
||||
Assert.AreEqual (NativeType.I2, info.ElementType);
|
||||
Assert.AreEqual (-1, info.Size);
|
||||
Assert.AreEqual (-1, info.SizeParameterIndex);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ArrayMarshalerSized ()
|
||||
{
|
||||
TestModule ("marshal.dll", module => {
|
||||
var delegate_type = module.GetType ("SomeMethod");
|
||||
var parameter = delegate_type.GetMethod ("Invoke").Parameters [1];
|
||||
|
||||
Assert.IsTrue (parameter.HasMarshalInfo);
|
||||
var array_info = (ArrayMarshalInfo) parameter.MarshalInfo;
|
||||
|
||||
Assert.IsNotNull (array_info);
|
||||
|
||||
Assert.AreEqual (0, array_info.SizeParameterMultiplier);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NullableConstant ()
|
||||
{
|
||||
TestModule ("nullable-constant.exe", module => {
|
||||
var type = module.GetType ("Program");
|
||||
var method = type.GetMethod ("Foo");
|
||||
|
||||
Assert.IsTrue (method.Parameters [0].HasConstant);
|
||||
Assert.IsTrue (method.Parameters [1].HasConstant);
|
||||
Assert.IsTrue (method.Parameters [2].HasConstant);
|
||||
|
||||
Assert.AreEqual (1234, method.Parameters [0].Constant);
|
||||
Assert.AreEqual (null, method.Parameters [1].Constant);
|
||||
Assert.AreEqual (12, method.Parameters [2].Constant);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BoxedDefaultArgumentValue ()
|
||||
{
|
||||
TestModule ("boxedoptarg.dll", module => {
|
||||
var foo = module.GetType ("Foo");
|
||||
var bar = foo.GetMethod ("Bar");
|
||||
var baz = bar.Parameters [0];
|
||||
|
||||
Assert.IsTrue (baz.HasConstant);
|
||||
Assert.AreEqual (-1, baz.Constant);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddParameterIndex ()
|
||||
{
|
||||
var object_ref = new TypeReference ("System", "Object", null, null, false);
|
||||
var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
|
||||
|
||||
var x = new ParameterDefinition ("x", ParameterAttributes.None, object_ref);
|
||||
var y = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
|
||||
|
||||
method.Parameters.Add (x);
|
||||
method.Parameters.Add (y);
|
||||
|
||||
Assert.AreEqual (0, x.Index);
|
||||
Assert.AreEqual (1, y.Index);
|
||||
|
||||
Assert.AreEqual (method, x.Method);
|
||||
Assert.AreEqual (method, y.Method);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAtParameterIndex ()
|
||||
{
|
||||
var object_ref = new TypeReference ("System", "Object", null, null, false);
|
||||
var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
|
||||
|
||||
var x = new ParameterDefinition ("x", ParameterAttributes.None, object_ref);
|
||||
var y = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
|
||||
var z = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
|
||||
|
||||
method.Parameters.Add (x);
|
||||
method.Parameters.Add (y);
|
||||
method.Parameters.Add (z);
|
||||
|
||||
Assert.AreEqual (0, x.Index);
|
||||
Assert.AreEqual (1, y.Index);
|
||||
Assert.AreEqual (2, z.Index);
|
||||
|
||||
method.Parameters.RemoveAt (1);
|
||||
|
||||
Assert.AreEqual (0, x.Index);
|
||||
Assert.AreEqual (-1, y.Index);
|
||||
Assert.AreEqual (1, z.Index);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveParameterIndex ()
|
||||
{
|
||||
var object_ref = new TypeReference ("System", "Object", null, null, false);
|
||||
var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
|
||||
|
||||
var x = new ParameterDefinition ("x", ParameterAttributes.None, object_ref);
|
||||
var y = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
|
||||
var z = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
|
||||
|
||||
method.Parameters.Add (x);
|
||||
method.Parameters.Add (y);
|
||||
method.Parameters.Add (z);
|
||||
|
||||
Assert.AreEqual (0, x.Index);
|
||||
Assert.AreEqual (1, y.Index);
|
||||
Assert.AreEqual (2, z.Index);
|
||||
|
||||
method.Parameters.Remove (y);
|
||||
|
||||
Assert.AreEqual (0, x.Index);
|
||||
Assert.AreEqual (-1, y.Index);
|
||||
Assert.AreEqual (1, z.Index);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertParameterIndex ()
|
||||
{
|
||||
var object_ref = new TypeReference ("System", "Object", null, null, false);
|
||||
var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref);
|
||||
|
||||
var x = new ParameterDefinition ("x", ParameterAttributes.None, object_ref);
|
||||
var y = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
|
||||
var z = new ParameterDefinition ("y", ParameterAttributes.None, object_ref);
|
||||
|
||||
method.Parameters.Add (x);
|
||||
method.Parameters.Add (z);
|
||||
|
||||
Assert.AreEqual (0, x.Index);
|
||||
Assert.AreEqual (-1, y.Index);
|
||||
Assert.AreEqual (1, z.Index);
|
||||
|
||||
method.Parameters.Insert (1, y);
|
||||
|
||||
Assert.AreEqual (0, x.Index);
|
||||
Assert.AreEqual (1, y.Index);
|
||||
Assert.AreEqual (2, z.Index);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericParameterConstant ()
|
||||
{
|
||||
TestIL ("hello.il", module => {
|
||||
var foo = module.GetType ("Foo");
|
||||
var method = foo.GetMethod ("GetState");
|
||||
|
||||
Assert.IsNotNull (method);
|
||||
|
||||
var parameter = method.Parameters [1];
|
||||
|
||||
Assert.IsTrue (parameter.HasConstant);
|
||||
Assert.IsNull (parameter.Constant);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NullablePrimitiveParameterConstant ()
|
||||
{
|
||||
TestModule ("nullable-parameter.dll", module => {
|
||||
var test = module.GetType ("Test");
|
||||
var method = test.GetMethod ("Foo");
|
||||
|
||||
Assert.IsNotNull (method);
|
||||
|
||||
var param = method.Parameters [0];
|
||||
Assert.IsTrue (param.HasConstant);
|
||||
Assert.AreEqual (1234, param.Constant);
|
||||
|
||||
param = method.Parameters [1];
|
||||
Assert.IsTrue (param.HasConstant);
|
||||
Assert.AreEqual (null, param.Constant);
|
||||
|
||||
param = method.Parameters [2];
|
||||
Assert.IsTrue (param.HasConstant);
|
||||
Assert.AreEqual (12, param.Constant);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
355
external/linker/cecil/Test/Mono.Cecil.Tests/PortablePdbTests.cs
vendored
Normal file
355
external/linker/cecil/Test/Mono.Cecil.Tests/PortablePdbTests.cs
vendored
Normal file
@@ -0,0 +1,355 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Mono.Cecil.Cil;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class PortablePdbTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void SequencePoints ()
|
||||
{
|
||||
TestPortablePdbModule (module => {
|
||||
var type = module.GetType ("PdbTarget.Program");
|
||||
var main = type.GetMethod ("Main");
|
||||
|
||||
AssertCode (@"
|
||||
.locals init (System.Int32 a, System.String[] V_1, System.Int32 V_2, System.String arg)
|
||||
.line 21,21:3,4 'C:\sources\PdbTarget\Program.cs'
|
||||
IL_0000: nop
|
||||
.line 22,22:4,11 'C:\sources\PdbTarget\Program.cs'
|
||||
IL_0001: nop
|
||||
.line 22,22:24,28 'C:\sources\PdbTarget\Program.cs'
|
||||
IL_0002: ldarg.0
|
||||
IL_0003: stloc.1
|
||||
IL_0004: ldc.i4.0
|
||||
IL_0005: stloc.2
|
||||
.line 16707566,0:16707566,0 'C:\sources\PdbTarget\Program.cs'
|
||||
IL_0006: br.s IL_0017
|
||||
.line 22,22:13,20 'C:\sources\PdbTarget\Program.cs'
|
||||
IL_0008: ldloc.1
|
||||
IL_0009: ldloc.2
|
||||
IL_000a: ldelem.ref
|
||||
IL_000b: stloc.3
|
||||
.line 23,23:5,20 'C:\sources\PdbTarget\Program.cs'
|
||||
IL_000c: ldloc.3
|
||||
IL_000d: call System.Void System.Console::WriteLine(System.String)
|
||||
IL_0012: nop
|
||||
.line 16707566,0:16707566,0 'C:\sources\PdbTarget\Program.cs'
|
||||
IL_0013: ldloc.2
|
||||
IL_0014: ldc.i4.1
|
||||
IL_0015: add
|
||||
IL_0016: stloc.2
|
||||
.line 22,22:21,23 'C:\sources\PdbTarget\Program.cs'
|
||||
IL_0017: ldloc.2
|
||||
IL_0018: ldloc.1
|
||||
IL_0019: ldlen
|
||||
IL_001a: conv.i4
|
||||
IL_001b: blt.s IL_0008
|
||||
.line 25,25:4,22 'C:\sources\PdbTarget\Program.cs'
|
||||
IL_001d: ldc.i4.1
|
||||
IL_001e: ldc.i4.2
|
||||
IL_001f: call System.Int32 System.Math::Min(System.Int32,System.Int32)
|
||||
IL_0024: stloc.0
|
||||
.line 26,26:3,4 'C:\sources\PdbTarget\Program.cs'
|
||||
IL_0025: ret
|
||||
", main);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SequencePointsMultipleDocument ()
|
||||
{
|
||||
TestPortablePdbModule (module => {
|
||||
var type = module.GetType ("PdbTarget.B");
|
||||
var main = type.GetMethod (".ctor");
|
||||
|
||||
AssertCode (@"
|
||||
.locals ()
|
||||
.line 7,7:3,25 'C:\sources\PdbTarget\B.cs'
|
||||
IL_0000: ldarg.0
|
||||
IL_0001: ldstr """"
|
||||
IL_0006: stfld System.String PdbTarget.B::s
|
||||
.line 110,110:3,21 'C:\sources\PdbTarget\Program.cs'
|
||||
IL_000b: ldarg.0
|
||||
IL_000c: ldc.i4.2
|
||||
IL_000d: stfld System.Int32 PdbTarget.B::a
|
||||
.line 111,111:3,21 'C:\sources\PdbTarget\Program.cs'
|
||||
IL_0012: ldarg.0
|
||||
IL_0013: ldc.i4.3
|
||||
IL_0014: stfld System.Int32 PdbTarget.B::b
|
||||
.line 9,9:3,13 'C:\sources\PdbTarget\B.cs'
|
||||
IL_0019: ldarg.0
|
||||
IL_001a: call System.Void System.Object::.ctor()
|
||||
IL_001f: nop
|
||||
.line 10,10:3,4 'C:\sources\PdbTarget\B.cs'
|
||||
IL_0020: nop
|
||||
.line 11,11:4,19 'C:\sources\PdbTarget\B.cs'
|
||||
IL_0021: ldstr ""B""
|
||||
IL_0026: call System.Void System.Console::WriteLine(System.String)
|
||||
IL_002b: nop
|
||||
.line 12,12:3,4 'C:\sources\PdbTarget\B.cs'
|
||||
IL_002c: ret
|
||||
", main);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LocalVariables ()
|
||||
{
|
||||
TestPortablePdbModule (module => {
|
||||
var type = module.GetType ("PdbTarget.Program");
|
||||
var method = type.GetMethod ("Bar");
|
||||
var debug_info = method.DebugInformation;
|
||||
|
||||
Assert.IsNotNull (debug_info.Scope);
|
||||
Assert.IsTrue (debug_info.Scope.HasScopes);
|
||||
Assert.AreEqual (2, debug_info.Scope.Scopes.Count);
|
||||
|
||||
var scope = debug_info.Scope.Scopes [0];
|
||||
|
||||
Assert.IsNotNull (scope);
|
||||
Assert.IsTrue (scope.HasVariables);
|
||||
Assert.AreEqual (1, scope.Variables.Count);
|
||||
|
||||
var variable = scope.Variables [0];
|
||||
|
||||
Assert.AreEqual ("s", variable.Name);
|
||||
Assert.IsFalse (variable.IsDebuggerHidden);
|
||||
Assert.AreEqual (2, variable.Index);
|
||||
|
||||
scope = debug_info.Scope.Scopes [1];
|
||||
|
||||
Assert.IsNotNull (scope);
|
||||
Assert.IsTrue (scope.HasVariables);
|
||||
Assert.AreEqual (1, scope.Variables.Count);
|
||||
|
||||
variable = scope.Variables [0];
|
||||
|
||||
Assert.AreEqual ("s", variable.Name);
|
||||
Assert.IsFalse (variable.IsDebuggerHidden);
|
||||
Assert.AreEqual (3, variable.Index);
|
||||
|
||||
Assert.IsTrue (scope.HasScopes);
|
||||
Assert.AreEqual (1, scope.Scopes.Count);
|
||||
|
||||
scope = scope.Scopes [0];
|
||||
|
||||
Assert.IsNotNull (scope);
|
||||
Assert.IsTrue (scope.HasVariables);
|
||||
Assert.AreEqual (1, scope.Variables.Count);
|
||||
|
||||
variable = scope.Variables [0];
|
||||
|
||||
Assert.AreEqual ("u", variable.Name);
|
||||
Assert.IsFalse (variable.IsDebuggerHidden);
|
||||
Assert.AreEqual (5, variable.Index);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LocalConstants ()
|
||||
{
|
||||
TestPortablePdbModule (module => {
|
||||
var type = module.GetType ("PdbTarget.Program");
|
||||
var method = type.GetMethod ("Bar");
|
||||
var debug_info = method.DebugInformation;
|
||||
|
||||
Assert.IsNotNull (debug_info.Scope);
|
||||
Assert.IsTrue (debug_info.Scope.HasScopes);
|
||||
Assert.AreEqual (2, debug_info.Scope.Scopes.Count);
|
||||
|
||||
var scope = debug_info.Scope.Scopes [1];
|
||||
|
||||
Assert.IsNotNull (scope);
|
||||
Assert.IsTrue (scope.HasConstants);
|
||||
Assert.AreEqual (2, scope.Constants.Count);
|
||||
|
||||
var constant = scope.Constants [0];
|
||||
|
||||
Assert.AreEqual ("b", constant.Name);
|
||||
Assert.AreEqual (12, constant.Value);
|
||||
Assert.AreEqual (MetadataType.Int32, constant.ConstantType.MetadataType);
|
||||
|
||||
constant = scope.Constants [1];
|
||||
Assert.AreEqual ("c", constant.Name);
|
||||
Assert.AreEqual ((decimal) 74, constant.Value);
|
||||
Assert.AreEqual (MetadataType.ValueType, constant.ConstantType.MetadataType);
|
||||
|
||||
method = type.GetMethod ("Foo");
|
||||
debug_info = method.DebugInformation;
|
||||
|
||||
Assert.IsNotNull (debug_info.Scope);
|
||||
Assert.IsTrue (debug_info.Scope.HasConstants);
|
||||
Assert.AreEqual (4, debug_info.Scope.Constants.Count);
|
||||
|
||||
constant = debug_info.Scope.Constants [0];
|
||||
Assert.AreEqual ("s", constant.Name);
|
||||
Assert.AreEqual ("const string", constant.Value);
|
||||
Assert.AreEqual (MetadataType.String, constant.ConstantType.MetadataType);
|
||||
|
||||
constant = debug_info.Scope.Constants [1];
|
||||
Assert.AreEqual ("f", constant.Name);
|
||||
Assert.AreEqual (1, constant.Value);
|
||||
Assert.AreEqual (MetadataType.Int32, constant.ConstantType.MetadataType);
|
||||
|
||||
constant = debug_info.Scope.Constants [2];
|
||||
Assert.AreEqual ("o", constant.Name);
|
||||
Assert.AreEqual (null, constant.Value);
|
||||
Assert.AreEqual (MetadataType.Object, constant.ConstantType.MetadataType);
|
||||
|
||||
constant = debug_info.Scope.Constants [3];
|
||||
Assert.AreEqual ("u", constant.Name);
|
||||
Assert.AreEqual (null, constant.Value);
|
||||
Assert.AreEqual (MetadataType.String, constant.ConstantType.MetadataType);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ImportScope ()
|
||||
{
|
||||
TestPortablePdbModule (module => {
|
||||
var type = module.GetType ("PdbTarget.Program");
|
||||
var method = type.GetMethod ("Bar");
|
||||
var debug_info = method.DebugInformation;
|
||||
|
||||
Assert.IsNotNull (debug_info.Scope);
|
||||
|
||||
var import = debug_info.Scope.Import;
|
||||
Assert.IsNotNull (import);
|
||||
|
||||
Assert.IsFalse (import.HasTargets);
|
||||
Assert.IsNotNull (import.Parent);
|
||||
|
||||
import = import.Parent;
|
||||
|
||||
Assert.IsTrue (import.HasTargets);
|
||||
Assert.AreEqual (9, import.Targets.Count);
|
||||
var target = import.Targets [0];
|
||||
|
||||
Assert.AreEqual (ImportTargetKind.ImportAlias, target.Kind);
|
||||
Assert.AreEqual ("XML", target.Alias);
|
||||
|
||||
target = import.Targets [1];
|
||||
|
||||
Assert.AreEqual (ImportTargetKind.ImportNamespace, target.Kind);
|
||||
Assert.AreEqual ("System", target.Namespace);
|
||||
|
||||
target = import.Targets [2];
|
||||
|
||||
Assert.AreEqual (ImportTargetKind.ImportNamespace, target.Kind);
|
||||
Assert.AreEqual ("System.Collections.Generic", target.Namespace);
|
||||
|
||||
target = import.Targets [3];
|
||||
|
||||
Assert.AreEqual (ImportTargetKind.ImportNamespace, target.Kind);
|
||||
Assert.AreEqual ("System.IO", target.Namespace);
|
||||
|
||||
target = import.Targets [4];
|
||||
|
||||
Assert.AreEqual (ImportTargetKind.ImportNamespace, target.Kind);
|
||||
Assert.AreEqual ("System.Threading.Tasks", target.Namespace);
|
||||
|
||||
target = import.Targets [5];
|
||||
|
||||
Assert.AreEqual (ImportTargetKind.ImportNamespaceInAssembly, target.Kind);
|
||||
Assert.AreEqual ("System.Xml.Resolvers", target.Namespace);
|
||||
Assert.AreEqual ("System.Xml", target.AssemblyReference.Name);
|
||||
|
||||
|
||||
target = import.Targets [6];
|
||||
|
||||
Assert.AreEqual (ImportTargetKind.ImportType, target.Kind);
|
||||
Assert.AreEqual ("System.Console", target.Type.FullName);
|
||||
|
||||
target = import.Targets [7];
|
||||
|
||||
Assert.AreEqual (ImportTargetKind.ImportType, target.Kind);
|
||||
Assert.AreEqual ("System.Math", target.Type.FullName);
|
||||
|
||||
target = import.Targets [8];
|
||||
|
||||
Assert.AreEqual (ImportTargetKind.DefineTypeAlias, target.Kind);
|
||||
Assert.AreEqual ("Foo", target.Alias);
|
||||
Assert.AreEqual ("System.Xml.XmlDocumentType", target.Type.FullName);
|
||||
|
||||
Assert.IsNotNull (import.Parent);
|
||||
|
||||
import = import.Parent;
|
||||
|
||||
Assert.IsTrue (import.HasTargets);
|
||||
Assert.AreEqual (1, import.Targets.Count);
|
||||
Assert.IsNull (import.Parent);
|
||||
|
||||
target = import.Targets [0];
|
||||
|
||||
Assert.AreEqual (ImportTargetKind.DefineAssemblyAlias, target.Kind);
|
||||
Assert.AreEqual ("XML", target.Alias);
|
||||
Assert.AreEqual ("System.Xml", target.AssemblyReference.Name);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StateMachineKickOff ()
|
||||
{
|
||||
TestPortablePdbModule (module => {
|
||||
var state_machine = module.GetType ("PdbTarget.Program/<Baz>d__7");
|
||||
var main = state_machine.GetMethod ("MoveNext");
|
||||
var symbol = main.DebugInformation;
|
||||
|
||||
Assert.IsNotNull (symbol);
|
||||
Assert.IsNotNull (symbol.StateMachineKickOffMethod);
|
||||
Assert.AreEqual ("System.Threading.Tasks.Task PdbTarget.Program::Baz(System.IO.StreamReader)", symbol.StateMachineKickOffMethod.FullName);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StateMachineCustomDebugInformation ()
|
||||
{
|
||||
TestPortablePdbModule (module => {
|
||||
var state_machine = module.GetType ("PdbTarget.Program/<Baz>d__7");
|
||||
var move_next = state_machine.GetMethod ("MoveNext");
|
||||
|
||||
Assert.IsTrue (move_next.HasCustomDebugInformations);
|
||||
Assert.AreEqual (2, move_next.CustomDebugInformations.Count);
|
||||
|
||||
var state_machine_scope = move_next.CustomDebugInformations [0] as StateMachineScopeDebugInformation;
|
||||
Assert.IsNotNull (state_machine_scope);
|
||||
Assert.AreEqual (0, state_machine_scope.Start.Offset);
|
||||
Assert.IsTrue (state_machine_scope.End.IsEndOfMethod);
|
||||
|
||||
var async_body = move_next.CustomDebugInformations [1] as AsyncMethodBodyDebugInformation;
|
||||
Assert.IsNotNull (async_body);
|
||||
Assert.AreEqual (-1, async_body.CatchHandler.Offset);
|
||||
|
||||
Assert.AreEqual (2, async_body.Yields.Count);
|
||||
Assert.AreEqual (61, async_body.Yields [0].Offset);
|
||||
Assert.AreEqual (221, async_body.Yields [1].Offset);
|
||||
|
||||
Assert.AreEqual (2, async_body.Resumes.Count);
|
||||
Assert.AreEqual (91, async_body.Resumes [0].Offset);
|
||||
Assert.AreEqual (252, async_body.Resumes [1].Offset);
|
||||
|
||||
Assert.AreEqual (move_next, async_body.MoveNextMethod);
|
||||
});
|
||||
}
|
||||
|
||||
void TestPortablePdbModule (Action<ModuleDefinition> test)
|
||||
{
|
||||
TestModule ("PdbTarget.exe", test, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
|
||||
TestModule ("EmbeddedPdbTarget.exe", test, verify: !Platform.OnMono);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RoundTripCecilPortablePdb ()
|
||||
{
|
||||
TestModule ("cecil.dll", module => {
|
||||
Assert.IsTrue (module.HasSymbols);
|
||||
}, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
|
||||
}
|
||||
}
|
||||
}
|
||||
128
external/linker/cecil/Test/Mono.Cecil.Tests/PropertyTests.cs
vendored
Normal file
128
external/linker/cecil/Test/Mono.Cecil.Tests/PropertyTests.cs
vendored
Normal file
@@ -0,0 +1,128 @@
|
||||
using System.Linq;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Mono.Cecil.Tests {
|
||||
|
||||
[TestFixture]
|
||||
public class PropertyTests : BaseTestFixture {
|
||||
|
||||
[Test]
|
||||
public void AbstractMethod ()
|
||||
{
|
||||
TestCSharp ("Properties.cs", module => {
|
||||
var type = module.GetType ("Foo");
|
||||
|
||||
Assert.IsTrue (type.HasProperties);
|
||||
|
||||
var properties = type.Properties;
|
||||
|
||||
Assert.AreEqual (3, properties.Count);
|
||||
|
||||
var property = properties [0];
|
||||
|
||||
Assert.IsNotNull (property);
|
||||
Assert.AreEqual ("Bar", property.Name);
|
||||
Assert.IsNotNull (property.PropertyType);
|
||||
Assert.AreEqual ("System.Int32", property.PropertyType.FullName);
|
||||
|
||||
Assert.IsNotNull (property.GetMethod);
|
||||
Assert.AreEqual (MethodSemanticsAttributes.Getter, property.GetMethod.SemanticsAttributes);
|
||||
Assert.IsNull (property.SetMethod);
|
||||
|
||||
property = properties [1];
|
||||
|
||||
Assert.IsNotNull (property);
|
||||
Assert.AreEqual ("Baz", property.Name);
|
||||
Assert.IsNotNull (property.PropertyType);
|
||||
Assert.AreEqual ("System.String", property.PropertyType.FullName);
|
||||
|
||||
Assert.IsNotNull (property.GetMethod);
|
||||
Assert.AreEqual (MethodSemanticsAttributes.Getter, property.GetMethod.SemanticsAttributes);
|
||||
Assert.IsNotNull (property.SetMethod);
|
||||
Assert.AreEqual (MethodSemanticsAttributes.Setter, property.SetMethod.SemanticsAttributes);
|
||||
|
||||
property = properties [2];
|
||||
|
||||
Assert.IsNotNull (property);
|
||||
Assert.AreEqual ("Gazonk", property.Name);
|
||||
Assert.IsNotNull (property.PropertyType);
|
||||
Assert.AreEqual ("System.String", property.PropertyType.FullName);
|
||||
|
||||
Assert.IsNull (property.GetMethod);
|
||||
Assert.IsNotNull (property.SetMethod);
|
||||
Assert.AreEqual (MethodSemanticsAttributes.Setter, property.SetMethod.SemanticsAttributes);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OtherMethod ()
|
||||
{
|
||||
TestIL ("others.il", module => {
|
||||
var type = module.GetType ("Others");
|
||||
|
||||
Assert.IsTrue (type.HasProperties);
|
||||
|
||||
var properties = type.Properties;
|
||||
|
||||
Assert.AreEqual (1, properties.Count);
|
||||
|
||||
var property = properties [0];
|
||||
|
||||
Assert.IsNotNull (property);
|
||||
Assert.AreEqual ("Context", property.Name);
|
||||
Assert.IsNotNull (property.PropertyType);
|
||||
Assert.AreEqual ("System.String", property.PropertyType.FullName);
|
||||
|
||||
Assert.IsTrue (property.HasOtherMethods);
|
||||
|
||||
Assert.AreEqual (2, property.OtherMethods.Count);
|
||||
|
||||
var other = property.OtherMethods [0];
|
||||
Assert.AreEqual ("let_Context", other.Name);
|
||||
|
||||
other = property.OtherMethods [1];
|
||||
Assert.AreEqual ("bet_Context", other.Name);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SetOnlyIndexer ()
|
||||
{
|
||||
TestCSharp ("Properties.cs", module => {
|
||||
var type = module.GetType ("Bar");
|
||||
var indexer = type.Properties.Where (property => property.Name == "Item").First ();
|
||||
|
||||
var parameters = indexer.Parameters;
|
||||
|
||||
Assert.AreEqual (2, parameters.Count);
|
||||
Assert.AreEqual ("System.Int32", parameters [0].ParameterType.FullName);
|
||||
Assert.AreEqual ("System.String", parameters [1].ParameterType.FullName);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadSemanticsFirst ()
|
||||
{
|
||||
TestCSharp ("Properties.cs", module => {
|
||||
var type = module.GetType ("Baz");
|
||||
var setter = type.GetMethod ("set_Bingo");
|
||||
|
||||
Assert.AreEqual (MethodSemanticsAttributes.Setter, setter.SemanticsAttributes);
|
||||
|
||||
var property = type.Properties.Where (p => p.Name == "Bingo").First ();
|
||||
|
||||
Assert.AreEqual (setter, property.SetMethod);
|
||||
Assert.AreEqual (type.GetMethod ("get_Bingo"), property.GetMethod);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UnattachedProperty ()
|
||||
{
|
||||
var property = new PropertyDefinition ("Property", PropertyAttributes.None, typeof (int).ToDefinition ());
|
||||
|
||||
Assert.IsNull (property.GetMethod);
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user