Skip to content

Commit

Permalink
Improve stringify test coverage (#2320)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlewis committed Mar 25, 2022
1 parent 6cd5d41 commit 1694b79
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 16 deletions.
53 changes: 52 additions & 1 deletion github/gen-stringify-test.go
Expand Up @@ -68,6 +68,12 @@ var (
return "github.Timestamp{0001-01-01 00:00:00 +0000 UTC}"
case "nil":
return "map[]"
case `[]int{0}`:
return `[0]`
case `[]string{""}`:
return `[""]`
case "[]Scope{ScopeNone}":
return `["(no scope)"]`
}
log.Fatalf("Unhandled zero value: %q", v)
return ""
Expand Down Expand Up @@ -144,7 +150,18 @@ func (t *templateData) processAST(f *ast.File) error {
logf("Got FuncDecl: Name=%q, id.Name=%#v", fn.Name.Name, id.Name)
t.StringFuncs[id.Name] = true
} else {
logf("Ignoring FuncDecl: Name=%q, Type=%T", fn.Name.Name, fn.Recv.List[0].Type)
star, ok := fn.Recv.List[0].Type.(*ast.StarExpr)
if ok && fn.Name.Name == "String" {
id, ok := star.X.(*ast.Ident)
if ok {
logf("Got FuncDecl: Name=%q, id.Name=%#v", fn.Name.Name, id.Name)
t.StringFuncs[id.Name] = true
} else {
logf("Ignoring FuncDecl: Name=%q, Type=%T", fn.Name.Name, fn.Recv.List[0].Type)
}
} else {
logf("Ignoring FuncDecl: Name=%q, Type=%T", fn.Name.Name, fn.Recv.List[0].Type)
}
}
} else {
logf("Ignoring FuncDecl: Name=%q, fn=%#v", fn.Name.Name, fn)
Expand All @@ -157,6 +174,7 @@ func (t *templateData) processAST(f *ast.File) error {
logf("Ignoring AST decl type %T", decl)
continue
}

for _, spec := range gd.Specs {
ts, ok := spec.(*ast.TypeSpec)
if !ok {
Expand Down Expand Up @@ -188,6 +206,13 @@ func (t *templateData) processAST(f *ast.File) error {
continue
}

if at, ok := field.Type.(*ast.ArrayType); ok {
if id, ok := at.Elt.(*ast.Ident); ok {
t.addIdentSlice(id, ts.Name.String(), fieldName.String())
continue
}
}

se, ok := field.Type.(*ast.StarExpr)
if !ok {
logf("Ignoring type %T for Name=%q, FieldName=%q", field.Type, ts.Name.String(), fieldName.String())
Expand Down Expand Up @@ -272,6 +297,32 @@ func (t *templateData) addIdentPtr(x *ast.Ident, receiverType, fieldName string)
t.StructFields[receiverType] = append(t.StructFields[receiverType], newStructField(receiverType, fieldName, x.String(), zeroValue, namedStruct))
}

func (t *templateData) addIdentSlice(x *ast.Ident, receiverType, fieldName string) {
var zeroValue string
var namedStruct = false
switch x.String() {
case "int":
zeroValue = "[]int{0}"
case "int64":
zeroValue = "[]int64{0}"
case "float64":
zeroValue = "[]float64{0}"
case "string":
zeroValue = `[]string{""}`
case "bool":
zeroValue = "[]bool{false}"
case "Scope":
zeroValue = "[]Scope{ScopeNone}"
// case "Timestamp":
// zeroValue = "&Timestamp{}"
default:
zeroValue = "nil"
namedStruct = true
}

t.StructFields[receiverType] = append(t.StructFields[receiverType], newStructField(receiverType, fieldName, x.String(), zeroValue, namedStruct))
}

func (t *templateData) dump() error {
if len(t.StructFields) == 0 {
logf("No StructFields for %v; skipping.", t.filename)
Expand Down
96 changes: 81 additions & 15 deletions github/github-stringify_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1694b79

Please sign in to comment.