From 0a2f9a41624c4d79b6e3dc11c918818f8c7ccdc5 Mon Sep 17 00:00:00 2001 From: Taichi Sasaki Date: Wed, 19 Jun 2024 02:08:31 +0900 Subject: [PATCH] Add test for eval.InvalidArgError() (#3536) * Add test for eval.InvalidArgError() * Fix dsl.Example --------- Co-authored-by: Raphael Simon --- dsl/attribute.go | 2 +- eval/eval_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/dsl/attribute.go b/dsl/attribute.go index 6ee52ca635..f34e6624fe 100644 --- a/dsl/attribute.go +++ b/dsl/attribute.go @@ -316,7 +316,7 @@ func Example(args ...any) { var ok bool summary, ok = args[0].(string) if !ok { - eval.InvalidArgError("summary (string)", summary) + eval.InvalidArgError("summary (string)", args[0]) return } arg = args[1] diff --git a/eval/eval_test.go b/eval/eval_test.go index c316f52ac2..4afb4d07aa 100644 --- a/eval/eval_test.go +++ b/eval/eval_test.go @@ -9,6 +9,32 @@ import ( "goa.design/goa/v3/expr" ) +func TestInvalidArgError(t *testing.T) { + dsls := map[string]struct { + dsl func() + want string + }{ + "Attribute": {func() { Type("name", func() { Attribute("name", String, "description", 1) }) }, "cannot use 1 (type int) as type func()"}, + "Body": {func() { Service("s", func() { Method("m", func() { HTTP(func() { Body(1) }) }) }) }, "cannot use 1 (type int) as type attribute name, user type or DSL"}, + "ErrorName (bool)": {func() { Type("name", func() { ErrorName(true) }) }, "cannot use true (type bool) as type name or position"}, + "ErrorName (int)": {func() { Type("name", func() { ErrorName(1, 2) }) }, "cannot use 2 (type int) as type name"}, + "Example": {func() { Example(1, 2) }, "cannot use 1 (type int) as type summary (string)"}, + "Headers": {func() { Headers(1) }, "cannot use 1 (type int) as type function"}, + "Param": {func() { API("name", func() { HTTP(func() { Params(1) }) }) }, "cannot use 1 (type int) as type function"}, + "Response": {func() { Service("s", func() { HTTP(func() { Response(1) }) }) }, "cannot use 1 (type int) as type name of error"}, + "ResultType": {func() { ResultType("identifier", 1) }, "cannot use 1 (type int) as type function or string"}, + "Security": {func() { Security(1) }, "cannot use 1 (type int) as type security scheme or security scheme name"}, + "Type": {func() { Type("name", 1) }, "cannot use 1 (type int) as type type or function"}, + } + for name, tc := range dsls { + t.Run(name, func(t *testing.T) { + err := expr.RunInvalidDSL(t, tc.dsl) + assert.Len(t, strings.Split(err.Error(), "\n"), 1) + assert.Contains(t, err.Error(), tc.want) + }) + } +} + func TestTooManyArgError(t *testing.T) { dsls := map[string]func(){ "APIKey": func() { Type("name", func() { APIKey("scheme", "name", 1, 2, 3) }) },