fspl/analyzer/assignment_test.go
2023-10-31 21:52:47 -04:00

249 lines
4.1 KiB
Go

package analyzer
import "testing"
func TestAssignmentLiteralErrUnsignedNegative (test *testing.T) {
testStringErr (test,
"integer literal out of range for type UInt", 3, 11,
`
[main] = {
x:UInt = -5
}
`)
}
func TestAssignmentLiteralErrIntegerFloat (test *testing.T) {
testStringErr (test,
"cannot use float literal as Int", 3, 10,
`
[main] = {
x:Int = 5.5
}
`)
}
func TestAssignmentLiteralErrIntegerStruct (test *testing.T) {
testStringErr (test,
"cannot use struct literal as Int", 3, 10,
`
[main] = {
x:Int = (x: 5)
}
`)
}
func TestAssignmentLiteralErrIntegerArray (test *testing.T) {
testStringErr (test,
"cannot use array literal as Int", 3, 10,
`
[main] = {
x:Int = (* 5)
}
`)
}
func TestAssignmentLiteralErrIntegerOverflow (test *testing.T) {
testStringErr (test,
"integer literal out of range for type Byte", 3, 11,
`
[main] = {
x:Byte = 1000
}
`)
}
func TestAssignmentLiteralErrArrayInt (test *testing.T) {
testStringErr (test,
"cannot use integer literal as 3:Int", 3, 12,
`
[main] = {
x:3:Int = 5
}
`)
}
func TestAssignmentLiteralErrArrayWrongLength (test *testing.T) {
testStringErr (test,
"expected 3 elements", 3, 12,
`
[main] = {
x:3:Int = (* 1 2 3 4)
}
`)
}
func TestAssignmentLiteralErrArrayWrongType (test *testing.T) {
testStringErr (test,
"cannot use float literal as Int", 3, 17,
`
[main] = {
x:3:Int = (* 1 3.3 4)
}
`)
}
func TestAssignmentLiteralErrSliceWrongType (test *testing.T) {
testStringErr (test,
"cannot use float literal as Int", 3, 17,
`
[main] = {
x:*:Int = (* 1 3.3 4)
}
`)
}
func TestAssignmentLiteralErrStructWrongType (test *testing.T) {
testStringErr (test,
"cannot use float literal as Int", 3, 24,
`
[main] = {
x:(x:Int y:Int) = (x: 5.5 y: 3)
}
`)
}
func TestAssignmentLiteralErrStructInteger (test *testing.T) {
testStringErr (test,
"cannot use integer literal as (x:Int y:Int)", 3, 20,
`
[main] = {
x:(x:Int y:Int) = 5
}
`)
}
func TestAssignmentLiteralErrInterfaceInt (test *testing.T) {
testStringErr (test,
"cannot use integer literal as Bird", 4, 11,
`
Bird: ([fly distance:F64] [land])
[main] = {
b:Bird = 5
}
`)
}
func TestAssignmentLiteralErrInterfaceFloat (test *testing.T) {
testStringErr (test,
"cannot use float literal as Bird", 4, 11,
`
Bird: ([fly distance:F64] [land])
[main] = {
b:Bird = 5.5
}
`)
}
func TestAssignmentLiteralErrInterfaceArray (test *testing.T) {
testStringErr (test,
"cannot use array literal as Bird", 4, 11,
`
Bird: ([fly distance:F64] [land])
[main] = {
b:Bird = (* 1 2 3 4)
}
`)
}
func TestAssignmentLiteralErrInterfaceStruct (test *testing.T) {
testStringErr (test,
"cannot use struct literal as Bird", 4, 11,
`
Bird: ([fly distance:F64] [land])
[main] = {
b:Bird = (x: 5 y: 6)
}
`)
}
func TestAssignmentLiteral (test *testing.T) {
testString (test,
`
[main] = {
a:F64 = 5.3
b:F32 = 5.3
c:F64 = -5
d:F32 = -5
e:Byte = 64
z:UInt = 5
x:Int = -5
arr:4:2:Int = (*
(* 1 2)
(* 3 4)
(* 5 6)
(* 7 8))
slice:*:Int = (* 3 1 2 3)
struct:(x:Int y:Int) = (
x: 9
y: 10)
}
`)
}
func TestAssignmentInterfaceErrBadSignature (test *testing.T) {
testStringErr (test,
"BlueJay has wrong signature for method fly", 7, 11,
`
Bird: ([fly distance:F64] [land])
BlueJay: Int
BlueJay.[fly] = { }
BlueJay.[land] = { }
[main] = {
b:Bird = a:BlueJay
}
`)
}
func TestAssignmentInterfaceErrMissingMethod (test *testing.T) {
testStringErr (test,
"no method named fly defined on this type", 6, 11,
`
Bird: ([fly distance:F64] [land])
BlueJay: Int
BlueJay.[land] = { }
[main] = {
b:Bird = a:BlueJay
}
`)
}
func TestAssignmentInterface (test *testing.T) {
testString (test,
`
Bird: ([fly distance:F64] [land])
BlueJay: Int
BlueJay.[fly distance:F64] = { }
BlueJay.[land] = { }
[main] = {
b:Bird = a:BlueJay
}
`)
}
// TODO: complete and test error cases
func TestAssignmentPropagateRules (test *testing.T) {
testString (test,
`
[f]:Byte = 5
A:Int
A.[g]:Int = 2
B:([g]:Int)
[main] = {
a:Int
b:Int = a
c:Int = d:Int
d = { a:F64 b }
e:Byte = [f]
g:(x:Int y:(w:F64 z:F64)) = (x: 1 y: (w: 1.2 z: 78.5))
gx:(w:F64 z:F64) = g.y
h:F64 = gx.z
i:A
j:Int = i.[g]
k:B = i
l:Int = k.[g]
m:5:Int = (* 0 1 2 3 4)
n:Int = [. m 3]
o:*:Int = m
}
`)
}