You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.2 KiB
Go

package ast
import (
"golox/token"
"testing"
)
func TestPrinter(t *testing.T) {
tests := []struct {
name string
expr Expr
expected string
}{
{
name: "Binary expression",
expr: &BinaryExpr{
Left: &LiteralExpr{Value: 1},
Operator: token.Token{Type: token.PLUS, Lexeme: "+"},
Right: &LiteralExpr{Value: 2},
},
expected: "(+ 1 2)",
},
{
name: "Grouping expression",
expr: &GroupingExpr{
Expression: &LiteralExpr{Value: 1},
},
expected: "(group 1)",
},
{
name: "Literal expression",
expr: &LiteralExpr{Value: 123},
expected: "123",
},
{
name: "Unary expression",
expr: &UnaryExpr{
Operator: token.Token{Type: token.MINUS, Lexeme: "-"},
Right: &LiteralExpr{Value: 123},
},
expected: "(- 123)",
},
{
name: "Nil literal expression",
expr: &LiteralExpr{Value: nil},
expected: "nil",
},
{
name: "Error expression",
expr: &ErrorExpr{Value: "error"},
expected: "error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
printer := NewPrinter()
result := printer.Print(tt.expr)
if result != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}