Added eval tests
This commit is contained in:
parent
33768c6d84
commit
b5f5fa4598
|
@ -0,0 +1,92 @@
|
|||
package evaltest
|
||||
|
||||
import "strings"
|
||||
import "testing"
|
||||
import "git.tebibyte.media/fspl/fspl/eval"
|
||||
import "git.tebibyte.media/fspl/fspl/lexer"
|
||||
import "git.tebibyte.media/fspl/fspl/entity"
|
||||
import "git.tebibyte.media/fspl/fspl/errors"
|
||||
import "git.tebibyte.media/fspl/fspl/analyzer"
|
||||
import "git.tebibyte.media/fspl/fspl/testcommon"
|
||||
import "git.tebibyte.media/fspl/fspl/parser/fspl"
|
||||
|
||||
func testString (test *testing.T, correct, input string) {
|
||||
address := entity.Address("main.fspl")
|
||||
ast := treeOf(test, address, input, false)
|
||||
|
||||
tree := analyzer.Tree { }
|
||||
err := tree.Analyze(address.UUID(), nil, ast)
|
||||
if err != nil {
|
||||
test.Fatal("analyzer returned error:\n" + errors.Format(err))
|
||||
return
|
||||
}
|
||||
|
||||
expression := getMain(test, address, tree)
|
||||
output, err := eval.EvaluateConstant(expression)
|
||||
if err != nil {
|
||||
test.Fatal("evaluator returned error:\n" + errors.Format(err))
|
||||
}
|
||||
|
||||
got := output.String()
|
||||
if got != correct {
|
||||
test.Logf("results do not match")
|
||||
testcommon.Compare(test, correct, got)
|
||||
test.Log("SOURCE FSPL CODE:")
|
||||
test.Log("\033[32m" + input + "\033[0m")
|
||||
test.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func testStringErr (
|
||||
test *testing.T,
|
||||
errFile string,
|
||||
errMessage string,
|
||||
errRow int,
|
||||
errStart int,
|
||||
input string,
|
||||
) {
|
||||
address := entity.Address("main.fspl")
|
||||
ast := treeOf(test, address, input, false)
|
||||
|
||||
tree := analyzer.Tree { }
|
||||
err := tree.Analyze(address.UUID(), nil, ast)
|
||||
if err != nil {
|
||||
test.Fatal("analyzer returned error:\n" + errors.Format(err))
|
||||
}
|
||||
|
||||
expression := getMain(test, address, tree)
|
||||
_, err = eval.EvaluateConstant(expression)
|
||||
testcommon.CompareErr(test, errFile, errMessage, errRow, errStart, err)
|
||||
}
|
||||
|
||||
func treeOf (test *testing.T, address entity.Address, input string, skim bool) fsplParser.Tree {
|
||||
ast := fsplParser.Tree { }
|
||||
lx, err := lexer.LexReader (
|
||||
string(address),
|
||||
strings.NewReader(input))
|
||||
if err != nil {
|
||||
test.Fatal("lexer returned error:\n" + errors.Format(err))
|
||||
}
|
||||
if skim {
|
||||
err = ast.Skim(lx)
|
||||
} else {
|
||||
err = ast.Parse(lx)
|
||||
}
|
||||
if err != nil {
|
||||
test.Fatal("parser returned error:\n" + errors.Format(err))
|
||||
}
|
||||
|
||||
return ast
|
||||
}
|
||||
|
||||
func getMain (test *testing.T, address entity.Address, tree analyzer.Tree) (entity.Expression) {
|
||||
mainKey := entity.Key {
|
||||
Unit: address.UUID(),
|
||||
Name: "main",
|
||||
}
|
||||
main, ok := tree.Functions[mainKey]
|
||||
if !ok {
|
||||
test.Fatal("ast does not have a main function")
|
||||
}
|
||||
return main.Body
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package evaltest
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEvalLiteralInt (test *testing.T) {
|
||||
testString(test,
|
||||
`-234`,
|
||||
`
|
||||
[main]:Int = -234
|
||||
`)}
|
||||
|
||||
func TestEvalLiteralFloat (test *testing.T) {
|
||||
testString(test,
|
||||
`854.32`,
|
||||
`
|
||||
[main]:F64 = 854.32
|
||||
`)}
|
||||
|
||||
func TestEvalLiteralString (test *testing.T) {
|
||||
testString(test,
|
||||
`'hello'`,
|
||||
`
|
||||
[main]:String = 'hello'
|
||||
`)}
|
||||
|
||||
func TestEvalArray (test *testing.T) {
|
||||
testString(test,
|
||||
`((2 3 1) (8 9 -23) (432 89 1))`,
|
||||
`
|
||||
[main]:3:3:Int = (
|
||||
( 2 3 1)
|
||||
( 8 9 -23)
|
||||
(432 89 1))
|
||||
`)}
|
||||
|
||||
func TestEvalStruct (test *testing.T) {
|
||||
testString(test,
|
||||
`(. a:489 b:5.283 c:'a string' d:(2 3 1 293) e:(. x:32 y:92) f:true g:nil)`,
|
||||
`
|
||||
T:(. a:Int b:F64 c:String d:*:Int e:(. x:Int y:Int) f:Bool g:*Int)
|
||||
[main]:T = (.
|
||||
a: 489
|
||||
b: 5.283
|
||||
c: 'a string'
|
||||
d: (2 3 1 293)
|
||||
e: (. x: 32 y: 92)
|
||||
f: true
|
||||
g: nil)
|
||||
`)}
|
||||
|
||||
func TestEvalLiteralBoolean (test *testing.T) {
|
||||
testString(test,
|
||||
`true`,
|
||||
`
|
||||
[main]:Bool = true
|
||||
`)}
|
||||
|
||||
func TestEvalLiteralNil (test *testing.T) {
|
||||
testString(test,
|
||||
`nil`,
|
||||
`
|
||||
[main]:*Int = nil
|
||||
`)}
|
||||
|
Loading…
Reference in New Issue