PageRenderTime 69ms CodeModel.GetById 28ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/icsharpcode/ILSpy
C# | 394 lines | 304 code | 90 blank | 0 comment | 1 complexity | c094bd60d0a1fba15bd8546528d80833 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.Corlib;
 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.Corlib;
 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.Corlib;
 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.Corlib;
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, 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, 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			var module = GetCurrentModule ();
237
238			var fullname = string.Format ("System.Collections.Generic.Dictionary`2[[System.Int32, {0}],[System.String, {0}]]",
239				typeof (object).Assembly.FullName);
240
241			var type = TypeParser.ParseType (module, fullname);
242
243			Assert.AreEqual (fullname, TypeParser.ToParseable (type));
244
245			var instance = type as GenericInstanceType;
246			Assert.IsNotNull (instance);
247			Assert.AreEqual (2, instance.GenericArguments.Count);
248			Assert.AreEqual ("mscorlib", type.Scope.Name);
249			Assert.AreEqual (module, type.Module);
250			Assert.AreEqual ("System.Collections.Generic", type.Namespace);
251			Assert.AreEqual ("Dictionary`2", type.Name);
252
253			type = instance.ElementType;
254
255			Assert.AreEqual (2, type.GenericParameters.Count);
256
257			var argument = instance.GenericArguments [0];
258			Assert.AreEqual ("mscorlib", argument.Scope.Name);
259			Assert.AreEqual (module, argument.Module);
260			Assert.AreEqual ("System", argument.Namespace);
261			Assert.AreEqual ("Int32", argument.Name);
262
263			argument = instance.GenericArguments [1];
264			Assert.AreEqual ("mscorlib", argument.Scope.Name);
265			Assert.AreEqual (module, argument.Module);
266			Assert.AreEqual ("System", argument.Namespace);
267			Assert.AreEqual ("String", argument.Name);
268		}
269
270		[Test]
271		public void GenericInstanceMixedArguments ()
272		{
273			var module = GetCurrentModule ();
274
275			var fullname = string.Format ("System.Collections.Generic.Dictionary`2[Mono.Cecil.Tests.TypeParserTests,[System.String, {0}]]",
276				typeof (object).Assembly.FullName);
277
278			var type = TypeParser.ParseType (module, fullname);
279
280			var instance = type as GenericInstanceType;
281			Assert.IsNotNull (instance);
282			Assert.AreEqual (2, instance.GenericArguments.Count);
283			Assert.AreEqual ("mscorlib", type.Scope.Name);
284			Assert.AreEqual (module, type.Module);
285			Assert.AreEqual ("System.Collections.Generic", type.Namespace);
286			Assert.AreEqual ("Dictionary`2", type.Name);
287
288			type = instance.ElementType;
289
290			Assert.AreEqual (2, type.GenericParameters.Count);
291
292			var argument = instance.GenericArguments [0];
293			Assert.IsInstanceOf (typeof (TypeDefinition), argument);
294			Assert.AreEqual (module, argument.Module);
295			Assert.AreEqual ("Mono.Cecil.Tests", argument.Namespace);
296			Assert.AreEqual ("TypeParserTests", argument.Name);
297
298			argument = instance.GenericArguments [1];
299			Assert.AreEqual ("mscorlib", argument.Scope.Name);
300			Assert.AreEqual (module, argument.Module);
301			Assert.AreEqual ("System", argument.Namespace);
302			Assert.AreEqual ("String", argument.Name);
303		}
304
305		public class Foo<TX, TY> {
306		}
307
308		public class Bar {}
309
310		[Test]
311		public void GenericInstanceTwoNonFqArguments ()
312		{
313			var module = GetCurrentModule ();
314
315			var fullname = string.Format ("System.Collections.Generic.Dictionary`2[Mono.Cecil.Tests.TypeParserTests+Bar,Mono.Cecil.Tests.TypeParserTests+Bar], {0}", typeof (object).Assembly.FullName);
316
317			var type = TypeParser.ParseType (module, fullname);
318
319			var instance = type as GenericInstanceType;
320			Assert.IsNotNull (instance);
321			Assert.AreEqual (2, instance.GenericArguments.Count);
322			Assert.AreEqual ("mscorlib", type.Scope.Name);
323			Assert.AreEqual (module, type.Module);
324			Assert.AreEqual ("System.Collections.Generic", type.Namespace);
325			Assert.AreEqual ("Dictionary`2", type.Name);
326
327			type = instance.ElementType;
328
329			Assert.AreEqual (2, type.GenericParameters.Count);
330
331			var argument = instance.GenericArguments [0];
332			Assert.AreEqual (module, argument.Module);
333			Assert.AreEqual ("", argument.Namespace);
334			Assert.AreEqual ("Bar", argument.Name);
335			Assert.IsInstanceOf (typeof (TypeDefinition), argument);
336
337			argument = instance.GenericArguments [1];
338			Assert.AreEqual (module, argument.Module);
339			Assert.AreEqual ("", argument.Namespace);
340			Assert.AreEqual ("Bar", argument.Name);
341			Assert.IsInstanceOf (typeof (TypeDefinition), argument);
342		}
343
344		[Test]
345		public void ComplexGenericInstanceMixedArguments ()
346		{
347			var module = GetCurrentModule ();
348
349			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}]]]",
350				typeof (object).Assembly.FullName);
351
352			var type = TypeParser.ParseType (module, fullname);
353
354			var instance = type as GenericInstanceType;
355			Assert.IsNotNull (instance);
356			Assert.AreEqual (2, instance.GenericArguments.Count);
357			Assert.AreEqual ("mscorlib", type.Scope.Name);
358			Assert.AreEqual (module, type.Module);
359			Assert.AreEqual ("System.Collections.Generic", type.Namespace);
360			Assert.AreEqual ("Dictionary`2", type.Name);
361
362			type = instance.ElementType;
363
364			Assert.AreEqual (2, type.GenericParameters.Count);
365
366			var argument = instance.GenericArguments [0];
367			Assert.AreEqual ("mscorlib", argument.Scope.Name);
368			Assert.AreEqual (module, argument.Module);
369			Assert.AreEqual ("System", argument.Namespace);
370			Assert.AreEqual ("String", argument.Name);
371
372			argument = instance.GenericArguments [1];
373
374			instance = argument as GenericInstanceType;
375			Assert.IsNotNull (instance);
376			Assert.AreEqual (2, instance.GenericArguments.Count);
377			Assert.AreEqual (module, instance.Module);
378			Assert.AreEqual ("Mono.Cecil.Tests.TypeParserTests/Foo`2", instance.ElementType.FullName);
379			Assert.IsInstanceOf (typeof (TypeDefinition), instance.ElementType);
380
381			argument = instance.GenericArguments [0];
382			Assert.AreEqual (module, argument.Module);
383			Assert.AreEqual ("Mono.Cecil.Tests", argument.Namespace);
384			Assert.AreEqual ("TypeParserTests", argument.Name);
385			Assert.IsInstanceOf (typeof (TypeDefinition), argument);
386
387			argument = instance.GenericArguments [1];
388			Assert.AreEqual ("mscorlib", argument.Scope.Name);
389			Assert.AreEqual (module, argument.Module);
390			Assert.AreEqual ("System", argument.Namespace);
391			Assert.AreEqual ("Int32", argument.Name);
392		}
393	}
394}