v2 / vlib / v / tests / comptime / comptime_selector_ptr_test.v
46 lines · 37 sloc · 780 bytes · acf6b344f733169ec6ecc9881f8a8c2c795b9883
Raw
1struct Encoder {}
2
3struct Writer {}
4
5struct APrice {}
6
7struct Association {
8 association &Association = unsafe { nil }
9 price APrice
10}
11
12fn get_value_from_ref[T](val &T) T {
13 return *val
14}
15
16fn (e &Encoder) encode_struct[U](val U, mut wr Writer) ! {
17 $for field in U.fields {
18 $if field.typ is &Association {
19 assert get_value_from_ref(val.$(field.name)) == Association{
20 association: unsafe { nil }
21 price: APrice{}
22 }
23 }
24 }
25}
26
27fn test_main() {
28 e := Encoder{}
29 mut sb := Writer{}
30
31 value := Association{
32 association: &Association{
33 price: APrice{}
34 }
35 price: APrice{}
36 }
37
38 assert get_value_from_ref(&Association{
39 price: APrice{}
40 }) == Association{
41 association: unsafe { nil }
42 price: APrice{}
43 }
44
45 e.encode_struct(value, mut sb)!
46}
47