forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributeTests.cs
More file actions
331 lines (260 loc) · 11.7 KB
/
Copy pathAttributeTests.cs
File metadata and controls
331 lines (260 loc) · 11.7 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Linq;
using NUnit.Framework;
namespace ServiceStack.Text.Tests
{
using System.Collections.Generic;
[TestFixture]
public class AttributeTests
{
[Test]
public void Does_get_Single_Default_Attribute()
{
var attrs = typeof(DefaultWithSingleAttribute).AllAttributes<RouteDefaultAttribute>();
Assert.That(attrs[0].ToString(), Is.EqualTo("/path:"));
var attr = typeof(DefaultWithSingleAttribute).FirstAttribute<RouteDefaultAttribute>();
Assert.That(attr.ToString(), Is.EqualTo("/path:"));
}
[Test]
public void Does_get_Single_TypeId_Attribute()
{
var attrs = typeof(TypeIdWithSingleAttribute).AllAttributes<RouteTypeIdAttribute>();
Assert.That(attrs[0].ToString(), Is.EqualTo("/path:"));
var attr = typeof(TypeIdWithSingleAttribute).FirstAttribute<RouteTypeIdAttribute>();
Assert.That(attr.ToString(), Is.EqualTo("/path:"));
}
[Test]
public void Does_get_Multiple_RouteDefault_Attributes()
{
// AllAttributes<T>() makes this call to get attrs
var referenceGeneric =
typeof(DefaultWithMultipleAttributes).GetCustomAttributes(typeof(RouteDefaultAttribute), true)
.OfType<RouteDefaultAttribute>();
// Attribute inheritance hierarchies (InheritedRouteAttribute) are returned in results
Assert.That(referenceGeneric.Count(), Is.EqualTo(4));
// AllAttributes() makes this call to get attrs
var reference =
typeof(DefaultWithMultipleAttributes).GetCustomAttributes(typeof(RouteDefaultAttribute), true);
// Attribute inheritance hierarchies (InheritedRouteAttribute) are returned in results
Assert.That(reference.Count(), Is.EqualTo(4));
// Loses one of the attrs with inheritence when union
var referenceUnion = referenceGeneric.Union(new List<RouteDefaultAttribute>());
Assert.That(referenceUnion.Count(), Is.EqualTo(4));
// Keeps all items when concat
var referenceConcat = referenceGeneric.Concat(new List<RouteDefaultAttribute>());
Assert.That(referenceConcat.Count(), Is.EqualTo(4));
var attrsGeneric = typeof(DefaultWithMultipleAttributes).AllAttributes<RouteDefaultAttribute>();
// Attribute inheritance hierarchies (InheritedRouteAttribute) are NOT ALL returned in results
Assert.That(attrsGeneric.Length, Is.EqualTo(4)); // union loses one
var attrs = typeof(DefaultWithMultipleAttributes).AllAttributes(typeof(RouteDefaultAttribute));
// Attribute inheritance hierarchies (InheritedRouteAttribute) are NOT ALL returned in results
Assert.That(attrs.Length, Is.EqualTo(4)); // union loses one
var values = attrs.ToList().ConvertAll(x => x.ToString());
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
}));
var objAttrs = typeof(DefaultWithMultipleAttributes).AllAttributes();
values = objAttrs.ToList().ConvertAll(x => x.ToString());
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
}));
objAttrs = typeof(DefaultWithMultipleAttributes).AllAttributes(typeof(RouteDefaultAttribute));
values = objAttrs.ToList().ConvertAll(x => x.ToString());
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
}));
}
[Test]
public void Does_get_Multiple_Route_Attributes()
{
var routeAttrs = typeof(DefaultWithMultipleRouteAttributes)
.AllAttributes<RouteAttribute>();
Assert.That(routeAttrs.Length, Is.EqualTo(4));
var values = routeAttrs.ToList().ConvertAll(x => "{0}:{1}".Fmt(x.Path, x.Verbs));
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
}));
var inheritedRouteAttrs = typeof(DefaultWithMultipleRouteAttributes)
.AllAttributes<InheritedRouteAttribute>();
Assert.That(inheritedRouteAttrs.Length, Is.EqualTo(2));
}
[Test]
public void Does_get_Multiple_TypeId_Attributes()
{
var attrs = typeof(TypeIdWithMultipleAttributes).AllAttributes<RouteTypeIdAttribute>();
Assert.That(attrs.Length, Is.EqualTo(4));
var values = attrs.ToList().ConvertAll(x => x.ToString());
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
}));
var objAttrs = typeof(TypeIdWithMultipleAttributes).AllAttributes();
values = objAttrs.ToList().ConvertAll(x => x.ToString());
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
}));
objAttrs = typeof(TypeIdWithMultipleAttributes).AllAttributes(typeof(RouteTypeIdAttribute));
values = objAttrs.ToList().ConvertAll(x => x.ToString());
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
}));
}
}
[TestFixture]
public class RuntimeAttributesTests
{
[Test]
public void Can_add_to_Multiple_Default_Attributes()
{
typeof (DefaultWithMultipleAttributes).AddAttributes(
new RouteDefaultAttribute("/path-add"),
new RouteDefaultAttribute("/path-add", "GET"));
var attrs = typeof(DefaultWithMultipleAttributes).AllAttributes<RouteDefaultAttribute>();
Assert.That(attrs.Length, Is.EqualTo(6));
var values = attrs.ToList().ConvertAll(x => x.ToString());
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
"/path-add:", "/path-add:GET",
}));
var objAttrs = typeof(DefaultWithMultipleAttributes).AllAttributes();
values = objAttrs.ToList().ConvertAll(x => x.ToString());
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
"/path-add:", "/path-add:GET",
}));
objAttrs = typeof(DefaultWithMultipleAttributes).AllAttributes(typeof(RouteDefaultAttribute));
values = objAttrs.ToList().ConvertAll(x => x.ToString());
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
"/path-add:", "/path-add:GET",
}));
}
[Test]
public void Does_get_Multiple_TypeId_Attributes()
{
typeof(TypeIdWithMultipleAttributes).AddAttributes(
new RouteTypeIdAttribute("/path-add"),
new RouteTypeIdAttribute("/path-add", "GET"));
var attrs = typeof(TypeIdWithMultipleAttributes).AllAttributes<RouteTypeIdAttribute>();
Assert.That(attrs.Length, Is.EqualTo(6));
var values = attrs.ToList().ConvertAll(x => x.ToString());
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
"/path-add:", "/path-add:GET",
}));
var objAttrs = typeof(TypeIdWithMultipleAttributes).AllAttributes();
values = objAttrs.ToList().ConvertAll(x => x.ToString());
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
"/path-add:", "/path-add:GET",
}));
objAttrs = typeof(TypeIdWithMultipleAttributes).AllAttributes(typeof(RouteTypeIdAttribute));
values = objAttrs.ToList().ConvertAll(x => x.ToString());
Assert.That(values, Is.EquivalentTo(new[] {
"/path:", "/path/2:", "/path:GET", "/path:POST",
"/path-add:", "/path-add:GET",
}));
}
}
[RouteTypeId("/path")]
public class TypeIdWithSingleAttribute { }
[RouteTypeId("/path")]
[RouteTypeId("/path/2")]
[RouteTypeId("/path", "GET")]
[RouteTypeId("/path", "POST")]
public class TypeIdWithMultipleAttributes { }
[RouteDefault("/path")]
public class DefaultWithSingleAttribute { }
[RouteDefault("/path")]
[RouteDefault("/path/2")]
[InheritedRouteDefault("/path", "GET")]
[InheritedRouteDefault("/path", "POST")]
public class DefaultWithMultipleAttributes { }
[Route("/path")]
[Route("/path/2")]
[InheritedRoute("/path", "GET")]
[InheritedRoute("/path", "POST")]
public class DefaultWithMultipleRouteAttributes { }
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class RouteTypeIdAttribute : Attribute
{
public RouteTypeIdAttribute(string path) : this(path, null) {}
public RouteTypeIdAttribute(string path, string verbs)
{
Path = path;
Verbs = verbs;
}
public string Path { get; set; }
public string Verbs { get; set; }
public override object TypeId
{
get
{
return (Path ?? "")
+ (Verbs ?? "");
}
}
public override string ToString()
{
return "{0}:{1}".Fmt(Path, Verbs);
}
}
public class InheritedRouteDefaultAttribute : RouteDefaultAttribute {
public InheritedRouteDefaultAttribute(string path)
: base(path)
{
}
public InheritedRouteDefaultAttribute(string path, string verbs)
: base(path, verbs)
{
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class RouteDefaultAttribute : Attribute
{
public RouteDefaultAttribute(string path) : this(path, null) {}
public RouteDefaultAttribute(string path, string verbs)
{
Path = path;
Verbs = verbs;
}
public string Path { get; set; }
public string Verbs { get; set; }
public override string ToString()
{
return "{0}:{1}".Fmt(Path, Verbs);
}
protected bool Equals(RouteDefaultAttribute other)
{
return base.Equals(other) && string.Equals(Path, other.Path) && string.Equals(Verbs, other.Verbs);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((RouteDefaultAttribute) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode*397) ^ (Path != null ? Path.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (Verbs != null ? Verbs.GetHashCode() : 0);
return hashCode;
}
}
}
public class InheritedRouteAttribute : RouteAttribute
{
public InheritedRouteAttribute(string path)
: base(path)
{
}
public InheritedRouteAttribute(string path, string verbs)
: base(path, verbs)
{
}
public string Custom { get; set; }
}
}