v2 / vlib / v / tests / comptime / comptime_attribute_call_syntax_test.v
69 lines · 62 sloc · 1.54 KB · 3ffc951cf555dc4818309a507ccb9d0da4de748f
Raw
1// vfmt off
2@[deprecated('use NewPositional instead')]
3struct OldPositional {}
4
5@[deprecated(msg: 'use NewNamed instead', after: '2999-01-01')]
6struct OldNamed {}
7
8@[deprecated('use NewMixed instead', after: '2999-01-02')]
9struct OldMixed {}
10
11@[custom(flag: true, count: 2)]
12struct CustomNamed {}
13// vfmt on
14
15fn test_attribute_call_syntax_positional_and_named_args() {
16 mut positional_msg := ''
17 $for attr in OldPositional.attributes {
18 if attr.name == 'deprecated' {
19 positional_msg = attr.arg
20 }
21 }
22 assert positional_msg == 'use NewPositional instead'
23
24 mut named_msg := ''
25 mut named_after := ''
26 $for attr in OldNamed.attributes {
27 if attr.name == 'deprecated' {
28 named_msg = attr.arg
29 }
30 if attr.name == 'deprecated_after' {
31 named_after = attr.arg
32 }
33 }
34 assert named_msg == 'use NewNamed instead'
35 assert named_after == '2999-01-01'
36
37 mut mixed_msg := ''
38 mut mixed_after := ''
39 $for attr in OldMixed.attributes {
40 if attr.name == 'deprecated' {
41 mixed_msg = attr.arg
42 }
43 if attr.name == 'deprecated_after' {
44 mixed_after = attr.arg
45 }
46 }
47 assert mixed_msg == 'use NewMixed instead'
48 assert mixed_after == '2999-01-02'
49}
50
51fn test_attribute_call_syntax_generic_named_args() {
52 mut has_custom := false
53 mut custom_flag := ''
54 mut custom_count := ''
55 $for attr in CustomNamed.attributes {
56 if attr.name == 'custom' {
57 has_custom = true
58 }
59 if attr.name == 'custom_flag' {
60 custom_flag = attr.arg
61 }
62 if attr.name == 'custom_count' {
63 custom_count = attr.arg
64 }
65 }
66 assert has_custom
67 assert custom_flag == 'true'
68 assert custom_count == '2'
69}
70