PageRenderTime 44ms CodeModel.GetById 2ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 0ms

/Test/Mono.Cecil.Tests/TypeTests.cs

http://github.com/jbevain/cecil
C# | 298 lines | 234 code | 63 blank | 1 comment | 3 complexity | 7180539f2ea019d25ba85fd2732e4113 MD5 | raw file
  1using System;
  2using System.Linq;
  3
  4using Mono.Cecil;
  5using Mono.Cecil.Cil;
  6using Mono.Cecil.Metadata;
  7
  8using NUnit.Framework;
  9
 10namespace Mono.Cecil.Tests {
 11
 12	[TestFixture]
 13	public class TypeTests : BaseTestFixture {
 14
 15		[Test]
 16		public void TypeLayout ()
 17		{
 18			TestCSharp ("Layouts.cs", module => {
 19				var foo = module.GetType ("Foo");
 20				Assert.IsNotNull (foo);
 21				Assert.IsTrue (foo.IsValueType);
 22
 23				Assert.IsTrue (foo.HasLayoutInfo);
 24				Assert.AreEqual (16, foo.ClassSize);
 25
 26				var babar = module.GetType ("Babar");
 27				Assert.IsNotNull (babar);
 28				Assert.IsFalse (babar.IsValueType);
 29				Assert.IsFalse (babar.HasLayoutInfo);
 30			});
 31		}
 32
 33		[Test]
 34		public void EmptyStructLayout ()
 35		{
 36			TestModule ("hello.exe", module =>
 37			{
 38				var foo = new TypeDefinition ("", "Foo",
 39					TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit | TypeAttributes.SequentialLayout,
 40					module.ImportReference (typeof (ValueType))) ;
 41
 42				module.Types.Add (foo) ;
 43			}) ;
 44		}
 45
 46		[Test]
 47		public void SimpleInterfaces ()
 48		{
 49			TestIL ("types.il", module => {
 50				var ibaz = module.GetType ("IBaz");
 51				Assert.IsNotNull (ibaz);
 52
 53				Assert.IsTrue (ibaz.HasInterfaces);
 54
 55				var interfaces = ibaz.Interfaces;
 56
 57				Assert.AreEqual (2, interfaces.Count);
 58
 59				// Mono's ilasm and .NET's are ordering interfaces differently
 60				Assert.IsNotNull (interfaces.Single (i => i.InterfaceType.FullName == "IBar"));
 61				Assert.IsNotNull (interfaces.Single (i => i.InterfaceType.FullName == "IFoo"));
 62			});
 63		}
 64
 65		[Test]
 66		public void GenericTypeDefinition ()
 67		{
 68			TestCSharp ("Generics.cs", module => {
 69				var foo = module.GetType ("Foo`2");
 70				Assert.IsNotNull (foo);
 71
 72				Assert.IsTrue (foo.HasGenericParameters);
 73				Assert.AreEqual (2, foo.GenericParameters.Count);
 74
 75				var tbar = foo.GenericParameters [0];
 76
 77				Assert.AreEqual ("TBar", tbar.Name);
 78				Assert.AreEqual (foo, tbar.Owner);
 79
 80				var tbaz = foo.GenericParameters [1];
 81
 82				Assert.AreEqual ("TBaz", tbaz.Name);
 83				Assert.AreEqual (foo, tbaz.Owner);
 84			});
 85		}
 86
 87		[Test]
 88		public void ConstrainedGenericType ()
 89		{
 90			TestCSharp ("Generics.cs", module => {
 91				var bongo_t = module.GetType ("Bongo`1");
 92				Assert.IsNotNull (bongo_t);
 93
 94				var t = bongo_t.GenericParameters [0];
 95				Assert.IsNotNull (t);
 96				Assert.AreEqual ("T", t.Name);
 97
 98				Assert.IsTrue (t.HasConstraints);
 99				Assert.AreEqual (2, t.Constraints.Count);
100
101				Assert.AreEqual ("Zap", t.Constraints [0].ConstraintType.FullName);
102				Assert.AreEqual ("IZoom", t.Constraints [1].ConstraintType.FullName);
103			});
104		}
105
106		[Test]
107		public void GenericBaseType ()
108		{
109			TestCSharp ("Generics.cs", module => {
110				var child = module.GetType ("Child`1");
111
112				var child_t = child.GenericParameters [0];
113				Assert.IsNotNull (child_t);
114
115				var instance = child.BaseType as GenericInstanceType;
116				Assert.IsNotNull (instance);
117				Assert.AreNotEqual (0, instance.MetadataToken.RID);
118
119				Assert.AreEqual (child_t, instance.GenericArguments [0]);
120			});
121		}
122
123		[Test]
124		public void GenericConstraintOnGenericParameter ()
125		{
126			TestCSharp ("Generics.cs", module => {
127				var duel = module.GetType ("Duel`3");
128
129				Assert.AreEqual (3, duel.GenericParameters.Count);
130
131				var t1 = duel.GenericParameters [0];
132				var t2 = duel.GenericParameters [1];
133				var t3 = duel.GenericParameters [2];
134
135				Assert.AreEqual (t1, t2.Constraints [0].ConstraintType);
136				Assert.AreEqual (t2, t3.Constraints [0].ConstraintType);
137			});
138		}
139
140		[Test]
141		public void GenericForwardBaseType ()
142		{
143			TestCSharp ("Generics.cs", module => {
144				var tamchild = module.GetType ("TamChild");
145
146				Assert.IsNotNull (tamchild);
147				Assert.IsNotNull (tamchild.BaseType);
148
149				var generic_instance = tamchild.BaseType as GenericInstanceType;
150
151				Assert.IsNotNull (generic_instance);
152
153				Assert.AreEqual (1, generic_instance.GenericArguments.Count);
154				Assert.AreEqual (module.GetType ("Tamtam"), generic_instance.GenericArguments [0]);
155			});
156		}
157
158		[Test]
159		public void TypeExtentingGenericOfSelf ()
160		{
161			TestCSharp ("Generics.cs", module => {
162				var rec_child = module.GetType ("RecChild");
163
164				Assert.IsNotNull (rec_child);
165				Assert.IsNotNull (rec_child.BaseType);
166
167				var generic_instance = rec_child.BaseType as GenericInstanceType;
168
169				Assert.IsNotNull (generic_instance);
170
171				Assert.AreEqual (1, generic_instance.GenericArguments.Count);
172				Assert.AreEqual (rec_child, generic_instance.GenericArguments [0]);
173			});
174		}
175
176		[Test]
177		public void TypeReferenceValueType ()
178		{
179			TestCSharp ("Methods.cs", module => {
180				var baz = module.GetType ("Baz");
181				var method = baz.GetMethod ("PrintAnswer");
182
183				var box = method.Body.Instructions.Where (i => i.OpCode == OpCodes.Box).First ();
184				var int32 = (TypeReference) box.Operand;
185
186				Assert.IsTrue (int32.IsValueType);
187			});
188		}
189
190		[Test]
191		public void GenericInterfaceReference ()
192		{
193			TestModule ("gifaceref.exe", module => {
194				var type = module.GetType ("Program");
195				var iface = type.Interfaces [0];
196
197				var instance = (GenericInstanceType) iface.InterfaceType;
198				var owner = instance.ElementType;
199
200				Assert.AreEqual (1, instance.GenericArguments.Count);
201				Assert.AreEqual (1, owner.GenericParameters.Count);
202			});
203		}
204
205		[Test]
206		public void UnboundGenericParameter ()
207		{
208			TestModule ("cscgpbug.dll", module => {
209				var type = module.GetType ("ListViewModel");
210				var method = type.GetMethod ("<>n__FabricatedMethod1");
211
212				var parameter = method.ReturnType as GenericParameter;
213
214				Assert.IsNotNull (parameter);
215				Assert.AreEqual (0, parameter.Position);
216				Assert.IsNull (parameter.Owner);
217			}, verify: false);
218		}
219
220		[Test]
221		public void GenericMultidimensionalArray ()
222		{
223			TestCSharp ("Generics.cs", module => {
224				var type = module.GetType ("LaMatrix");
225				var method = type.GetMethod ("At");
226
227				var call = method.Body.Instructions.Where (i => i.Operand is MethodReference).First ();
228				var get = (MethodReference) call.Operand;
229
230				Assert.IsNotNull (get);
231				Assert.AreEqual (0, get.GenericParameters.Count);
232				Assert.AreEqual (MethodCallingConvention.Default, get.CallingConvention);
233				Assert.AreEqual (method.GenericParameters [0], get.ReturnType);
234			});
235		}
236
237		[Test]
238		public void CorlibPrimitive ()
239		{
240			var module = typeof (TypeTests).ToDefinition ().Module;
241
242			var int32 = module.TypeSystem.Int32;
243			Assert.IsTrue (int32.IsPrimitive);
244			Assert.AreEqual (MetadataType.Int32, int32.MetadataType);
245
246			var int32_def = int32.Resolve ();
247			Assert.IsTrue (int32_def.IsPrimitive);
248			Assert.AreEqual (MetadataType.Int32, int32_def.MetadataType);
249		}
250
251		[Test]
252		public void ExplicitThis ()
253		{
254			TestIL ("explicitthis.il", module => {
255				var type = module.GetType ("MakeDecision");
256				var method = type.GetMethod ("Decide");
257				var fptr = method.ReturnType as FunctionPointerType;
258
259				Assert.IsNotNull (fptr);
260				Assert.IsTrue (fptr.HasThis);
261				Assert.IsTrue (fptr.ExplicitThis);
262
263				Assert.AreEqual (0, fptr.Parameters [0].Sequence);
264				Assert.AreEqual (1, fptr.Parameters [1].Sequence);
265			}, verify: false);
266		}
267
268		[Test]
269		public void DeferredCorlibTypeDef ()
270		{
271			using (var module = ModuleDefinition.ReadModule (typeof (object).Assembly.Location, new ReaderParameters (ReadingMode.Deferred))) {
272				var object_type = module.TypeSystem.Object;
273				Assert.IsInstanceOf<TypeDefinition> (object_type);
274			}
275		}
276
277		[Test]
278		public void CorlibTypesMetadataType ()
279		{
280			using (var module = ModuleDefinition.ReadModule (typeof (object).Assembly.Location)) {
281				var type = module.GetType ("System.String");
282				Assert.IsNotNull (type);
283				Assert.IsNotNull (type.BaseType);
284				Assert.AreEqual ("System.Object", type.BaseType.FullName);
285				Assert.IsInstanceOf<TypeDefinition> (type.BaseType);
286				Assert.AreEqual (MetadataType.String, type.MetadataType);
287				Assert.AreEqual (MetadataType.Object, type.BaseType.MetadataType);
288			}
289		}
290
291		[Test]
292		public void SelfReferencingTypeRef ()
293		{
294			TestModule ("self-ref-typeref.dll", module => {
295			}, verify: false);
296		}
297	}
298}