2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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                 public void TestGetCustomAttributesComplete ()
85                 {
86                         FieldBuilder field = _tb.DefineField ("name",
87                                 typeof(string), FieldAttributes.Public);
88                         _tb.CreateType ();
89                         field.GetCustomAttributes (false);
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof(NotSupportedException))]
94                 public void TestGetCustomAttributesOfTypeIncomplete ()
95                 {
96                         FieldBuilder field = _tb.DefineField ("name",
97                                 typeof(string), FieldAttributes.Public);
98                         field.GetCustomAttributes (typeof(ObsoleteAttribute), false);
99                 }
100
101                 [Test]
102                 [ExpectedException (typeof(NotSupportedException))]
103                 public void TestGetCustomAttributesOfTypeComplete ()
104                 {
105                         FieldBuilder field = _tb.DefineField ("name",
106                                 typeof(string), FieldAttributes.Public);
107                         _tb.CreateType ();
108                         field.GetCustomAttributes (typeof(ObsoleteAttribute), false);
109                 }
110
111                 [Test]
112                 [ExpectedException (typeof(NotSupportedException))]
113                 public void TestGetValueIncomplete ()
114                 {
115                         FieldBuilder field = _tb.DefineField ("name",
116                                 typeof(string), FieldAttributes.Public);
117                         field.GetValue (_tb);
118                 }
119
120                 [Test]
121                 [ExpectedException (typeof(NotSupportedException))]
122                 public void TestGetValueComplete ()
123                 {
124                         FieldBuilder field = _tb.DefineField ("name",
125                                 typeof(string), FieldAttributes.Public);
126                         _tb.CreateType ();
127                         field.GetValue (_tb);
128                 }
129
130                 [Test]
131                 [ExpectedException (typeof(NotSupportedException))]
132                 public void TestIsDefinedIncomplete ()
133                 {
134                         FieldBuilder field = _tb.DefineField ("name",
135                                 typeof(string), FieldAttributes.Public);
136                         field.IsDefined (typeof(ObsoleteAttribute), true);
137                 }
138
139                 [Test]
140                 [ExpectedException (typeof(NotSupportedException))]
141                 public void TestIsDefinedComplete ()
142                 {
143                         FieldBuilder field = _tb.DefineField ("name",
144                                 typeof(string), FieldAttributes.Public);
145                         _tb.CreateType ();
146                         field.IsDefined (typeof(ObsoleteAttribute), true);
147                 }
148
149                 [Test]
150                 public void TestSetConstantIncomplete ()
151                 {
152                         FieldBuilder field = _tb.DefineField ("name",
153                                 typeof(string), FieldAttributes.Public);
154                         field.SetConstant ("default");
155                 }
156
157                 [Test]
158                 [ExpectedException (typeof(InvalidOperationException))]
159                 public void TestSetConstantComplete ()
160                 {
161                         FieldBuilder field = _tb.DefineField ("name",
162                                 typeof(string), FieldAttributes.Public);
163                         _tb.CreateType ();
164                         field.SetConstant ("default");
165                 }
166
167                 [Test]
168                 [ExpectedException (typeof(InvalidOperationException))]
169                 public void TestSetCustomAttributeCaBuilderComplete ()
170                 {
171                         FieldBuilder field = _tb.DefineField ("name",
172                                 typeof(string), FieldAttributes.Public);
173                         _tb.CreateType ();
174
175                         ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor (
176                                 new Type[] {
177                                 typeof(string)
178                         });
179                         CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (guidCtor,
180                                 new object[] {
181                                 Guid.NewGuid ().ToString ("D")
182                         }, new FieldInfo[0], new object[0]);
183
184                         field.SetCustomAttribute (caBuilder);
185                 }
186
187                 [Test]
188                 [ExpectedException (typeof(InvalidOperationException))]
189                 public void TestSetCustomAttributeCtorComplete ()
190                 {
191                         FieldBuilder field = _tb.DefineField ("name",
192                                 typeof(string), FieldAttributes.Public);
193                         _tb.CreateType ();
194
195                         ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor (
196                                 new Type[] {
197                                 typeof(string)
198                         });
199
200                         field.SetCustomAttribute (guidCtor, new byte[] { 01,00,01,00,00 });
201                 }
202
203                 [Test]
204                 [ExpectedException (typeof(InvalidOperationException))]
205                 public void TestSetMarshalComplete ()
206                 {
207                         FieldBuilder field = _tb.DefineField ("name",
208                                 typeof(string), FieldAttributes.Public);
209                         _tb.CreateType ();
210                         field.SetMarshal (UnmanagedMarshal.DefineSafeArray (UnmanagedType.BStr));
211                 }
212
213                 [Test]
214                 [ExpectedException (typeof(InvalidOperationException))]
215                 public void TestSetOffsetComplete ()
216                 {
217                         FieldBuilder field = _tb.DefineField ("name",
218                                 typeof(string), FieldAttributes.Public);
219                         _tb.CreateType ();
220                         field.SetOffset (1);
221                 }
222
223                 [Test]
224                 [ExpectedException (typeof(NotSupportedException))]
225                 public void TestSetValueComplete ()
226                 {
227                         FieldBuilder field = _tb.DefineField ("name",
228                                 typeof(string), FieldAttributes.Public);
229                         _tb.CreateType ();
230                         field.SetValue ((object) 1, 1, BindingFlags.Public, null,
231                                 CultureInfo.InvariantCulture);
232                 }
233
234                 // Return a unique type name
235                 private string genTypeName ()
236                 {
237                         return "class" + (typeIndexer++);
238                 }
239         }
240 }