v2 / vlib / v / tests / options / nested_option_call_test.v
67 lines · 55 sloc · 809 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1fn ret(s string) string {
2 return s
3}
4
5fn raise() ?string {
6 return none
7}
8
9fn xx() {
10 s := ret(raise() or { return })
11 println(s)
12}
13
14fn test_nested_or() {
15 xx()
16}
17
18fn xx_prop() ?string {
19 s := ret(raise()?)
20 return s
21}
22
23fn test_nested_propagation() {
24 a := xx_prop() or { 'propagated' }
25 assert a == 'propagated'
26}
27
28struct St {
29mut:
30 z f64
31}
32
33fn (mut s St) raise() ?f64 {
34 return none
35}
36
37fn retf(f f64) f64 {
38 return f
39}
40
41fn (mut s St) aa() {
42 f := retf(s.raise() or { return })
43 s.z = 7.5
44 println(f)
45}
46
47fn test_nested_or_method_call() {
48 mut x := St{
49 z: 2.25
50 }
51 x.aa()
52 assert x.z == 2.25
53}
54
55fn (mut s St) aa_propagate() ? {
56 f := retf(s.raise()?)
57 s.z = 7.5
58 println(f)
59}
60
61fn test_nested_propagation_method() {
62 mut x := St{
63 z: 2.25
64 }
65 x.aa_propagate() or { x.z = 13.0625 }
66 assert x.z == 13.0625
67}
68