Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: panic due to invalid user input #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion generator/faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s service) ReplaceStringWithFakerWhenRequested(request string) (string, er
}

a := strings.Split(request, ".")
if len(a) == 1 {
if len(a) < 2 {
return request,
errors.New("requires more arguments to get a faker func")
}
Expand All @@ -51,6 +51,11 @@ func (s service) ReplaceStringWithFakerWhenRequested(request string) (string, er
if len(a) == FakerShortLen {
argsString := strings.Split(a[1], "(")

if len(argsString) < 2 {
return request,
errors.New("requires more arguments to get a faker func")
}

args, dErr := getMethodArguments(
&s.faker,
argsString[0],
Expand All @@ -74,6 +79,11 @@ func (s service) ReplaceStringWithFakerWhenRequested(request string) (string, er

argsString := strings.Split(a[2], "(")

if len(argsString) < 2 {
return request,
errors.New("requires more arguments to get a faker func")
}

obj := reflect.Indirect(res[0]).Interface()

args, err := getMethodArguments(
Expand Down
37 changes: 37 additions & 0 deletions generator/faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
)

func Test_service_ReplaceStringWithFakerWhenRequested(t *testing.T) {
t.Parallel()
type args struct {
request string
}
Expand Down Expand Up @@ -82,6 +83,8 @@ func Test_service_ReplaceStringWithFakerWhenRequested(t *testing.T) {
}

func Test_service_FakerError(t *testing.T) {
t.Parallel()

type args struct {
request string
}
Expand Down Expand Up @@ -158,6 +161,40 @@ func Test_service_FakerError(t *testing.T) {
"this should have errored",
true,
},
{
"call a property",
args{
"faker.Bla",
},
func(s interface{}) bool {
return true
},
"this should have errored",
true,
},
{
"started a function call but missing parameters",
args{
"faker.Bla(",
},
func(s interface{}) bool {
return true
},
"this should have errored",
true,
},

{
"missing parameters on long call",
args{
"faker.Bla().Fuu",
},
func(s interface{}) bool {
return true
},
"this should have errored",
true,
},
}
for _, tt := range tests {
t.Run(
Expand Down