1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tests: add new test for #16519 (#16520)

This commit is contained in:
yuyi 2022-11-24 21:56:07 +08:00 committed by GitHub
parent f0a23c8d3c
commit f6cc88fa69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 14 deletions

View File

@ -0,0 +1,44 @@
struct None {}
type Myfn = fn (int) int
type Myfnfact = fn () Myfn
type Maybefnfact = Myfnfact | None
// Myfn
fn abc(i int) int {
return i
}
// create Myfn
fn myfnfact() Myfn {
return abc
}
// run fn if exists
fn run(mmff Maybefnfact) string {
match mmff {
Myfnfact {
r := mmff()
return 'yes fn: ${r}'
}
None {
return 'None fn'
}
}
}
fn test_sumtype_with_alias_fntype_fn_call() {
r1 := main.myfnfact()(1)
println(r1)
assert r1 == 1
r2 := run(None{})
println(r2)
assert r2 == 'None fn'
r3 := run(myfnfact)
println(r3)
assert r3 == 'yes fn: fn (int) int'
}

View File

@ -11,25 +11,18 @@ fn abc(i int) int {
return i return i
} }
// create Myfn
fn myfnfact() Myfn {
return abc
}
// run fn if exists
fn run(mmff Maybefnfact) string { fn run(mmff Maybefnfact) string {
match mmff { match mmff {
Myfnfact { Myfnfact { return 'yes fn' }
r := mmff() None { return 'None fn' }
return 'yes fn: ${r}'
}
None {
return 'None fn'
}
} }
} }
fn test_sumtype_with_alias_fntype() { fn test_sumtype_with_alias_fntype() {
// create Myfn
myfnfact := fn () Myfn {
return abc
}
r1 := main.myfnfact()(1) r1 := main.myfnfact()(1)
println(r1) println(r1)
assert r1 == 1 assert r1 == 1
@ -40,5 +33,5 @@ fn test_sumtype_with_alias_fntype() {
r3 := run(myfnfact) r3 := run(myfnfact)
println(r3) println(r3)
assert r3 == 'yes fn: fn (int) int' assert r3 == 'yes fn'
} }