Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: zero filler #3465

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 88 additions & 1 deletion pkg/test/fuzz/krmgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
"testing"
)

type Filler interface {
// Fill populates the fields of a struct with values according to the Filler's rules.
Fill(t *testing.T, obj interface{})
}

type OverrideFiller func(t *testing.T, fieldName string, field reflect.Value)
type RandomFiller struct {
randStream *rand.Rand
Expand All @@ -43,7 +48,6 @@ func NewRandomFiller(fc *FillerConfig) *RandomFiller {
// two maps passed to the RandomFiller.
func (rf *RandomFiller) Fill(t *testing.T, obj interface{}) {
rf.fillWithRandom(t, "", reflect.ValueOf(obj).Elem())
rf.fillWithRandom(t, "", reflect.ValueOf(obj).Elem())
}

func (rf *RandomFiller) fillWithRandom(t *testing.T, fieldName string, field reflect.Value) {
Expand Down Expand Up @@ -112,3 +116,86 @@ func (rf *RandomFiller) fillWithRandom(t *testing.T, fieldName string, field ref
t.Fatalf("Unhandled field kind: %v", field.Kind())
}
}

type ZeroFiller struct {
fieldOverrides map[string]OverrideFiller
}

func NewZeroFiller(fc *FillerConfig) *ZeroFiller {
return &ZeroFiller{
fieldOverrides: fc.FieldOverrides,
}
}

func (rf *ZeroFiller) Fill(t *testing.T, obj interface{}) {
rf.fillWithZero(t, "", reflect.ValueOf(obj).Elem())
}

func (rf *ZeroFiller) fillWithZero(t *testing.T, fieldName string, field reflect.Value) {
if field.Kind() == reflect.Ptr {
if field.IsNil() {
field.Set(reflect.New(field.Type().Elem()))
}
rf.fillWithZero(t, fieldName, field.Elem())
return
}

if rf.fieldOverrides != nil {
if override, ok := rf.fieldOverrides[fieldName]; ok {
override(t, fieldName, field)
return
}
}

switch field.Kind() {
case reflect.Bool:
field.SetBool(false)

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetInt(0)

case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
field.SetUint(0)

case reflect.Float32, reflect.Float64:
field.SetFloat(0.0)

case reflect.String:
field.SetString("")

case reflect.Slice: // undefined for now

// count := rf.randStream.Intn(10) + 1
// slice := reflect.MakeSlice(field.Type(), count, count)
// for j := 0; j < count; j++ {
// element := reflect.New(field.Type().Elem()).Elem()
// rf.fillWithZero(t, "", element) // don't need to pass in a field name for slice elements
// slice.Index(j).Set(element)
// }
// field.Set(slice)

case reflect.Map: // undefined for now

// count := rf.randStream.Intn(10) + 1
// mapType := reflect.MakeMap(field.Type())
// for j := 0; j < count; j++ {
// key := reflect.New(field.Type().Key()).Elem()
// value := reflect.New(field.Type().Elem()).Elem()
// rf.fillWithZero(t, "", key) // no need to pass in a field name for keys
// rf.fillWithZero(t, "", value) // no need to pass in a field name for values
// mapType.SetMapIndex(key, value)
// }
// field.Set(mapType)

case reflect.Struct:
for i := 0; i < field.NumField(); i++ {
structFieldName := field.Type().Field(i).Name
nestedStructFieldname := fieldName + "." + structFieldName

rf.fillWithZero(t, nestedStructFieldname, field.Field(i))
}

default:
t.Fatalf("Unhandled field kind: %v", field.Kind())
}
}
Loading