Skip to content

Commit

Permalink
Merge pull request #64 from WheeskyJack/WheeskyJack-patch-2
Browse files Browse the repository at this point in the history
Update node_test.go with TestStaticNodeMatchRoute test case
  • Loading branch information
vardius authored Aug 26, 2024
2 parents b9cbbae + ea318a6 commit d124c05
Showing 1 changed file with 96 additions and 4 deletions.
100 changes: 96 additions & 4 deletions mux/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package mux

import (
"testing"

"github.com/vardius/gorouter/v4/context"
)

func TestNewNode(t *testing.T) {
Expand All @@ -18,18 +20,108 @@ func TestNewNode(t *testing.T) {

switch node := node.(type) {
case *staticNode:
t.Fatalf("Expecting: *mux.staticNode. Wrong node type: %T\n", node)
t.Fatalf("Expecting: *mux.regexpNode. Wrong node type: %T\n", node)
case *wildcardNode:
t.Fatalf("Expecting: *mux.staticNode. Wrong node type: %T\n", node)
t.Fatalf("Expecting: *mux.regexpNode. Wrong node type: %T\n", node)
}

node = NewNode("{lang}", 0)

switch node := node.(type) {
case *staticNode:
t.Fatalf("Expecting: *mux.staticNode. Wrong node type: %T\n", node)
t.Fatalf("Expecting: *mux.wildcardNode. Wrong node type: %T\n", node)
case *regexpNode:
t.Fatalf("Expecting: *mux.staticNode. Wrong node type: %T\n", node)
t.Fatalf("Expecting: *mux.wildcardNode. Wrong node type: %T\n", node)
}
}

type mockroute struct {
handler interface{}
}

func newRoute(h interface{}) *mockroute {
if h == nil {
panic("Handler can not be nil.")
}

return &mockroute{
handler: h,
}
}

func (r *mockroute) Handler() interface{} {
return r.handler
}

func TestStaticNodeMatchRoute(t *testing.T) {
homeRoute := newRoute("testhomeroute")
searchRoute := newRoute("testsearchroute")

homeSkipSubpath := staticNode{name: "home", route: homeRoute, maxParamsSize: 3}
homeSkipSubpath.SkipSubPath()

home := staticNode{name: "home", route: nil, maxParamsSize: 3}
home.WithRoute(homeRoute)

search := NewNode("search", home.MaxParamsSize())
search.WithRoute(searchRoute)

home.WithChildren(home.Tree().withNode(search).sort())
home.WithChildren(home.Tree().Compile())

tests := []struct {
name string
node staticNode
path string
expectedRoute Route
expectedParams context.Params
}{
{
name: "Exact Match",
node: home,
path: "home",
expectedRoute: homeRoute,
expectedParams: make(context.Params, 3),
},
{
name: "Exact Match with Skip SubPath",
node: homeSkipSubpath,
path: "home/about",
expectedRoute: homeRoute,
expectedParams: make(context.Params, 3),
},
{
name: "Match with SubPath",
node: home,
path: "home/search",
expectedRoute: searchRoute,
expectedParams: make(context.Params, 3),
},
{
name: "No Match",
node: home,
path: "about",
expectedRoute: nil,
expectedParams: nil,
},
{
name: "No Match without Slash",
node: home,
path: "homeabout",
expectedRoute: nil,
expectedParams: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
route, params := tt.node.MatchRoute(tt.path)
if route != tt.expectedRoute {
t.Errorf("%s: expected route %v, got %v", tt.name, tt.expectedRoute, route)
}
if len(params) != len(tt.expectedParams) {
t.Errorf("%s: expected params %v, got %v", tt.name, tt.expectedParams, params)
}
})
}
}

0 comments on commit d124c05

Please sign in to comment.