Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
feat: Add ability to ignore interface{} population with SetIgnoreInte…
Browse files Browse the repository at this point in the history
…rface() (#123)

* feat: Add ability to ignore interface{} population with SetIgnoreInterface()

* Update mod

* Revert "Update mod"

This reverts commit e78f28e.

* feat: update random integer boundary handling to accept a 0-span range of numbers. Good for reliable testing of 0 values.
  • Loading branch information
nashikb authored Jan 25, 2021
1 parent 693c158 commit 74585ae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
18 changes: 16 additions & 2 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var (
randomSize = 100
// Sets the single fake data generator to generate unique values
generateUniqueValues = false
// Sets whether interface{}s should be ignored.
ignoreInterface = false
// Unique values are kept in memory so the generator retries if the value already exists
uniqueValues = map[string][]interface{}{}
// Lang is selected language for random string generator
Expand Down Expand Up @@ -278,6 +280,11 @@ func SetGenerateUniqueValues(unique bool) {
generateUniqueValues = unique
}

// SetIgnoreInterface allows to set a flag to ignore found interface{}s.
func SetIgnoreInterface(ignore bool) {
ignoreInterface = ignore
}

// SetNilIfLenIsZero allows to set nil for the slice and maps, if size is 0.
func SetNilIfLenIsZero(setNil bool) {
shouldSetNil = setNil
Expand Down Expand Up @@ -407,6 +414,9 @@ func RemoveProvider(tag string) error {
func getValue(a interface{}) (reflect.Value, error) {
t := reflect.TypeOf(a)
if t == nil {
if ignoreInterface {
return reflect.New(reflect.TypeOf(reflect.Struct)), nil
}
return reflect.Value{}, fmt.Errorf("interface{} not allowed")
}
k := t.Kind()
Expand Down Expand Up @@ -1098,12 +1108,16 @@ func randomString(n int, lang *langRuneBoundary) (string, error) {

// randomIntegerWithBoundary returns a random integer between input start and end boundary. [start, end)
func randomIntegerWithBoundary(boundary numberBoundary) int {
return rand.Intn(boundary.end-boundary.start) + boundary.start
span := boundary.end - boundary.start
if span <= 0 {
return boundary.start
}
return rand.Intn(span) + boundary.start
}

// randomInteger returns a random integer between start and end boundary. [start, end)
func randomInteger() int {
return rand.Intn(nBoundary.end-nBoundary.start) + nBoundary.start
return randomIntegerWithBoundary(nBoundary)
}

// randomSliceAndMapSize returns a random integer between [0,randomSliceAndMapSize). If the testRandZero is set, returns 0
Expand Down
13 changes: 13 additions & 0 deletions faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,19 @@ func TestSetNilIfLenIsZero(t *testing.T) {
testRandZero = false
}

func TestSetIgnoreInterface(t *testing.T) {
SetIgnoreInterface(false)
var someInterface interface{}
if err := FakeData(&someInterface); err == nil {
t.Error("Fake data generation didn't fail on interface{}")
}
SetIgnoreInterface(true)
if err := FakeData(&someInterface); err != nil {
t.Error("Fake data generation fail on interface{} with SetIgnoreInterface(true)")
}
SetIgnoreInterface(false)
}

func TestBoundaryAndLen(t *testing.T) {
iterate := 10
someStruct := SomeStructWithLen{}
Expand Down

0 comments on commit 74585ae

Please sign in to comment.