Skip to content

Commit

Permalink
Add test for eval.TooManyArgError() (#3520)
Browse files Browse the repository at this point in the history
* Add test for eval.TooManyArgError()

* Report only one error using eval.TooManyArgError()

* Not return nil
  • Loading branch information
tchssk authored May 18, 2024
1 parent 66e90d3 commit 10e59a1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions dsl/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ func Param(name string, args ...any) {
func MapParams(args ...any) {
if len(args) > 1 {
eval.TooManyArgError()
return
}
e, ok := eval.Current().(*expr.HTTPEndpointExpr)
if !ok {
Expand Down
2 changes: 2 additions & 0 deletions dsl/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import (
func Payload(val any, args ...any) {
if len(args) > 2 {
eval.TooManyArgError()
return
}
e, ok := eval.Current().(*expr.MethodExpr)
if !ok {
Expand Down Expand Up @@ -125,6 +126,7 @@ func Payload(val any, args ...any) {
func StreamingPayload(val any, args ...any) {
if len(args) > 2 {
eval.TooManyArgError()
return
}
e, ok := eval.Current().(*expr.MethodExpr)
if !ok {
Expand Down
1 change: 1 addition & 0 deletions dsl/result_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func ResultType(identifier string, args ...any) *expr.ResultTypeExpr {
}
if len(args) > 2 {
eval.TooManyArgError()
return nil
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion dsl/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ import (
// })
// })
func Server(name string, fn ...func()) *expr.ServerExpr {
server := &expr.ServerExpr{Name: name}
if len(fn) > 1 {
eval.TooManyArgError()
return server
}
api, ok := eval.Current().(*expr.APIExpr)
if !ok {
eval.IncompatibleDSL()
}
server := &expr.ServerExpr{Name: name}
if len(fn) > 0 {
eval.Execute(fn[0], server)
}
Expand Down
38 changes: 38 additions & 0 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package eval_test

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
. "goa.design/goa/v3/dsl"
"goa.design/goa/v3/expr"
)

func TestTooManyArgError(t *testing.T) {
cases := map[string]struct {
DSL func()
Error string
}{
"ArrayOf": {func() { ArrayOf(String, func() {}, func() {}) }, "too many arguments given to ArrayOf"},
"Attribute": {func() { Type("name", func() { Attribute("name", 1, 2, 3, 4) }) }, "too many arguments given to Attribute"},
"Example": {func() { Example(1, 2, 3) }, "too many arguments given to Example"},
"Files": {func() { Files("path", "filename", func() {}, func() {}) }, "too many arguments given to Files"},
"MapOf": {func() { MapOf(String, String, func() {}, func() {}) }, "too many arguments given to MapOf"},
"MapParams": {func() { MapParams(1, 2) }, "too many arguments given to MapParams"},
"Payload": {func() { Payload(String, 1, 2, 3) }, "too many arguments given to Payload"},
"Response": {func() { API("name", func() { HTTP(func() { Response(StatusOK, "name", 1, 2) }) }) }, "too many arguments given to Response"},
"Result": {func() { Result(String, 1, 2, 3) }, "too many arguments given to Result"},
"ResultType": {func() { ResultType("identifier", "name", func() {}, func() {}) }, "too many arguments given to ResultType"},
"Scope": {func() { BasicAuthSecurity("name", func() { Scope("name", "1", "2") }) }, "too many arguments given to Scope"},
"Server": {func() { Server("name", func() {}, func() {}) }, "too many arguments given to Server"},
"StreamingPayload": {func() { StreamingPayload(String, 1, 2, 3) }, "too many arguments given to StreamingPayload"},
"StreamingResult": {func() { StreamingResult(String, 1, 2, 3) }, "too many arguments given to StreamingResult"},
"Type": {func() { Type("name", 1, 2, 3) }, "too many arguments given to Type"},
}
for _, tc := range cases {
err := expr.RunInvalidDSL(t, tc.DSL)
assert.Len(t, strings.Split(err.Error(), "\n"), 1)
assert.Contains(t, err.Error(), tc.Error)
}
}

0 comments on commit 10e59a1

Please sign in to comment.