MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / TestParser_CastExpression_Simple

Function TestParser_CastExpression_Simple

pkg/sql/parser/cast_test.go:29–78  ·  view source on GitHub ↗

TestParser_CastExpression_Simple tests basic CAST expression parsing

(t *testing.T)

Source from the content-addressed store, hash-verified

27
28// TestParser_CastExpression_Simple tests basic CAST expression parsing
29func TestParser_CastExpression_Simple(t *testing.T) {
30 // SELECT CAST(id AS VARCHAR) FROM users
31 tokens := []token.Token{
32 {Type: models.TokenTypeSelect, Literal: "SELECT"},
33 {Type: models.TokenTypeCast, Literal: "CAST"},
34 {Type: models.TokenTypeLParen, Literal: "("},
35 {Type: models.TokenTypeIdentifier, Literal: "id"},
36 {Type: models.TokenTypeAs, Literal: "AS"},
37 {Type: models.TokenTypeIdentifier, Literal: "VARCHAR"},
38 {Type: models.TokenTypeRParen, Literal: ")"},
39 {Type: models.TokenTypeFrom, Literal: "FROM"},
40 {Type: models.TokenTypeIdentifier, Literal: "users"},
41 }
42
43 parser := NewParser()
44 defer parser.Release()
45
46 tree, err := parser.Parse(tokens)
47 if err != nil {
48 t.Fatalf("unexpected error: %v", err)
49 }
50 defer ast.ReleaseAST(tree)
51
52 if len(tree.Statements) != 1 {
53 t.Fatalf("expected 1 statement, got %d", len(tree.Statements))
54 }
55
56 stmt, ok := tree.Statements[0].(*ast.SelectStatement)
57 if !ok {
58 t.Fatalf("expected SelectStatement, got %T", tree.Statements[0])
59 }
60
61 if len(stmt.Columns) != 1 {
62 t.Fatalf("expected 1 column, got %d", len(stmt.Columns))
63 }
64
65 castExpr, ok := stmt.Columns[0].(*ast.CastExpression)
66 if !ok {
67 t.Fatalf("expected CastExpression, got %T", stmt.Columns[0])
68 }
69
70 if castExpr.Type != "VARCHAR" {
71 t.Errorf("expected type VARCHAR, got %s", castExpr.Type)
72 }
73
74 ident, ok := castExpr.Expr.(*ast.Identifier)
75 if !ok || ident.Name != "id" {
76 t.Error("expected Expr to be identifier 'id'")
77 }
78}
79
80// TestParser_CastExpression_WithPrecision tests CAST with type precision
81func TestParser_CastExpression_WithPrecision(t *testing.T) {

Callers

nothing calls this directly

Calls 7

ReleaseMethod · 0.95
ParseMethod · 0.95
ReleaseASTFunction · 0.92
NewParserFunction · 0.70
FatalfMethod · 0.65
ErrorfMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected