Skip to content

Commit

Permalink
fakeable - updated to require an error as second return value
Browse files Browse the repository at this point in the history
brianvoe committed Nov 26, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 11aa1ba commit 5f00c22
Showing 3 changed files with 51 additions and 48 deletions.
7 changes: 5 additions & 2 deletions fakeable.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import (
// Fakeable is an interface that can be implemented by a type to provide a custom fake value.
type Fakeable interface {
// Fake returns a fake value for the type.
Fake(faker *Faker) any
Fake(faker *Faker) (any, error)
}

func isFakeable(t reflect.Type) bool {
@@ -24,7 +24,10 @@ func callFake(faker *Faker, v reflect.Value, possibleKinds ...reflect.Kind) (any
return nil, errors.New("not a Fakeable type")
}

fakedValue := f.Fake(faker)
fakedValue, err := f.Fake(faker)
if err != nil {
return nil, fmt.Errorf("error calling Fake: %w", err)
}
k := reflect.TypeOf(fakedValue).Kind()
if !containsKind(possibleKinds, k) {
return nil, fmt.Errorf("returned value kind %q is not amongst the valid ones: %v", k, possibleKinds)
76 changes: 38 additions & 38 deletions fakeable_external_test.go
Original file line number Diff line number Diff line change
@@ -15,92 +15,92 @@ var (

type CustomString string

func (c CustomString) Fake(faker *gofakeit.Faker) any {
return CustomString("hello test")
func (c CustomString) Fake(faker *gofakeit.Faker) (any, error) {
return CustomString("hello test"), nil
}

type CustomBool bool

func (c CustomBool) Fake(faker *gofakeit.Faker) any {
return CustomBool(true)
func (c CustomBool) Fake(faker *gofakeit.Faker) (any, error) {
return CustomBool(true), nil
}

type CustomInt int

func (c CustomInt) Fake(faker *gofakeit.Faker) any {
return CustomInt(-42)
func (c CustomInt) Fake(faker *gofakeit.Faker) (any, error) {
return CustomInt(-42), nil
}

type CustomInt8 int8

func (c CustomInt8) Fake(faker *gofakeit.Faker) any {
return CustomInt8(-42)
func (c CustomInt8) Fake(faker *gofakeit.Faker) (any, error) {
return CustomInt8(-42), nil
}

type CustomInt16 int16

func (c CustomInt16) Fake(faker *gofakeit.Faker) any {
return CustomInt16(-42)
func (c CustomInt16) Fake(faker *gofakeit.Faker) (any, error) {
return CustomInt16(-42), nil
}

type CustomInt32 int32

func (c CustomInt32) Fake(faker *gofakeit.Faker) any {
return CustomInt32(-42)
func (c CustomInt32) Fake(faker *gofakeit.Faker) (any, error) {
return CustomInt32(-42), nil
}

type CustomInt64 int64

func (c CustomInt64) Fake(faker *gofakeit.Faker) any {
return CustomInt64(-42)
func (c CustomInt64) Fake(faker *gofakeit.Faker) (any, error) {
return CustomInt64(-42), nil
}

type CustomUint uint

func (c CustomUint) Fake(faker *gofakeit.Faker) any {
return CustomUint(42)
func (c CustomUint) Fake(faker *gofakeit.Faker) (any, error) {
return CustomUint(42), nil
}

type CustomUint8 uint8

func (c CustomUint8) Fake(faker *gofakeit.Faker) any {
return CustomUint8(42)
func (c CustomUint8) Fake(faker *gofakeit.Faker) (any, error) {
return CustomUint8(42), nil
}

type CustomUint16 uint16

func (c CustomUint16) Fake(faker *gofakeit.Faker) any {
return CustomUint16(42)
func (c CustomUint16) Fake(faker *gofakeit.Faker) (any, error) {
return CustomUint16(42), nil
}

type CustomUint32 uint32

func (c CustomUint32) Fake(faker *gofakeit.Faker) any {
return CustomUint32(42)
func (c CustomUint32) Fake(faker *gofakeit.Faker) (any, error) {
return CustomUint32(42), nil
}

type CustomUint64 uint64

func (c CustomUint64) Fake(faker *gofakeit.Faker) any {
return CustomUint64(42)
func (c CustomUint64) Fake(faker *gofakeit.Faker) (any, error) {
return CustomUint64(42), nil
}

type CustomFloat32 float32

func (c CustomFloat32) Fake(faker *gofakeit.Faker) any {
return CustomFloat32(42.123)
func (c CustomFloat32) Fake(faker *gofakeit.Faker) (any, error) {
return CustomFloat32(42.123), nil
}

type CustomFloat64 float64

func (c CustomFloat64) Fake(faker *gofakeit.Faker) any {
return CustomFloat64(42.123)
func (c CustomFloat64) Fake(faker *gofakeit.Faker) (any, error) {
return CustomFloat64(42.123), nil
}

type CustomTime time.Time

func (c *CustomTime) Fake(faker *gofakeit.Faker) any {
return CustomTime(testTimeValue)
func (c *CustomTime) Fake(faker *gofakeit.Faker) (any, error) {
return CustomTime(testTimeValue), nil
}

func (c CustomTime) String() string {
@@ -109,26 +109,26 @@ func (c CustomTime) String() string {

type CustomSlice []string

func (c CustomSlice) Fake(faker *gofakeit.Faker) any {
return CustomSlice([]string{"hello", "test"})
func (c CustomSlice) Fake(faker *gofakeit.Faker) (any, error) {
return CustomSlice([]string{"hello", "test"}), nil
}

type CustomMap map[string]string

func (c CustomMap) Fake(faker *gofakeit.Faker) any {
return CustomMap(map[string]string{"hello": "1", "test": "2"})
func (c CustomMap) Fake(faker *gofakeit.Faker) (any, error) {
return CustomMap(map[string]string{"hello": "1", "test": "2"}), nil
}

type CustomStruct struct {
Str string
Int int
}

func (c CustomStruct) Fake(faker *gofakeit.Faker) any {
func (c CustomStruct) Fake(faker *gofakeit.Faker) (any, error) {
return CustomStruct{
Str: "hello test",
Int: 42,
}
}, nil
}

type NestedCustom struct {
@@ -707,8 +707,8 @@ func ExampleCustomInt() {

type EvenInt int

func (e EvenInt) Fake(faker *gofakeit.Faker) any {
return EvenInt(faker.Int8() * 2)
func (e EvenInt) Fake(faker *gofakeit.Faker) (any, error) {
return EvenInt(faker.Int8() * 2), nil
}

func ExampleEvenInt() {
16 changes: 8 additions & 8 deletions fakeable_test.go
Original file line number Diff line number Diff line change
@@ -9,14 +9,14 @@ import (

type strTyp string

func (t strTyp) Fake(faker *Faker) any {
return faker.FirstName()
func (t strTyp) Fake(faker *Faker) (any, error) {
return faker.FirstName(), nil
}

type strTypPtr string

func (t *strTypPtr) Fake(faker *Faker) any {
return strTypPtr("hello test ptr")
func (t *strTypPtr) Fake(faker *Faker) (any, error) {
return strTypPtr("hello test ptr"), nil
}

type testStruct1 struct {
@@ -83,7 +83,7 @@ func ExampleFakeable() {

type gammaFloat64 float64

func (gammaFloat64) Fake(faker *Faker) any {
func (gammaFloat64) Fake(faker *Faker) (any, error) {
alpha := 2.0

// Generate a random value from the Gamma distribution
@@ -98,7 +98,7 @@ func (gammaFloat64) Fake(faker *Faker) any {
r = x
}
}
return gammaFloat64(r)
return gammaFloat64(r), nil
}

func ExampleGammaFloat64() {
@@ -123,7 +123,7 @@ func ExampleGammaFloat64() {

type poissonInt64 int64

func (poissonInt64) Fake(faker *Faker) any {
func (poissonInt64) Fake(faker *Faker) (any, error) {
lambda := 15.0

// Generate a random value from the Poisson distribution
@@ -135,7 +135,7 @@ func (poissonInt64) Fake(faker *Faker) any {
p *= u
k++
}
return poissonInt64(k - 1)
return poissonInt64(k - 1), nil
}

type customerSupportEmployee struct {

0 comments on commit 5f00c22

Please sign in to comment.