v2 / vlib / v / tests / comptime / comptime_method_args_test.v
54 lines · 49 sloc · 1.22 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct TestStruct {
2mut:
3 one_arg_called bool
4 two_args_called bool
5 three_args_called bool
6}
7
8fn (mut t TestStruct) one_arg(a1 string) {
9 t.one_arg_called = true
10}
11
12fn (mut t TestStruct) two_args(a2 string, b2 int) {
13 t.two_args_called = true
14}
15
16fn (mut t TestStruct) three_args(a3 string, b3 int, c3 []string) {
17 t.three_args_called = true
18}
19
20fn test_comptime_method_names() {
21 mut num_methods := 0
22 $for method in TestStruct.methods {
23 if method.name == 'one_arg' {
24 assert method.args[0].name == 'a1'
25 num_methods++
26 } else if method.name == 'two_args' {
27 assert method.args[0].name == 'a2'
28 assert method.args[1].name == 'b2'
29 num_methods++
30 } else if method.name == 'three_args' {
31 assert method.args[0].name == 'a3'
32 assert method.args[1].name == 'b3'
33 assert method.args[2].name == 'c3'
34 num_methods++
35 }
36 }
37 assert num_methods == 3
38}
39
40fn test_comptime_call_method() {
41 mut t := TestStruct{}
42 $for method in TestStruct.methods {
43 if method.name == 'one_arg' {
44 t.$method('one')
45 } else if method.name == 'two_args' {
46 t.$method('two', 2)
47 } else if method.name == 'three_args' {
48 t.$method('three', 3, ['th', 'ree'])
49 }
50 }
51 assert t.one_arg_called
52 assert t.two_args_called
53 assert t.three_args_called
54}
55