| 1 | struct Test {} |
| 2 | |
| 3 | fn (test Test) v() { |
| 4 | println('Test.v()') |
| 5 | } |
| 6 | |
| 7 | fn (test Test) i() int { |
| 8 | return 4 |
| 9 | } |
| 10 | |
| 11 | fn (test Test) s() string { |
| 12 | return 'test' |
| 13 | } |
| 14 | |
| 15 | fn (test Test) s2() string { |
| 16 | return 'Two' |
| 17 | } |
| 18 | |
| 19 | fn 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 | |
| 31 | fn 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 | |
| 61 | struct S1 {} |
| 62 | |
| 63 | fn (t S1) rep(s string, i int) string { |
| 64 | return s.repeat(i) |
| 65 | } |
| 66 | |
| 67 | fn 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 | |