Skip to content

Commit 3dfc858

Browse files
convert: add unit test for lazy ConstGroup when all enum items are skipped
Adds TestNoEmptyConstGroupWhenAllEnumItemsSkipped to verify that no empty const() block is generated when all enum items are already registered and thus skipped in createEnumItems. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com>
1 parent 28e5668 commit 3dfc858

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

cl/internal/convert/package_bulitin_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package convert
22

33
import (
4+
"bytes"
45
"go/token"
56
"go/types"
7+
"strings"
68
"testing"
79

810
"github.com/goplus/gogen"
@@ -197,3 +199,57 @@ func TestProcessSymbol(t *testing.T) {
197199
}
198200
}
199201
}
202+
203+
func TestNoEmptyConstGroupWhenAllEnumItemsSkipped(t *testing.T) {
204+
pnc := cltest.NC(&llcppg.Config{}, nil, cltest.NewConvSym())
205+
pkg := emptyPkg(pnc)
206+
tempFile := &ncimpl.HeaderFile{
207+
File: "temp.h",
208+
FileType: llcppg.Inter,
209+
}
210+
pkg.p.SetCurFile(tempFile.ToGoFileName("testpkg"), true)
211+
212+
items := []*ast.EnumItem{
213+
{Name: &ast.Ident{Name: "Red"}, Value: &ast.BasicLit{Kind: ast.IntLit, Value: "0"}},
214+
{Name: &ast.Ident{Name: "Green"}, Value: &ast.BasicLit{Kind: ast.IntLit, Value: "1"}},
215+
}
216+
217+
// First enum: registers the items normally
218+
err := pkg.NewEnumTypeDecl("Color", &ast.EnumTypeDecl{
219+
Object: ast.Object{
220+
Loc: &ast.Location{File: "temp.h"},
221+
Name: &ast.Ident{Name: "Color"},
222+
},
223+
Type: &ast.EnumType{Items: items},
224+
}, pnc)
225+
if err != nil {
226+
t.Fatal(err)
227+
}
228+
229+
// Second enum: all items already registered, so all are skipped.
230+
// With lazy ConstGroup, no empty const() should be created.
231+
err = pkg.NewEnumTypeDecl("Color2", &ast.EnumTypeDecl{
232+
Object: ast.Object{
233+
Loc: &ast.Location{File: "temp.h"},
234+
Name: &ast.Ident{Name: "Color2"},
235+
},
236+
Type: &ast.EnumType{Items: items},
237+
}, pnc)
238+
if err != nil {
239+
t.Fatal(err)
240+
}
241+
242+
var buf bytes.Buffer
243+
err = pkg.p.WriteTo(&buf, "temp.go")
244+
if err != nil {
245+
t.Fatal(err)
246+
}
247+
output := buf.String()
248+
249+
// There should be exactly one const block from the first enum.
250+
// No empty const() from the second enum.
251+
constCount := strings.Count(output, "const (")
252+
if constCount != 1 {
253+
t.Errorf("expected exactly 1 const block, got %d.\nOutput:\n%s", constCount, output)
254+
}
255+
}

0 commit comments

Comments
 (0)