v2 / vlib / v / fmt / tests / comptime_keep.vv
93 lines · 82 sloc · 1.59 KB · 5a2b1ad8de04313d6089be9dc8d26ae2ae2bd6f2
Raw
1import veb
2
3const embedded_file = $embed_file(r'C:\Users\user\path\to\file')
4
5struct App {
6 a string
7 b string
8mut:
9 c int
10 d f32
11pub:
12 e f32
13 f u64
14pub mut:
15 g string
16 h u8
17}
18
19fn comptime_for() {
20 println(@FN)
21 $for method in App.methods {
22 println(' method: ${method.name} | ${method}')
23 }
24}
25
26fn comptime_for_with_if() {
27 println(@FN)
28 $for method in App.methods {
29 println(' method: ${method}')
30 $if method.typ is fn () {
31 assert method.name in ['run', 'method2']
32 }
33 $if method.return_type is int {
34 assert method.name in ['int_method1', 'int_method2']
35 }
36 $if method.args[0].typ is string {
37 assert method.name == 'my_method'
38 }
39 }
40}
41
42fn comptime_for_fields() {
43 println(@FN)
44 $for field in App.fields {
45 println(' field: ${field.name} | ${field}')
46 $if field.typ is string {
47 assert field.name in ['a', 'b', 'g']
48 }
49 $if field.typ is f32 {
50 assert field.name in ['d', 'e']
51 }
52 if field.is_mut {
53 assert field.name in ['c', 'd', 'g', 'h']
54 }
55 if field.is_pub {
56 assert field.name in ['e', 'f', 'g', 'h']
57 }
58 if field.is_pub && field.is_mut {
59 assert field.name in ['g', 'h']
60 }
61 }
62}
63
64struct Result {}
65
66fn (mut a App) my_method(p string) Result {
67 println('>>>> ${@FN} | p: ${p}')
68 return Result{}
69}
70
71fn handle_conn[T](mut app T) {
72 $for method in T.methods {
73 $if method.return_type is Result {
74 app.$method('abc', 'def')
75 }
76 }
77}
78
79fn comptime_call_dollar_method() {
80 mut app := App{}
81 handle_conn[App](mut app)
82}
83
84fn (mut app App) create() veb.Result {
85 return $veb.html()
86}
87
88fn main() {
89 comptime_for()
90 comptime_for_with_if()
91 comptime_for_fields()
92 comptime_call_dollar_method()
93}
94