diff --git a/fakeable.go b/fakeable.go index 1089fc38..1ed5b6fe 100644 --- a/fakeable.go +++ b/fakeable.go @@ -62,12 +62,13 @@ func callFake(faker *Faker, v reflect.Value, possibleKinds ...reflect.Kind) (any return float32(reflect.ValueOf(fakedValue).Float()), nil case reflect.Float64: return float64(reflect.ValueOf(fakedValue).Float()), nil - case reflect.Slice: + case reflect.Slice, reflect.Array: return reflect.ValueOf(fakedValue).Interface(), nil case reflect.Map: return reflect.ValueOf(fakedValue).Interface(), nil case reflect.Struct: return reflect.ValueOf(fakedValue).Interface(), nil + default: return nil, fmt.Errorf("unsupported type %q", k) } diff --git a/fakeable_external_test.go b/fakeable_external_test.go index fd736de1..13efa17a 100644 --- a/fakeable_external_test.go +++ b/fakeable_external_test.go @@ -119,6 +119,12 @@ func (c CustomMap) Fake(faker *gofakeit.Faker) (any, error) { return CustomMap(map[string]string{"hello": "1", "test": "2"}), nil } +type CustomArray [2]int + +func (c CustomArray) Fake(faker *gofakeit.Faker) (any, error) { + return CustomArray([2]int{1, 2}), nil +} + type CustomStruct struct { Str string Int int @@ -150,6 +156,7 @@ type NestedCustom struct { Timestamp CustomTime PtrTimestamp *CustomTime SliceStr CustomSlice + Array CustomArray MapStr CustomMap Struct CustomStruct PtrStruct *CustomStruct @@ -174,6 +181,7 @@ type NestedOverrideCustom struct { Timestamp CustomTime `fake:"{raw_test_date}"` PtrTimestamp *CustomTime `fake:"{raw_test_date}"` SliceStr CustomSlice `fake:"{word}"` + Array CustomArray `fake:"{number:100,1000}"` MapStr CustomMap `fakesize:"2"` } @@ -423,6 +431,21 @@ func TestCustomMap(t *testing.T) { } } +func TestCustomArray(t *testing.T) { + var d CustomArray + err := gofakeit.Struct(&d) + if err != nil { + t.Fatal(err) + } + + expected := [2]int{1, 2} + for i, v := range expected { + if d[i] != v { + t.Errorf("expected item %d of the array to be: %v, got %v", i, expected[i], d[i]) + } + } +} + func TestCustomStruct(t *testing.T) { var d CustomStruct err := gofakeit.Struct(&d) @@ -510,6 +533,13 @@ func TestNestedCustom(t *testing.T) { } } + expectedArray := [2]int{1, 2} + for i, v := range expectedArray { + if d.Array[i] != v { + t.Errorf("expected item %d of the slice to be: %v, got %v", i, expectedArray[i], d.Array[i]) + } + } + expectedMap := map[string]string{"hello": "1", "test": "2"} if len(d.MapStr) != len(expectedMap) { t.Fatalf("expected %v, got %v", expectedMap, d) @@ -644,6 +674,13 @@ func TestNestedOverrideCustom(t *testing.T) { } } + nonOverrideArray := [2]int{1, 2} + for i, v := range nonOverrideArray { + if d.Array[i] == v { + t.Errorf("Array: Got non-overriden item %d in the array", i) + } + } + nonOverrideMap := map[string]string{"hello": "1", "test": "2"} if len(d.MapStr) == len(nonOverrideMap) { t.Logf("Map: Got the same length as the non-overriden map: %v vs %v", nonOverrideMap, d.MapStr) diff --git a/struct.go b/struct.go index c587ca17..a01728fc 100644 --- a/struct.go +++ b/struct.go @@ -254,7 +254,7 @@ func rSlice(f *Faker, t reflect.Type, v reflect.Value, tag string, size int) err return nil } } else if isFakeable(t) { - value, err := callFake(f, v, reflect.Slice) + value, err := callFake(f, v, reflect.Slice, reflect.Array) if err != nil { return err }