2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / Test / System.Reflection.Emit / FieldBuilderTest.cs
blob1cb51009f2bb24d047ff577fea96ca7fe36a49d6
1 //
2 // FieldBuilderTest.cs - NUnit Test Cases for the FieldBuilder class
3 //
4 // Gert Driesen (drieseng@users.sourceforge.net)
5 //
6 // (C) Novell, Inc. http://www.novell.com
8 using System;
9 using System.Globalization;
10 using System.Threading;
11 using System.Reflection;
12 using System.Reflection.Emit;
13 using System.Runtime.CompilerServices;
14 using System.Security;
15 using System.Security.Permissions;
16 using System.Runtime.InteropServices;
18 using NUnit.Framework;
20 namespace MonoTests.System.Reflection.Emit
22 [TestFixture]
23 public class FieldBuilderTest
25 private static int typeIndexer = 0;
26 private TypeBuilder _tb;
27 private ModuleBuilder module;
29 [SetUp]
30 protected void SetUp ()
32 AssemblyName assemblyName = new AssemblyName ();
33 assemblyName.Name = "MonoTests.System.Reflection.Emit.FieldBuilderTest";
35 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
36 assemblyName, AssemblyBuilderAccess.Run);
38 module = assembly.DefineDynamicModule ("module1");
39 _tb = module.DefineType (genTypeName (), TypeAttributes.Public);
42 [Test]
43 public void TestFieldProperties ()
45 FieldBuilder field = _tb.DefineField ("name",
46 typeof(string), FieldAttributes.Public);
47 Assert.AreEqual (FieldAttributes.Public, field.Attributes);
48 Assert.AreEqual (_tb, field.DeclaringType);
49 Assert.AreEqual (typeof(string), field.FieldType);
50 Assert.AreEqual ("name", field.Name);
51 Assert.AreEqual (_tb, field.ReflectedType);
54 [Test]
55 [ExpectedException (typeof(NotSupportedException))]
56 public void TestFieldHandleIncomplete ()
58 FieldBuilder field = _tb.DefineField ("name",
59 typeof(string), FieldAttributes.Public);
60 RuntimeFieldHandle handle = field.FieldHandle;
63 [Test]
64 [ExpectedException (typeof(NotSupportedException))]
65 public void TestFieldHandleComplete ()
67 FieldBuilder field = _tb.DefineField ("name",
68 typeof(string), FieldAttributes.Public);
69 _tb.CreateType ();
70 RuntimeFieldHandle handle = field.FieldHandle;
73 [Test]
74 [ExpectedException (typeof(NotSupportedException))]
75 public void TestGetCustomAttributesIncomplete ()
77 FieldBuilder field = _tb.DefineField ("name",
78 typeof(string), FieldAttributes.Public);
79 field.GetCustomAttributes (false);
82 [Test]
83 [ExpectedException (typeof(NotSupportedException))]
84 [Ignore ("mono supports this")]
85 public void TestGetCustomAttributesComplete ()
87 FieldBuilder field = _tb.DefineField ("name",
88 typeof(string), FieldAttributes.Public);
89 _tb.CreateType ();
90 field.GetCustomAttributes (false);
93 [Test]
94 [ExpectedException (typeof(NotSupportedException))]
95 public void TestGetCustomAttributesOfTypeIncomplete ()
97 FieldBuilder field = _tb.DefineField ("name",
98 typeof(string), FieldAttributes.Public);
99 field.GetCustomAttributes (typeof(ObsoleteAttribute), false);
102 [Test]
103 [ExpectedException (typeof(NotSupportedException))]
104 [Ignore ("mono supports this")]
105 public void TestGetCustomAttributesOfTypeComplete ()
107 FieldBuilder field = _tb.DefineField ("name",
108 typeof(string), FieldAttributes.Public);
109 _tb.CreateType ();
110 field.GetCustomAttributes (typeof(ObsoleteAttribute), false);
113 [Test]
114 [ExpectedException (typeof(NotSupportedException))]
115 public void TestGetValueIncomplete ()
117 FieldBuilder field = _tb.DefineField ("name",
118 typeof(string), FieldAttributes.Public);
119 field.GetValue (_tb);
122 [Test]
123 [ExpectedException (typeof(NotSupportedException))]
124 public void TestGetValueComplete ()
126 FieldBuilder field = _tb.DefineField ("name",
127 typeof(string), FieldAttributes.Public);
128 _tb.CreateType ();
129 field.GetValue (_tb);
132 [Test]
133 [ExpectedException (typeof(NotSupportedException))]
134 public void TestIsDefinedIncomplete ()
136 FieldBuilder field = _tb.DefineField ("name",
137 typeof(string), FieldAttributes.Public);
138 field.IsDefined (typeof(ObsoleteAttribute), true);
141 [Test]
142 [ExpectedException (typeof(NotSupportedException))]
143 public void TestIsDefinedComplete ()
145 FieldBuilder field = _tb.DefineField ("name",
146 typeof(string), FieldAttributes.Public);
147 _tb.CreateType ();
148 field.IsDefined (typeof(ObsoleteAttribute), true);
151 [Test]
152 public void TestSetConstantIncomplete ()
154 FieldBuilder field = _tb.DefineField ("name",
155 typeof(string), FieldAttributes.Public);
156 field.SetConstant ("default");
159 [Test]
160 [ExpectedException (typeof(InvalidOperationException))]
161 public void TestSetConstantComplete ()
163 FieldBuilder field = _tb.DefineField ("name",
164 typeof(string), FieldAttributes.Public);
165 _tb.CreateType ();
166 field.SetConstant ("default");
169 [Test]
170 [ExpectedException (typeof(InvalidOperationException))]
171 public void TestSetCustomAttributeCaBuilderComplete ()
173 FieldBuilder field = _tb.DefineField ("name",
174 typeof(string), FieldAttributes.Public);
175 _tb.CreateType ();
177 ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor (
178 new Type[] {
179 typeof(string)
181 CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (guidCtor,
182 new object[] {
183 Guid.NewGuid ().ToString ("D")
184 }, new FieldInfo[0], new object[0]);
186 field.SetCustomAttribute (caBuilder);
189 [Test]
190 [ExpectedException (typeof(InvalidOperationException))]
191 public void TestSetCustomAttributeCtorComplete ()
193 FieldBuilder field = _tb.DefineField ("name",
194 typeof(string), FieldAttributes.Public);
195 _tb.CreateType ();
197 ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor (
198 new Type[] {
199 typeof(string)
202 field.SetCustomAttribute (guidCtor, new byte[] { 01,00,01,00,00 });
205 [Test]
206 [ExpectedException (typeof(InvalidOperationException))]
207 public void TestSetMarshalComplete ()
209 FieldBuilder field = _tb.DefineField ("name",
210 typeof(string), FieldAttributes.Public);
211 _tb.CreateType ();
212 field.SetMarshal (UnmanagedMarshal.DefineSafeArray (UnmanagedType.BStr));
215 [Test]
216 [ExpectedException (typeof(InvalidOperationException))]
217 public void TestSetOffsetComplete ()
219 FieldBuilder field = _tb.DefineField ("name",
220 typeof(string), FieldAttributes.Public);
221 _tb.CreateType ();
222 field.SetOffset (1);
225 [Test]
226 [ExpectedException (typeof(NotSupportedException))]
227 public void TestSetValueComplete ()
229 FieldBuilder field = _tb.DefineField ("name",
230 typeof(string), FieldAttributes.Public);
231 _tb.CreateType ();
232 field.SetValue ((object) 1, 1, BindingFlags.Public, null,
233 CultureInfo.InvariantCulture);
236 [Test]
237 public void GetCustomAttributes ()
239 FieldBuilder field = _tb.DefineField ("name",
240 typeof(string), FieldAttributes.Public);
242 Type attrType = typeof (ObsoleteAttribute);
243 ConstructorInfo ctorInfo =
244 attrType.GetConstructor (new Type [] { typeof (String) });
246 field.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
248 Type t = _tb.CreateType ();
250 // Try the created type
252 FieldInfo fi = t.GetField ("name");
253 object[] attrs = fi.GetCustomAttributes (true);
255 Assert.AreEqual (1, attrs.Length);
256 Assert.IsTrue (attrs [0] is ObsoleteAttribute);
257 Assert.AreEqual ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
260 // Try the type builder
262 FieldInfo fi = _tb.GetField ("name");
263 object[] attrs = fi.GetCustomAttributes (true);
265 Assert.AreEqual (1, attrs.Length);
266 Assert.IsTrue (attrs [0] is ObsoleteAttribute);
267 Assert.AreEqual ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
271 // Return a unique type name
272 private string genTypeName ()
274 return "class" + (typeIndexer++);