From e2efda233e75e954ef9527e4cc0167fbd2be8288 Mon Sep 17 00:00:00 2001 From: Alex Pana <8968914+acpana@users.noreply.github.com> Date: Wed, 8 Jan 2025 20:15:24 +0000 Subject: [PATCH] tests: zero filler Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com> --- pkg/test/fuzz/krmgen.go | 90 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/pkg/test/fuzz/krmgen.go b/pkg/test/fuzz/krmgen.go index e12c49413f..0b448cf148 100644 --- a/pkg/test/fuzz/krmgen.go +++ b/pkg/test/fuzz/krmgen.go @@ -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 @@ -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) { @@ -112,3 +116,87 @@ 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()) + } +}