Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test-397.cs « tests « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1270a51f9d9603b03a90e99427e488baffc60dc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// Access modifiers
// We use reflection to test that the flags are the correct ones
//

using System;
using System.Reflection;

 [AttributeUsage (AttributeTargets.Class)]
 public class TypeCheckAttribute : Attribute {

	 public TypeCheckAttribute ()
	 {
	 }
 }

 [AttributeUsage (AttributeTargets.Property)]
 public class PropertyCheckAttribute : Attribute {

	 public PropertyCheckAttribute ()
	 {
	 }
 }

 [AttributeUsage (AttributeTargets.Method)]
 public class AccessorCheckAttribute : Attribute {
	 MethodAttributes flags;

	 public AccessorCheckAttribute (MethodAttributes flags)
	 {
		 this.flags = flags;
	 }

	 public MethodAttributes Attributes {
		 get {
			 return flags;
		 }
	 }
 }

 public class Test {

	 public static int Main (string [] args)
	 {
		 Type t = typeof (A);
		 
		 foreach (PropertyInfo pi in t.GetProperties ()) {
			 object [] attrs = pi.GetCustomAttributes (typeof (PropertyCheckAttribute), true);
			 if (attrs == null)
				 return 0;
			 
			 MethodInfo get_accessor, set_accessor;
			 get_accessor = pi.GetGetMethod (true);
			 set_accessor = pi.GetSetMethod (true);
			 
			 if (get_accessor != null)
				 CheckFlags (pi, get_accessor);
			 if (set_accessor != null)
				 CheckFlags (pi, set_accessor);
		 }

		 return 0;
	 }

	 static void CheckFlags (PropertyInfo pi, MethodInfo accessor)
	 {
		 object [] attrs = accessor.GetCustomAttributes (typeof (AccessorCheckAttribute), true);
		 if (attrs == null)
			 return;

		 AccessorCheckAttribute accessor_attr = (AccessorCheckAttribute) attrs [0];
		 MethodAttributes accessor_flags = accessor.Attributes;

		 if ((accessor_flags & accessor_attr.Attributes) == accessor_attr.Attributes)
			 Console.WriteLine ("Test for {0}.{1} PASSED", pi.Name, accessor.Name);
		 else {
			 string message = String.Format ("Test for {0}.{1} INCORRECT: MethodAttributes should be {2}, but are {3}",
					 pi.Name, accessor.Name, accessor_attr.Attributes, accessor_flags);
			 throw new Exception (message);
		 }
	 }

 }

 [TypeCheck]
 public class A {

	 const MethodAttributes flags = MethodAttributes.HideBySig |
		 MethodAttributes.SpecialName;

	 [PropertyCheck]
	 public int Value1 {
		 [AccessorCheck (flags | MethodAttributes.Public)]
		 get {
			 return 0;
		 }
		 [AccessorCheck (flags | MethodAttributes.Public)]
		 set {
		 }
	 }

	 [PropertyCheck]
	 public int Value2 {
		 [AccessorCheck (flags | MethodAttributes.Public)]
		 get {
			 return 0;
		 }
		 [AccessorCheck (flags | MethodAttributes.FamORAssem)]
		 protected internal set {
		 }
	 }

	 [PropertyCheck]
	 public int Value3 {
		 [AccessorCheck (flags | MethodAttributes.Public)]
		 get {
			 return 0;
		 }
		 [AccessorCheck (flags | MethodAttributes.Family)]
		 protected set {
		 }
	 }

	 [PropertyCheck]
	 public int Value4 {
		 [AccessorCheck (flags | MethodAttributes.Assembly)]
		 internal get {
			 return 0;
		 }
		 [AccessorCheck (flags | MethodAttributes.Public)]
		 set {
		 }
	 }

	 [PropertyCheck]
	 public int Value5 {
		 [AccessorCheck (flags | MethodAttributes.Public)]
		 get {
			 return 0;
		 }
		 [AccessorCheck (flags | MethodAttributes.Private)]
		 private set {
		 }
	 }

 }