v2 / vlib / v / tests / builtin_arrays / array_elements_with_option_test.v
44 lines · 37 sloc · 612 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct Foo1 {
2 arr1 [5]?int
3 arr2 [5]int
4}
5
6fn get_has_option_fixed() ?int {
7 foo := Foo1{}
8 return foo.arr1[0]
9}
10
11fn get_no_option_fixed() int {
12 foo := Foo1{}
13 return foo.arr2[0]
14}
15
16fn test_option_fixed() {
17 x := get_has_option_fixed() or { 0 }
18 assert x == 0
19 assert get_no_option_fixed() == 0
20}
21
22struct Foo2 {
23mut:
24 arr1 []?int
25 arr2 []int
26}
27
28fn get_has_option() ?int {
29 mut foo := Foo2{}
30 foo.arr1 << 0
31 return foo.arr1[0]
32}
33
34fn get_no_option() int {
35 mut foo := Foo2{}
36 foo.arr2 << 0
37 return foo.arr2[0]
38}
39
40fn test_option_non_fixed() {
41 x := get_has_option()?
42 assert x == 0
43 assert get_no_option() == 0
44}
45