diff --git a/generate_internal_test.go b/generate_internal_test.go deleted file mode 100644 index 2c663da..0000000 --- a/generate_internal_test.go +++ /dev/null @@ -1,114 +0,0 @@ -package muxt - -import ( - "go/ast" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestTemplateName_HandlerFuncLit_err(t *testing.T) { - for _, tt := range []struct { - Name string - In string - ErrSub string - Method *ast.FuncType - }{ - { - Name: "missing arguments", - In: "GET / F()", - Method: &ast.FuncType{ - Params: &ast.FieldList{List: []*ast.Field{{Type: ast.NewIdent("string")}}}, - Results: &ast.FieldList{List: []*ast.Field{{Type: ast.NewIdent("any")}}}, - }, - ErrSub: "handler func F(string) any expects 1 arguments but call F() has 0", - }, - { - Name: "extra arguments", - In: "GET /{name} F(ctx, name)", - Method: &ast.FuncType{ - Params: &ast.FieldList{List: []*ast.Field{ - {Type: &ast.SelectorExpr{X: ast.NewIdent(contextPackageIdent), Sel: ast.NewIdent(contextContextTypeIdent)}}, - }}, - Results: &ast.FieldList{List: []*ast.Field{{Type: ast.NewIdent("any")}}}, - }, - ErrSub: "handler func F(context.Context) any expects 1 arguments but call F(ctx, name) has 2", - }, - { - Name: "wrong argument type request", - In: "GET / F(request)", - Method: &ast.FuncType{ - Params: &ast.FieldList{List: []*ast.Field{ - {Type: ast.NewIdent("string")}, - }}, - Results: &ast.FieldList{List: []*ast.Field{{Type: ast.NewIdent("any")}}}, - }, - ErrSub: "method expects type string but request is *http.Request", - }, - { - Name: "wrong argument type ctx", - In: "GET / F(ctx)", - Method: &ast.FuncType{ - Params: &ast.FieldList{List: []*ast.Field{ - {Type: ast.NewIdent("string")}, - }}, - Results: &ast.FieldList{List: []*ast.Field{{Type: ast.NewIdent("any")}}}, - }, - ErrSub: "method expects type string but ctx is context.Context", - }, - { - Name: "wrong argument type response", - In: "GET / F(response)", - Method: &ast.FuncType{ - Params: &ast.FieldList{List: []*ast.Field{ - {Type: ast.NewIdent("string")}, - }}, - Results: &ast.FieldList{List: []*ast.Field{{Type: ast.NewIdent("any")}}}, - }, - ErrSub: "method expects type string but response is http.ResponseWriter", - }, - { - Name: "wrong argument type path value", - In: "GET /{name} F(name)", - Method: &ast.FuncType{ - Params: &ast.FieldList{List: []*ast.Field{ - {Type: ast.NewIdent("float64")}, - }}, - Results: &ast.FieldList{List: []*ast.Field{{Type: ast.NewIdent("any")}}}, - }, - ErrSub: "method param type float64 not supported", - }, - { - Name: "wrong argument type request ptr", - In: "GET / F(request)", - Method: &ast.FuncType{ - Params: &ast.FieldList{List: []*ast.Field{ - {Type: &ast.StarExpr{X: ast.NewIdent("T")}}, - }}, - Results: &ast.FieldList{List: []*ast.Field{{Type: ast.NewIdent("any")}}}, - }, - ErrSub: "method expects type *T but request is *http.Request", - }, - { - Name: "wrong argument type in field list", - In: "GET /post/{postID}/comment/{commentID} F(ctx, request, commentID)", - Method: &ast.FuncType{ - Params: &ast.FieldList{List: []*ast.Field{ - {Type: contextContextField().Type, Names: []*ast.Ident{{Name: "ctx"}}}, - {Names: []*ast.Ident{ast.NewIdent("postID"), ast.NewIdent("commentID")}, Type: ast.NewIdent("string")}, - }}, - Results: &ast.FieldList{List: []*ast.Field{{Type: ast.NewIdent("any")}}}, - }, - ErrSub: "method expects type string but request is *http.Request", - }, - } { - t.Run(tt.Name, func(t *testing.T) { - pat, err, ok := NewTemplateName(tt.In) - require.True(t, ok) - require.NoError(t, err) - _, _, err = pat.funcLit(tt.Method, nil) - assert.ErrorContains(t, err, tt.ErrSub) - }) - } -} diff --git a/generate_test.go b/generate_test.go index 613cef4..3f66cfb 100644 --- a/generate_test.go +++ b/generate_test.go @@ -26,7 +26,6 @@ func TestGenerate(t *testing.T) { TemplatesVar string RoutesFunc string Imports []string - Method *ast.FuncType ExpectedError string ExpectedFile string @@ -1186,6 +1185,7 @@ func routes(mux *http.ServeMux, receiver RoutesReceiver) { { Name: "call F with multiple arguments", Templates: `{{define "GET /{userName} F(ctx, userName)"}}{{end}}`, + Receiver: "T", ReceiverPackage: `-- in.go -- package main @@ -1218,6 +1218,150 @@ func routes(mux *http.ServeMux, receiver RoutesReceiver) { } `, }, + { + Name: "missing arguments", + Templates: `{{define "GET / F()"}}{{end}}`, + Receiver: "T", + ReceiverPackage: `-- in.go -- +package main + +type T struct{} + +func (T) F(string) any {return nil} + +func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) {} +`, + + ExpectedError: "handler func F(string) any expects 1 arguments but call F() has 0", + }, + { + Name: "extra arguments", + Templates: `{{define "GET /{name} F(ctx, name)"}}{{end}}`, + Receiver: "T", + ReceiverPackage: `-- in.go -- +package main + +import ( + "context" + "net/html" +) + +type T struct{} + +func (T) F(context.Context) any {return nil} + +func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) {} +`, + ExpectedError: "handler func F(context.Context) any expects 1 arguments but call F(ctx, name) has 2", + }, + { + Name: "wrong argument type request", + Templates: `{{define "GET / F(request)"}}{{end}}`, + Receiver: "T", + ReceiverPackage: `-- in.go -- +package main + +import ( + "context" + "net/html" +) + +type T struct{} + +func (T) F(string) any {return nil} + +func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) {} +`, + ExpectedError: "method expects type string but request is *http.Request", + }, + { + Name: "wrong argument type ctx", + Templates: `{{define "GET / F(ctx)"}}{{end}}`, + Receiver: "T", + ReceiverPackage: `-- in.go -- +package main + +import "net/html" + +type T struct{} + +func (T) F(string) any {return nil} + +func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) {} +`, + ExpectedError: "method expects type string but ctx is context.Context", + }, + { + Name: "wrong argument type response", + Templates: `{{define "GET / F(response)"}}{{end}}`, + Receiver: "T", + ReceiverPackage: `-- in.go -- +package main + +import "net/html" + +type T struct{} + +func (T) F(string) any {return nil} + +func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) {} +`, + ExpectedError: "method expects type string but response is http.ResponseWriter", + }, + { + Name: "wrong argument type path value", + Templates: `{{define "GET /{name} F(name)"}}{{end}}`, + Receiver: "T", + ReceiverPackage: `-- in.go -- +package main + +import "net/html" + +type T struct{} + +func (T) F(float64) any {return nil} + +func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) {} +`, + ExpectedError: "method param type float64 not supported", + }, + { + Name: "wrong argument type request ptr", + Templates: `{{define "GET / F(request)"}}{{end}}`, + Receiver: "T", + ReceiverPackage: `-- in.go -- +package main + +import "net/html" + +type T struct{} + +func (T) F(*T) any {return nil} + +func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) {} +`, + ExpectedError: "method expects type *T but request is *http.Request", + }, + { + Name: "wrong argument type in field list", + Templates: `{{define "GET /post/{postID}/comment/{commentID} F(ctx, request, commentID)"}}{{end}}`, + Receiver: "T", + ReceiverPackage: `-- in.go -- +package main + +import ( + "context" + "net/html" +) + +type T struct{} + +func (T) F(context.Context, string, string) any {return nil} + +func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) {} +`, + ExpectedError: "method expects type string but request is *http.Request", + }, } { t.Run(tt.Name, func(t *testing.T) { ts := template.Must(template.New(tt.Name).Parse(tt.Templates))