1
+ using System ;
2
+ using System . Linq ;
3
+ using NUnit . Framework ;
4
+
5
+ namespace ServiceStack . Text . Tests
6
+ {
7
+ [ TestFixture ]
8
+ public class AttributeTests
9
+ {
10
+ [ Test ]
11
+ public void Does_get_Single_Default_Attribute ( )
12
+ {
13
+ var attrs = typeof ( DefaultWithSingleAttribute ) . AllAttributes < RouteDefaultAttribute > ( ) ;
14
+ Assert . That ( attrs [ 0 ] . ToString ( ) , Is . EqualTo ( "/path:" ) ) ;
15
+
16
+ var attr = typeof ( DefaultWithSingleAttribute ) . FirstAttribute < RouteDefaultAttribute > ( ) ;
17
+ Assert . That ( attr . ToString ( ) , Is . EqualTo ( "/path:" ) ) ;
18
+ }
19
+
20
+ [ Test ]
21
+ public void Does_get_Single_TypeId_Attribute ( )
22
+ {
23
+ var attrs = typeof ( TypeIdWithSingleAttribute ) . AllAttributes < RouteTypeIdAttribute > ( ) ;
24
+ Assert . That ( attrs [ 0 ] . ToString ( ) , Is . EqualTo ( "/path:" ) ) ;
25
+
26
+ var attr = typeof ( TypeIdWithSingleAttribute ) . FirstAttribute < RouteTypeIdAttribute > ( ) ;
27
+ Assert . That ( attr . ToString ( ) , Is . EqualTo ( "/path:" ) ) ;
28
+ }
29
+
30
+ [ Test ]
31
+ public void Does_get_Multiple_Default_Attributes ( )
32
+ {
33
+ var attrs = typeof ( DefaultWithMultipleAttributes ) . AllAttributes < RouteDefaultAttribute > ( ) ;
34
+ Assert . That ( attrs . Length , Is . EqualTo ( 4 ) ) ;
35
+
36
+ var values = attrs . ToList ( ) . ConvertAll ( x => x . ToString ( ) ) ;
37
+
38
+ Assert . That ( values , Is . EquivalentTo ( new [ ] {
39
+ "/path:" , "/path/2:" , "/path:GET" , "/path:POST" ,
40
+ } ) ) ;
41
+
42
+ var objAttrs = typeof ( DefaultWithMultipleAttributes ) . AllAttributes ( ) ;
43
+ values = objAttrs . ToList ( ) . ConvertAll ( x => x . ToString ( ) ) ;
44
+
45
+ Assert . That ( values , Is . EquivalentTo ( new [ ] {
46
+ "/path:" , "/path/2:" , "/path:GET" , "/path:POST" ,
47
+ } ) ) ;
48
+
49
+ objAttrs = typeof ( DefaultWithMultipleAttributes ) . AllAttributes ( typeof ( RouteDefaultAttribute ) ) ;
50
+ values = objAttrs . ToList ( ) . ConvertAll ( x => x . ToString ( ) ) ;
51
+
52
+ Assert . That ( values , Is . EquivalentTo ( new [ ] {
53
+ "/path:" , "/path/2:" , "/path:GET" , "/path:POST" ,
54
+ } ) ) ;
55
+ }
56
+
57
+ [ Test ]
58
+ public void Does_get_Multiple_TypeId_Attributes ( )
59
+ {
60
+ var attrs = typeof ( TypeIdWithMultipleAttributes ) . AllAttributes < RouteTypeIdAttribute > ( ) ;
61
+ Assert . That ( attrs . Length , Is . EqualTo ( 4 ) ) ;
62
+
63
+ var values = attrs . ToList ( ) . ConvertAll ( x => x . ToString ( ) ) ;
64
+
65
+ Assert . That ( values , Is . EquivalentTo ( new [ ] {
66
+ "/path:" , "/path/2:" , "/path:GET" , "/path:POST" ,
67
+ } ) ) ;
68
+
69
+ var objAttrs = typeof ( TypeIdWithMultipleAttributes ) . AllAttributes ( ) ;
70
+ values = objAttrs . ToList ( ) . ConvertAll ( x => x . ToString ( ) ) ;
71
+
72
+ Assert . That ( values , Is . EquivalentTo ( new [ ] {
73
+ "/path:" , "/path/2:" , "/path:GET" , "/path:POST" ,
74
+ } ) ) ;
75
+
76
+ objAttrs = typeof ( TypeIdWithMultipleAttributes ) . AllAttributes ( typeof ( RouteTypeIdAttribute ) ) ;
77
+ values = objAttrs . ToList ( ) . ConvertAll ( x => x . ToString ( ) ) ;
78
+
79
+ Assert . That ( values , Is . EquivalentTo ( new [ ] {
80
+ "/path:" , "/path/2:" , "/path:GET" , "/path:POST" ,
81
+ } ) ) ;
82
+ }
83
+ }
84
+
85
+ [ TestFixture ]
86
+ public class RuntimeAttributesTests
87
+ {
88
+ [ Test ]
89
+ public void Can_add_to_Multiple_Default_Attributes ( )
90
+ {
91
+ typeof ( DefaultWithMultipleAttributes ) . AddAttributes (
92
+ new RouteDefaultAttribute ( "/path-add" ) ,
93
+ new RouteDefaultAttribute ( "/path-add" , "GET" ) ) ;
94
+
95
+ var attrs = typeof ( DefaultWithMultipleAttributes ) . AllAttributes < RouteDefaultAttribute > ( ) ;
96
+ Assert . That ( attrs . Length , Is . EqualTo ( 6 ) ) ;
97
+
98
+ var values = attrs . ToList ( ) . ConvertAll ( x => x . ToString ( ) ) ;
99
+
100
+ Assert . That ( values , Is . EquivalentTo ( new [ ] {
101
+ "/path:" , "/path/2:" , "/path:GET" , "/path:POST" ,
102
+ "/path-add:" , "/path-add:GET" ,
103
+ } ) ) ;
104
+
105
+ var objAttrs = typeof ( DefaultWithMultipleAttributes ) . AllAttributes ( ) ;
106
+ values = objAttrs . ToList ( ) . ConvertAll ( x => x . ToString ( ) ) ;
107
+
108
+ Assert . That ( values , Is . EquivalentTo ( new [ ] {
109
+ "/path:" , "/path/2:" , "/path:GET" , "/path:POST" ,
110
+ "/path-add:" , "/path-add:GET" ,
111
+ } ) ) ;
112
+
113
+ objAttrs = typeof ( DefaultWithMultipleAttributes ) . AllAttributes ( typeof ( RouteDefaultAttribute ) ) ;
114
+ values = objAttrs . ToList ( ) . ConvertAll ( x => x . ToString ( ) ) ;
115
+
116
+ Assert . That ( values , Is . EquivalentTo ( new [ ] {
117
+ "/path:" , "/path/2:" , "/path:GET" , "/path:POST" ,
118
+ "/path-add:" , "/path-add:GET" ,
119
+ } ) ) ;
120
+ }
121
+
122
+ [ Test ]
123
+ public void Does_get_Multiple_TypeId_Attributes ( )
124
+ {
125
+ typeof ( TypeIdWithMultipleAttributes ) . AddAttributes (
126
+ new RouteTypeIdAttribute ( "/path-add" ) ,
127
+ new RouteTypeIdAttribute ( "/path-add" , "GET" ) ) ;
128
+
129
+ var attrs = typeof ( TypeIdWithMultipleAttributes ) . AllAttributes < RouteTypeIdAttribute > ( ) ;
130
+ Assert . That ( attrs . Length , Is . EqualTo ( 6 ) ) ;
131
+
132
+ var values = attrs . ToList ( ) . ConvertAll ( x => x . ToString ( ) ) ;
133
+
134
+ Assert . That ( values , Is . EquivalentTo ( new [ ] {
135
+ "/path:" , "/path/2:" , "/path:GET" , "/path:POST" ,
136
+ "/path-add:" , "/path-add:GET" ,
137
+ } ) ) ;
138
+
139
+ var objAttrs = typeof ( TypeIdWithMultipleAttributes ) . AllAttributes ( ) ;
140
+ values = objAttrs . ToList ( ) . ConvertAll ( x => x . ToString ( ) ) ;
141
+
142
+ Assert . That ( values , Is . EquivalentTo ( new [ ] {
143
+ "/path:" , "/path/2:" , "/path:GET" , "/path:POST" ,
144
+ "/path-add:" , "/path-add:GET" ,
145
+ } ) ) ;
146
+
147
+ objAttrs = typeof ( TypeIdWithMultipleAttributes ) . AllAttributes ( typeof ( RouteTypeIdAttribute ) ) ;
148
+ values = objAttrs . ToList ( ) . ConvertAll ( x => x . ToString ( ) ) ;
149
+
150
+ Assert . That ( values , Is . EquivalentTo ( new [ ] {
151
+ "/path:" , "/path/2:" , "/path:GET" , "/path:POST" ,
152
+ "/path-add:" , "/path-add:GET" ,
153
+ } ) ) ;
154
+ }
155
+ }
156
+
157
+ [ RouteTypeId ( "/path" ) ]
158
+ public class TypeIdWithSingleAttribute { }
159
+
160
+ [ RouteTypeId ( "/path" ) ]
161
+ [ RouteTypeId ( "/path/2" ) ]
162
+ [ RouteTypeId ( "/path" , "GET" ) ]
163
+ [ RouteTypeId ( "/path" , "POST" ) ]
164
+ public class TypeIdWithMultipleAttributes { }
165
+
166
+ [ RouteDefault ( "/path" ) ]
167
+ public class DefaultWithSingleAttribute { }
168
+
169
+ [ RouteDefault ( "/path" ) ]
170
+ [ RouteDefault ( "/path/2" ) ]
171
+ [ RouteDefault ( "/path" , "GET" ) ]
172
+ [ RouteDefault ( "/path" , "POST" ) ]
173
+ public class DefaultWithMultipleAttributes { }
174
+
175
+ [ AttributeUsage ( AttributeTargets . Class | AttributeTargets . Method , AllowMultiple = true , Inherited = true ) ]
176
+ public class RouteTypeIdAttribute : Attribute
177
+ {
178
+ public RouteTypeIdAttribute ( string path ) : this ( path , null ) { }
179
+ public RouteTypeIdAttribute ( string path , string verbs )
180
+ {
181
+ Path = path ;
182
+ Verbs = verbs ;
183
+ }
184
+
185
+ public string Path { get ; set ; }
186
+ public string Verbs { get ; set ; }
187
+
188
+ public override object TypeId
189
+ {
190
+ get
191
+ {
192
+ return ( Path ?? "" )
193
+ + ( Verbs ?? "" ) ;
194
+ }
195
+ }
196
+
197
+ public override string ToString ( )
198
+ {
199
+ return "{0}:{1}" . Fmt ( Path , Verbs ) ;
200
+ }
201
+ }
202
+
203
+ [ AttributeUsage ( AttributeTargets . Class | AttributeTargets . Method , AllowMultiple = true , Inherited = true ) ]
204
+ public class RouteDefaultAttribute : Attribute
205
+ {
206
+ public RouteDefaultAttribute ( string path ) : this ( path , null ) { }
207
+ public RouteDefaultAttribute ( string path , string verbs )
208
+ {
209
+ Path = path ;
210
+ Verbs = verbs ;
211
+ }
212
+
213
+ public string Path { get ; set ; }
214
+ public string Verbs { get ; set ; }
215
+
216
+ public override string ToString ( )
217
+ {
218
+ return "{0}:{1}" . Fmt ( Path , Verbs ) ;
219
+ }
220
+ }
221
+
222
+ }
0 commit comments