| 1576 | } |
| 1577 | |
| 1578 | func TestWalkSingleDepth(t *testing.T) { |
| 1579 | r0 := NewRouter() |
| 1580 | r1 := NewRouter() |
| 1581 | r2 := NewRouter() |
| 1582 | |
| 1583 | r0.Path("/g") |
| 1584 | r0.Path("/o") |
| 1585 | r0.Path("/d").Handler(r1) |
| 1586 | r0.Path("/r").Handler(r2) |
| 1587 | r0.Path("/a") |
| 1588 | |
| 1589 | r1.Path("/z") |
| 1590 | r1.Path("/i") |
| 1591 | r1.Path("/l") |
| 1592 | r1.Path("/l") |
| 1593 | |
| 1594 | r2.Path("/i") |
| 1595 | r2.Path("/l") |
| 1596 | r2.Path("/l") |
| 1597 | |
| 1598 | paths := []string{"g", "o", "r", "i", "l", "l", "a"} |
| 1599 | depths := []int{0, 0, 0, 1, 1, 1, 0} |
| 1600 | i := 0 |
| 1601 | err := r0.Walk(func(route *Route, router *Router, ancestors []*Route) error { |
| 1602 | matcher := route.matchers[0].(*routeRegexp) |
| 1603 | if matcher.template == "/d" { |
| 1604 | return SkipRouter |
| 1605 | } |
| 1606 | if len(ancestors) != depths[i] { |
| 1607 | t.Errorf(`Expected depth of %d at i = %d; got "%d"`, depths[i], i, len(ancestors)) |
| 1608 | } |
| 1609 | if matcher.template != "/"+paths[i] { |
| 1610 | t.Errorf(`Expected "/%s" at i = %d; got "%s"`, paths[i], i, matcher.template) |
| 1611 | } |
| 1612 | i++ |
| 1613 | return nil |
| 1614 | }) |
| 1615 | if err != nil { |
| 1616 | panic(err) |
| 1617 | } |
| 1618 | if i != len(paths) { |
| 1619 | t.Errorf("Expected %d routes, found %d", len(paths), i) |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | func TestWalkNested(t *testing.T) { |
| 1624 | router := NewRouter() |