PageRenderTime 31ms CodeModel.GetById 2ms app.highlight 25ms RepoModel.GetById 0ms app.codeStats 1ms

/Test/Mono.Cecil.Tests/TypeParserTests.cs

http://github.com/jbevain/cecil
C# | 397 lines | 306 code | 91 blank | 0 comment | 2 complexity | 4a35cb68f670f91d57c9aa4aab38e6bb MD5 | raw file
  1using System;
  2using System.Linq;
  3
  4using NUnit.Framework;
  5
  6namespace Mono.Cecil.Tests {
  7
  8	[TestFixture]
  9	public class TypeParserTests : BaseTestFixture {
 10
 11		[Test]
 12		public void SimpleStringReference ()
 13		{
 14			var module = GetCurrentModule ();
 15			var corlib = module.TypeSystem.CoreLibrary;
 16
 17			const string fullname = "System.String";
 18
 19			var type = TypeParser.ParseType (module, fullname);
 20			Assert.IsNotNull (type);
 21			Assert.AreEqual (corlib, type.Scope);
 22			Assert.AreEqual (module, type.Module);
 23			Assert.AreEqual ("System", type.Namespace);
 24			Assert.AreEqual ("String", type.Name);
 25			Assert.AreEqual (MetadataType.String, type.MetadataType);
 26			Assert.IsFalse (type.IsValueType);
 27			Assert.IsInstanceOf (typeof (TypeReference), type);
 28		}
 29
 30		[Test]
 31		public void SimpleInt32Reference ()
 32		{
 33			var module = GetCurrentModule ();
 34			var corlib = module.TypeSystem.CoreLibrary;
 35
 36			const string fullname = "System.Int32";
 37
 38			var type = TypeParser.ParseType (module, fullname);
 39			Assert.IsNotNull (type);
 40			Assert.AreEqual (corlib, type.Scope);
 41			Assert.AreEqual (module, type.Module);
 42			Assert.AreEqual ("System", type.Namespace);
 43			Assert.AreEqual ("Int32", type.Name);
 44			Assert.AreEqual (MetadataType.Int32, type.MetadataType);
 45			Assert.IsTrue (type.IsValueType);
 46			Assert.IsInstanceOf (typeof (TypeReference), type);
 47		}
 48
 49		[Test]
 50		public void SimpleTypeDefinition ()
 51		{
 52			var module = GetCurrentModule ();
 53
 54			const string fullname = "Mono.Cecil.Tests.TypeParserTests";
 55
 56			var type = TypeParser.ParseType (module, fullname);
 57			Assert.IsNotNull (type);
 58			Assert.AreEqual (module, type.Scope);
 59			Assert.AreEqual (module, type.Module);
 60			Assert.AreEqual ("Mono.Cecil.Tests", type.Namespace);
 61			Assert.AreEqual ("TypeParserTests", type.Name);
 62			Assert.IsInstanceOf (typeof (TypeDefinition), type);
 63		}
 64
 65		[Test]
 66		public void ByRefTypeReference ()
 67		{
 68			var module = GetCurrentModule ();
 69			var corlib = module.TypeSystem.CoreLibrary;
 70
 71			const string fullname = "System.String&";
 72
 73			var type = TypeParser.ParseType (module, fullname);
 74
 75			Assert.IsInstanceOf (typeof (ByReferenceType), type);
 76
 77			type = ((ByReferenceType) type).ElementType;
 78
 79			Assert.IsNotNull (type);
 80			Assert.AreEqual (corlib, type.Scope);
 81			Assert.AreEqual (module, type.Module);
 82			Assert.AreEqual ("System", type.Namespace);
 83			Assert.AreEqual ("String", type.Name);
 84			Assert.IsInstanceOf (typeof (TypeReference), type);
 85		}
 86
 87		[Test]
 88		public void FullyQualifiedTypeReference ()
 89		{
 90			var module = GetCurrentModule ();
 91			var cecil = module.AssemblyReferences.Where (reference => reference.Name == "Mono.Cecil").First ();
 92
 93			var fullname = "Mono.Cecil.TypeDefinition, " + cecil.FullName;
 94
 95			var type = TypeParser.ParseType (module, fullname);
 96			Assert.IsNotNull (type);
 97			Assert.AreEqual (cecil, type.Scope);
 98			Assert.AreEqual (module, type.Module);
 99			Assert.AreEqual ("Mono.Cecil", type.Namespace);
100			Assert.AreEqual ("TypeDefinition", type.Name);
101			Assert.IsInstanceOf (typeof (TypeReference), type);
102		}
103
104		[Test]
105		public void OpenGenericType ()
106		{
107			var module = GetCurrentModule ();
108			var corlib = module.TypeSystem.CoreLibrary;
109
110			const string fullname = "System.Collections.Generic.Dictionary`2";
111
112			var type = TypeParser.ParseType (module, fullname);
113			Assert.IsNotNull (type);
114			Assert.AreEqual (corlib, type.Scope);
115			Assert.AreEqual (module, type.Module);
116			Assert.AreEqual ("System.Collections.Generic", type.Namespace);
117			Assert.AreEqual ("Dictionary`2", type.Name);
118			Assert.IsInstanceOf (typeof (TypeReference), type);
119			Assert.AreEqual (2, type.GenericParameters.Count);
120		}
121
122		public class ID {}
123
124		[Test]
125		public void SimpleNestedType ()
126		{
127			var module = GetCurrentModule ();
128
129			const string fullname = "Mono.Cecil.Tests.TypeParserTests+ID";
130
131			var type = TypeParser.ParseType (module, fullname);
132
133			Assert.IsNotNull (type);
134			Assert.AreEqual (module, type.Module);
135			Assert.AreEqual (module, type.Scope);
136			Assert.AreEqual ("", type.Namespace);
137			Assert.AreEqual ("ID", type.Name);
138
139			Assert.AreEqual ("Mono.Cecil.Tests.TypeParserTests/ID", type.FullName);
140			Assert.AreEqual (fullname, TypeParser.ToParseable (type));
141		}
142
143		[Test]
144		public void TripleNestedTypeWithScope ()
145		{
146			var module = GetCurrentModule ();
147
148			const string fullname = "Bingo.Foo`1+Bar`1+Baz`1, Bingo";
149
150			var type = TypeParser.ParseType (module, fullname);
151
152			Assert.AreEqual ("Bingo.Foo`1+Bar`1+Baz`1, Bingo, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", TypeParser.ToParseable (type));
153
154			Assert.IsNotNull (type);
155			Assert.AreEqual ("Bingo", type.Scope.Name);
156			Assert.AreEqual (module, type.Module);
157			Assert.AreEqual ("", type.Namespace);
158			Assert.AreEqual ("Baz`1", type.Name);
159			Assert.IsInstanceOf (typeof (TypeReference), type);
160			Assert.AreEqual (1, type.GenericParameters.Count);
161
162			type = type.DeclaringType;
163
164			Assert.IsNotNull (type);
165			Assert.AreEqual ("Bingo", type.Scope.Name);
166			Assert.AreEqual (module, type.Module);
167			Assert.AreEqual ("", type.Namespace);
168			Assert.AreEqual ("Bar`1", type.Name);
169			Assert.IsInstanceOf (typeof (TypeReference), type);
170			Assert.AreEqual (1, type.GenericParameters.Count);
171
172			type = type.DeclaringType;
173
174			Assert.IsNotNull (type);
175			Assert.AreEqual ("Bingo", type.Scope.Name);
176			Assert.AreEqual (module, type.Module);
177			Assert.AreEqual ("Bingo", type.Namespace);
178			Assert.AreEqual ("Foo`1", type.Name);
179			Assert.IsInstanceOf (typeof (TypeReference), type);
180			Assert.AreEqual (1, type.GenericParameters.Count);
181		}
182
183		[Test]
184		public void Vector ()
185		{
186			var module = GetCurrentModule ();
187
188			const string fullname = "Bingo.Gazonk[], Bingo";
189
190			var type = TypeParser.ParseType (module, fullname);
191
192			Assert.AreEqual ("Bingo.Gazonk[], Bingo, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", TypeParser.ToParseable (type));
193
194			var array = type as ArrayType;
195			Assert.IsNotNull (array);
196			Assert.AreEqual (1, array.Rank);
197			Assert.IsTrue (array.IsVector);
198
199			type = array.ElementType;
200
201			Assert.IsNotNull (type);
202			Assert.AreEqual ("Bingo", type.Scope.Name);
203			Assert.AreEqual (module, type.Module);
204			Assert.AreEqual ("Bingo", type.Namespace);
205			Assert.AreEqual ("Gazonk", type.Name);
206			Assert.IsInstanceOf (typeof (TypeReference), type);
207		}
208
209		[Test]
210		public void ThreeDimensionalArray ()
211		{
212			var module = GetCurrentModule ();
213
214			const string fullname = "Bingo.Gazonk[,,], Bingo";
215
216			var type = TypeParser.ParseType (module, fullname);
217
218			var array = type as ArrayType;
219			Assert.IsNotNull (array);
220			Assert.AreEqual (3, array.Rank);
221			Assert.IsFalse (array.IsVector);
222
223			type = array.ElementType;
224
225			Assert.IsNotNull (type);
226			Assert.AreEqual ("Bingo", type.Scope.Name);
227			Assert.AreEqual (module, type.Module);
228			Assert.AreEqual ("Bingo", type.Namespace);
229			Assert.AreEqual ("Gazonk", type.Name);
230			Assert.IsInstanceOf (typeof (TypeReference), type);
231		}
232
233		[Test]
234		public void GenericInstanceExternArguments ()
235		{
236			if (Platform.OnCoreClr)
237				return;
238
239			var module = GetCurrentModule ();
240
241			var fullname = string.Format ("System.Collections.Generic.Dictionary`2[[System.Int32, {0}],[System.String, {0}]]",
242				typeof (object).Assembly.FullName);
243
244			var type = TypeParser.ParseType (module, fullname);
245
246			Assert.AreEqual (fullname, TypeParser.ToParseable (type));
247
248			var instance = type as GenericInstanceType;
249			Assert.IsNotNull (instance);
250			Assert.AreEqual (2, instance.GenericArguments.Count);
251			Assert.AreEqual ("mscorlib", type.Scope.Name);
252			Assert.AreEqual (module, type.Module);
253			Assert.AreEqual ("System.Collections.Generic", type.Namespace);
254			Assert.AreEqual ("Dictionary`2", type.Name);
255
256			type = instance.ElementType;
257
258			Assert.AreEqual (2, type.GenericParameters.Count);
259
260			var argument = instance.GenericArguments [0];
261			Assert.AreEqual ("mscorlib", argument.Scope.Name);
262			Assert.AreEqual (module, argument.Module);
263			Assert.AreEqual ("System", argument.Namespace);
264			Assert.AreEqual ("Int32", argument.Name);
265
266			argument = instance.GenericArguments [1];
267			Assert.AreEqual ("mscorlib", argument.Scope.Name);
268			Assert.AreEqual (module, argument.Module);
269			Assert.AreEqual ("System", argument.Namespace);
270			Assert.AreEqual ("String", argument.Name);
271		}
272
273		[Test]
274		public void GenericInstanceMixedArguments ()
275		{
276			var module = GetCurrentModule ();
277
278			var fullname = string.Format ("System.Collections.Generic.Dictionary`2[Mono.Cecil.Tests.TypeParserTests,[System.String, {0}]]",
279				typeof (object).Assembly.FullName);
280
281			var type = TypeParser.ParseType (module, fullname);
282
283			var instance = type as GenericInstanceType;
284			Assert.IsNotNull (instance);
285			Assert.AreEqual (2, instance.GenericArguments.Count);
286			Assert.AreEqual (Platform.OnCoreClr ? "System.Runtime" : "mscorlib", type.Scope.Name);
287			Assert.AreEqual (module, type.Module);
288			Assert.AreEqual ("System.Collections.Generic", type.Namespace);
289			Assert.AreEqual ("Dictionary`2", type.Name);
290
291			type = instance.ElementType;
292
293			Assert.AreEqual (2, type.GenericParameters.Count);
294
295			var argument = instance.GenericArguments [0];
296			Assert.IsInstanceOf (typeof (TypeDefinition), argument);
297			Assert.AreEqual (module, argument.Module);
298			Assert.AreEqual ("Mono.Cecil.Tests", argument.Namespace);
299			Assert.AreEqual ("TypeParserTests", argument.Name);
300
301			argument = instance.GenericArguments [1];
302			Assert.AreEqual (Platform.OnCoreClr ? "System.Private.CoreLib" : "mscorlib", argument.Scope.Name);
303			Assert.AreEqual (module, argument.Module);
304			Assert.AreEqual ("System", argument.Namespace);
305			Assert.AreEqual ("String", argument.Name);
306		}
307
308		public class Foo<TX, TY> {
309		}
310
311		public class Bar {}
312
313		[Test]
314		public void GenericInstanceTwoNonFqArguments ()
315		{
316			var module = GetCurrentModule ();
317
318			var fullname = string.Format ("System.Collections.Generic.Dictionary`2[Mono.Cecil.Tests.TypeParserTests+Bar,Mono.Cecil.Tests.TypeParserTests+Bar], {0}", typeof (object).Assembly.FullName);
319
320			var type = TypeParser.ParseType (module, fullname);
321
322			var instance = type as GenericInstanceType;
323			Assert.IsNotNull (instance);
324			Assert.AreEqual (2, instance.GenericArguments.Count);
325			Assert.AreEqual (Platform.OnCoreClr ? "System.Private.CoreLib" : "mscorlib", type.Scope.Name);
326			Assert.AreEqual (module, type.Module);
327			Assert.AreEqual ("System.Collections.Generic", type.Namespace);
328			Assert.AreEqual ("Dictionary`2", type.Name);
329
330			type = instance.ElementType;
331
332			Assert.AreEqual (2, type.GenericParameters.Count);
333
334			var argument = instance.GenericArguments [0];
335			Assert.AreEqual (module, argument.Module);
336			Assert.AreEqual ("", argument.Namespace);
337			Assert.AreEqual ("Bar", argument.Name);
338			Assert.IsInstanceOf (typeof (TypeDefinition), argument);
339
340			argument = instance.GenericArguments [1];
341			Assert.AreEqual (module, argument.Module);
342			Assert.AreEqual ("", argument.Namespace);
343			Assert.AreEqual ("Bar", argument.Name);
344			Assert.IsInstanceOf (typeof (TypeDefinition), argument);
345		}
346
347		[Test]
348		public void ComplexGenericInstanceMixedArguments ()
349		{
350			var module = GetCurrentModule ();
351
352			var fullname = string.Format ("System.Collections.Generic.Dictionary`2[[System.String, {0}],Mono.Cecil.Tests.TypeParserTests+Foo`2[Mono.Cecil.Tests.TypeParserTests,[System.Int32, {0}]]]",
353				typeof (object).Assembly.FullName);
354
355			var type = TypeParser.ParseType (module, fullname);
356
357			var instance = type as GenericInstanceType;
358			Assert.IsNotNull (instance);
359			Assert.AreEqual (2, instance.GenericArguments.Count);
360			Assert.AreEqual (Platform.OnCoreClr ? "System.Runtime" : "mscorlib", type.Scope.Name);
361			Assert.AreEqual (module, type.Module);
362			Assert.AreEqual ("System.Collections.Generic", type.Namespace);
363			Assert.AreEqual ("Dictionary`2", type.Name);
364
365			type = instance.ElementType;
366
367			Assert.AreEqual (2, type.GenericParameters.Count);
368
369			var argument = instance.GenericArguments [0];
370			Assert.AreEqual (Platform.OnCoreClr ? "System.Private.CoreLib" : "mscorlib", argument.Scope.Name);
371			Assert.AreEqual (module, argument.Module);
372			Assert.AreEqual ("System", argument.Namespace);
373			Assert.AreEqual ("String", argument.Name);
374
375			argument = instance.GenericArguments [1];
376
377			instance = argument as GenericInstanceType;
378			Assert.IsNotNull (instance);
379			Assert.AreEqual (2, instance.GenericArguments.Count);
380			Assert.AreEqual (module, instance.Module);
381			Assert.AreEqual ("Mono.Cecil.Tests.TypeParserTests/Foo`2", instance.ElementType.FullName);
382			Assert.IsInstanceOf (typeof (TypeDefinition), instance.ElementType);
383
384			argument = instance.GenericArguments [0];
385			Assert.AreEqual (module, argument.Module);
386			Assert.AreEqual ("Mono.Cecil.Tests", argument.Namespace);
387			Assert.AreEqual ("TypeParserTests", argument.Name);
388			Assert.IsInstanceOf (typeof (TypeDefinition), argument);
389
390			argument = instance.GenericArguments [1];
391			Assert.AreEqual (Platform.OnCoreClr ? "System.Private.CoreLib" : "mscorlib", argument.Scope.Name);
392			Assert.AreEqual (module, argument.Module);
393			Assert.AreEqual ("System", argument.Namespace);
394			Assert.AreEqual ("Int32", argument.Name);
395		}
396	}
397}