1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/v/tests/match_expr_returning_option_test.v
2023-01-09 09:36:45 +03:00

51 lines
660 B
V

type Any = int | string
fn ok(s string) Any {
return match s {
'foo' {
Any(1)
}
else {
Any('asdf')
}
}
}
fn fails(s string) ?Any {
return match s {
'foo' {
Any(1)
}
else {
Any('asdf')
}
}
}
fn test_match_expr_returning_option() {
ret1 := ok('foo')
println(ret1)
assert ret1 == Any(1)
ret2 := fails('foo') or {
assert false
return
}
println(ret2)
assert ret2 == Any(1)
}
fn func() ?string {
code := 0
return match code {
0 { 'zero' }
else { error('as we are returning an option') }
}
}
fn test_match_expr_returning_option_with_error() {
ret := func() or { 'error' }
println(ret)
assert ret == 'zero'
}