v2 / vlib / v / tests / match_or_expr_test.v
56 lines · 49 sloc · 755 bytes · 81a8786ca22b8edf8b18c8208bf1bffd868cd8fd
Raw
1struct Foo {
2 option ?int = unsafe { none }
3}
4
5fn test_main() {
6 test := true
7 foo := Foo{}
8 result := foo.option or {
9 match test {
10 true { 1 }
11 else { 2 }
12 }
13 }
14
15 assert result == 1
16}
17
18struct Issue17850Error {
19 Error
20}
21
22struct Issue17850Data {
23pub:
24 name string
25}
26
27fn (_ &Issue17850Error) msg() string {
28 return 'issue 17850'
29}
30
31fn issue17850_do_thing(name string, fail bool) !Issue17850Data {
32 if fail {
33 return Issue17850Error{}
34 }
35 return Issue17850Data{
36 name: name
37 }
38}
39
40fn test_result_or_block_match_error_type() {
41 data := issue17850_do_thing('my_db', true) or {
42 match err {
43 Issue17850Error {
44 Issue17850Data{
45 name: 'my_db'
46 }
47 }
48 else {
49 panic(err)
50 }
51 }
52 }
53 assert data == Issue17850Data{
54 name: 'my_db'
55 }
56}
57