Skip to content

Commit b480006

Browse files
authored
Merge pull request #345 from dmksnnk/fix/fakeable-slice
add support for fakeable array
2 parents 3ea2e1a + fef693b commit b480006

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

fakeable.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ func callFake(faker *Faker, v reflect.Value, possibleKinds ...reflect.Kind) (any
6262
return float32(reflect.ValueOf(fakedValue).Float()), nil
6363
case reflect.Float64:
6464
return float64(reflect.ValueOf(fakedValue).Float()), nil
65-
case reflect.Slice:
65+
case reflect.Slice, reflect.Array:
6666
return reflect.ValueOf(fakedValue).Interface(), nil
6767
case reflect.Map:
6868
return reflect.ValueOf(fakedValue).Interface(), nil
6969
case reflect.Struct:
7070
return reflect.ValueOf(fakedValue).Interface(), nil
71+
7172
default:
7273
return nil, fmt.Errorf("unsupported type %q", k)
7374
}

fakeable_external_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ func (c CustomMap) Fake(faker *gofakeit.Faker) (any, error) {
119119
return CustomMap(map[string]string{"hello": "1", "test": "2"}), nil
120120
}
121121

122+
type CustomArray [2]int
123+
124+
func (c CustomArray) Fake(faker *gofakeit.Faker) (any, error) {
125+
return CustomArray([2]int{1, 2}), nil
126+
}
127+
122128
type CustomStruct struct {
123129
Str string
124130
Int int
@@ -150,6 +156,7 @@ type NestedCustom struct {
150156
Timestamp CustomTime
151157
PtrTimestamp *CustomTime
152158
SliceStr CustomSlice
159+
Array CustomArray
153160
MapStr CustomMap
154161
Struct CustomStruct
155162
PtrStruct *CustomStruct
@@ -174,6 +181,7 @@ type NestedOverrideCustom struct {
174181
Timestamp CustomTime `fake:"{raw_test_date}"`
175182
PtrTimestamp *CustomTime `fake:"{raw_test_date}"`
176183
SliceStr CustomSlice `fake:"{word}"`
184+
Array CustomArray `fake:"{number:100,1000}"`
177185
MapStr CustomMap `fakesize:"2"`
178186
}
179187

@@ -423,6 +431,21 @@ func TestCustomMap(t *testing.T) {
423431
}
424432
}
425433

434+
func TestCustomArray(t *testing.T) {
435+
var d CustomArray
436+
err := gofakeit.Struct(&d)
437+
if err != nil {
438+
t.Fatal(err)
439+
}
440+
441+
expected := [2]int{1, 2}
442+
for i, v := range expected {
443+
if d[i] != v {
444+
t.Errorf("expected item %d of the array to be: %v, got %v", i, expected[i], d[i])
445+
}
446+
}
447+
}
448+
426449
func TestCustomStruct(t *testing.T) {
427450
var d CustomStruct
428451
err := gofakeit.Struct(&d)
@@ -510,6 +533,13 @@ func TestNestedCustom(t *testing.T) {
510533
}
511534
}
512535

536+
expectedArray := [2]int{1, 2}
537+
for i, v := range expectedArray {
538+
if d.Array[i] != v {
539+
t.Errorf("expected item %d of the slice to be: %v, got %v", i, expectedArray[i], d.Array[i])
540+
}
541+
}
542+
513543
expectedMap := map[string]string{"hello": "1", "test": "2"}
514544
if len(d.MapStr) != len(expectedMap) {
515545
t.Fatalf("expected %v, got %v", expectedMap, d)
@@ -644,6 +674,13 @@ func TestNestedOverrideCustom(t *testing.T) {
644674
}
645675
}
646676

677+
nonOverrideArray := [2]int{1, 2}
678+
for i, v := range nonOverrideArray {
679+
if d.Array[i] == v {
680+
t.Errorf("Array: Got non-overriden item %d in the array", i)
681+
}
682+
}
683+
647684
nonOverrideMap := map[string]string{"hello": "1", "test": "2"}
648685
if len(d.MapStr) == len(nonOverrideMap) {
649686
t.Logf("Map: Got the same length as the non-overriden map: %v vs %v", nonOverrideMap, d.MapStr)

struct.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func rSlice(f *Faker, t reflect.Type, v reflect.Value, tag string, size int) err
254254
return nil
255255
}
256256
} else if isFakeable(t) {
257-
value, err := callFake(f, v, reflect.Slice)
257+
value, err := callFake(f, v, reflect.Slice, reflect.Array)
258258
if err != nil {
259259
return err
260260
}

0 commit comments

Comments
 (0)