fspl/analyzer/assignment_test.go

324 lines
5.2 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 or less", 3, 12,
`
[main] = {
x:3:Int = (1 2 3 4)
}
`)
}
func TestAssignmentLiteralErrArrayWrongType (test *testing.T) {
testStringErr (test,
"cannot use float literal as Int", 3, 15,
`
[main] = {
x:3:Int = (1 3.3 4)
}
`)
}
func TestAssignmentLiteralErrSliceWrongType (test *testing.T) {
testStringErr (test,
"cannot use float literal as Int", 3, 15,
`
[main] = {
x:*:Int = (1 3.3 4)
}
`)
}
func TestAssignmentLiteralErrStructWrongType (test *testing.T) {
testStringErr (test,
"cannot use float literal as Int", 3, 28,
`
[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, 22,
`
[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 TestAssignmentLiteralErrDerefByteFloat (test *testing.T) {
testStringErr (test,
"cannot use float literal as Byte", 4, 17,
`
[main] = {
buffer:8:Byte
[. buffer 7] = 0.0
}
`)
}
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)
[.[.arr 1] 0] = 6
[.slice 4] = 5
}
`)
}
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 TestAssignmentInterfaceErrBadLayer (test *testing.T) {
testStringErr (test,
"no method named fly defined on this type", 7, 11,
`
Bird: (& [fly distance:F64])
BlueJay: Int
BlueJay.[fly distance:F64] = { }
BlueJayRef: *BlueJay
[main] = {
b:Bird = a:BlueJayRef
}
`)
}
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
ref:*Bird = [@ a]
c:Bird = ref
}
`)
}
func TestAssignmentUnion (test *testing.T) {
testString (test,
`
U: (| Int F64)
[main] = {
x:F64 = 7.7
y:U = x
}
`)
}
func TestAssignmentUnionToUnion (test *testing.T) {
testString (test,
`
U: (| Int F64)
[main] = {
x:U y:U
x = y
}
`)
}
func TestAssignmentUnionErrNotListed (test *testing.T) {
testStringErr (test,
"I64 is not included within union U", 5, 8,
`
U: (| Int F64)
[main] = {
x:I64 = 7
y:U = x
}
`)
}
func TestAssignmentErrByteSliceString (test *testing.T) {
testStringErr (test,
"expected *:Byte", 4, 13,
`
[main] = {
a:String = 'hello'
b:*:Byte = a
}
`)
}
// 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
}
`)
}