2003-06-15 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / Test / System.Reflection.Emit / AssemblyBuilderTest.cs
1 //\r
2 // AssemblyBuilderTest.cs - NUnit Test Cases for the AssemblyBuilder class\r
3 //\r
4 // Zoltan Varga (vargaz@freemail.hu)\r
5 //\r
6 // (C) Ximian, Inc.  http://www.ximian.com\r
7 //\r
8 //\r
9 \r
10 \r
11 using System;\r
12 using System.Threading;\r
13 using System.Reflection;\r
14 using System.Reflection.Emit;\r
15 using System.IO;\r
16 \r
17 using NUnit.Framework;\r
18 \r
19 namespace MonoTests.System.Reflection.Emit\r
20 {\r
21 \r
22 [TestFixture]\r
23 public class AssemblyBuilderTest : Assertion\r
24 {       \r
25         [AttributeUsage (AttributeTargets.Assembly)]\r
26         public sealed class FooAttribute : Attribute\r
27         {\r
28                 public FooAttribute (string arg)\r
29                 {\r
30                 }\r
31         }\r
32 \r
33         static int nameIndex = 0;\r
34 \r
35         static AppDomain domain;\r
36 \r
37         static AssemblyBuilder ab;\r
38 \r
39         [SetUp]\r
40         protected void SetUp () {\r
41                 domain = Thread.GetDomain ();\r
42                 ab = genAssembly ();\r
43         }\r
44 \r
45         private AssemblyName genAssemblyName () {\r
46                 AssemblyName assemblyName = new AssemblyName();\r
47                 assemblyName.Name = "MonoTests.System.Reflection.Emit.AssemblyBuilderTest" + (nameIndex ++);\r
48                 return assemblyName;\r
49         }\r
50 \r
51         private AssemblyBuilder genAssembly () {\r
52                 return domain.DefineDynamicAssembly (genAssemblyName (),\r
53                                                                                          AssemblyBuilderAccess.RunAndSave);\r
54         }\r
55 \r
56         private MethodInfo genEntryFunction (AssemblyBuilder assembly) {\r
57                 ModuleBuilder module = assembly.DefineDynamicModule("module1");\r
58                 TypeBuilder tb = module.DefineType ("A");\r
59                 MethodBuilder mb = tb.DefineMethod ("A",\r
60                         MethodAttributes.Static, typeof (void), new Type [0]);\r
61                 mb.GetILGenerator ().Emit (OpCodes.Ret);\r
62                 return mb;\r
63         }\r
64 \r
65         public void TestCodeBase () {\r
66                 try {\r
67                         string codebase = ab.CodeBase;\r
68                         Fail ();\r
69                 }\r
70                 catch (NotSupportedException) {\r
71                 }\r
72         }\r
73 \r
74         public void TestEntryPoint () {\r
75                 AssertEquals ("EntryPoint defaults to null",\r
76                                           null, ab.EntryPoint);\r
77 \r
78                 MethodInfo mi = genEntryFunction (ab);\r
79                 ab.SetEntryPoint (mi);\r
80 \r
81                 AssertEquals ("EntryPoint works", mi, ab.EntryPoint);\r
82         }\r
83 \r
84         public void TestSetEntryPoint () {\r
85                 // Check invalid arguments\r
86                 try {\r
87                         ab.SetEntryPoint (null);\r
88                         Fail ();\r
89                 }\r
90                 catch (ArgumentNullException) {\r
91                 }\r
92 \r
93                 // Check method from other assembly\r
94                 try {\r
95                         ab.SetEntryPoint (typeof (AssemblyBuilderTest).GetMethod ("TestSetEntryPoint"));\r
96                         Fail ();\r
97                 }\r
98                 catch (InvalidOperationException) {\r
99                 }\r
100         }\r
101 \r
102         public void TestIsDefined () {\r
103                 CustomAttributeBuilder cab = new CustomAttributeBuilder (typeof (FooAttribute).GetConstructor (new Type [1] {typeof (string)}), new object [1] { "A" });\r
104                 ab.SetCustomAttribute (cab);\r
105 \r
106                 AssertEquals ("IsDefined works",\r
107                                           true, ab.IsDefined (typeof (FooAttribute), false));\r
108                 AssertEquals ("IsDefined works",\r
109                                           false, ab.IsDefined (typeof (AssemblyVersionAttribute), false));\r
110         }\r
111 \r
112         [ExpectedException (typeof (NotSupportedException))]\r
113         public void TestGetManifestResourceNames () {\r
114                 ab.GetManifestResourceNames ();\r
115         }\r
116 \r
117         [ExpectedException (typeof (NotSupportedException))]\r
118         public void TestGetManifestResourceInfo () {\r
119                 ab.GetManifestResourceInfo ("foo");\r
120         }\r
121 \r
122         [ExpectedException (typeof (NotSupportedException))]\r
123         public void TestGetManifestResourceStream1 () {\r
124                 ab.GetManifestResourceStream ("foo");\r
125         }\r
126 \r
127         [ExpectedException (typeof (NotSupportedException))]\r
128         public void TestGetManifestResourceStream2 () {\r
129                 ab.GetManifestResourceStream (typeof (int), "foo");\r
130         }\r
131 \r
132         [ExpectedException (typeof (NotSupportedException))]\r
133         public void TestGetFiles1 () {\r
134                 ab.GetFiles ();\r
135         }\r
136 \r
137         [ExpectedException (typeof (NotSupportedException))]\r
138         public void TestGetFiles2 () {\r
139                 ab.GetFiles (true);\r
140         }\r
141 \r
142         [ExpectedException (typeof (NotSupportedException))]\r
143         public void TestGetFile () {\r
144                 ab.GetFile ("foo");\r
145         }\r
146 \r
147         [ExpectedException (typeof (NotSupportedException))]\r
148         public void TestGetExportedTypes () {\r
149                 ab.GetExportedTypes ();\r
150         }\r
151 \r
152         [ExpectedException (typeof (ArgumentNullException))]\r
153         public void TestGetDynamicModule1 () {\r
154                 ab.GetDynamicModule (null);\r
155         }\r
156 \r
157         [ExpectedException (typeof (ArgumentException))]\r
158         public void TestGetDynamicModule2 () {\r
159                 ab.GetDynamicModule ("");\r
160         }\r
161 \r
162         public void TestGetDynamicModule3 () {\r
163                 AssertNull (ab.GetDynamicModule ("FOO2"));\r
164 \r
165                 ModuleBuilder mb = ab.DefineDynamicModule ("FOO");\r
166 \r
167                 AssertEquals (mb, ab.GetDynamicModule ("FOO"));\r
168 \r
169                 AssertNull (ab.GetDynamicModule ("FOO4"));\r
170         }\r
171 }\r
172 }\r
173 \r