v2 / vlib / v / tests / conditions / matches / match_in_fn_call_test.v
50 lines · 43 sloc · 873 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1@[params]
2struct Data {
3 array []int
4}
5
6fn (d Data) len() int {
7 return d.array.len
8}
9
10fn make_result() []Data {
11 return []
12}
13
14// make_result2 is here to ensure that
15// the branches contain different code,
16// so the tests passes even with -cstrict -cc gcc
17fn make_result2() []Data {
18 return []
19}
20
21fn f_doesnotcompile(d Data) []Data {
22 return match d.len() {
23 1 { make_result() }
24 else { make_result2() }
25 }
26}
27
28fn f_compiles1(d Data) []Data {
29 return match d.array.len {
30 1 { make_result() }
31 else { make_result2() }
32 }
33}
34
35fn f_compiles2(d Data) []Data {
36 length := d.array.len
37 return match length {
38 1 { make_result() }
39 else { make_result2() }
40 }
41}
42
43fn test_match_in_fn_call() {
44 println(f_doesnotcompile())
45 assert f_doesnotcompile() == []Data{}
46 println(f_compiles1())
47 assert f_compiles1() == []Data{}
48 println(f_compiles2())
49 assert f_compiles2() == []Data{}
50}
51