fspl/parser/parser_test.go

204 lines
4.1 KiB
Go

package parser
import "testing"
func TestType (test *testing.T) {
testString (test,
// correct
`BasicInt: Int
Structure: (x:Int y:Int)
Interface: ([aMethod x:Int]:*U8 [otherMethod arg:5:Int]:*U8)
Array: 16:16:16:Int
StructArray: 31:24:340920:(x:Int y:Int)
String: *:U8`,
// input
`
BasicInt: Int
Structure: (
x:Int
y:Int)
Interface: (
[aMethod x:Int]:*U8
[otherMethod arg:5:Int]:*U8)
Array: 16:16:16:Int
StructArray: 31:24:340920:(
x:Int
y:Int)
String: *:U8
`)
}
func TestLiteral (test *testing.T) {
testString (test,
// correct
`[int]:Int = 5
[float]:F64 = 324.23409
[boolean]:Bool = true
[boolean2]:Bool = false
[struct]:(x:Int y:(w:F64 z:F64)) = (x:1 y:(w:1.2 z:78.5))
[string]:String = 'hahah \'sup\005'`,
// input
`
[int]:Int = 5
[float]:F64 = 324.23409
[boolean]:Bool = true
[boolean2]:Bool = false
[struct]:(x:Int y:(w:F64 z:F64)) = (x: 1 y: (w: 1.2 z: 78.5))
[string]:String = 'hahah \'sup\005'
`)
}
func TestExpression (test *testing.T) {
testString (test,
// correct
`[var] = sdfdf
[memberAccess] = sdfdf.a
[methodAccess] = sdfdf.[method 238 9 203]
[declaration] = sdfdf:Int
[call] = [print 3 1]
[emptyBlock] = {}
[nestedBlock] = {342 93.34 {3948 32}}
[subscript] = [.(* 3 1 2 4) 3]
[slice] = [\(* 1 2 3 4 5 6 7 8 9) consumed:]
[dereference] = [.ptr]
[reference] = [@val]
[valueCast] = [~ F32 someValue]
[bitCast] = [~~ 4:U8 someValue]
[math] = {[+ 3 4 2 [/ 24 3]] [|| 3 [<< 56 4]] [++ 3] [-- 3] [- 3 1]}
[ifElse]:Int = if true then 4 else 5
[dangleElse]:Int = if true then if false then 3 else 5
[loop]:Int = {i:Int=0 if [< i 3] then return 1 loop {[print 3] if [> i 3] then break 0 i=[++ i]}}`,
// input
`
[var] = sdfdf
[memberAccess] = sdfdf.a
[methodAccess] = sdfdf.[method 238 9 203]
[declaration] = sdfdf:Int
[call] = [print 3 1]
[emptyBlock] = { }
[nestedBlock] = {
342
93.34
{
3948
32
}
}
[subscript] = [. (* 3 1 2 4) 3]
[slice] = [\ (* 1 2 3 4 5 6 7 8 9) consumed:]
[dereference] = [. ptr]
[reference] = [@ val]
[valueCast] = [~ F32 someValue]
[bitCast] = [~~ 4:U8 someValue]
[math] = {
[+ 3 4 2 [/ 24 3]]
[|| 3 [<< 56 4]]
[++ 3]
[-- 3]
[- 3 1]
}
[ifElse]:Int = if true then 4 else 5
[dangleElse]:Int =
if true then
if false then
3
else
5
[loop]:Int = {
i:Int = 0
if [< i 3] then return 1
loop {
[print 3]
if [> i 3] then break 0
i = [++ i]
}
}
`)
}
func TestFunction (test *testing.T) {
testString (test,
// correct
`[arguments x:Int y:Int z:Int] = {}
[arrayArguments arg:4:54:23:28:(x:Int y:Int)] = {}`,
//input
`
[arguments x:Int y:Int z:Int] = { }
[arrayArguments arg:4:54:23:28:(x:Int y:Int)] = { }
`)
}
func TestMethod (test *testing.T) {
testString (test,
// correct
`BopIt: Int
BopIt.[bop force:UInt] = {}
BopIt.[twist angle:F64] = {}
BopIt.[pull distance:Int] = {}`,
//input
`
BopIt: Int
BopIt.[bop force:UInt] = { }
BopIt.[twist angle:F64] = { }
BopIt.[pull distance:Int] = { }
`)
}
func TestAccess (test *testing.T) {
testString (test,
// correct
`+ PublicType: Int
# RestrictedType: (x:Int y:Int)
- PrivateType: String
AlsoPrivateType: Byte
+ [publicFn]:Int = 0
# [restrictedFn]:(x:Int y:Int) = (x:0 y:0)
- [privateFn]:Rune = 'a'
[alsoPrivateFn]:Byte = 0
T: Int
+ T.[publicFn]:Int = 0
# T.[restrictedFn]:(x:Int y:Int) = (x:0 y:0)
- T.[privateFn]:Rune = 'a'
T.[alsoPrivateFn]:Byte = 0`,
//input
`
+ PublicType: Int
# RestrictedType: (x:Int y:Int)
- PrivateType: String
AlsoPrivateType: Byte
+ [publicFn]: Int = 0
# [restrictedFn]: (x:Int y:Int) = (x:0 y:0)
- [privateFn]: Rune = 'a'
[alsoPrivateFn]: Byte = 0
T: Int
+ T.[publicFn]: Int = 0
# T.[restrictedFn]: (x:Int y:Int) = (x:0 y:0)
- T.[privateFn]: Rune = 'a'
T.[alsoPrivateFn]: Byte = 0
`)
}
func TestLinkName (test *testing.T) {
testString (test,
// correct
`[puts byte:*:Byte]:Index 'puts'
File.[write size:Index nmemb:Index]:Index 'fwrite'
[main]:Int 'main' = 0`,
// input
`
[puts byte:*:Byte]: Index 'puts'
File.[write size:Index nmemb:Index]: Index 'fwrite'
[main]: Int 'main' = 0
`)
}
func TestModuleNamespace (test *testing.T) {
testString (test,
// correct
`[hello out:io::Writer] = ioutil::[writeString out 'hello']`,
// input
`
[hello out:io::Writer] = ioutil::[writeString out 'hello']
`)
}