v2 / vlib / v / tests / comptime / comptime_deref_or_ref_test.v
41 lines · 37 sloc · 789 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1pub struct Association {
2 price string
3 association &Association = unsafe { nil }
4}
5
6// needed to check the condition for #20400
7fn myprintln(s string) string {
8 println('---------> ${s}')
9 return s
10}
11
12fn encode_struct[U](a U) {
13 mut c := 0
14 $for field in U.fields {
15 $if field.name == 'association' {
16 x := myprintln(ptr_str(a.$(field.name)))
17 println(field.name)
18 c += 1
19 assert ptr_str(a.$(field.name)) == '0'
20 } $else {
21 println(field.name)
22 c += 1
23 assert ptr_str(a.$(field.name)) != '0'
24 }
25 x := myprintln(ptr_str(a.$(field.name)))
26 if field.name == 'association' {
27 dump(x)
28 assert x == '0'
29 }
30 }
31 dump(c)
32 assert c == 2
33}
34
35fn test_main() {
36 a := Association{}
37
38 assert ptr_str(a.price) != '0'
39 assert ptr_str(a.association) == '0'
40 encode_struct(a)
41}
42