Skip to content

Commit 845eb48

Browse files
convert: add AST-level check for empty const GenDecl in unit test
Enhance TestNoEmptyConstGroupWhenAllEnumItemsSkipped to also verify at the Go AST level that no const GenDecl with empty Specs exists in the package's corresponding ast.File, covering the behavior that lazy ConstGroup initialization avoids empty const nodes in the AST. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com>
1 parent 3dfc858 commit 845eb48

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

cl/internal/convert/package_bulitin_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package convert
22

33
import (
44
"bytes"
5+
goast "go/ast"
56
"go/token"
67
"go/types"
78
"strings"
@@ -252,4 +253,25 @@ func TestNoEmptyConstGroupWhenAllEnumItemsSkipped(t *testing.T) {
252253
if constCount != 1 {
253254
t.Errorf("expected exactly 1 const block, got %d.\nOutput:\n%s", constCount, output)
254255
}
256+
257+
// Also verify at the AST level: no const GenDecl with empty Specs
258+
// should exist in the package's corresponding ast.File.
259+
goFile := pkg.p.ASTFile("temp.go")
260+
if goFile == nil {
261+
t.Fatal("expected ast.File for temp.go, got nil")
262+
}
263+
constDeclCount := 0
264+
for _, decl := range goFile.Decls {
265+
genDecl, ok := decl.(*goast.GenDecl)
266+
if !ok || genDecl.Tok != token.CONST {
267+
continue
268+
}
269+
constDeclCount++
270+
if len(genDecl.Specs) == 0 {
271+
t.Error("found empty const declaration in AST (no Specs)")
272+
}
273+
}
274+
if constDeclCount != 1 {
275+
t.Errorf("expected exactly 1 const GenDecl in AST, got %d", constDeclCount)
276+
}
255277
}

0 commit comments

Comments
 (0)