Skip to content

Commit

Permalink
make default generated routes function private and standardize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Aug 20, 2024
1 parent cae1039 commit 885c93c
Show file tree
Hide file tree
Showing 21 changed files with 50 additions and 51 deletions.
4 changes: 2 additions & 2 deletions cmd/muxt/testdata/generate/argument_context.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
muxt generate --routes-func=TemplateRoutes
muxt generate

exec go test -cover

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cmd/muxt/testdata/generate/argument_path_param.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
muxt generate --routes-func=TemplateRoutes
muxt generate

exec go test -cover

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cmd/muxt/testdata/generate/argument_request.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
muxt generate --routes-func=TemplateRoutes
muxt generate

exec go test -cover

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cmd/muxt/testdata/generate/argument_response.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
muxt generate --routes-func=TemplateRoutes
muxt generate

exec go test -cover

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion cmd/muxt/testdata/generate/error_duplicate_pattern.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
! muxt generate --routes-func=TemplateRoutes
! muxt generate
stderr 'duplicate route pattern: GET /'

-- template.gohtml --
Expand Down
2 changes: 1 addition & 1 deletion cmd/muxt/testdata/generate/error_unknown_flag.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
! muxt generate --routes-func=TemplateRoutes --unknown
! muxt generate --unknown
stderr 'flag'

-- template.gohtml --
Expand Down
4 changes: 2 additions & 2 deletions cmd/muxt/testdata/generate/override_execute.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
muxt generate --routes-func=TemplateRoutes
muxt generate
! stdout execute

exec go test -v -cover
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cmd/muxt/testdata/generate/path_end.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
muxt generate --routes-func=TemplateRoutes
muxt generate

exec go test -cover

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cmd/muxt/testdata/generate/path_param.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
muxt generate --routes-func=TemplateRoutes
muxt generate

exec go test -cover

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cmd/muxt/testdata/generate/simple_get.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
muxt generate --routes-func=TemplateRoutes
muxt generate

exec go test -cover

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cmd/muxt/testdata/generate/simple_handler.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
muxt generate --routes-func=TemplateRoutes
muxt generate

exec go test -cover

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cmd/muxt/testdata/generate/simple_patch.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
muxt generate --routes-func=TemplateRoutes
muxt generate

exec go test -cover

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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 /" }}
Expand Down
4 changes: 2 additions & 2 deletions cmd/muxt/testdata/generate/templates_glob_filter.txtar
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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 /" }}
Expand Down
8 changes: 4 additions & 4 deletions cmd/muxt/testdata/generate/templates_multiple_globs.txtar
Original file line number Diff line number Diff line change
@@ -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 /" }}
Expand Down
8 changes: 4 additions & 4 deletions cmd/muxt/testdata/generate/templates_multiple_parsefs.txtar
Original file line number Diff line number Diff line change
@@ -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 /" }}
Expand Down
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ func main() {
},
}
mux := http.NewServeMux()
Routes(mux, backend)
routes(mux, backend)
log.Fatal(http.ListenAndServe(":8080", mux))
}
2 changes: 1 addition & 1 deletion example/template_routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ const (
executeIdentName = "execute"
receiverIdent = "receiver"

receiverInterfaceIdent = "RoutesReceiver"

dataVarIdent = "data"
muxVarIdent = "mux"

Expand All @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 885c93c

Please sign in to comment.