| 1 |
|
| 2 |
using System.IO; |
| 3 |
using System.Linq; |
| 4 |
|
| 5 |
using Mono.Cecil.Cil; |
| 6 |
using Mono.Cecil.Pdb; |
| 7 |
|
| 8 |
using NUnit.Framework; |
| 9 |
|
| 10 |
namespace Mono.Cecil.Tests { |
| 11 |
|
| 12 |
[TestFixture] |
| 13 |
public class PdbTests : BaseTestFixture { |
| 14 |
|
| 15 |
[Test] |
| 16 |
public void Main () |
| 17 |
{ |
| 18 |
TestModule ("test.exe", module => { |
| 19 |
var type = module.GetType ("Program"); |
| 20 |
var main = type.GetMethod ("Main"); |
| 21 |
|
| 22 |
AssertCode (@" |
| 23 |
.locals init (System.Int32 i, System.Int32 CS$1$0000, System.Boolean CS$4$0001) |
| 24 |
.line 6,6:2,3 'c:\sources\cecil\symbols\Mono.Cecil.Pdb\Test\Resources\assemblies\test.cs' |
| 25 |
IL_0000: nop |
| 26 |
.line 7,7:8,18 'c:\sources\cecil\symbols\Mono.Cecil.Pdb\Test\Resources\assemblies\test.cs' |
| 27 |
IL_0001: ldc.i4.0 |
| 28 |
IL_0002: stloc.0 |
| 29 |
.line 16707566,16707566:0,0 'c:\sources\cecil\symbols\Mono.Cecil.Pdb\Test\Resources\assemblies\test.cs' |
| 30 |
IL_0003: br.s IL_0012 |
| 31 |
.line 8,8:4,21 'c:\sources\cecil\symbols\Mono.Cecil.Pdb\Test\Resources\assemblies\test.cs' |
| 32 |
IL_0005: ldarg.0 |
| 33 |
IL_0006: ldloc.0 |
| 34 |
IL_0007: ldelem.ref |
| 35 |
IL_0008: call System.Void Program::Print(System.String) |
| 36 |
IL_000d: nop |
| 37 |
.line 7,7:36,39 'c:\sources\cecil\symbols\Mono.Cecil.Pdb\Test\Resources\assemblies\test.cs' |
| 38 |
IL_000e: ldloc.0 |
| 39 |
IL_000f: ldc.i4.1 |
| 40 |
IL_0010: add |
| 41 |
IL_0011: stloc.0 |
| 42 |
.line 7,7:19,34 'c:\sources\cecil\symbols\Mono.Cecil.Pdb\Test\Resources\assemblies\test.cs' |
| 43 |
IL_0012: ldloc.0 |
| 44 |
IL_0013: ldarg.0 |
| 45 |
IL_0014: ldlen |
| 46 |
IL_0015: conv.i4 |
| 47 |
IL_0016: clt |
| 48 |
IL_0018: stloc.2 |
| 49 |
.line 16707566,16707566:0,0 'c:\sources\cecil\symbols\Mono.Cecil.Pdb\Test\Resources\assemblies\test.cs' |
| 50 |
IL_0019: ldloc.2 |
| 51 |
IL_001a: brtrue.s IL_0005 |
| 52 |
.line 10,10:3,12 'c:\sources\cecil\symbols\Mono.Cecil.Pdb\Test\Resources\assemblies\test.cs' |
| 53 |
IL_001c: ldc.i4.0 |
| 54 |
IL_001d: stloc.1 |
| 55 |
IL_001e: br.s IL_0020 |
| 56 |
.line 11,11:2,3 'c:\sources\cecil\symbols\Mono.Cecil.Pdb\Test\Resources\assemblies\test.cs' |
| 57 |
IL_0020: ldloc.1 |
| 58 |
IL_0021: ret |
| 59 |
", main); |
| 60 |
}, readOnly: Platform.OnMono, symbolReaderProvider: typeof(PdbReaderProvider), symbolWriterProvider: typeof(PdbWriterProvider)); |
| 61 |
} |
| 62 |
|
| 63 |
[Test] |
| 64 |
public void Document () |
| 65 |
{ |
| 66 |
TestModule ("test.exe", module => { |
| 67 |
var type = module.GetType ("Program"); |
| 68 |
var method = type.GetMethod ("Main"); |
| 69 |
|
| 70 |
var sequence_point = method.Body.Instructions.Where (i => i.SequencePoint != null).First ().SequencePoint; |
| 71 |
var document = sequence_point.Document; |
| 72 |
|
| 73 |
Assert.IsNotNull (document); |
| 74 |
|
| 75 |
Assert.AreEqual (@"c:\sources\cecil\symbols\Mono.Cecil.Pdb\Test\Resources\assemblies\test.cs", document.Url); |
| 76 |
Assert.AreEqual (DocumentType.Text, document.Type); |
| 77 |
Assert.AreEqual (DocumentHashAlgorithm.None, document.HashAlgorithm); |
| 78 |
Assert.AreEqual (DocumentLanguage.CSharp, document.Language); |
| 79 |
Assert.AreEqual (DocumentLanguageVendor.Microsoft, document.LanguageVendor); |
| 80 |
}, readOnly: Platform.OnMono, symbolReaderProvider: typeof(PdbReaderProvider), symbolWriterProvider: typeof(PdbWriterProvider)); |
| 81 |
} |
| 82 |
|
| 83 |
[Test] |
| 84 |
public void BasicDocument () |
| 85 |
{ |
| 86 |
TestModule ("VBConsApp.exe", module => { |
| 87 |
var type = module.GetType ("VBConsApp.Program"); |
| 88 |
var method = type.GetMethod ("Main"); |
| 89 |
|
| 90 |
var sequence_point = method.Body.Instructions.Where (i => i.SequencePoint != null).First ().SequencePoint; |
| 91 |
var document = sequence_point.Document; |
| 92 |
|
| 93 |
Assert.IsNotNull (document); |
| 94 |
|
| 95 |
Assert.AreEqual (@"c:\tmp\VBConsApp\Program.vb", document.Url); |
| 96 |
Assert.AreEqual (DocumentType.Text, document.Type); |
| 97 |
Assert.AreEqual (DocumentHashAlgorithm.None, document.HashAlgorithm); |
| 98 |
Assert.AreEqual (DocumentLanguage.Basic, document.Language); |
| 99 |
Assert.AreEqual (DocumentLanguageVendor.Microsoft, document.LanguageVendor); |
| 100 |
}, readOnly: Platform.OnMono, symbolReaderProvider: typeof(PdbReaderProvider), symbolWriterProvider: typeof(PdbWriterProvider)); |
| 101 |
} |
| 102 |
|
| 103 |
[Test] |
| 104 |
public void FSharpDocument () |
| 105 |
{ |
| 106 |
TestModule ("fsapp.exe", module => { |
| 107 |
var type = module.GetType ("Program"); |
| 108 |
var method = type.GetMethod ("fact"); |
| 109 |
|
| 110 |
var sequence_point = method.Body.Instructions.Where (i => i.SequencePoint != null).First ().SequencePoint; |
| 111 |
var document = sequence_point.Document; |
| 112 |
|
| 113 |
Assert.IsNotNull (document); |
| 114 |
|
| 115 |
Assert.AreEqual (@"c:\tmp\fsapp\Program.fs", document.Url); |
| 116 |
Assert.AreEqual (DocumentType.Text, document.Type); |
| 117 |
Assert.AreEqual (DocumentHashAlgorithm.None, document.HashAlgorithm); |
| 118 |
Assert.AreEqual (DocumentLanguage.FSharp, document.Language); |
| 119 |
Assert.AreEqual (DocumentLanguageVendor.Microsoft, document.LanguageVendor); |
| 120 |
}, readOnly: Platform.OnMono, symbolReaderProvider: typeof(PdbReaderProvider), symbolWriterProvider: typeof(PdbWriterProvider)); |
| 121 |
} |
| 122 |
|
| 123 |
[Test] |
| 124 |
public void CreateMethodFromScratch () |
| 125 |
{ |
| 126 |
IgnoreOnMono (); |
| 127 |
|
| 128 |
var module = ModuleDefinition.CreateModule ("Pan", ModuleKind.Dll); |
| 129 |
var type = new TypeDefinition ("Pin", "Pon", TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.Sealed, module.Import (typeof (object))); |
| 130 |
module.Types.Add (type); |
| 131 |
|
| 132 |
var method = new MethodDefinition ("Pang", MethodAttributes.Public | MethodAttributes.Static, module.Import (typeof (string))); |
| 133 |
type.Methods.Add (method); |
| 134 |
|
| 135 |
var body = method.Body; |
| 136 |
|
| 137 |
body.InitLocals = true; |
| 138 |
|
| 139 |
var il = body.GetILProcessor (); |
| 140 |
var temp = new VariableDefinition ("temp", module.Import (typeof (string))); |
| 141 |
body.Variables.Add (temp); |
| 142 |
|
| 143 |
il.Emit (OpCodes.Nop); |
| 144 |
il.Emit (OpCodes.Ldstr, "hello"); |
| 145 |
il.Emit (OpCodes.Stloc, temp); |
| 146 |
il.Emit (OpCodes.Ldloc, temp); |
| 147 |
il.Emit (OpCodes.Ret); |
| 148 |
|
| 149 |
body.Instructions [0].SequencePoint = new SequencePoint (new Document (@"C:\test.cs")) { |
| 150 |
StartLine = 0, |
| 151 |
StartColumn = 0, |
| 152 |
EndLine = 0, |
| 153 |
EndColumn = 4, |
| 154 |
}; |
| 155 |
|
| 156 |
var file = Path.Combine (Path.GetTempPath (), "Pan.dll"); |
| 157 |
module.Write (file, new WriterParameters { |
| 158 |
SymbolWriterProvider = new PdbWriterProvider (), |
| 159 |
}); |
| 160 |
|
| 161 |
module = ModuleDefinition.ReadModule (file, new ReaderParameters { |
| 162 |
SymbolReaderProvider = new PdbReaderProvider (), |
| 163 |
}); |
| 164 |
|
| 165 |
method = module.GetType ("Pin.Pon").GetMethod ("Pang"); |
| 166 |
|
| 167 |
Assert.AreEqual ("temp", method.Body.Variables [0].Name); |
| 168 |
} |
| 169 |
|
| 170 |
static void AssertCode (string expected, MethodDefinition method) |
| 171 |
{ |
| 172 |
Assert.IsTrue (method.HasBody); |
| 173 |
Assert.IsNotNull (method.Body); |
| 174 |
|
| 175 |
Assert.AreEqual (Normalize (expected), Normalize (Formatter.FormatMethodBody (method))); |
| 176 |
} |
| 177 |
|
| 178 |
static string Normalize (string str) |
| 179 |
{ |
| 180 |
return str.Trim ().Replace ("\r\n", "\n"); |
| 181 |
} |
| 182 |
} |
| 183 |
} |