PageRenderTime 33ms CodeModel.GetById 14ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/Mono.Cecil/symbols/pdb/Test/Mono.Cecil.Tests/PdbTests.cs

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