blob: 2af413ceb097ab0d261dc930897566356cd10250 [file] [log] [blame]
package ast
import (
"matheval/token"
"testing"
)
// Compile-time check: both types satisfy Node.
var (
_ Node = (*NumberLit)(nil)
_ Node = (*BinaryExpr)(nil)
)
func TestNumberLit(t *testing.T) {
n := &NumberLit{Value: 3.14}
if n.Value != 3.14 {
t.Fatalf("expected 3.14, got %f", n.Value)
}
}
func TestBinaryExpr(t *testing.T) {
left := &NumberLit{Value: 1}
right := &NumberLit{Value: 2}
expr := &BinaryExpr{
Op: token.Plus,
Left: left,
Right: right,
}
if expr.Op != token.Plus {
t.Fatalf("expected Plus, got %v", expr.Op)
}
if expr.Left != left {
t.Fatal("Left child mismatch")
}
if expr.Right != right {
t.Fatal("Right child mismatch")
}
}
func TestNestedBinaryExpr(t *testing.T) {
// Represents: (1 + 2) * 3
tree := &BinaryExpr{
Op: token.Star,
Left: &BinaryExpr{
Op: token.Plus,
Left: &NumberLit{Value: 1},
Right: &NumberLit{Value: 2},
},
Right: &NumberLit{Value: 3},
}
if tree.Op != token.Star {
t.Fatalf("expected Star, got %v", tree.Op)
}
inner, ok := tree.Left.(*BinaryExpr)
if !ok {
t.Fatal("Left should be *BinaryExpr")
}
if inner.Op != token.Plus {
t.Fatalf("expected Plus, got %v", inner.Op)
}
}