fspl/analyzer/control-flow_test.go

112 lines
1.8 KiB
Go

package analyzer
import "testing"
func TestMatch (test *testing.T) {
testString (test,
`
U: (| Int F64)
[matchToInt u:U]:Int = match u in
| u:Int u
| u:F64 [~Int u]
`)
}
func TestMatchReturn (test *testing.T) {
testString (test,
`
U: (| Int F64)
[isInt u:U]:Bool = {
match u in | u:Int [return true]
false
}
`)
}
func TestMatchReturnValueUsed (test *testing.T) {
testString (test,
`
U: (| Int F64)
[isInt u:U]:Bool = match u in
| u:Int [return true]
| u:F64 false
`)
}
func TestMatchUnionUnderComplete (test *testing.T) {
testString (test,
`
U: (| Int F64 UInt)
[print str:String]
[matchToInt u:U] = match u in
| u:Int [print 'Int']
| u:F64 [print 'F64']
`)
}
func TestMatchErrUnionOverComplete (test *testing.T) {
testStringErr (test,
"UInt is not included within U", 5, 6,
`
U: (| Int F64)
[matchToInt u:U]:Int = match u in
| u:Int u
| u:UInt [~Int u]
| u:F64 [~Int u]
`)
}
func TestMatchErrNotUnion (test *testing.T) {
testStringErr (test,
"type U is not a union", 3, 24,
`
U: Int
[matchToInt u:U]:Int = match u in
| u:Int u
| u:F64 [~Int u]
`)
}
func TestMatchErrUnionUnderComplete (test *testing.T) {
testStringErr (test,
"match does not cover all types within U", 3, 24,
`
U: (| Int F64 UInt)
[matchToInt u:U]:Int = match u in
| u:Int u
| u:F64 [~Int u]
`)
}
func TestMatchErrDuplicate (test *testing.T) {
testStringErr (test,
"Int already listed in match at stream0.fspl:4:6", 6, 6,
`
U: (| Int F64 UInt)
[matchToInt u:U]:Int = match u in
| u:Int u
| u:F64 [~Int u]
| u:Int u
`)
}
func TestIfElseReturnValueUsed (test *testing.T) {
testString (test,
`
[is5 x:Int]:Bool = if [= x 5]
then true
else [return false]
`)
}
func TestIfElseBreakValueUsed (test *testing.T) {
testString (test,
`
[f x:Int]: Int = loop {
y:Int = if [= x 5]
then [break 0]
else x
}
`)
}