TestTryCastASTShape verifies that a TRY_CAST expression has Try=true and TokenLiteral() returns "TRY_CAST", while a plain CAST returns "CAST".
(t *testing.T)
| 52 | // TestTryCastASTShape verifies that a TRY_CAST expression has Try=true and |
| 53 | // TokenLiteral() returns "TRY_CAST", while a plain CAST returns "CAST". |
| 54 | func TestTryCastASTShape(t *testing.T) { |
| 55 | tcs := map[string]struct { |
| 56 | query string |
| 57 | wantTry bool |
| 58 | wantLit string |
| 59 | }{ |
| 60 | "try_cast": {`SELECT TRY_CAST(value AS INT) FROM events`, true, "TRY_CAST"}, |
| 61 | "cast": {`SELECT CAST(value AS INT) FROM events`, false, "CAST"}, |
| 62 | } |
| 63 | for name, tc := range tcs { |
| 64 | tc := tc |
| 65 | t.Run(name, func(t *testing.T) { |
| 66 | tree, err := gosqlx.ParseWithDialect(tc.query, keywords.DialectSnowflake) |
| 67 | if err != nil { |
| 68 | t.Fatalf("parse failed: %v", err) |
| 69 | } |
| 70 | var found bool |
| 71 | var visit func(n ast.Node) |
| 72 | visit = func(n ast.Node) { |
| 73 | if n == nil || found { |
| 74 | return |
| 75 | } |
| 76 | if c, ok := n.(*ast.CastExpression); ok { |
| 77 | if c.Try != tc.wantTry { |
| 78 | t.Fatalf("Try: want %v, got %v", tc.wantTry, c.Try) |
| 79 | } |
| 80 | if c.TokenLiteral() != tc.wantLit { |
| 81 | t.Fatalf("TokenLiteral: want %q, got %q", tc.wantLit, c.TokenLiteral()) |
| 82 | } |
| 83 | found = true |
| 84 | return |
| 85 | } |
| 86 | for _, ch := range n.Children() { |
| 87 | visit(ch) |
| 88 | } |
| 89 | } |
| 90 | for _, stmt := range tree.Statements { |
| 91 | visit(stmt) |
| 92 | } |
| 93 | if !found { |
| 94 | t.Fatal("CastExpression not found in AST") |
| 95 | } |
| 96 | }) |
| 97 | } |
| 98 | } |
nothing calls this directly
no test coverage detected