v2 / vlib / v / tests / comptime / comptime_unwrap_test.v
35 lines · 32 sloc · 531 bytes · 5be2fcab7bb566124c7f8ef50cbffe0690595487
Raw
1struct Foo {
2 a ?int
3 b ?string
4}
5
6fn receives_int(a int) {}
7
8fn receives_string(a string) {}
9
10fn test_main() {
11 t := Foo{
12 a: 1
13 b: 'foo'
14 }
15 mut c := 0
16 $for f in t.fields {
17 $if f.typ is ?int {
18 assert t.$(f.name) ?.str() == '1'
19 w := t.$(f.name) ?
20 assert w == 1
21 receives_int(w)
22 receives_int(t.$(f.name) ?)
23 c++
24 }
25 $if f.typ is ?string {
26 assert t.$(f.name) ?.str() == 'foo'
27 a := t.$(f.name) ?
28 assert a == 'foo'
29 receives_string(a)
30 receives_string(t.$(f.name) ?)
31 c++
32 }
33 }
34 assert c == 2
35}
36