New tests.
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / FieldBuilderTest.cs
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
7
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;
17
18 using NUnit.Framework;
19
20 namespace MonoTests.System.Reflection.Emit
21 {
22         [TestFixture]
23         public class FieldBuilderTest
24         {
25                 private static int typeIndexer = 0;
26                 private TypeBuilder _tb;
27                 private ModuleBuilder module;
28
29                 [SetUp]
30                 protected void SetUp ()
31                 {
32                         AssemblyName assemblyName = new AssemblyName ();
33                         assemblyName.Name = "MonoTests.System.Reflection.Emit.FieldBuilderTest";
34
35                         AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
36                                 assemblyName, AssemblyBuilderAccess.Run);
37
38                         module = assembly.DefineDynamicModule ("module1");
39                         _tb = module.DefineType (genTypeName (), TypeAttributes.Public);
40                 }
41
42                 [Test]
43                 public void TestFieldProperties ()
44                 {
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);
52                 }
53
54                 [Test]
55                 [ExpectedException (typeof(NotSupportedException))]
56                 public void TestFieldHandleIncomplete ()
57                 {
58                         FieldBuilder field = _tb.DefineField ("name",
59                                 typeof(string), FieldAttributes.Public);
60                         RuntimeFieldHandle handle = field.FieldHandle;
61                 }
62
63                 [Test]
64                 [ExpectedException (typeof(NotSupportedException))]
65                 public void TestFieldHandleComplete ()
66                 {
67                         FieldBuilder field = _tb.DefineField ("name",
68                                 typeof(string), FieldAttributes.Public);
69                         _tb.CreateType ();
70                         RuntimeFieldHandle handle = field.FieldHandle;
71                 }
72
73                 [Test]
74                 [ExpectedException (typeof(NotSupportedException))]
75                 public void TestGetCustomAttributesIncomplete ()
76                 {
77                         FieldBuilder field = _tb.DefineField ("name",
78                                 typeof(string), FieldAttributes.Public);
79                         field.GetCustomAttributes (false);
80                 }
81
82                 [Test]
83                 [ExpectedException (typeof(NotSupportedException))]
84                 [Ignore ("mono supports this")]
85                 public void TestGetCustomAttributesComplete ()
86                 {
87                         FieldBuilder field = _tb.DefineField ("name",
88                                 typeof(string), FieldAttributes.Public);
89                         _tb.CreateType ();
90                         field.GetCustomAttributes (false);
91                 }
92
93                 [Test]
94                 [ExpectedException (typeof(NotSupportedException))]
95                 public void TestGetCustomAttributesOfTypeIncomplete ()
96                 {
97                         FieldBuilder field = _tb.DefineField ("name",
98                                 typeof(string), FieldAttributes.Public);
99                         field.GetCustomAttributes (typeof(ObsoleteAttribute), false);
100                 }
101
102                 [Test]
103                 [ExpectedException (typeof(NotSupportedException))]
104                 [Ignore ("mono supports this")]
105                 public void TestGetCustomAttributesOfTypeComplete ()
106                 {
107                         FieldBuilder field = _tb.DefineField ("name",
108                                 typeof(string), FieldAttributes.Public);
109                         _tb.CreateType ();
110                         field.GetCustomAttributes (typeof(ObsoleteAttribute), false);
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof(NotSupportedException))]
115                 public void TestGetValueIncomplete ()
116                 {
117                         FieldBuilder field = _tb.DefineField ("name",
118                                 typeof(string), FieldAttributes.Public);
119                         field.GetValue (_tb);
120                 }
121
122                 [Test]
123                 [ExpectedException (typeof(NotSupportedException))]
124                 public void TestGetValueComplete ()
125                 {
126                         FieldBuilder field = _tb.DefineField ("name",
127                                 typeof(string), FieldAttributes.Public);
128                         _tb.CreateType ();
129                         field.GetValue (_tb);
130                 }
131
132                 [Test]
133                 [ExpectedException (typeof(NotSupportedException))]
134                 public void TestIsDefinedIncomplete ()
135                 {
136                         FieldBuilder field = _tb.DefineField ("name",
137                                 typeof(string), FieldAttributes.Public);
138                         field.IsDefined (typeof(ObsoleteAttribute), true);
139                 }
140
141                 [Test]
142                 [ExpectedException (typeof(NotSupportedException))]
143                 public void TestIsDefinedComplete ()
144                 {
145                         FieldBuilder field = _tb.DefineField ("name",
146                                 typeof(string), FieldAttributes.Public);
147                         _tb.CreateType ();
148                         field.IsDefined (typeof(ObsoleteAttribute), true);
149                 }
150
151                 [Test]
152                 public void TestSetConstantIncomplete ()
153                 {
154                         FieldBuilder field = _tb.DefineField ("name",
155                                 typeof(string), FieldAttributes.Public);
156                         field.SetConstant ("default");
157                 }
158
159                 [Test]
160                 [ExpectedException (typeof(InvalidOperationException))]
161                 public void TestSetConstantComplete ()
162                 {
163                         FieldBuilder field = _tb.DefineField ("name",
164                                 typeof(string), FieldAttributes.Public);
165                         _tb.CreateType ();
166                         field.SetConstant ("default");
167                 }
168
169                 [Test]
170                 [ExpectedException (typeof(InvalidOperationException))]
171                 public void TestSetCustomAttributeCaBuilderComplete ()
172                 {
173                         FieldBuilder field = _tb.DefineField ("name",
174                                 typeof(string), FieldAttributes.Public);
175                         _tb.CreateType ();
176
177                         ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor (
178                                 new Type[] {
179                                 typeof(string)
180                         });
181                         CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (guidCtor,
182                                 new object[] {
183                                 Guid.NewGuid ().ToString ("D")
184                         }, new FieldInfo[0], new object[0]);
185
186                         field.SetCustomAttribute (caBuilder);
187                 }
188
189                 [Test]
190                 [ExpectedException (typeof(InvalidOperationException))]
191                 public void TestSetCustomAttributeCtorComplete ()
192                 {
193                         FieldBuilder field = _tb.DefineField ("name",
194                                 typeof(string), FieldAttributes.Public);
195                         _tb.CreateType ();
196
197                         ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor (
198                                 new Type[] {
199                                 typeof(string)
200                         });
201
202                         field.SetCustomAttribute (guidCtor, new byte[] { 01,00,01,00,00 });
203                 }
204
205                 [Test]
206                 [ExpectedException (typeof(InvalidOperationException))]
207                 public void TestSetMarshalComplete ()
208                 {
209                         FieldBuilder field = _tb.DefineField ("name",
210                                 typeof(string), FieldAttributes.Public);
211                         _tb.CreateType ();
212                         field.SetMarshal (UnmanagedMarshal.DefineSafeArray (UnmanagedType.BStr));
213                 }
214
215                 [Test]
216                 [ExpectedException (typeof(InvalidOperationException))]
217                 public void TestSetOffsetComplete ()
218                 {
219                         FieldBuilder field = _tb.DefineField ("name",
220                                 typeof(string), FieldAttributes.Public);
221                         _tb.CreateType ();
222                         field.SetOffset (1);
223                 }
224
225                 [Test]
226                 [ExpectedException (typeof(NotSupportedException))]
227                 public void TestSetValueComplete ()
228                 {
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);
234                 }
235
236                 [Test]
237                 public void GetCustomAttributes ()
238                 {
239                         FieldBuilder field = _tb.DefineField ("name",
240                                 typeof(string), FieldAttributes.Public);
241
242                         Type attrType = typeof (ObsoleteAttribute);
243                         ConstructorInfo ctorInfo =
244                                 attrType.GetConstructor (new Type [] { typeof (String) });
245                         
246                         field.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
247
248                         Type t = _tb.CreateType ();
249
250                         // Try the created type
251                         {
252                                 FieldInfo fi = t.GetField ("name");
253                                 object[] attrs = fi.GetCustomAttributes (true);
254
255                                 Assert.AreEqual (1, attrs.Length);
256                                 Assert.IsTrue (attrs [0] is ObsoleteAttribute);
257                                 Assert.AreEqual ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
258                         }
259
260                         // Try the type builder
261                         {
262                                 FieldInfo fi = _tb.GetField ("name");
263                                 object[] attrs = fi.GetCustomAttributes (true);
264
265                                 Assert.AreEqual (1, attrs.Length);
266                                 Assert.IsTrue (attrs [0] is ObsoleteAttribute);
267                                 Assert.AreEqual ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
268                         }
269                 }
270
271                 // Return a unique type name
272                 private string genTypeName ()
273                 {
274                         return "class" + (typeIndexer++);
275                 }
276         }
277 }