Skip to content

Commit

Permalink
Merge pull request #345 from dmksnnk/fix/fakeable-slice
Browse files Browse the repository at this point in the history
add support for fakeable array
  • Loading branch information
brianvoe committed Feb 9, 2024
2 parents 3ea2e1a + fef693b commit b480006
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
3 changes: 2 additions & 1 deletion fakeable.go
Expand Up @@ -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)
}
Expand Down
37 changes: 37 additions & 0 deletions fakeable_external_test.go
Expand Up @@ -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
Expand Down Expand Up @@ -150,6 +156,7 @@ type NestedCustom struct {
Timestamp CustomTime
PtrTimestamp *CustomTime
SliceStr CustomSlice
Array CustomArray
MapStr CustomMap
Struct CustomStruct
PtrStruct *CustomStruct
Expand All @@ -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"`
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion struct.go
Expand Up @@ -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
}
Expand Down

0 comments on commit b480006

Please sign in to comment.