Skip to content

Commit

Permalink
tests: zero filler
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Pana <[email protected]>
  • Loading branch information
acpana committed Jan 8, 2025
1 parent 01f3f16 commit e2efda2
Showing 1 changed file with 89 additions and 1 deletion.
90 changes: 89 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,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())
}
}

0 comments on commit e2efda2

Please sign in to comment.