PageRenderTime 27ms CodeModel.GetById 1ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Test/Mono.Cecil.Tests/FieldTests.cs

http://github.com/jbevain/cecil
C# | 372 lines | 292 code | 80 blank | 0 comment | 1 complexity | 7c2a5adbd10aff9d99f6104b6f0275ac MD5 | raw file
  1using System;
  2
  3using Mono.Cecil.PE;
  4
  5using NUnit.Framework;
  6
  7namespace Mono.Cecil.Tests {
  8
  9	[TestFixture]
 10	public class FieldTests : BaseTestFixture {
 11
 12		[Test]
 13		public void TypeDefField ()
 14		{
 15			TestCSharp ("Fields.cs", module => {
 16				var type = module.Types [1];
 17				Assert.AreEqual ("Foo", type.Name);
 18				Assert.AreEqual (1, type.Fields.Count);
 19
 20				var field = type.Fields [0];
 21				Assert.AreEqual ("bar", field.Name);
 22				Assert.AreEqual (1, field.MetadataToken.RID);
 23				Assert.IsNotNull (field.FieldType);
 24				Assert.AreEqual ("Bar", field.FieldType.FullName);
 25				Assert.AreEqual (TokenType.Field, field.MetadataToken.TokenType);
 26				Assert.IsFalse (field.HasConstant);
 27				Assert.IsNull (field.Constant);
 28			});
 29		}
 30
 31		[Test]
 32		public void PrimitiveTypes ()
 33		{
 34			TestCSharp ("Fields.cs", module => {
 35				var type = module.GetType ("Baz");
 36
 37				AssertField (type, "char", typeof (char));
 38				AssertField (type, "bool", typeof (bool));
 39				AssertField (type, "sbyte", typeof (sbyte));
 40				AssertField (type, "byte", typeof (byte));
 41				AssertField (type, "int16", typeof (short));
 42				AssertField (type, "uint16", typeof (ushort));
 43				AssertField (type, "int32", typeof (int));
 44				AssertField (type, "uint32", typeof (uint));
 45				AssertField (type, "int64", typeof (long));
 46				AssertField (type, "uint64", typeof (ulong));
 47				AssertField (type, "single", typeof (float));
 48				AssertField (type, "double", typeof (double));
 49				AssertField (type, "string", typeof (string));
 50				AssertField (type, "object", typeof (object));
 51			});
 52		}
 53
 54		[Test]
 55		public void VolatileField ()
 56		{
 57			TestCSharp ("Fields.cs", module => {
 58				var type = module.GetType ("Bar");
 59
 60				Assert.IsTrue (type.HasFields);
 61				Assert.AreEqual (1, type.Fields.Count);
 62
 63				var field = type.Fields [0];
 64
 65				Assert.AreEqual ("oiseau", field.Name);
 66				Assert.AreEqual ("System.Int32 modreq(System.Runtime.CompilerServices.IsVolatile)", field.FieldType.FullName);
 67
 68				Assert.IsFalse (field.HasConstant);
 69			});
 70		}
 71
 72		[Test]
 73		public void FieldLayout ()
 74		{
 75			TestCSharp ("Layouts.cs", module => {
 76				var foo = module.GetType ("Foo");
 77				Assert.IsNotNull (foo);
 78
 79				Assert.IsTrue (foo.HasFields);
 80
 81				var fields = foo.Fields;
 82
 83				var field = fields [0];
 84
 85				Assert.AreEqual ("Bar", field.Name);
 86				Assert.IsTrue (field.HasLayoutInfo);
 87				Assert.AreEqual (0, field.Offset);
 88
 89				field = fields [1];
 90
 91				Assert.AreEqual ("Baz", field.Name);
 92				Assert.IsTrue (field.HasLayoutInfo);
 93				Assert.AreEqual (2, field.Offset);
 94
 95				field = fields [2];
 96
 97				Assert.AreEqual ("Gazonk", field.Name);
 98				Assert.IsTrue (field.HasLayoutInfo);
 99				Assert.AreEqual (4, field.Offset);
100			});
101		}
102
103		[Test]
104		public void FieldRVA ()
105		{
106			TestCSharp ("Layouts.cs", module => {
107				var priv_impl = GetPrivateImplementationType (module);
108				Assert.IsNotNull (priv_impl);
109
110				Assert.AreEqual (1, priv_impl.Fields.Count);
111
112				var field = priv_impl.Fields [0];
113
114				Assert.IsNotNull (field);
115				Assert.AreNotEqual (0, field.RVA);
116				Assert.IsNotNull (field.InitialValue);
117				Assert.AreEqual (16, field.InitialValue.Length);
118
119				var buffer = new ByteBuffer (field.InitialValue);
120
121				Assert.AreEqual (1, buffer.ReadUInt32 ());
122				Assert.AreEqual (2, buffer.ReadUInt32 ());
123				Assert.AreEqual (3, buffer.ReadUInt32 ());
124				Assert.AreEqual (4, buffer.ReadUInt32 ());
125			});
126		}
127
128		[Test]
129		public void GenericFieldDefinition ()
130		{
131			TestCSharp ("Generics.cs", module => {
132				var bar = module.GetType ("Bar`1");
133				Assert.IsNotNull (bar);
134
135				Assert.IsTrue (bar.HasGenericParameters);
136				var t = bar.GenericParameters [0];
137
138				Assert.AreEqual ("T", t.Name);
139				Assert.AreEqual (t.Owner, bar);
140
141				var bang = bar.GetField ("bang");
142
143				Assert.IsNotNull (bang);
144
145				Assert.AreEqual (t, bang.FieldType);
146			});
147		}
148
149		[Test]
150		public void ArrayFields ()
151		{
152			TestIL ("types.il", module => {
153				var types = module.GetType ("Types");
154				Assert.IsNotNull (types);
155
156				var rank_two = types.GetField ("rank_two");
157
158				var array = rank_two.FieldType as ArrayType;
159				Assert.IsNotNull (array);
160
161				Assert.AreEqual (2, array.Rank);
162				Assert.IsFalse (array.Dimensions [0].IsSized);
163				Assert.IsFalse (array.Dimensions [1].IsSized);
164
165				var rank_two_low_bound_zero = types.GetField ("rank_two_low_bound_zero");
166
167				array = rank_two_low_bound_zero.FieldType as ArrayType;
168				Assert.IsNotNull (array);
169
170				Assert.AreEqual (2, array.Rank);
171				Assert.IsTrue (array.Dimensions [0].IsSized);
172				Assert.AreEqual (0, array.Dimensions [0].LowerBound);
173				Assert.AreEqual (null, array.Dimensions [0].UpperBound);
174				Assert.IsTrue (array.Dimensions [1].IsSized);
175				Assert.AreEqual (0, array.Dimensions [1].LowerBound);
176				Assert.AreEqual (null, array.Dimensions [1].UpperBound);
177
178				var rank_one_low_bound_m1 = types.GetField ("rank_one_low_bound_m1");
179				array = rank_one_low_bound_m1.FieldType as ArrayType;
180				Assert.IsNotNull (array);
181
182				Assert.AreEqual (1, array.Rank);
183				Assert.IsTrue (array.Dimensions [0].IsSized);
184				Assert.AreEqual (-1, array.Dimensions [0].LowerBound);
185				Assert.AreEqual (4, array.Dimensions [0].UpperBound);
186			});
187		}
188
189		[Test]
190		public void EnumFieldsConstant ()
191		{
192			TestCSharp ("Fields.cs", module => {
193				var pim = module.GetType ("Pim");
194				Assert.IsNotNull (pim);
195
196				var field = pim.GetField ("Pam");
197				Assert.IsTrue (field.HasConstant);
198				Assert.AreEqual (1, (int) field.Constant);
199
200				field = pim.GetField ("Poum");
201				Assert.AreEqual (2, (int) field.Constant);
202			});
203		}
204
205		[Test]
206		public void StringAndClassConstant ()
207		{
208			TestCSharp ("Fields.cs", module => {
209				var panpan = module.GetType ("PanPan");
210				Assert.IsNotNull (panpan);
211
212				var field = panpan.GetField ("Peter");
213				Assert.IsTrue (field.HasConstant);
214				Assert.IsNull (field.Constant);
215
216				field = panpan.GetField ("QQ");
217				Assert.AreEqual ("qq", (string) field.Constant);
218
219				field = panpan.GetField ("nil");
220				Assert.AreEqual (null, (string) field.Constant);
221			});
222		}
223
224		[Test]
225		public void ObjectConstant ()
226		{
227			TestCSharp ("Fields.cs", module => {
228				var panpan = module.GetType ("PanPan");
229				Assert.IsNotNull (panpan);
230
231				var field = panpan.GetField ("obj");
232				Assert.IsTrue (field.HasConstant);
233				Assert.IsNull (field.Constant);
234			});
235		}
236
237		[Test]
238		public void NullPrimitiveConstant ()
239		{
240			TestIL ("types.il", module => {
241				var fields = module.GetType ("Fields");
242
243				var field = fields.GetField ("int32_nullref");
244				Assert.IsTrue (field.HasConstant);
245				Assert.AreEqual (null, field.Constant);
246			});
247		}
248
249		[Test]
250		public void ArrayConstant ()
251		{
252			TestCSharp ("Fields.cs", module => {
253				var panpan = module.GetType ("PanPan");
254				Assert.IsNotNull (panpan);
255
256				var field = panpan.GetField ("ints");
257				Assert.IsTrue (field.HasConstant);
258				Assert.IsNull (field.Constant);
259			});
260		}
261
262		[Test]
263		public void ConstantCoalescing ()
264		{
265			TestIL ("types.il", module => {
266				var fields = module.GetType ("Fields");
267
268				var field = fields.GetField ("int32_int16");
269				Assert.AreEqual ("System.Int32", field.FieldType.FullName);
270				Assert.IsTrue (field.HasConstant);
271				Assert.IsInstanceOf (typeof (short), field.Constant);
272				Assert.AreEqual ((short) 1, field.Constant);
273
274				field = fields.GetField ("int16_int32");
275				Assert.AreEqual ("System.Int16", field.FieldType.FullName);
276				Assert.IsTrue (field.HasConstant);
277				Assert.IsInstanceOf (typeof (int), field.Constant);
278				Assert.AreEqual (1, field.Constant);
279
280				field = fields.GetField ("char_int16");
281				Assert.AreEqual ("System.Char", field.FieldType.FullName);
282				Assert.IsTrue (field.HasConstant);
283				Assert.IsInstanceOf (typeof (short), field.Constant);
284				Assert.AreEqual ((short) 1, field.Constant);
285
286				field = fields.GetField ("int16_char");
287				Assert.AreEqual ("System.Int16", field.FieldType.FullName);
288				Assert.IsTrue (field.HasConstant);
289				Assert.IsInstanceOf (typeof (char), field.Constant);
290				Assert.AreEqual ('s', field.Constant);
291			});
292		}
293
294		[Test]
295		public void NestedEnumOfGenericTypeDefinition ()
296		{
297			TestCSharp ("Generics.cs", module => {
298				var dang = module.GetType ("Bongo`1/Dang");
299				Assert.IsNotNull (dang);
300
301				var field = dang.GetField ("Ding");
302				Assert.IsNotNull (field);
303				Assert.AreEqual (2, field.Constant);
304
305				field = dang.GetField ("Dong");
306				Assert.IsNotNull (field);
307				Assert.AreEqual (12, field.Constant);
308			});
309		}
310
311		[Test]
312		public void MarshalAsFixedStr ()
313		{
314			TestModule ("marshal.dll", module => {
315				var boc = module.GetType ("Boc");
316				var field = boc.GetField ("a");
317
318				Assert.IsNotNull (field);
319
320				Assert.IsTrue (field.HasMarshalInfo);
321
322				var info = (FixedSysStringMarshalInfo) field.MarshalInfo;
323
324				Assert.AreEqual (42, info.Size);
325			});
326		}
327
328		[Test]
329		public void MarshalAsFixedArray ()
330		{
331			TestModule ("marshal.dll", module => {
332				var boc = module.GetType ("Boc");
333				var field = boc.GetField ("b");
334
335				Assert.IsNotNull (field);
336
337				Assert.IsTrue (field.HasMarshalInfo);
338
339				var info = (FixedArrayMarshalInfo) field.MarshalInfo;
340
341				Assert.AreEqual (12, info.Size);
342				Assert.AreEqual (NativeType.Boolean, info.ElementType);
343			});
344		}
345
346		[Test]
347		public void UnattachedField ()
348		{
349			var field = new FieldDefinition ("Field", FieldAttributes.Public, typeof (int).ToDefinition ());
350
351			Assert.IsFalse (field.HasConstant);
352			Assert.IsNull (field.Constant);
353		}
354
355		static TypeDefinition GetPrivateImplementationType (ModuleDefinition module)
356		{
357			foreach (var type in module.Types)
358				if (type.FullName.Contains ("<PrivateImplementationDetails>"))
359					return type;
360
361			return null;
362		}
363
364		static void AssertField (TypeDefinition type, string name, Type expected)
365		{
366			var field = type.GetField (name);
367			Assert.IsNotNull (field, name);
368
369			Assert.AreEqual (expected.FullName, field.FieldType.FullName);
370		}
371	}
372}