TestVariantPathASTShape asserts the VariantPath node is produced with the expected Root and Segments, and that the trailing :: cast wraps it.
(t *testing.T)
| 41 | // TestVariantPathASTShape asserts the VariantPath node is produced with the |
| 42 | // expected Root and Segments, and that the trailing :: cast wraps it. |
| 43 | func TestVariantPathASTShape(t *testing.T) { |
| 44 | q := `SELECT col:field.sub[0]::string FROM t` |
| 45 | tree, err := gosqlx.ParseWithDialect(q, keywords.DialectSnowflake) |
| 46 | if err != nil { |
| 47 | t.Fatalf("parse failed: %v", err) |
| 48 | } |
| 49 | var vp *ast.VariantPath |
| 50 | var cast *ast.CastExpression |
| 51 | var visit func(n ast.Node) |
| 52 | visit = func(n ast.Node) { |
| 53 | if n == nil { |
| 54 | return |
| 55 | } |
| 56 | switch x := n.(type) { |
| 57 | case *ast.VariantPath: |
| 58 | if vp == nil { |
| 59 | vp = x |
| 60 | } |
| 61 | case *ast.CastExpression: |
| 62 | if cast == nil { |
| 63 | cast = x |
| 64 | } |
| 65 | } |
| 66 | for _, c := range n.Children() { |
| 67 | visit(c) |
| 68 | } |
| 69 | } |
| 70 | for _, s := range tree.Statements { |
| 71 | visit(s) |
| 72 | } |
| 73 | if vp == nil { |
| 74 | t.Fatal("VariantPath not found") |
| 75 | } |
| 76 | if vp.Root == nil { |
| 77 | t.Fatal("VariantPath.Root nil") |
| 78 | } |
| 79 | if len(vp.Segments) != 3 { |
| 80 | t.Fatalf("Segments: want 3 (field, sub, [0]), got %d", len(vp.Segments)) |
| 81 | } |
| 82 | if vp.Segments[0].Name != "field" { |
| 83 | t.Fatalf("Segments[0].Name: want %q, got %q", "field", vp.Segments[0].Name) |
| 84 | } |
| 85 | if vp.Segments[1].Name != "sub" { |
| 86 | t.Fatalf("Segments[1].Name: want %q, got %q", "sub", vp.Segments[1].Name) |
| 87 | } |
| 88 | if vp.Segments[2].Index == nil { |
| 89 | t.Fatal("Segments[2].Index (bracket subscript) missing") |
| 90 | } |
| 91 | if cast == nil || cast.Type != "string" { |
| 92 | t.Fatalf("Cast: want CastExpression with Type=string, got %+v", cast) |
| 93 | } |
| 94 | } |
nothing calls this directly
no test coverage detected