Skip to content

Commit

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

* Fix dsl.Example

---------

Co-authored-by: Raphael Simon <simon.raphael@gmail.com>
  • Loading branch information
tchssk and raphael committed Jun 18, 2024
1 parent 3eefb33 commit 0a2f9a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dsl/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
26 changes: 26 additions & 0 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }) },
Expand Down

0 comments on commit 0a2f9a4

Please sign in to comment.