v2 / vlib / v / tests / comptime / comptime_call_test.v
74 lines · 65 sloc · 1.43 KB · 66207866bb98f11345ce49c67014dde5b4a65cdb
Raw
1struct Test {}
2
3fn (test Test) v() {
4 println('Test.v()')
5}
6
7fn (test Test) i() int {
8 return 4
9}
10
11fn (test Test) s() string {
12 return 'test'
13}
14
15fn (test Test) s2() string {
16 return 'Two'
17}
18
19fn test_string_identifier() {
20 test := Test{}
21 sv := 'v'
22 test.$sv()
23 si := 'i'
24 ri := test.$si()
25 assert ri == 4
26 ss := 's'
27 rs := test.$ss()
28 assert rs == 'test'
29}
30
31fn test_for_methods() {
32 test := Test{}
33 mut r := ''
34 $for method in Test.methods {
35 // currently the checker thinks all $method calls return string
36 $if method.return_type is string {
37 v := test.$method()
38 r += v.str()
39 if method.name == 's' {
40 assert method.location.ends_with('vlib/v/tests/comptime/comptime_call_test.v:11:15')
41 } else if method.name == 's2' {
42 assert method.location.ends_with('vlib/v/tests/comptime/comptime_call_test.v:15:15')
43 }
44 } $else $if method.return_type is int {
45 // TODO
46 // v := test.$method()
47 v := '?'
48 r += v.str()
49 assert method.name == 'i'
50 assert method.location.ends_with('vlib/v/tests/comptime/comptime_call_test.v:7:15')
51 } $else {
52 // no return type
53 test.$method()
54 assert method.name == 'v'
55 assert method.location.ends_with('vlib/v/tests/comptime/comptime_call_test.v:3:15')
56 }
57 }
58 assert r == '?testTwo'
59}
60
61struct S1 {}
62
63fn (t S1) rep(s string, i int) string {
64 return s.repeat(i)
65}
66
67fn test_methods_arg() {
68 s1 := S1{}
69 $for method in S1.methods {
70 arr := ['!', '3']
71 r := s1.$method(...arr)
72 assert r == '!!!'
73 }
74}
75