2005-06-05 Peter Bartok <pbartok@novell.com>
[mono.git] / mcs / tests / acc-modifiers2.cs
1 //
2 // acc-modifiers2.cs: We use reflection to test that the flags are the correct ones
3 //
4
5 using System;
6 using System.Reflection;
7
8  [AttributeUsage (AttributeTargets.Class)]
9  public class TypeCheckAttribute : Attribute {
10
11          public TypeCheckAttribute ()
12          {
13          }
14  }
15
16  [AttributeUsage (AttributeTargets.Property)]
17  public class PropertyCheckAttribute : Attribute {
18
19          public PropertyCheckAttribute ()
20          {
21          }
22  }
23
24  [AttributeUsage (AttributeTargets.Method)]
25  public class AccessorCheckAttribute : Attribute {
26          MethodAttributes flags;
27
28          public AccessorCheckAttribute (MethodAttributes flags)
29          {
30                  this.flags = flags;
31          }
32
33          public MethodAttributes Attributes {
34                  get {
35                          return flags;
36                  }
37          }
38  }
39
40  public class Test {
41
42          public static int Main (string [] args)
43          {
44                  Type t = typeof (A);
45                  
46                  foreach (PropertyInfo pi in t.GetProperties ()) {
47                          object [] attrs = pi.GetCustomAttributes (typeof (PropertyCheckAttribute), true);
48                          if (attrs == null)
49                                  return 0;
50                          
51                          MethodInfo get_accessor, set_accessor;
52                          get_accessor = pi.GetGetMethod (true);
53                          set_accessor = pi.GetSetMethod (true);
54                          
55                          if (get_accessor != null)
56                                  CheckFlags (pi, get_accessor);
57                          if (set_accessor != null)
58                                  CheckFlags (pi, set_accessor);
59                  }
60
61                  return 0;
62          }
63
64          static void CheckFlags (PropertyInfo pi, MethodInfo accessor)
65          {
66                  object [] attrs = accessor.GetCustomAttributes (typeof (AccessorCheckAttribute), true);
67                  if (attrs == null)
68                          return;
69
70                  AccessorCheckAttribute accessor_attr = (AccessorCheckAttribute) attrs [0];
71                  MethodAttributes accessor_flags = accessor.Attributes;
72
73                  if ((accessor_flags & accessor_attr.Attributes) == accessor_attr.Attributes)
74                          Console.WriteLine ("Test for {0}.{1} PASSED", pi.Name, accessor.Name);
75                  else {
76                          string message = String.Format ("Test for {0}.{1} INCORRECT: MethodAttributes should be {2}, but are {3}",
77                                          pi.Name, accessor.Name, accessor_attr.Attributes, accessor_flags);
78                          throw new Exception (message);
79                  }
80          }
81
82  }
83
84  [TypeCheck]
85  public class A {
86
87          const MethodAttributes flags = MethodAttributes.HideBySig |
88                  MethodAttributes.SpecialName;
89
90          [PropertyCheck]
91          public int Value1 {
92                  [AccessorCheck (flags | MethodAttributes.Public)]
93                  get {
94                          return 0;
95                  }
96                  [AccessorCheck (flags | MethodAttributes.Public)]
97                  set {
98                  }
99          }
100
101          [PropertyCheck]
102          public int Value2 {
103                  [AccessorCheck (flags | MethodAttributes.Public)]
104                  get {
105                          return 0;
106                  }
107                  [AccessorCheck (flags | MethodAttributes.FamORAssem)]
108                  protected internal set {
109                  }
110          }
111
112          [PropertyCheck]
113          public int Value3 {
114                  [AccessorCheck (flags | MethodAttributes.Public)]
115                  get {
116                          return 0;
117                  }
118                  [AccessorCheck (flags | MethodAttributes.Family)]
119                  protected set {
120                  }
121          }
122
123          [PropertyCheck]
124          public int Value4 {
125                  [AccessorCheck (flags | MethodAttributes.Assembly)]
126                  internal get {
127                          return 0;
128                  }
129                  [AccessorCheck (flags | MethodAttributes.Public)]
130                  set {
131                  }
132          }
133
134          [PropertyCheck]
135          public int Value5 {
136                  [AccessorCheck (flags | MethodAttributes.Public)]
137                  get {
138                          return 0;
139                  }
140                  [AccessorCheck (flags | MethodAttributes.Private)]
141                  private set {
142                  }
143          }
144
145  }
146