Skip to content

Commit f4f4f4a

Browse files
committed
Add tests for bitfield enums and cleanup error printing
1 parent f13df70 commit f4f4f4a

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

generator/generator.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func (g *Generator) Generate(f *ast.File) ([]byte, error) {
189189
// Parse the enum doc statement
190190
enum, pErr := g.parseEnum(ts)
191191
if pErr != nil {
192+
fmt.Println(pErr)
192193
continue
193194
}
194195

@@ -358,15 +359,13 @@ func (g *Generator) parseEnum(ts *ast.TypeSpec) (*Enum, error) {
358359
newData, err := strconv.ParseUint(dataVal, 0, 64)
359360
if err != nil {
360361
err = fmt.Errorf("failed parsing the data part of enum value '%s': %w", value, err)
361-
fmt.Println(err)
362362
return nil, err
363363
}
364364
data = newData
365365
} else {
366366
newData, err := strconv.ParseInt(dataVal, 0, 64)
367367
if err != nil {
368368
err = fmt.Errorf("failed parsing the data part of enum value '%s': %w", value, err)
369-
fmt.Println(err)
370369
return nil, err
371370
}
372371
data = newData

generator/generator_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,52 @@ func TestQuotedStrings(t *testing.T) {
469469
}
470470
}
471471

472+
func TestBitFieldOfStringFailure(t *testing.T) {
473+
input := `package test
474+
// This is a pre-enum comment that needs (to be handled properly)
475+
// ENUM(
476+
// abc
477+
// ghi
478+
//). This is an extra string comment (With parentheses of it's own)
479+
// And (another line) with Parentheses
480+
type Animal string
481+
`
482+
g := NewGenerator(WithBitfield())
483+
f, err := parser.ParseFile(g.fileSet, "TestRequiredErrors", input, parser.ParseComments)
484+
assert.Nil(t, err, "Error parsing input")
485+
486+
enums := g.inspect(f)
487+
output, err := g.parseEnum(enums["Animal"])
488+
assert.ErrorContains(t, err, "bitfield option is not allowed on string types")
489+
assert.Empty(t, output)
490+
if false { // Debugging statement
491+
fmt.Println(output)
492+
}
493+
}
494+
495+
func TestBitFieldManuallyValueFailure(t *testing.T) {
496+
input := `package test
497+
// This is a pre-enum comment that needs (to be handled properly)
498+
// ENUM(
499+
// abc = 1
500+
// ghi
501+
//). This is an extra string comment (With parentheses of it's own)
502+
// And (another line) with Parentheses
503+
type Animal int
504+
`
505+
g := NewGenerator(WithBitfield())
506+
f, err := parser.ParseFile(g.fileSet, "TestRequiredErrors", input, parser.ParseComments)
507+
assert.Nil(t, err, "Error parsing input")
508+
509+
enums := g.inspect(f)
510+
output, err := g.parseEnum(enums["Animal"])
511+
assert.ErrorContains(t, err, "manually setting values is not allowed with the bitfield option")
512+
assert.Empty(t, output)
513+
if false { // Debugging statement
514+
fmt.Println(output)
515+
}
516+
}
517+
472518
func TestStringWithSingleDoubleQuoteValue(t *testing.T) {
473519
input := `package test
474520
// ENUM(DoubleQuote='"')

generator/options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,10 @@ func WithNoParse() Option {
213213
g.NoParse = true
214214
}
215215
}
216+
217+
// WithBitfield is used to create bitfield enums.
218+
func WithBitfield() Option {
219+
return func(g *GeneratorConfig) {
220+
g.BitField = true
221+
}
222+
}

0 commit comments

Comments
 (0)