v2 / vlib / v / tests / comptime / comptime_propagate_test.v
38 lines · 33 sloc · 739 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 assert fixed.a.str() == '123'
13 assert fixed.b.int() == 456
14
15 assert fixed.c?.str() == '789'
16 assert fixed.d?.int() == 321
17
18 e := Encoder{}
19 e.encode_struct(fixed)
20}
21
22fn (e &Encoder) encode_struct[T](val T) {
23 $for field in T.fields {
24 $if !field.is_option {
25 $if field.typ is int {
26 assert val.$(field.name).str() == '123'
27 } $else $if field.typ is string {
28 assert val.$(field.name).int() == 456
29 }
30 } $else {
31 $if field.typ is ?int {
32 assert val.$(field.name) ?.str() == '789'
33 } $else $if field.typ is ?string {
34 assert val.$(field.name) ?.int() == 321
35 }
36 }
37 }
38}
39