blob: 2af413ceb097ab0d261dc930897566356cd10250 [file] [log] [blame]
Sketch🕴️ad743922026-02-28 19:11:41 +04001package ast
2
3import (
4 "matheval/token"
5 "testing"
6)
7
8// Compile-time check: both types satisfy Node.
9var (
10 _ Node = (*NumberLit)(nil)
11 _ Node = (*BinaryExpr)(nil)
12)
13
14func TestNumberLit(t *testing.T) {
15 n := &NumberLit{Value: 3.14}
16 if n.Value != 3.14 {
17 t.Fatalf("expected 3.14, got %f", n.Value)
18 }
19}
20
21func TestBinaryExpr(t *testing.T) {
22 left := &NumberLit{Value: 1}
23 right := &NumberLit{Value: 2}
24 expr := &BinaryExpr{
25 Op: token.Plus,
26 Left: left,
27 Right: right,
28 }
29
30 if expr.Op != token.Plus {
31 t.Fatalf("expected Plus, got %v", expr.Op)
32 }
33 if expr.Left != left {
34 t.Fatal("Left child mismatch")
35 }
36 if expr.Right != right {
37 t.Fatal("Right child mismatch")
38 }
39}
40
41func TestNestedBinaryExpr(t *testing.T) {
42 // Represents: (1 + 2) * 3
43 tree := &BinaryExpr{
44 Op: token.Star,
45 Left: &BinaryExpr{
46 Op: token.Plus,
47 Left: &NumberLit{Value: 1},
48 Right: &NumberLit{Value: 2},
49 },
50 Right: &NumberLit{Value: 3},
51 }
52
53 if tree.Op != token.Star {
54 t.Fatalf("expected Star, got %v", tree.Op)
55 }
56
57 inner, ok := tree.Left.(*BinaryExpr)
58 if !ok {
59 t.Fatal("Left should be *BinaryExpr")
60 }
61 if inner.Op != token.Plus {
62 t.Fatalf("expected Plus, got %v", inner.Op)
63 }
64}