/
TagAttribute.cs
158 lines (128 loc) · 5.48 KB
/
TagAttribute.cs
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
148
149
150
151
152
153
154
155
156
157
158
using System.Reflection;
namespace Towel_Testing;
[TestClass]
public class TagAttribute_Testing
{
internal const string TestAttribute1 = nameof(TestAttribute1);
internal const string TestValue1 = nameof(TestValue1);
internal const string TestAttribute2 = nameof(TestAttribute2);
internal const string TestValue2 = nameof(TestValue2);
internal const string not_null = nameof(not_null);
internal const string TestAttributeFail = nameof(TestAttributeFail);
internal const string AmbiguousAttribute = nameof(AmbiguousAttribute);
internal const int IntValueOne = 7;
internal const int IntValueTwo = -7;
[TestMethod]
public void Test()
{
BindingFlags bf =
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static |
BindingFlags.Instance;
Type type = typeof(A);
MethodInfo methodInfo = typeof(TagAttribute_Testing).GetMethod(nameof(Method), bf)!;
EventInfo eventInfo = typeof(TagAttribute_Testing).GetEvent(nameof(Event))!;
ConstructorInfo constructorInfo = type.GetConstructor(Array.Empty<Type>())!;
FieldInfo fieldInfo = typeof(A).GetField(nameof(A.Field), bf)!;
PropertyInfo propertyInfo = typeof(A).GetProperty(nameof(A.Property), bf)!;
ParameterInfo parameterInfo = methodInfo.GetParameters()[0];
// TestAttribute1 -----------------------------------
Assert.IsTrue(type.GetTag(TestAttribute1) is (true, TestValue1));
Assert.IsTrue(methodInfo.GetTag(TestAttribute1) is (true, TestValue1));
Assert.IsTrue(eventInfo.GetTag(TestAttribute1) is (true, TestValue1));
Assert.IsTrue(constructorInfo.GetTag(TestAttribute1) is (true, TestValue1));
Assert.IsTrue(fieldInfo.GetTag(TestAttribute1) is (true, TestValue1));
Assert.IsTrue(propertyInfo.GetTag(TestAttribute1) is (true, TestValue1));
Assert.IsTrue(parameterInfo.GetTag(TestAttribute1) is (true, TestValue1));
// TestAttribute2 -----------------------------------
Assert.IsTrue(type.GetTag(TestAttribute2) is (true, TestValue2));
Assert.IsTrue(methodInfo.GetTag(TestAttribute2) is (true, TestValue2));
Assert.IsTrue(eventInfo.GetTag(TestAttribute2) is (true, TestValue2));
Assert.IsTrue(constructorInfo.GetTag(TestAttribute2) is (true, TestValue2));
Assert.IsTrue(fieldInfo.GetTag(TestAttribute2) is (true, TestValue2));
Assert.IsTrue(propertyInfo.GetTag(TestAttribute2) is (true, TestValue2));
Assert.IsTrue(parameterInfo.GetTag(TestAttribute2) is (true, TestValue2));
// Testing null -----------------------------------
Assert.IsTrue(typeof(C).GetTag(null) is (true, null));
Assert.IsTrue(typeof(D).GetTag(not_null) is (true, null));
Assert.IsTrue(typeof(E).GetTag(null) is (true, not_null));
// Test int ----------------------------------
Assert.IsTrue(typeof(F).GetTag(IntValueOne) is (true, IntValueTwo));
// Failure Not Found -----------------------------------
Assert.IsTrue(type.GetTag(TestAttributeFail) is (false, null));
Assert.IsTrue(methodInfo.GetTag(TestAttributeFail) is (false, null));
Assert.IsTrue(eventInfo.GetTag(TestAttributeFail) is (false, null));
Assert.IsTrue(constructorInfo.GetTag(TestAttributeFail) is (false, null));
Assert.IsTrue(fieldInfo.GetTag(TestAttributeFail) is (false, null));
Assert.IsTrue(propertyInfo.GetTag(TestAttributeFail) is (false, null));
Assert.IsTrue(parameterInfo.GetTag(TestAttributeFail) is (false, null));
// Failure Ambiguous -----------------------------------
MethodInfo b_methodInfo = typeof(B).GetMethod(nameof(B.Method), bf)!;
ParameterInfo b_parameterInfo = b_methodInfo.GetParameters()[0];
Assert.IsTrue(typeof(B).GetTag(AmbiguousAttribute) is (false, null));
Assert.IsTrue(b_parameterInfo.GetTag(AmbiguousAttribute) is (false, null));
// Enum Value Tests -------------------------------------
Assert.IsTrue(G.A.GetTag(TestAttribute1) is (true, TestValue1));
Assert.IsTrue(G.A.GetTag(TestAttribute2) is (true, TestValue2));
Assert.IsTrue(G.B.GetTag(AmbiguousAttribute) is (false, null));
Assert.IsTrue(G.C.GetTag(null) is (true, null));
Assert.IsTrue(G.D.GetTag(not_null) is (true, null));
Assert.IsTrue(G.E.GetTag(null) is (true, not_null));
}
[Tag(TestAttribute1, TestValue1)]
[Tag(TestAttribute2, TestValue2)]
public class A
{
[Tag(TestAttribute1, TestValue1)]
[Tag(TestAttribute2, TestValue2)]
public object Field;
[Tag(TestAttribute1, TestValue1)]
[Tag(TestAttribute2, TestValue2)]
public object Property { get; set; }
[Tag(TestAttribute1, TestValue1)]
[Tag(TestAttribute2, TestValue2)]
public A() => throw new Exception();
}
[Tag(TestAttribute1, TestValue1)]
[Tag(TestAttribute2, TestValue2)]
public static void Method(
[Tag(TestAttribute1, TestValue1)]
[Tag(TestAttribute2, TestValue2)]
Exception a) => throw a;
[Tag(TestAttribute1, TestValue1)]
[Tag(TestAttribute2, TestValue2)]
public static event Action? Event;
[Tag(AmbiguousAttribute, 1)]
[Tag(AmbiguousAttribute, 1)]
public class B
{
public static void Method(
[Tag(AmbiguousAttribute, 1)]
[Tag(AmbiguousAttribute, 1)]
Exception a) => throw a;
}
[Tag(null, null)]
public class C { }
[Tag(not_null, null)]
public class D { }
[Tag(null, not_null)]
public class E { }
[Tag(IntValueOne, IntValueTwo)]
public class F { }
public enum G
{
[Tag(TestAttribute1, TestValue1)]
[Tag(TestAttribute2, TestValue2)]
A,
[Tag(AmbiguousAttribute, 1)]
[Tag(AmbiguousAttribute, 1)]
B,
[Tag(null, null)]
C,
[Tag(not_null, null)]
D,
[Tag(null, not_null)]
E,
}
}