From 885c93c6c80b6b3f3b3d62e139ccffabeb94939e Mon Sep 17 00:00:00 2001 From: Chrstopher Hunter <8398225+crhntr@users.noreply.github.com> Date: Mon, 19 Aug 2024 19:01:48 -0700 Subject: [PATCH] make default generated routes function private and standardize tests --- .../testdata/generate/argument_context.txtar | 4 ++-- .../testdata/generate/argument_path_param.txtar | 4 ++-- .../testdata/generate/argument_request.txtar | 4 ++-- .../testdata/generate/argument_response.txtar | 4 ++-- .../generate/error_duplicate_pattern.txtar | 2 +- .../testdata/generate/error_unknown_flag.txtar | 2 +- .../testdata/generate/override_execute.txtar | 4 ++-- cmd/muxt/testdata/generate/path_end.txtar | 4 ++-- cmd/muxt/testdata/generate/path_param.txtar | 4 ++-- cmd/muxt/testdata/generate/simple_get.txtar | 4 ++-- cmd/muxt/testdata/generate/simple_handler.txtar | 4 ++-- cmd/muxt/testdata/generate/simple_patch.txtar | 4 ++-- .../templates_and_embed_in_gen_decl.txtar | 4 ++-- .../generate/templates_glob_filter.txtar | 4 ++-- .../templates_multiple_embed_lines.txtar | 8 ++++---- .../generate/templates_multiple_globs.txtar | 8 ++++---- .../generate/templates_multiple_parsefs.txtar | 8 ++++---- example/main.go | 2 +- example/template_routes.go | 2 +- generate.go | 5 ++--- generate_test.go | 16 ++++++++-------- 21 files changed, 50 insertions(+), 51 deletions(-) diff --git a/cmd/muxt/testdata/generate/argument_context.txtar b/cmd/muxt/testdata/generate/argument_context.txtar index 3cd98fe..929c4cf 100644 --- a/cmd/muxt/testdata/generate/argument_context.txtar +++ b/cmd/muxt/testdata/generate/argument_context.txtar @@ -1,4 +1,4 @@ -muxt generate --routes-func=TemplateRoutes +muxt generate exec go test -cover @@ -40,7 +40,7 @@ func Test(t *testing.T) { mux := http.NewServeMux() var service T - TemplateRoutes(mux, service) + routes(mux, service) req := httptest.NewRequest(http.MethodGet, "/", nil) rec := httptest.NewRecorder() diff --git a/cmd/muxt/testdata/generate/argument_path_param.txtar b/cmd/muxt/testdata/generate/argument_path_param.txtar index 1aa6e91..83564b4 100644 --- a/cmd/muxt/testdata/generate/argument_path_param.txtar +++ b/cmd/muxt/testdata/generate/argument_path_param.txtar @@ -1,4 +1,4 @@ -muxt generate --routes-func=TemplateRoutes +muxt generate exec go test -cover @@ -40,7 +40,7 @@ func Test(t *testing.T) { mux := http.NewServeMux() var service T - TemplateRoutes(mux, service) + routes(mux, service) req := httptest.NewRequest(http.MethodGet, "/user/crhntr", nil) rec := httptest.NewRecorder() diff --git a/cmd/muxt/testdata/generate/argument_request.txtar b/cmd/muxt/testdata/generate/argument_request.txtar index cdd50c6..6976b39 100644 --- a/cmd/muxt/testdata/generate/argument_request.txtar +++ b/cmd/muxt/testdata/generate/argument_request.txtar @@ -1,4 +1,4 @@ -muxt generate --routes-func=TemplateRoutes +muxt generate exec go test -cover @@ -40,7 +40,7 @@ func Test(t *testing.T) { mux := http.NewServeMux() var service T - TemplateRoutes(mux, service) + routes(mux, service) req := httptest.NewRequest(http.MethodGet, "/", nil) rec := httptest.NewRecorder() diff --git a/cmd/muxt/testdata/generate/argument_response.txtar b/cmd/muxt/testdata/generate/argument_response.txtar index f8f7c26..720f3c9 100644 --- a/cmd/muxt/testdata/generate/argument_response.txtar +++ b/cmd/muxt/testdata/generate/argument_response.txtar @@ -1,4 +1,4 @@ -muxt generate --routes-func=TemplateRoutes +muxt generate exec go test -cover @@ -40,7 +40,7 @@ func Test(t *testing.T) { mux := http.NewServeMux() var service T - TemplateRoutes(mux, service) + routes(mux, service) req := httptest.NewRequest(http.MethodGet, "/", nil) rec := httptest.NewRecorder() diff --git a/cmd/muxt/testdata/generate/error_duplicate_pattern.txtar b/cmd/muxt/testdata/generate/error_duplicate_pattern.txtar index c389f81..cc961f6 100644 --- a/cmd/muxt/testdata/generate/error_duplicate_pattern.txtar +++ b/cmd/muxt/testdata/generate/error_duplicate_pattern.txtar @@ -1,4 +1,4 @@ -! muxt generate --routes-func=TemplateRoutes +! muxt generate stderr 'duplicate route pattern: GET /' -- template.gohtml -- diff --git a/cmd/muxt/testdata/generate/error_unknown_flag.txtar b/cmd/muxt/testdata/generate/error_unknown_flag.txtar index 86ea1cf..5f5a565 100644 --- a/cmd/muxt/testdata/generate/error_unknown_flag.txtar +++ b/cmd/muxt/testdata/generate/error_unknown_flag.txtar @@ -1,4 +1,4 @@ -! muxt generate --routes-func=TemplateRoutes --unknown +! muxt generate --unknown stderr 'flag' -- template.gohtml -- diff --git a/cmd/muxt/testdata/generate/override_execute.txtar b/cmd/muxt/testdata/generate/override_execute.txtar index e067441..85b988c 100644 --- a/cmd/muxt/testdata/generate/override_execute.txtar +++ b/cmd/muxt/testdata/generate/override_execute.txtar @@ -1,4 +1,4 @@ -muxt generate --routes-func=TemplateRoutes +muxt generate ! stdout execute exec go test -v -cover @@ -47,7 +47,7 @@ import ( func Test(t *testing.T) { mux := http.NewServeMux() - TemplateRoutes(mux, nil) + routes(mux, nil) req := httptest.NewRequest(http.MethodPatch, "/", nil) rec := httptest.NewRecorder() diff --git a/cmd/muxt/testdata/generate/path_end.txtar b/cmd/muxt/testdata/generate/path_end.txtar index f42deda..3174c55 100644 --- a/cmd/muxt/testdata/generate/path_end.txtar +++ b/cmd/muxt/testdata/generate/path_end.txtar @@ -1,4 +1,4 @@ -muxt generate --routes-func=TemplateRoutes +muxt generate exec go test -cover @@ -37,7 +37,7 @@ import ( func Test(t *testing.T) { mux := http.NewServeMux() - TemplateRoutes(mux, nil) + routes(mux, nil) req := httptest.NewRequest(http.MethodGet, "/end/", nil) rec := httptest.NewRecorder() diff --git a/cmd/muxt/testdata/generate/path_param.txtar b/cmd/muxt/testdata/generate/path_param.txtar index 02240fd..16056af 100644 --- a/cmd/muxt/testdata/generate/path_param.txtar +++ b/cmd/muxt/testdata/generate/path_param.txtar @@ -1,4 +1,4 @@ -muxt generate --routes-func=TemplateRoutes +muxt generate exec go test -cover @@ -45,7 +45,7 @@ func (T) ToUpper(in string) any { return Data{Name: strings.ToUpper(in)} } func Test(t *testing.T) { mux := http.NewServeMux() - TemplateRoutes(mux, T{}) + routes(mux, T{}) req := httptest.NewRequest(http.MethodGet, "/fruits/peach/tree", nil) rec := httptest.NewRecorder() diff --git a/cmd/muxt/testdata/generate/simple_get.txtar b/cmd/muxt/testdata/generate/simple_get.txtar index 44f116f..1a2c7fb 100644 --- a/cmd/muxt/testdata/generate/simple_get.txtar +++ b/cmd/muxt/testdata/generate/simple_get.txtar @@ -1,4 +1,4 @@ -muxt generate --routes-func=TemplateRoutes +muxt generate exec go test -cover @@ -37,7 +37,7 @@ import ( func Test(t *testing.T) { mux := http.NewServeMux() - TemplateRoutes(mux, nil) + routes(mux, nil) req := httptest.NewRequest(http.MethodGet, "/", nil) rec := httptest.NewRecorder() diff --git a/cmd/muxt/testdata/generate/simple_handler.txtar b/cmd/muxt/testdata/generate/simple_handler.txtar index 68e3fa0..9bb136b 100644 --- a/cmd/muxt/testdata/generate/simple_handler.txtar +++ b/cmd/muxt/testdata/generate/simple_handler.txtar @@ -1,4 +1,4 @@ -muxt generate --routes-func=TemplateRoutes +muxt generate exec go test -cover @@ -40,7 +40,7 @@ func Test(t *testing.T) { mux := http.NewServeMux() var service T - TemplateRoutes(mux, service) + routes(mux, service) req := httptest.NewRequest(http.MethodGet, "/", nil) rec := httptest.NewRecorder() diff --git a/cmd/muxt/testdata/generate/simple_patch.txtar b/cmd/muxt/testdata/generate/simple_patch.txtar index 6a2c43e..09ac9d2 100644 --- a/cmd/muxt/testdata/generate/simple_patch.txtar +++ b/cmd/muxt/testdata/generate/simple_patch.txtar @@ -1,4 +1,4 @@ -muxt generate --routes-func=TemplateRoutes +muxt generate exec go test -cover @@ -37,7 +37,7 @@ import ( func Test(t *testing.T) { mux := http.NewServeMux() - TemplateRoutes(mux, nil) + routes(mux, nil) req := httptest.NewRequest(http.MethodPatch, "/", nil) rec := httptest.NewRecorder() diff --git a/cmd/muxt/testdata/generate/templates_and_embed_in_gen_decl.txtar b/cmd/muxt/testdata/generate/templates_and_embed_in_gen_decl.txtar index 1d0a575..af77142 100644 --- a/cmd/muxt/testdata/generate/templates_and_embed_in_gen_decl.txtar +++ b/cmd/muxt/testdata/generate/templates_and_embed_in_gen_decl.txtar @@ -1,5 +1,5 @@ -muxt generate --routes-func=TemplateRoutes -stdout 'Routes has route for GET /' +muxt generate +stdout 'routes has route for GET /' -- index.gohtml -- {{define "GET /" }} diff --git a/cmd/muxt/testdata/generate/templates_glob_filter.txtar b/cmd/muxt/testdata/generate/templates_glob_filter.txtar index c2358a3..aa413dc 100644 --- a/cmd/muxt/testdata/generate/templates_glob_filter.txtar +++ b/cmd/muxt/testdata/generate/templates_glob_filter.txtar @@ -1,5 +1,5 @@ -muxt generate --routes-func=TemplateRoutes -! stdout 'Routes has route for GET /example' +muxt generate +! stdout 'routes has route for GET /example' exec go test diff --git a/cmd/muxt/testdata/generate/templates_multiple_embed_lines.txtar b/cmd/muxt/testdata/generate/templates_multiple_embed_lines.txtar index 94dbf29..cae3271 100644 --- a/cmd/muxt/testdata/generate/templates_multiple_embed_lines.txtar +++ b/cmd/muxt/testdata/generate/templates_multiple_embed_lines.txtar @@ -1,7 +1,7 @@ -muxt generate --routes-func=TemplateRoutes -stdout 'Routes has route for GET /' -stdout 'Routes has route for GET /form' -stdout 'Routes has route for POST /form' +muxt generate +stdout 'routes has route for GET /' +stdout 'routes has route for GET /form' +stdout 'routes has route for POST /form' -- index.gohtml -- {{define "GET /" }} diff --git a/cmd/muxt/testdata/generate/templates_multiple_globs.txtar b/cmd/muxt/testdata/generate/templates_multiple_globs.txtar index 0ecc7a5..fc01b3d 100644 --- a/cmd/muxt/testdata/generate/templates_multiple_globs.txtar +++ b/cmd/muxt/testdata/generate/templates_multiple_globs.txtar @@ -1,7 +1,7 @@ -muxt generate --routes-func=TemplateRoutes -stdout 'Routes has route for GET /' -stdout 'Routes has route for GET /form' -stdout 'Routes has route for POST /form' +muxt generate +stdout 'routes has route for GET /' +stdout 'routes has route for GET /form' +stdout 'routes has route for POST /form' -- index.gohtml -- {{define "GET /" }} diff --git a/cmd/muxt/testdata/generate/templates_multiple_parsefs.txtar b/cmd/muxt/testdata/generate/templates_multiple_parsefs.txtar index 7edb8c9..d5ae935 100644 --- a/cmd/muxt/testdata/generate/templates_multiple_parsefs.txtar +++ b/cmd/muxt/testdata/generate/templates_multiple_parsefs.txtar @@ -1,7 +1,7 @@ -muxt generate --routes-func=TemplateRoutes -stdout 'Routes has route for GET /' -stdout 'Routes has route for GET /form' -stdout 'Routes has route for POST /form' +muxt generate +stdout 'routes has route for GET /' +stdout 'routes has route for GET /form' +stdout 'routes has route for POST /form' -- index.gohtml -- {{define "GET /" }} diff --git a/example/main.go b/example/main.go index 4c17041..828fcc7 100644 --- a/example/main.go +++ b/example/main.go @@ -65,6 +65,6 @@ func main() { }, } mux := http.NewServeMux() - Routes(mux, backend) + routes(mux, backend) log.Fatal(http.ListenAndServe(":8080", mux)) } diff --git a/example/template_routes.go b/example/template_routes.go index c47c7b0..fb47b75 100644 --- a/example/template_routes.go +++ b/example/template_routes.go @@ -15,7 +15,7 @@ type RoutesReceiver interface { List(_ context.Context) []Row } -func Routes(mux *http.ServeMux, receiver RoutesReceiver) { +func routes(mux *http.ServeMux, receiver RoutesReceiver) { mux.HandleFunc("PATCH /fruits/{fruit}", func(response http.ResponseWriter, request *http.Request) { fruit := request.PathValue("fruit") data := receiver.SubmitFormEditRow(request, fruit) diff --git a/generate.go b/generate.go index e970594..a74c35e 100644 --- a/generate.go +++ b/generate.go @@ -17,8 +17,6 @@ const ( executeIdentName = "execute" receiverIdent = "receiver" - receiverInterfaceIdent = "RoutesReceiver" - dataVarIdent = "data" muxVarIdent = "mux" @@ -40,8 +38,9 @@ const ( defaultPackageName = "main" DefaultTemplatesVariableName = "templates" - DefaultRoutesFunctionName = "Routes" + DefaultRoutesFunctionName = "routes" DefaultOutputFileName = "template_routes.go" + receiverInterfaceIdent = "RoutesReceiver" ) func Generate(templateNames []TemplateName, packageName, templatesVariableName, routesFunctionName, receiverTypeIdent string, _ *token.FileSet, receiverPackage, templatesPackage []*ast.File, log *log.Logger) (string, error) { diff --git a/generate_test.go b/generate_test.go index 599bace..c79aa72 100644 --- a/generate_test.go +++ b/generate_test.go @@ -45,7 +45,7 @@ import ( type RoutesReceiver interface { } -func Routes(mux *http.ServeMux, receiver RoutesReceiver) { +func routes(mux *http.ServeMux, receiver RoutesReceiver) { mux.HandleFunc("GET /", func(response http.ResponseWriter, request *http.Request) { execute(response, request, templates.Lookup("GET /"), http.StatusOK, request) }) @@ -76,7 +76,7 @@ type RoutesReceiver interface { F() any } -func Routes(mux *http.ServeMux, receiver RoutesReceiver) { +func routes(mux *http.ServeMux, receiver RoutesReceiver) { mux.HandleFunc("GET /", func(response http.ResponseWriter, request *http.Request) { data := receiver.F() execute(response, request, templates.Lookup("GET / F()"), http.StatusOK, data) @@ -109,7 +109,7 @@ type RoutesReceiver interface { F(ctx context.Context, response http.ResponseWriter, request *http.Request, projectID string, taskID string) any } -func Routes(mux *http.ServeMux, receiver RoutesReceiver) { +func routes(mux *http.ServeMux, receiver RoutesReceiver) { mux.HandleFunc("GET /project/{projectID}/task/{taskID}", func(response http.ResponseWriter, request *http.Request) { ctx := request.Context() projectID := request.PathValue("projectID") @@ -153,7 +153,7 @@ type RoutesReceiver interface { F(username string) int } -func Routes(mux *http.ServeMux, receiver RoutesReceiver) { +func routes(mux *http.ServeMux, receiver RoutesReceiver) { mux.HandleFunc("GET /age/{username}", func(response http.ResponseWriter, request *http.Request) { username := request.PathValue("username") data := receiver.F(username) @@ -195,7 +195,7 @@ type RoutesReceiver interface { F(username string) int } -func Routes(mux *http.ServeMux, receiver RoutesReceiver) { +func routes(mux *http.ServeMux, receiver RoutesReceiver) { mux.HandleFunc("GET /age/{username}", func(response http.ResponseWriter, request *http.Request) { username := request.PathValue("username") data := receiver.F(username) @@ -238,7 +238,7 @@ type RoutesReceiver interface { F(username string) int } -func Routes(mux *http.ServeMux, receiver RoutesReceiver) { +func routes(mux *http.ServeMux, receiver RoutesReceiver) { mux.HandleFunc("GET /age/{username}", func(response http.ResponseWriter, request *http.Request) { username := request.PathValue("username") data := receiver.F(username) @@ -271,7 +271,7 @@ type RoutesReceiver interface { F(username string) (int, error) } -func Routes(mux *http.ServeMux, receiver RoutesReceiver) { +func routes(mux *http.ServeMux, receiver RoutesReceiver) { mux.HandleFunc("GET /age/{username}", func(response http.ResponseWriter, request *http.Request) { username := request.PathValue("username") data, err := receiver.F(username) @@ -347,7 +347,7 @@ type RoutesReceiver interface { F(ctx context.Context, username string) int } -func Routes(mux *http.ServeMux, receiver RoutesReceiver) { +func routes(mux *http.ServeMux, receiver RoutesReceiver) { mux.HandleFunc("GET /age/{username}", func(response http.ResponseWriter, request *http.Request) { ctx := request.Context() username := request.PathValue("username")