v2 / vlib / v / tests / fns / call_to_str_on_option_test.v
38 lines · 33 sloc · 633 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct FixedStruct1 {
2 a int
3 b string
4 c ?int
5 d ?string
6}
7
8struct Encoder {}
9
10fn test_main() {
11 fixed := FixedStruct1{123, '456', 789, '321'}
12 // this work well
13 println(fixed.a.str())
14 println(fixed.c?.str())
15
16 println(fixed.b.int())
17 println(fixed.d?.int())
18
19 e := Encoder{}
20 // this not work
21 e.encode_struct(fixed)
22}
23
24fn (e &Encoder) encode_struct[T](val T) {
25 mut count := 0
26 $for field in T.fields {
27 mut value := val.$(field.name)
28 $if field.is_option {
29 if field.name in ['c', 'd'] {
30 assert true
31 }
32 println('>> ${value ?.str()}')
33 println(val.$(field.name) ?.str())
34 count += 1
35 }
36 }
37 assert count == 2
38}
39