@@ -141,6 +141,14 @@ func TestTree(t *testing.T) {
141141 require .Equal (t , "wow" , wildcards .Value ("id" ))
142142 })
143143
144+ test := func (t * testing.T , tree * Node [int ], path string , value int , wKey , wVal string ) {
145+ w := kv .New ()
146+ val , found := tree .Lookup (path , w )
147+ require .True (t , found )
148+ require .Equal (t , value , val )
149+ require .Equal (t , wVal , w .Value (wKey ))
150+ }
151+
144152 t .Run ("dynamic in the middle" , func (t * testing.T ) {
145153 tree := New [int ]()
146154 require .NoError (t , tree .Insert ("/user/:id" , 1 ))
@@ -220,14 +228,40 @@ func TestTree(t *testing.T) {
220228 test (t , tree , "/prefix42" , 1 , "path" , "42" )
221229 test (t , tree , "/prefixnowhere/like/this" , 1 , "path" , "nowhere/like/this" )
222230 })
223- }
224231
225- func test (t * testing.T , tree * Node [int ], path string , value int , wKey , wVal string ) {
226- w := kv .New ()
227- val , found := tree .Lookup (path , w )
228- require .True (t , found )
229- require .Equal (t , value , val )
230- require .Equal (t , wVal , w .Value (wKey ))
232+ t .Run ("escape wildcard" , func (t * testing.T ) {
233+ t .Run ("static leftmost" , func (t * testing.T ) {
234+ tree := New [int ]()
235+ require .NoError (t , tree .Insert ("\\ :hello/\\ :world" , 1 ))
236+ value , found := tree .Lookup (":hello/:world" , nil )
237+ require .True (t , found )
238+ require .Equal (t , 1 , value )
239+ })
240+
241+ t .Run ("static rightmost" , func (t * testing.T ) {
242+ tree := New [int ]()
243+ require .NoError (t , tree .Insert ("/\\ :hello" , 1 ))
244+ value , found := tree .Lookup ("/:hello" , nil )
245+ require .True (t , found )
246+ require .Equal (t , 1 , value )
247+ })
248+
249+ t .Run ("dynamic" , func (t * testing.T ) {
250+ tree := New [int ]()
251+ require .NoError (t , tree .Insert ("/\\ :hello/:name" , 1 ))
252+ wildcards := kv .New ()
253+ value , found := tree .Lookup ("/:hello/Pavlo" , wildcards )
254+ require .True (t , found )
255+ require .Equal (t , 1 , value )
256+ require .Equal (t , "Pavlo" , wildcards .Value ("name" ))
257+ })
258+ })
259+
260+ t .Run ("path classifier" , func (t * testing.T ) {
261+ require .False (t , IsDynamicTemplate ("\\ :hello/\\ :world" ))
262+ require .False (t , IsDynamicTemplate ("/\\ :hello" ))
263+ require .True (t , IsDynamicTemplate ("/\\ :hello/:name" ))
264+ })
231265}
232266
233267// isn't used anymore. Left just in case the tree needs to be debugged.
0 commit comments