@@ -43,3 +43,39 @@ func TestTreeMatch(t *testing.T) {
43
43
t .Fatalf ("route did not match expected %s (%s)" , "pl/blog/comments/123/new" , commentNew .Name ())
44
44
}
45
45
}
46
+
47
+ func TestTreeFindNode (t * testing.T ) {
48
+ blog := NewNode ("blog" , 0 )
49
+
50
+ search := NewNode ("search" , blog .MaxParamsSize ())
51
+ page := NewNode ("page" , blog .MaxParamsSize ())
52
+ posts := NewNode ("posts" , blog .MaxParamsSize ())
53
+
54
+ blog .WithChildren (blog .Tree ().withNode (search ).sort ())
55
+ blog .WithChildren (blog .Tree ().withNode (page ).sort ())
56
+ blog .WithChildren (blog .Tree ().withNode (posts ).sort ())
57
+
58
+ blog .WithChildren (blog .Tree ().Compile ())
59
+
60
+ tests := []struct {
61
+ name string
62
+ input string
63
+ expected Node
64
+ }{
65
+ {"Find existing node 1" , "search" , search },
66
+ {"Find existing node 2" , "page" , page },
67
+ {"Find existing node 3" , "posts" , posts },
68
+ {"Find non-existing node" , "comments" , nil },
69
+ {"Find with empty name" , "" , nil },
70
+ }
71
+
72
+ for _ , tt := range tests {
73
+ t .Run (tt .name , func (t * testing.T ) {
74
+ result := blog .Tree ().Find (tt .input )
75
+ if result != tt .expected {
76
+ t .Errorf ("expected %v, got %v" , tt .expected , result )
77
+ }
78
+ })
79
+ }
80
+ }
81
+
0 commit comments