v2 / vlib / v / tests / comptime / comptime_method_attributes_test.v
39 lines · 35 sloc · 1.03 KB · d025b7d80d18675ee5817f060e40a5e6c89b6458
Raw
1struct MethodAttributeHolder {}
2
3@[api_name: 'handler']
4@[secured: true]
5@[retries: 3]
6fn (m MethodAttributeHolder) endpoint() {}
7
8fn (m MethodAttributeHolder) no_attrs() {}
9
10fn find_method_attr(attrs []VAttribute, name string, kind AttributeKind) string {
11 for attr in attrs {
12 if attr.name == name && attr.kind == kind {
13 return attr.arg
14 }
15 }
16 return ''
17}
18
19fn test_comptime_method_attributes_are_structured() {
20 mut saw_endpoint := false
21 mut saw_no_attrs := false
22 $for method in MethodAttributeHolder.methods {
23 if method.name == 'endpoint' {
24 saw_endpoint = true
25 assert method.attrs.len == 3
26 assert method.attributes.len == 3
27 assert find_method_attr(method.attributes, 'api_name', .string) == 'handler'
28 assert find_method_attr(method.attributes, 'secured', .bool) == 'true'
29 assert find_method_attr(method.attributes, 'retries', .number) == '3'
30 }
31 if method.name == 'no_attrs' {
32 saw_no_attrs = true
33 assert method.attrs.len == 0
34 assert method.attributes.len == 0
35 }
36 }
37 assert saw_endpoint
38 assert saw_no_attrs
39}
40