TestParser_CastExpression_MultipleCasts tests multiple CAST expressions
(t *testing.T)
| 151 | |
| 152 | // TestParser_CastExpression_MultipleCasts tests multiple CAST expressions |
| 153 | func TestParser_CastExpression_MultipleCasts(t *testing.T) { |
| 154 | // SELECT CAST(id AS VARCHAR), CAST(price AS DECIMAL) FROM products |
| 155 | tokens := []token.Token{ |
| 156 | {Type: models.TokenTypeSelect, Literal: "SELECT"}, |
| 157 | {Type: models.TokenTypeCast, Literal: "CAST"}, |
| 158 | {Type: models.TokenTypeLParen, Literal: "("}, |
| 159 | {Type: models.TokenTypeIdentifier, Literal: "id"}, |
| 160 | {Type: models.TokenTypeAs, Literal: "AS"}, |
| 161 | {Type: models.TokenTypeIdentifier, Literal: "VARCHAR"}, |
| 162 | {Type: models.TokenTypeRParen, Literal: ")"}, |
| 163 | {Type: models.TokenTypeComma, Literal: ","}, |
| 164 | {Type: models.TokenTypeCast, Literal: "CAST"}, |
| 165 | {Type: models.TokenTypeLParen, Literal: "("}, |
| 166 | {Type: models.TokenTypeIdentifier, Literal: "price"}, |
| 167 | {Type: models.TokenTypeAs, Literal: "AS"}, |
| 168 | {Type: models.TokenTypeIdentifier, Literal: "DECIMAL"}, |
| 169 | {Type: models.TokenTypeRParen, Literal: ")"}, |
| 170 | {Type: models.TokenTypeFrom, Literal: "FROM"}, |
| 171 | {Type: models.TokenTypeIdentifier, Literal: "products"}, |
| 172 | } |
| 173 | |
| 174 | parser := NewParser() |
| 175 | defer parser.Release() |
| 176 | |
| 177 | tree, err := parser.Parse(tokens) |
| 178 | if err != nil { |
| 179 | t.Fatalf("unexpected error: %v", err) |
| 180 | } |
| 181 | defer ast.ReleaseAST(tree) |
| 182 | |
| 183 | stmt := tree.Statements[0].(*ast.SelectStatement) |
| 184 | |
| 185 | if len(stmt.Columns) != 2 { |
| 186 | t.Fatalf("expected 2 columns, got %d", len(stmt.Columns)) |
| 187 | } |
| 188 | |
| 189 | // Check first CAST |
| 190 | cast1, ok := stmt.Columns[0].(*ast.CastExpression) |
| 191 | if !ok { |
| 192 | t.Fatalf("expected first column to be CastExpression, got %T", stmt.Columns[0]) |
| 193 | } |
| 194 | if cast1.Type != "VARCHAR" { |
| 195 | t.Errorf("expected first CAST type VARCHAR, got %s", cast1.Type) |
| 196 | } |
| 197 | |
| 198 | // Check second CAST |
| 199 | cast2, ok := stmt.Columns[1].(*ast.CastExpression) |
| 200 | if !ok { |
| 201 | t.Fatalf("expected second column to be CastExpression, got %T", stmt.Columns[1]) |
| 202 | } |
| 203 | if cast2.Type != "DECIMAL" { |
| 204 | t.Errorf("expected second CAST type DECIMAL, got %s", cast2.Type) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // TestParser_CastExpression_WithArithmetic tests CAST with arithmetic expression |
| 209 | func TestParser_CastExpression_WithArithmetic(t *testing.T) { |