Skip to content

Commit

Permalink
Use eval.TooManyArgError() more (#3526)
Browse files Browse the repository at this point in the history
Co-authored-by: Raphael Simon <simon.raphael@gmail.com>
  • Loading branch information
tchssk and raphael committed Jun 15, 2024
1 parent c87dff9 commit fd639f3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion dsl/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ func Field(tag any, name string, args ...any) {
// })
func OneOf(name string, args ...any) {
if len(args) > 2 {
eval.ReportError("OneOf: wrong number of arguments")
eval.TooManyArgError()
return
}
fn, ok := args[len(args)-1].(func())
if !ok {
Expand Down
3 changes: 1 addition & 2 deletions dsl/http.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dsl

import (
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -146,7 +145,7 @@ const (
// })
func HTTP(fns ...func()) {
if len(fns) > 1 {
eval.InvalidArgError("zero or one function", fmt.Sprintf("%d functions", len(fns)))
eval.TooManyArgError()
return
}
fn := func() {}
Expand Down
2 changes: 1 addition & 1 deletion dsl/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func parseResponseArgs(val any, args ...any) (code int, fn func()) {
}
case func():
if len(args) > 0 {
eval.InvalidArgError("int (HTTP status code)", val)
eval.TooManyArgError()
return
}
fn = t
Expand Down
2 changes: 1 addition & 1 deletion dsl/user_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func Type(name string, args ...any) expr.UserType {
base = &expr.Object{}
fn = a
if len(args) == 2 {
eval.ReportError("only one argument allowed when it is a function")
eval.TooManyArgError()
return nil
}
default:
Expand Down
8 changes: 6 additions & 2 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ func TestTooManyArgError(t *testing.T) {
"Example": func() { Example(1, 2, 3) },
"Field": func() { Type("name", func() { Field("tag", "name", 1, 2, 3, 4) }) },
"Files": func() { Files("path", "filename", func() {}, func() {}) },
"HTTP": func() { API("name", func() { HTTP(func() {}, func() {}) }) },
"Header": func() { API("name", func() { HTTP(func() { Header("name", 1, 2, 3, 4) }) }) },
"MapOf": func() { MapOf(String, String, func() {}, func() {}) },
"MapParams": func() { MapParams(1, 2) },
"OneOf": func() { OneOf("name", 1, 2, 3) },
"Param": func() { API("name", func() { HTTP(func() { Param("name", 1, 2, 3, 4) }) }) },
"Password": func() { Type("name", func() { Password("name", 1, 2, 3) }) },
"PasswordField": func() { Type("name", func() { PasswordField("tag", "name", 1, 2, 3) }) },
"Payload": func() { Payload(String, 1, 2, 3) },
"Response": func() { API("name", func() { HTTP(func() { Response(StatusOK, "name", 1, 2) }) }) },
"Response (int)": func() { API("name", func() { HTTP(func() { Response(StatusOK, "name", 1, 2) }) }) },
"Response (func)": func() { API("name", func() { HTTP(func() { Response("name", func() {}, func() {}) }) }) },
"Result": func() { Result(String, 1, 2, 3) },
"ResultType": func() { ResultType("identifier", "name", func() {}, func() {}) },
"Scope": func() { BasicAuthSecurity("name", func() { Scope("name", "1", "2") }) },
Expand All @@ -40,6 +43,7 @@ func TestTooManyArgError(t *testing.T) {
"Token": func() { Type("name", func() { Token("name", 1, 2, 3) }) },
"TokenField": func() { Type("name", func() { TokenField("tag", "name", 1, 2, 3) }) },
"Type": func() { Type("name", 1, 2, 3) },
"Type (func)": func() { Type("name", func() {}, func() {}) },
"Username": func() { Type("name", func() { Username("name", 1, 2, 3) }) },
"UsernameField": func() { Type("name", func() { UsernameField("tag", "name", 1, 2, 3) }) },
"Variable": func() { API("a", func() { Server("s", func() { Host("h", func() { Variable("v", 1, 2, 3, 4) }) }) }) },
Expand All @@ -48,7 +52,7 @@ func TestTooManyArgError(t *testing.T) {
t.Run(name, func(t *testing.T) {
err := expr.RunInvalidDSL(t, dsl)
assert.Len(t, strings.Split(err.Error(), "\n"), 1)
assert.Contains(t, err.Error(), "too many arguments given to "+name)
assert.Contains(t, err.Error(), "too many arguments given to "+strings.Split(name, " ")[0])
})
}
}

0 comments on commit fd639f3

Please sign in to comment.