[loader] Init MonoClass:sizes.element_size lazily (Fixes #43563) (#5559)
[mono.git] / mono / tests / custom-attr-errors.cs
1 using System;
2 using System.Reflection;
3
4 // this for test_0_missing_attr_on_assembly
5 [assembly: MissingAttribute]
6
7 public sealed class MyAttribute : Attribute
8 {
9         public Type Type { get; set; }
10         public MyAttribute (Type t) {
11                 Type = t;
12                 // throw new Exception ();
13         }
14
15         public override string ToString () {
16                 return "my " + Type;
17         }
18 }
19
20 public sealed class My2Attribute : Attribute
21 {
22         public object Obj { get; set; }
23         public My2Attribute (object t) {
24                 Obj = t;
25                 // throw new Exception ();
26         }
27
28         public override string ToString () {
29                 return "my2 " + Obj;
30         }
31 }
32
33 public sealed class My3Attribute : Attribute
34 {
35         public My3Attribute (object[] arr) {
36         }
37 }
38
39 public class MyException : Exception {}
40 public sealed class ExceptionOnCtor : Attribute
41 {
42         public ExceptionOnCtor () {
43                 throw new MyException ();
44         }
45 }
46
47 public class Bar {}
48
49 class Tests {
50
51         public static int test_0_missing_attr_on_assembly () {
52                 try {
53                         Assembly.GetExecutingAssembly().GetCustomAttributes (false);
54                         return 1;
55                 } catch (TypeLoadException exn) {
56                         return 0;
57                 }
58         }
59
60         [My3 (new object[] { DisappearingEnum.V0 })]
61         public static int test_0_missing_enum_arg_alt3 () {
62                 try {
63                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
64                         return 1;
65                 } catch (TypeLoadException) {
66                         return 0;
67                 }
68         }
69
70         [My2 (new DisappearingEnum[] { DisappearingEnum.V0 })]
71         public static int test_0_missing_enum_arg_alt2 () {
72                 try {
73                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
74                         return 1;
75                 } catch (TypeLoadException) {
76                         return 0;
77                 }
78         }
79
80         [My2 (new object[] { DisappearingEnum.V0 })]
81         public static int test_0_missing_enum_arg_alt () {
82                 try {
83                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
84                         return 1;
85                 } catch (TypeLoadException) {
86                         return 0;
87                 }
88         }
89
90         [My2 (DisappearingEnum.V0)]
91         public static int test_0_missing_enum_arg () {
92                 try {
93                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
94                         return 1;
95                 } catch (TypeLoadException) {
96                         return 0;
97                 }
98         }
99
100         [My3 (new object[] { typeof (DisappearingType)})] 
101         public static int test_0_array_of_missing_type_alt2 () {
102                 try {
103                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
104                         return 1;
105                 } catch (TypeLoadException) {
106                         return 0;
107                 }
108         }
109         [My2 (new Type[] { typeof (DisappearingType)})] 
110         public static int test_0_array_of_missing_type () {
111                 try {
112                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
113                         return 1;
114                 } catch (TypeLoadException) {
115                         return 0;
116                 }
117         }
118
119
120
121         [My2 (typeof (DisappearingType))]
122         public static int test_0_missing_type_arg_alt () {
123                 try {
124                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
125                         return 1;
126                 } catch (TypeLoadException) {
127                         return 0;
128                 }
129         }
130
131         [My (typeof (DisappearingType))]
132         public static int test_0_missing_type_arg () {
133                 try {
134                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
135                         return 1;
136                 } catch (TypeLoadException) {
137                         return 0;
138                 }
139         }
140
141
142         [MissingCtor (1)]
143         public static int test_0_missing_ctor () {
144                 try {
145                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
146                         return 1;
147                 } catch (MissingMethodException) {
148                         return 0;
149                 }
150         }
151
152         [BadAttr (Field = 1)]
153         public static int test_0_missing_field () {
154                 try {
155                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
156                         return 1;
157                 } catch (CustomAttributeFormatException) {
158                         return 0;
159                 }
160         }
161
162         [BadAttr (Property = 1)]
163         public static int test_0_missing_property () {
164                 try {
165                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
166                         return 1;
167                 } catch (CustomAttributeFormatException) {
168                         return 0;
169                 }
170         }
171
172         /* FIXME Verify the type of the cattr with the one on the field/property
173         [BadAttr (Field2 = 1)]
174         public static int test_0_bad_field () {
175                 try {
176                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
177                         return 1;
178                 } catch (CustomAttributeFormatException) {
179                         return 0;
180                 }
181         }
182
183         [BadAttr (Property2 = 1)]
184         public static int test_0_bad_property () {
185                 try {
186                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
187                         return 1;
188                 } catch (CustomAttributeFormatException) {
189                         return 0;
190                 }
191         }
192         */
193
194         [BadAttr (Property3 = 1)]
195         public static int test_0_bad_property_no_setter () {
196                 try {
197                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
198                         return 1;
199                 } catch (CustomAttributeFormatException) {
200                         return 0;
201                 }
202         }
203
204         [ExceptionOnCtor]
205         public static int test_0_cattr_ctor_throws () {
206                 try {
207                         MethodBase.GetCurrentMethod ().GetCustomAttributes (false);
208                         return 1;
209                 } catch (MyException) {
210                         return 0;
211                 }
212         }
213
214
215     static int Main (String[] args) {
216             return TestDriver.RunTests (typeof (Tests), args);
217     }
218
219 }