105 lines
2.4 KiB
Go
105 lines
2.4 KiB
Go
package parser_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.sharkk.net/Sharkk/Mako/parser"
|
|
)
|
|
|
|
// Helper functions for testing specific node types
|
|
func testNumberLiteral(t *testing.T, expr parser.Expression, expected float64) {
|
|
t.Helper()
|
|
num, ok := expr.(*parser.NumberLiteral)
|
|
if !ok {
|
|
t.Fatalf("expected NumberLiteral, got %T", expr)
|
|
}
|
|
if num.Value != expected {
|
|
t.Errorf("expected %f, got %f", expected, num.Value)
|
|
}
|
|
}
|
|
|
|
func testStringLiteral(t *testing.T, expr parser.Expression, expected string) {
|
|
t.Helper()
|
|
str, ok := expr.(*parser.StringLiteral)
|
|
if !ok {
|
|
t.Fatalf("expected StringLiteral, got %T", expr)
|
|
}
|
|
if str.Value != expected {
|
|
t.Errorf("expected %s, got %s", expected, str.Value)
|
|
}
|
|
}
|
|
|
|
func testBooleanLiteral(t *testing.T, expr parser.Expression, expected bool) {
|
|
t.Helper()
|
|
boolean, ok := expr.(*parser.BooleanLiteral)
|
|
if !ok {
|
|
t.Fatalf("expected BooleanLiteral, got %T", expr)
|
|
}
|
|
if boolean.Value != expected {
|
|
t.Errorf("expected %t, got %t", expected, boolean.Value)
|
|
}
|
|
}
|
|
|
|
func testNilLiteral(t *testing.T, expr parser.Expression) {
|
|
t.Helper()
|
|
_, ok := expr.(*parser.NilLiteral)
|
|
if !ok {
|
|
t.Fatalf("expected NilLiteral, got %T", expr)
|
|
}
|
|
}
|
|
|
|
func testIdentifier(t *testing.T, expr parser.Expression, expected string) {
|
|
t.Helper()
|
|
ident, ok := expr.(*parser.Identifier)
|
|
if !ok {
|
|
t.Fatalf("expected Identifier, got %T", expr)
|
|
}
|
|
if ident.Value != expected {
|
|
t.Errorf("expected %s, got %s", expected, ident.Value)
|
|
}
|
|
}
|
|
|
|
func testInfixExpression(t *testing.T, expr parser.Expression, left any, operator string, right any) {
|
|
t.Helper()
|
|
infix, ok := expr.(*parser.InfixExpression)
|
|
if !ok {
|
|
t.Fatalf("expected InfixExpression, got %T", expr)
|
|
}
|
|
|
|
if infix.Operator != operator {
|
|
t.Errorf("expected operator %s, got %s", operator, infix.Operator)
|
|
}
|
|
|
|
switch leftVal := left.(type) {
|
|
case float64:
|
|
testNumberLiteral(t, infix.Left, leftVal)
|
|
case string:
|
|
testIdentifier(t, infix.Left, leftVal)
|
|
case bool:
|
|
testBooleanLiteral(t, infix.Left, leftVal)
|
|
}
|
|
|
|
switch rightVal := right.(type) {
|
|
case float64:
|
|
testNumberLiteral(t, infix.Right, rightVal)
|
|
case string:
|
|
testIdentifier(t, infix.Right, rightVal)
|
|
case bool:
|
|
testBooleanLiteral(t, infix.Right, rightVal)
|
|
}
|
|
}
|
|
|
|
func checkParserErrors(t *testing.T, p *parser.Parser) {
|
|
t.Helper()
|
|
errors := p.Errors()
|
|
if len(errors) == 0 {
|
|
return
|
|
}
|
|
|
|
t.Errorf("parser has %d errors", len(errors))
|
|
for _, err := range errors {
|
|
t.Errorf("parser error: %s", err.Error())
|
|
}
|
|
t.FailNow()
|
|
}
|