| 1 | // Copyright 2026 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package edge_test |
| 6 | |
| 7 | import ( |
| 8 | "go/ast" |
| 9 | "testing" |
| 10 | |
| 11 | "golang.org/x/tools/go/ast/edge" |
| 12 | ) |
| 13 | |
| 14 | func TestGetNilConcrete(t *testing.T) { |
| 15 | branchStmt := &ast.BranchStmt{Label: nil} |
| 16 | |
| 17 | out := edge.BranchStmt_Label.Get(branchStmt, -1) |
| 18 | if out.(*ast.Ident) != nil { |
| 19 | t.Fatal("out.(*ast.Ident) != nil") |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestGetNilIface(t *testing.T) { |
| 24 | ifStmt := &ast.IfStmt{Init: nil} |
| 25 | |
| 26 | out := edge.IfStmt_Init.Get(ifStmt, -1) // should not panic |
| 27 | if out != nil { |
| 28 | t.Fatal("out != nil") |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestGetPanics(t *testing.T) { |
| 33 | t.Run("-1 with indexable field", func(t *testing.T) { |
| 34 | defer func() { _ = recover() }() |
| 35 | blockstmt := &ast.BlockStmt{List: []ast.Stmt{&ast.IfStmt{}}} |
| 36 | edge.BlockStmt_List.Get(blockstmt, -1) // panic: slice index out of range |
| 37 | t.Fatal("Get did not panic") |
| 38 | }) |
| 39 | |
| 40 | t.Run("idx with non-indexable field", func(t *testing.T) { |
| 41 | defer func() { _ = recover() }() |
| 42 | id := &ast.IfStmt{Cond: ast.NewIdent("foo")} |
| 43 | edge.IfStmt_Cond.Get(id, 1) // panic: cannot index non-slice |
| 44 | t.Fatal("Get did not panic") |
| 45 | }) |
| 46 | } |