| 1 | package ast |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | // TestExpr_expr tests the expr method of all the types in the ast package. |
| 6 | // Note that this doesn't actually do anything and is just forcing test coverage. |
| 7 | // The expr func only exists for type safety with the Expr interface. |
| 8 | func TestExpr_expr(t *testing.T) { |
| 9 | NumberFloatExpr{}.expr() |
| 10 | NumberIntExpr{}.expr() |
| 11 | StringExpr{}.expr() |
| 12 | BoolExpr{}.expr() |
| 13 | BinaryExpr{}.expr() |
| 14 | UnaryExpr{}.expr() |
| 15 | CallExpr{}.expr() |
| 16 | ChainedExpr{}.expr() |
| 17 | SpreadExpr{}.expr() |
| 18 | RangeExpr{}.expr() |
| 19 | IndexExpr{}.expr() |
| 20 | ArrayExpr{}.expr() |
| 21 | PropertyExpr{}.expr() |
| 22 | ObjectExpr{}.expr() |
| 23 | MapExpr{}.expr() |
| 24 | EachExpr{}.expr() |
| 25 | VariableExpr{}.expr() |
| 26 | GroupExpr{}.expr() |
| 27 | ConditionalExpr{}.expr() |
| 28 | BranchExpr{}.expr() |
| 29 | FilterExpr{}.expr() |
| 30 | SearchExpr{}.expr() |
| 31 | RecursiveDescentExpr{}.expr() |
| 32 | SortByExpr{}.expr() |
| 33 | GroupByExpr{}.expr() |
| 34 | ReduceExpr{}.expr() |
| 35 | MapValuesExpr{}.expr() |
| 36 | AnyExpr{}.expr() |
| 37 | AllExpr{}.expr() |
| 38 | CountExpr{}.expr() |
| 39 | AssignExpr{}.expr() |
| 40 | NullExpr{}.expr() |
| 41 | RegexExpr{}.expr() |
| 42 | } |
| 43 | |
| 44 | func TestChainExprs(t *testing.T) { |
| 45 | // 0 args → nil. |
| 46 | if got := ChainExprs(); got != nil { |
| 47 | t.Fatalf("expected nil, got %v", got) |
| 48 | } |
| 49 | |
| 50 | // 1 arg → returns that arg unwrapped. |
| 51 | single := StringExpr{Value: "a"} |
| 52 | if got := ChainExprs(single); got != single { |
| 53 | t.Fatalf("expected single expr returned as-is, got %T", got) |
| 54 | } |
| 55 | |
| 56 | // 2+ args → ChainedExpr. |
| 57 | a := StringExpr{Value: "a"} |
| 58 | b := StringExpr{Value: "b"} |
| 59 | result := ChainExprs(a, b) |
| 60 | chained, ok := result.(ChainedExpr) |
| 61 | if !ok { |
| 62 | t.Fatalf("expected ChainedExpr, got %T", result) |
| 63 | } |
| 64 | if len(chained.Exprs) != 2 { |
| 65 | t.Fatalf("expected 2 exprs, got %d", len(chained.Exprs)) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestBranchExprs(t *testing.T) { |
| 70 | a := StringExpr{Value: "a"} |
| 71 | b := StringExpr{Value: "b"} |
| 72 | result := BranchExprs(a, b) |
| 73 | branch, ok := result.(BranchExpr) |
| 74 | if !ok { |
| 75 | t.Fatalf("expected BranchExpr, got %T", result) |
| 76 | } |
| 77 | if len(branch.Exprs) != 2 { |
| 78 | t.Fatalf("expected 2 exprs, got %d", len(branch.Exprs)) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestIsType(t *testing.T) { |
| 83 | s := StringExpr{Value: "x"} |
| 84 | if !IsType[StringExpr](s) { |
| 85 | t.Fatal("expected IsType[StringExpr] to be true") |
| 86 | } |
| 87 | if IsType[NumberIntExpr](s) { |
| 88 | t.Fatal("expected IsType[NumberIntExpr] to be false") |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestAsType(t *testing.T) { |
| 93 | s := StringExpr{Value: "x"} |
| 94 | got, ok := AsType[StringExpr](s) |
| 95 | if !ok { |
| 96 | t.Fatal("expected AsType to succeed") |
| 97 | } |
| 98 | if got.Value != "x" { |
| 99 | t.Fatalf("expected x, got %s", got.Value) |
| 100 | } |
| 101 | |
| 102 | _, ok = AsType[NumberIntExpr](s) |
| 103 | if ok { |
| 104 | t.Fatal("expected AsType to fail for wrong type") |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestLast(t *testing.T) { |
| 109 | a := StringExpr{Value: "a"} |
| 110 | b := StringExpr{Value: "b"} |
| 111 | c := StringExpr{Value: "c"} |
| 112 | |
| 113 | // Chained → last element. |
| 114 | chain := ChainedExpr{Exprs: Expressions{a, b, c}} |
| 115 | if got := Last(chain); got != c { |
| 116 | t.Fatalf("expected last to be c, got %v", got) |
| 117 | } |
| 118 | |
| 119 | // Non-chained → passthrough. |
| 120 | if got := Last(a); got != a { |
| 121 | t.Fatalf("expected passthrough, got %v", got) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestLastAsType(t *testing.T) { |
| 126 | a := NumberIntExpr{Value: 1} |
| 127 | b := StringExpr{Value: "end"} |
| 128 | chain := ChainedExpr{Exprs: Expressions{a, b}} |
| 129 | |
| 130 | got, ok := LastAsType[StringExpr](chain) |
| 131 | if !ok { |
| 132 | t.Fatal("expected LastAsType to succeed") |
| 133 | } |
| 134 | if got.Value != "end" { |
| 135 | t.Fatalf("expected end, got %s", got.Value) |
| 136 | } |
| 137 | |
| 138 | _, ok = LastAsType[NumberIntExpr](chain) |
| 139 | if ok { |
| 140 | t.Fatal("expected LastAsType to fail for wrong type") |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func TestRemoveLast(t *testing.T) { |
| 145 | a := StringExpr{Value: "a"} |
| 146 | b := StringExpr{Value: "b"} |
| 147 | c := StringExpr{Value: "c"} |
| 148 | |
| 149 | // Chain of 3 → chain of 2. |
| 150 | chain := ChainedExpr{Exprs: Expressions{a, b, c}} |
| 151 | result := RemoveLast(chain) |
| 152 | chained, ok := result.(ChainedExpr) |
| 153 | if !ok { |
| 154 | t.Fatalf("expected ChainedExpr, got %T", result) |
| 155 | } |
| 156 | if len(chained.Exprs) != 2 { |
| 157 | t.Fatalf("expected 2 exprs, got %d", len(chained.Exprs)) |
| 158 | } |
| 159 | |
| 160 | // Single expr → nil (RemoveLast on non-chained yields ChainExprs() with 0 args). |
| 161 | if got := RemoveLast(a); got != nil { |
| 162 | t.Fatalf("expected nil, got %v", got) |
| 163 | } |
| 164 | } |