v2 / vlib / v / tests / fns / optional_fn_selector_call_test.v
59 lines · 51 sloc · 959 bytes · 986153bec4ba578b080619e5ebf939583af37002
Raw
1type GuardedFn = fn (arg Bar) bool
2
3type GuardedResultFn = fn (arg Bar) !bool
4
5struct GuardedFoo {
6 f ?GuardedFn
7}
8
9struct GuardedResultFoo {
10 f ?GuardedResultFn
11}
12
13@[params]
14struct Bar {}
15
16fn (foo GuardedFoo) call(arg Bar) bool {
17 if foo.f != none {
18 // vfmt off
19 return foo.f or { panic(err) }(arg)
20 // vfmt on
21 }
22 return false
23}
24
25fn (foo GuardedResultFoo) call(arg Bar) bool {
26 if foo.f != none {
27 // vfmt off
28 return foo.f or { panic(err) }(arg) or { return false }
29 // vfmt on
30 }
31 return false
32}
33
34fn test_optional_fn_selector_call_in_if_guard() {
35 foo := GuardedFoo{
36 f: fn (arg Bar) bool {
37 _ = arg
38 return true
39 }
40 }
41 assert foo.call(Bar{})
42}
43
44fn test_optional_fn_selector_call_with_nested_or_blocks() {
45 ok := GuardedResultFoo{
46 f: fn (arg Bar) !bool {
47 _ = arg
48 return true
49 }
50 }
51 failing := GuardedResultFoo{
52 f: fn (arg Bar) !bool {
53 _ = arg
54 return error('boom')
55 }
56 }
57 assert ok.call(Bar{})
58 assert !failing.call(Bar{})
59}
60