v2 / vlib / v / tests / options / option_struct_init_default_test.v
58 lines · 54 sloc · 962 bytes · 56739fe97126504c58950c8d6fd243c050adc40a
Raw
1struct Struct {
2 f1 string
3 f2 int
4 f3 bool
5 f4 f64
6}
7
8struct StructWithFieldsOfOptional {
9 f1 ?string
10 f2 ?int
11 f3 ?bool
12 f4 ?f64
13}
14
15struct StructWithFieldsOfOptionalArray {
16 f1 []?string
17 f2 []?int
18 f3 []?bool
19 f4 []?f64
20}
21
22fn test_struct_with_fields_of_optional() {
23 v1 := StructWithFieldsOfOptional{
24 f1: ?string('a')
25 f2: ?int(1)
26 f3: ?bool(true)
27 f4: ?f64(1.1)
28 }
29 v2 := Struct{
30 f1: v1.f1 or { '' }
31 f2: v1.f2 or { 0 }
32 f3: v1.f3 or { false }
33 f4: v1.f4 or { 0.0 }
34 }
35 assert v2.f1 == 'a'
36 assert v2.f2 == 1
37 assert v2.f3 == true
38 assert v2.f4 == 1.1
39}
40
41fn test_struct_with_fields_of_optional_array() {
42 v1 := StructWithFieldsOfOptionalArray{
43 f1: [?string('a'), 'b']
44 f2: [?int(1), 2]
45 f3: [?bool(true), false]
46 f4: [?f64(1.1), 2.2]
47 }
48 v2 := Struct{
49 f1: v1.f1[0] or { '' }
50 f2: v1.f2[0] or { 0 }
51 f3: v1.f3[0] or { false }
52 f4: v1.f4[0] or { 0.0 }
53 }
54 assert v2.f1 == 'a'
55 assert v2.f2 == 1
56 assert v2.f3 == true
57 assert v2.f4 == 1.1
58}
59