[resgen] Implement conditional resources (#if/#ifdef)
[mono.git] / mcs / class / corlib / Test / System.Reflection / ModuleTest.cs
1 //
2 // ModuleTest - NUnit Test Cases for the Module class
3 //
4 // Zoltan Varga (vargaz@freemail.hu)
5 //
6 // (C) Ximian, Inc.  http://www.ximian.com
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 using System;
11 using System.Threading;
12 using System.Reflection;
13 #if !MONOTOUCH
14 using System.Reflection.Emit;
15 #endif
16 using System.Runtime.Serialization;
17 using System.IO;
18 using System.Collections;
19
20 using NUnit.Framework;
21
22 namespace MonoTests.System.Reflection
23 {
24 [TestFixture]
25 public class ModuleTest
26 {
27         static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.Reflection.ModuleTest");
28
29         [SetUp]
30         public void SetUp ()
31         {
32                 while (Directory.Exists (TempFolder))
33                         TempFolder = Path.Combine (TempFolder, "2");
34                 Directory.CreateDirectory (TempFolder);
35         }
36
37         [TearDown]
38         public void TearDown ()
39         {
40                 try {
41                         // This throws an exception under MS.NET, since the directory contains loaded
42                         // assemblies.
43                         Directory.Delete (TempFolder, true);
44                 } catch (Exception) {
45                 }
46         }
47
48         [Test]
49         public void IsDefined_AttributeType_Null ()
50         {
51                 Type t = typeof (ModuleTest);
52                 Module module = t.Module;
53
54                 try {
55                         module.IsDefined ((Type) null, false);
56                         Assert.Fail ("#1");
57                 } catch (ArgumentNullException ex) {
58                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
59                         Assert.IsNull (ex.InnerException, "#3");
60                         Assert.IsNotNull (ex.Message, "#4");
61                         Assert.IsNotNull (ex.ParamName, "#5");
62                         Assert.AreEqual ("attributeType", ex.ParamName, "#6");
63                 }
64         }
65
66         [Test]
67         public void GetField_Name_Null ()
68         {
69                 Type t = typeof (ModuleTest);
70                 Module module = t.Module;
71
72                 try {
73                         module.GetField (null);
74                         Assert.Fail ("#A1");
75                 } catch (ArgumentNullException ex) {
76                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
77                         Assert.IsNull (ex.InnerException, "#A3");
78                         Assert.IsNotNull (ex.Message, "#A4");
79                         Assert.IsNotNull (ex.ParamName, "#A5");
80                         Assert.AreEqual ("name", ex.ParamName, "#A6");
81                 }
82
83                 try {
84                         module.GetField (null, 0);
85                         Assert.Fail ("#B1");
86                 } catch (ArgumentNullException ex) {
87                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
88                         Assert.IsNull (ex.InnerException, "#B3");
89                         Assert.IsNotNull (ex.Message, "#B4");
90                         Assert.IsNotNull (ex.ParamName, "#B5");
91                         Assert.AreEqual ("name", ex.ParamName, "#B6");
92                 }
93         }
94
95         // Some of these tests overlap with the tests for ModuleBuilder
96 #if !MONOTOUCH
97         [Test]
98         [Category("NotDotNet")] // path length can cause suprious failures
99         public void TestGlobalData () {
100
101                 string name = "moduletest-assembly";
102                 string fileName = name + ".dll";
103
104                 AssemblyName assemblyName = new AssemblyName();
105                 assemblyName.Name = name;
106
107                 AssemblyBuilder ab
108                         = Thread.GetDomain().DefineDynamicAssembly(
109                                 assemblyName, AssemblyBuilderAccess.RunAndSave, TempFolder);
110
111                 string resfile = Path.Combine (TempFolder, "res");
112                 using (StreamWriter sw = new StreamWriter (resfile)) {
113                         sw.WriteLine ("FOO");
114                 }
115
116                 ab.AddResourceFile ("res", "res");
117
118                 ModuleBuilder mb = ab.DefineDynamicModule(fileName, fileName);
119
120                 mb.DefineInitializedData ("DATA", new byte [100], FieldAttributes.Public);
121                 mb.DefineInitializedData ("DATA2", new byte [100], FieldAttributes.Public);
122                 mb.DefineInitializedData ("DATA3", new byte [99], FieldAttributes.Public);
123                 mb.DefineUninitializedData ("DATA4", 101, FieldAttributes.Public);
124                 mb.DefineInitializedData ("DATA_PRIVATE", new byte [100], 0);
125                 mb.CreateGlobalFunctions ();
126
127                 ab.Save (fileName);
128
129                 Assembly assembly = Assembly.LoadFrom (Path.Combine (TempFolder, fileName));
130
131                 Module module = assembly.GetLoadedModules ()[0];
132
133                 string[] expectedFieldNames = new string [] {
134                         "DATA", "DATA2", "DATA3", "DATA4"
135                 };
136                 ArrayList fieldNames = new ArrayList ();
137                 foreach (FieldInfo fi in module.GetFields ()) {
138                         fieldNames.Add (fi.Name);
139                 }
140                 AssertArrayEqualsSorted (expectedFieldNames, fieldNames.ToArray (typeof (string)));
141
142                 Assert.IsNotNull (module.GetField ("DATA"), "#A1");
143                 Assert.IsNotNull (module.GetField ("DATA2"), "#A2");
144                 Assert.IsNotNull (module.GetField ("DATA3"), "#A3");
145                 Assert.IsNotNull (module.GetField ("DATA4"), "#A4");
146                 Assert.IsNull (module.GetField ("DATA_PRIVATE"), "#A5");
147                 Assert.IsNotNull (module.GetField ("DATA_PRIVATE", BindingFlags.NonPublic | BindingFlags.Static), "#A6");
148
149                 // Check that these methods work correctly on resource modules
150                 Module m2 = assembly.GetModule ("res");
151                 Assert.IsNotNull (m2, "#B1");
152                 Assert.AreEqual (0, m2.GetFields ().Length, "#B2");
153                 Assert.IsNull (m2.GetField ("DATA"), "#B3");
154                 Assert.IsNull (m2.GetField ("DATA", BindingFlags.Public), "#B4");
155         }
156 #endif
157
158         [Test]
159         public void ResolveType ()
160         {
161                 Type t = typeof (ModuleTest);
162                 Module module = t.Module;
163
164                 Assert.AreEqual (t, module.ResolveType (t.MetadataToken), "#1");
165
166                 /* We currently throw ArgumentException for this one */
167                 try {
168                         module.ResolveType (1234);
169                         Assert.Fail ("#2");
170                 } catch (ArgumentException) {
171                 }
172
173                 try {
174                         module.ResolveType (t.GetMethod ("ResolveType").MetadataToken);
175                         Assert.Fail ("#3");
176                 } catch (ArgumentException) {
177                 }
178
179                 try {
180                         module.ResolveType (t.MetadataToken + 10000);
181                         Assert.Fail ("#4");
182                 } catch (ArgumentOutOfRangeException) {
183                 }
184         }
185
186         [Test]
187         public void ResolveMethod ()
188         {
189                 Type t = typeof (ModuleTest);
190                 Module module = t.Module;
191
192                 Assert.AreEqual (t.GetMethod ("ResolveMethod"), module.ResolveMethod (t.GetMethod ("ResolveMethod").MetadataToken));
193
194                 try {
195                         module.ResolveMethod (1234);
196                         Assert.Fail ();
197                 } catch (ArgumentException) {
198                 }
199
200                 try {
201                         module.ResolveMethod (t.MetadataToken);
202                         Assert.Fail ();
203                 } catch (ArgumentException) {
204                 }
205
206                 try {
207                         module.ResolveMethod (t.GetMethod ("ResolveMethod").MetadataToken + 10000);
208                         Assert.Fail ();
209                 } catch (ArgumentOutOfRangeException) {
210                 }
211         }
212
213         public int aField;
214
215         [Test]
216         public void ResolveField ()
217         {
218                 Type t = typeof (ModuleTest);
219                 Module module = t.Module;
220
221                 Assert.AreEqual (t.GetField ("aField"), module.ResolveField (t.GetField ("aField").MetadataToken));
222
223                 try {
224                         module.ResolveField (1234);
225                         Assert.Fail ();
226                 } catch (ArgumentException) {
227                 }
228
229                 try {
230                         module.ResolveField (t.MetadataToken);
231                         Assert.Fail ();
232                 } catch (ArgumentException) {
233                 }
234
235                 try {
236                         module.ResolveField (t.GetField ("aField").MetadataToken + 10000);
237                         Assert.Fail ();
238                 } catch (ArgumentOutOfRangeException) {
239                 }
240         }
241
242         [Ignore ("it breaks nunit-console.exe execution under .NET 2.0")]
243         [Test]
244         public void ResolveString ()
245         {
246                 Type t = typeof (ModuleTest);
247                 Module module = t.Module;
248
249                 for (int i = 1; i < 10000; ++i) {
250                         try {
251                                 module.ResolveString (0x70000000 + i);
252                         } catch (Exception) {
253                         }
254                 }
255
256                 try {
257                         module.ResolveString (1234);
258                         Assert.Fail ();
259                 } catch (ArgumentException) {
260                 }
261
262                 try {
263                         module.ResolveString (t.MetadataToken);
264                         Assert.Fail ();
265                 } catch (ArgumentException) {
266                 }
267
268                 try {
269                         module.ResolveString (0x70000000 | 10000);
270                         Assert.Fail ();
271                 } catch (ArgumentOutOfRangeException) {
272                 }
273         }
274
275
276         [Test]
277         public void ResolveMember ()
278         {
279                 Type t = typeof (ModuleTest);
280                 Module module = t.Module;
281
282                 Assert.AreEqual (t, module.ResolveMember (t.MetadataToken), "#1");
283                 Assert.AreEqual (t.GetField ("aField"), module.ResolveMember (t.GetField ("aField").MetadataToken), "#2");
284                 Assert.AreEqual (t.GetMethod ("ResolveMember"), module.ResolveMember (t.GetMethod ("ResolveMember").MetadataToken), "#3");
285
286                 try {
287                         module.ResolveMember (module.MetadataToken);
288                         Assert.Fail ("#4");
289                 } catch (ArgumentException) {
290                 }
291         }
292
293         public class Foo<T>  {
294                 public void Bar(T t) {}
295         }
296
297         [Test]
298         public void ResolveMethodOfGenericClass ()
299         {
300                 Type type = typeof (Foo<>);
301                 Module mod = type.Module;
302                 MethodInfo method = type.GetMethod ("Bar");
303                 MethodBase res = mod.ResolveMethod (method.MetadataToken);
304                 Assert.AreEqual (method, res, "#1");
305         }
306
307         [Test]
308         public void FindTypes ()
309         {
310                 Module m = typeof (ModuleTest).Module;
311
312                 Type[] t;
313
314                 t = m.FindTypes (Module.FilterTypeName, "FindTypesTest*");
315                 Assert.AreEqual (2, t.Length, "#A1");
316                 Assert.AreEqual ("FindTypesTestFirstClass", t [0].Name, "#A2");
317                 Assert.AreEqual ("FindTypesTestSecondClass", t [1].Name, "#A3");
318                 t = m.FindTypes (Module.FilterTypeNameIgnoreCase, "findtypestest*");
319                 Assert.AreEqual (2, t.Length, "#B1");
320                 Assert.AreEqual ("FindTypesTestFirstClass", t [0].Name, "#B2");
321                 Assert.AreEqual ("FindTypesTestSecondClass", t [1].Name, "#B3");
322         }
323
324         [Test]
325         [ExpectedException (typeof (ArgumentNullException))]
326         public void GetObjectData_Null ()
327         {
328                 Module m = typeof (ModuleTest).Module;
329                 m.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
330         }
331 #if !MONOTOUCH
332         [Test]
333         public void GetTypes ()
334         {
335                 AssemblyName newName = new AssemblyName ();
336                 newName.Name = "ModuleTest";
337
338                 AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
339
340                 ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule" + ".dll", true);
341
342                 TypeBuilder tb = mb.DefineType ("Foo", TypeAttributes.Public);
343                 tb.CreateType ();
344
345                 ab.Save ("test_assembly.dll");
346
347                 Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
348                 ArrayList types = new ArrayList ();
349                 // The order of the modules is different between MS.NET and mono
350                 foreach (Module m in ass.GetModules ()) {
351                         Type[] t = m.GetTypes ();
352                         types.AddRange (t);
353                 }
354                 Assert.AreEqual (1, types.Count);
355                 Assert.AreEqual ("Foo", ((Type)(types [0])).Name);
356         }
357 #endif
358         class FindTypesTestFirstClass {
359         }
360
361         class FindTypesTestSecondClass {
362         }
363
364         private static void AssertArrayEqualsSorted (Array o1, Array o2) {
365                 Array s1 = (Array)o1.Clone ();
366                 Array s2 = (Array)o2.Clone ();
367
368                 Array.Sort (s1);
369                 Array.Sort (s2);
370
371                 Assert.AreEqual (s1.Length, s2.Length);
372                 for (int i = 0; i < s1.Length; ++i)
373                         Assert.AreEqual (s1.GetValue (i), s2.GetValue (i));
374         }
375 }
376 }
377