Skip to content

Commit

Permalink
Merge pull request #66 from WheeskyJack/WheeskyJack-patch-4
Browse files Browse the repository at this point in the history
Update node_test.go with test case TestWildcardNodeMatchRoute
  • Loading branch information
vardius authored Aug 27, 2024
2 parents 8a3ec11 + 44b972f commit cddd74a
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions mux/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,94 @@ func TestWildcardNodeMatchRoute(t *testing.T) {
})
}
}

func TestRegexpdNodeMatchRoute(t *testing.T) {
paramSize := 3
productRoute := newMockRoute("testproductroute")
itemRoute := newMockRoute("testitemroute")
viewRoute := newMockRoute("testviewroute")
params := make(context.Params, paramSize)

product := staticNode{name: "product", route: nil, maxParamsSize: uint8(paramSize)}
product.WithRoute(productRoute)

// Create a regexp node for "{item}" under "product"
item := NewNode("{item:item1|item2}", product.MaxParamsSize())
item.WithRoute(itemRoute)

view := NewNode("view", product.MaxParamsSize()+1)
view.WithRoute(viewRoute)

// Build the tree structure
product.WithChildren(product.Tree().withNode(item).sort())
item.WithChildren(item.Tree().withNode(view).sort())
product.WithChildren(product.Tree().Compile())

// Create a static node for "product" with skip subpath functionality
productSkipSubpath := staticNode{name: "product", route: nil, maxParamsSize: uint8(paramSize)}
productSkipSubpath.WithRoute(productRoute)

itemSkipSubpath := NewNode("{item:item1|item2}", productSkipSubpath.MaxParamsSize()) // regexpNode
itemSkipSubpath.WithRoute(itemRoute)
itemSkipSubpath.SkipSubPath()

// Build the tree structure
productSkipSubpath.WithChildren(productSkipSubpath.Tree().withNode(itemSkipSubpath).sort())
productSkipSubpath.WithChildren(productSkipSubpath.Tree().Compile())

tests := []struct {
name string
node staticNode
path string
expectedRoute Route
expectedParams context.Params
}{
{
name: "Exact Match",
node: product,
path: "product/item1",
expectedRoute: item.Route(),
expectedParams: append(params, context.Param{Key: "item", Value: "item1"}),
},
{
name: "Match with SubPath",
node: product,
path: "product/item1/view",
expectedRoute: view.Route(),
expectedParams: append(params, context.Param{Key: "item", Value: "item1"}),
},
{
name: "Exact Match with Skip SubPath",
node: productSkipSubpath,
path: "product/item2/order",
expectedRoute: item.Route(),
expectedParams: append(params, context.Param{Key: "item", Value: "item2"}),
},
{
name: "No Match with SubPath",
node: product,
path: "product/item1/order",
expectedRoute: nil,
expectedParams: nil,
},
{
name: "No Match",
node: product,
path: "product/item3/view",
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 !reflect.DeepEqual(params, tt.expectedParams) {
t.Errorf("%s: expected params %v, got %v", tt.name, tt.expectedParams, params)
}
})
}
}

0 comments on commit cddd74a

Please sign in to comment.