Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Task #1941 (#1943)
Browse files Browse the repository at this point in the history
* covering non id cases on the route name generator

* fixing broken tests

* adding a few more test cases

* moving to just use a string for simplified ID
  • Loading branch information
paganotoni authored Apr 14, 2020
1 parent 22ba6f4 commit 92a7563
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion default_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func Test_DefaultContext_Redirect_Helper(t *testing.T) {
return c.Render(http.StatusOK, render.String(c.Param("bar")))
})
a.GET("/", func(c Context) error {
return c.Redirect(http.StatusPermanentRedirect, "fooPath()", tt.I)
return c.Redirect(http.StatusPermanentRedirect, "fooBarPath()", tt.I)
})
a.GET("/nomap", func(c Context) error {
return c.Redirect(http.StatusPermanentRedirect, "rootPath()")
Expand Down
24 changes: 19 additions & 5 deletions route_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,35 @@ func (a *App) buildRouteName(p string) string {

for index, part := range parts {

if strings.Contains(part, "{") || part == "" {
originalPart := parts[index]

var previousPart string
if index > 0 {
previousPart = parts[index-1]
}

var nextPart string
if len(parts) > index+1 {
nextPart = parts[index+1]
}

isIdentifierPart := strings.Contains(part, "{") && (strings.Contains(part, flect.Singularize(previousPart)))
isSimplifiedID := part == `{id}`

if isIdentifierPart || isSimplifiedID || part == "" {
continue
}

shouldSingularize := (len(parts) > index+1) && strings.Contains(parts[index+1], "{")
if shouldSingularize {
if strings.Contains(nextPart, "{") {
part = flect.Singularize(part)
}

if parts[index] == "new" || parts[index] == "edit" {
if originalPart == "new" || originalPart == "edit" {
resultParts = append([]string{part}, resultParts...)
continue
}

if index > 0 && strings.Contains(parts[index-1], "}") {
if strings.Contains(previousPart, "}") {
resultParts = append(resultParts, part)
continue
}
Expand Down
19 changes: 16 additions & 3 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ func Test_App_NamedRoutes(t *testing.T) {
11. <%= rootPath({"special/":"12=ss"}) %>
12. <%= resourcePath({resource_id: 1}) %>
13. <%= editResourcePath({resource_id: 1}) %>
14. <%= testPath() %>
15. <%= testNamePath({name: "myTest"}) %>
16. <%= paganoPath({id: 1}) %>
`))
}

Expand All @@ -437,6 +440,9 @@ func Test_App_NamedRoutes(t *testing.T) {
a.GET("/peeps", sampleHandler).Name("myPeeps")
a.Resource("/car", carsResource)
a.Resource("/resources", resourcesResource)
a.GET("/test", sampleHandler)
a.GET("/test/{name}", sampleHandler)
a.GET("/pagano/{id}", sampleHandler)

w := httptest.New(a)
res := w.HTML("/").Get()
Expand All @@ -454,6 +460,9 @@ func Test_App_NamedRoutes(t *testing.T) {
r.Contains(res.Body.String(), "11. /?special%2F=12%3Dss")
r.Contains(res.Body.String(), "12. /resources/1")
r.Contains(res.Body.String(), "13. /resources/1/edit")
r.Contains(res.Body.String(), "14. /test")
r.Contains(res.Body.String(), "15. /test/myTest")
r.Contains(res.Body.String(), "16. /pagano/1")
}

func Test_App_NamedRoutes_MissingParameter(t *testing.T) {
Expand Down Expand Up @@ -719,9 +728,13 @@ func Test_buildRouteName(t *testing.T) {
"/users/{user_id}/children/{child_id}": "userChild",
"/users/{user_id}/children/new": "newUserChildren",
"/users/{user_id}/children/{child_id}/build": "userChildBuild",
"/admin/planes": "adminPlanes",
"/admin/planes/{plane_id}": "adminPlane",
"/admin/planes/{plane_id}/edit": "editAdminPlane",
"/admin/planes": "adminPlanes",
"/admin/planes/{plane_id}": "adminPlane",
"/admin/planes/{plane_id}/edit": "editAdminPlane",
"/test": "test",
"/tests/{name}": "testName",
"/tests/{name_id}/cases/{case_id}": "testNameIdCase",
"/tests/{name_id}/cases/{case_id}/edit": "editTestNameIdCase",
}

a := New(Options{})
Expand Down

0 comments on commit 92a7563

Please sign in to comment.