Skip to content

Commit

Permalink
Add eval.TooFewArgError()
Browse files Browse the repository at this point in the history
  • Loading branch information
tchssk committed Jul 10, 2024
1 parent 1b21c5a commit 1213c35
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dsl/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func Default(def any) {
// })
func Example(args ...any) {
if len(args) == 0 {
eval.ReportError("not enough arguments")
eval.TooFewArgError()
return
}
if len(args) > 2 {
Expand Down
4 changes: 2 additions & 2 deletions dsl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func Error(name string, args ...any) {
// }
func ErrorName(args ...any) {
if len(args) == 0 {
eval.IncompatibleDSL()
eval.TooFewArgError()
return
}
dsl, ok := args[len(args)-1].(func())
Expand All @@ -192,7 +192,7 @@ func ErrorName(args ...any) {
Attribute(actual, args[1:]...)
case int:
if len(args) == 1 {
eval.IncompatibleDSL()
eval.TooFewArgError()
return
}
name, ok := args[1].(string)
Expand Down
2 changes: 1 addition & 1 deletion dsl/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ func SkipResponseBodyEncodeDecode() {
// })
func Body(args ...any) {
if len(args) == 0 {
eval.ReportError("not enough arguments, use Body(name), Body(type), Body(func()) or Body(type, func())")
eval.TooFewArgError()
return
}

Expand Down
4 changes: 2 additions & 2 deletions dsl/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func Code(code int) {

func grpcError(n string, p eval.Expression, args ...any) *expr.GRPCErrorExpr {
if len(args) == 0 {
eval.ReportError("not enough arguments, use Response(name, status), Response(name, status, func()) or Response(name, func())")
eval.TooFewArgError()
return nil
}
var (
Expand Down Expand Up @@ -260,7 +260,7 @@ func parseResponseArgs(val any, args ...any) (code int, fn func()) {

func httpError(n string, p eval.Expression, args ...any) *expr.HTTPErrorExpr {
if len(args) == 0 {
eval.ReportError("not enough arguments, use Response(name, status), Response(name, status, func()) or Response(name, func())")
eval.TooFewArgError()
return nil
}
var (
Expand Down
6 changes: 6 additions & 0 deletions eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ func InvalidArgError(expected string, actual any) {
ReportError("cannot use %#v (type %s) as type %s", actual, reflect.TypeOf(actual), expected)
}

// TooFewArgError records a too few arguments error. It is used by DSL
// functions that take dynamic arguments.
func TooFewArgError() {
ReportError("too few arguments given to %s", caller())
}

// TooManyArgError records a too many arguments error. It is used by DSL
// functions that take dynamic arguments.
func TooManyArgError() {
Expand Down
18 changes: 18 additions & 0 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ func TestInvalidArgError(t *testing.T) {
}
}

func TestTooFewArgError(t *testing.T) {
dsls := map[string]func(){
"Body": func() { Body() },
"ErrorName": func() { ErrorName() },
"ErrorName (int)": func() { Type("name", func() { ErrorName(1) }) },
"Example": func() { Example() },
"Response (grpc)": func() { Service("s", func() { GRPC(func() { Response("name") }) }) },
"Response (http)": func() { Service("s", func() { HTTP(func() { Response("name") }) }) },
}
for name, dsl := range dsls {
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 few arguments given to "+strings.Split(name, " ")[0])
})
}
}

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 1213c35

Please sign in to comment.