Skip to content

Commit f6cc88f

Browse files
authored
tests: add new test for #16519 (#16520)
1 parent f0a23c8 commit f6cc88f

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
struct None {}
2+
3+
type Myfn = fn (int) int
4+
5+
type Myfnfact = fn () Myfn
6+
7+
type Maybefnfact = Myfnfact | None
8+
9+
// Myfn
10+
fn abc(i int) int {
11+
return i
12+
}
13+
14+
// create Myfn
15+
fn myfnfact() Myfn {
16+
return abc
17+
}
18+
19+
// run fn if exists
20+
fn run(mmff Maybefnfact) string {
21+
match mmff {
22+
Myfnfact {
23+
r := mmff()
24+
return 'yes fn: ${r}'
25+
}
26+
None {
27+
return 'None fn'
28+
}
29+
}
30+
}
31+
32+
fn test_sumtype_with_alias_fntype_fn_call() {
33+
r1 := main.myfnfact()(1)
34+
println(r1)
35+
assert r1 == 1
36+
37+
r2 := run(None{})
38+
println(r2)
39+
assert r2 == 'None fn'
40+
41+
r3 := run(myfnfact)
42+
println(r3)
43+
assert r3 == 'yes fn: fn (int) int'
44+
}

vlib/v/tests/sumtype_with_alias_fntype_test.v

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,18 @@ fn abc(i int) int {
1111
return i
1212
}
1313

14-
// create Myfn
15-
fn myfnfact() Myfn {
16-
return abc
17-
}
18-
19-
// run fn if exists
2014
fn run(mmff Maybefnfact) string {
2115
match mmff {
22-
Myfnfact {
23-
r := mmff()
24-
return 'yes fn: ${r}'
25-
}
26-
None {
27-
return 'None fn'
28-
}
16+
Myfnfact { return 'yes fn' }
17+
None { return 'None fn' }
2918
}
3019
}
3120

3221
fn test_sumtype_with_alias_fntype() {
22+
// create Myfn
23+
myfnfact := fn () Myfn {
24+
return abc
25+
}
3326
r1 := main.myfnfact()(1)
3427
println(r1)
3528
assert r1 == 1
@@ -40,5 +33,5 @@ fn test_sumtype_with_alias_fntype() {
4033

4134
r3 := run(myfnfact)
4235
println(r3)
43-
assert r3 == 'yes fn: fn (int) int'
36+
assert r3 == 'yes fn'
4437
}

0 commit comments

Comments
 (0)