v2 / vlib / v / tests / options / option_mut_struct_init_test.v
39 lines · 35 sloc · 461 bytes · e4e568945e44f2ce1a440712407aeb7dd9a7274e
Raw
1struct Bar {
2mut:
3 a int
4}
5
6struct Foo {
7 field ?&Bar
8}
9
10fn t(mut opt ?Bar) {
11 v := Foo{
12 field: opt
13 }
14 if opt == none {
15 assert opt == none
16 assert v.field == none
17 } else {
18 assert opt.a == 123
19 assert v.field != none
20 }
21 if mut opt != none {
22 opt.a = 321
23 }
24}
25
26fn test_main() {
27 mut var := ?&Bar(none)
28 t(mut var)
29 assert var == none
30}
31
32fn test_not_none() {
33 mut var := ?&Bar(&Bar{
34 a: 123
35 })
36 t(mut var)
37 assert var != none
38 assert var?.a == 321
39}
40