Repository: goadesign/goa
Pull request #3520 (2024-05-16 14:49:59)

Add test for eval.TooManyArgError()
Related to https://github.com/goadesign/goa/pull/3512


Patch:
diff --git a/dsl/http.go b/dsl/http.go
index 632cda8e..0ddd4403 100644
--- a/dsl/http.go
+++ b/dsl/http.go
@@ -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 {
diff --git a/dsl/payload.go b/dsl/payload.go
index dded16e9..8dc9a41d 100644
--- a/dsl/payload.go
+++ b/dsl/payload.go
@@ -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 {
@@ -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 {
diff --git a/dsl/result_type.go b/dsl/result_type.go
index de883d28..9f443acf 100644
--- a/dsl/result_type.go
+++ b/dsl/result_type.go
@@ -99,6 +99,7 @@ func ResultType(identifier string, args ...any) *expr.ResultTypeExpr {
 				}
 				if len(args) > 2 {
 					eval.TooManyArgError()
+					return nil
 				}
 			}
 		}
diff --git a/dsl/server.go b/dsl/server.go
index f230c71a..dfb861ef 100644
--- a/dsl/server.go
+++ b/dsl/server.go
@@ -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)
 	}


Test patch:
diff --git a/eval/eval_test.go b/eval/eval_test.go
new file mode 100644
index 00000000..00d1b5b5
--- /dev/null
+++ b/eval/eval_test.go
@@ -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)
+	}
+}