Skip to content

Commit

Permalink
matched "/*filepath"
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Nov 12, 2024
1 parent 3f29794 commit 1d86f3d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions radix/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/hedzr/evendeep"
logz "github.com/hedzr/logg/slog"
)

type nodeType int
Expand Down
52 changes: 48 additions & 4 deletions radix/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func TestTrieS_InsertPath(t *testing.T) {

expect := ` / <B>
s <B>
earch <L> /search => 1
earch/*kwargs <L> /search/*kwargs => 1
upport <L> /support => 2
blog/:post/ <L> /blog/:post/ => 3
blog/:year/:month/:post <L> /blog/:year/:month/:post => 3
about-us/ <B>
team <L> /about-us/team => 4 // comment | tag = 3.13 ~ desc
legal <L> /about-us/legal => 6
Expand Down Expand Up @@ -166,13 +166,57 @@ func TestTrieS_Delimiter(t *testing.T) { //nolint:revive
})
}

func TestTrieS_Locate(t *testing.T) {
trie := newTrieTree2()
trie.SetDelimiter('/')
t.Logf("\nPath of 'trie' (delimeter=%v)\n%v\n",
trie.Delimiter(),
trie.Dump())

node, kvp, br, pm, found := trie.Locate("/search/any/thing/here")
if !found {
t.Fail()
} else if br || pm {
t.Fail()
} else {
t.Logf("node matched ok: %v", node)
if kvp == nil {
t.Fail()
}
if kvp["kwargs"] != "any/thing/here" {
t.Fail()
}
}

node, kvp, br, pm, found = trie.Locate("/blog/2011/09/why-so-concise")
if !found {
t.Fail()
} else if br || pm {
t.Fail()
} else {
t.Logf("node matched ok: %v", node)
if kvp == nil {
t.Fail()
}
if kvp["year"] != "2011" {
t.Fail()
}
if kvp["month"] != "09" {
t.Fail()
}
if kvp["post"] != "why-so-concise" {
t.Fail()
}
}
}

func newTrieTree2() *trieS[any] {
trie := NewTrie[any]()
trie.Insert("/search", 1)
trie.Insert("/search/*kwargs", 1)
// t.Logf("\nPath\n%v\n", trie.dump())
trie.Insert("/support", 2)
// t.Logf("\nPath\n%v\n", trie.dump())
trie.Insert("/blog/:post/", 3)
trie.Insert("/blog/:year/:month/:post", 3)
trie.Insert("/about-us/team", 4)
trie.Insert("/contact", 5)
trie.Insert("/about-us/legal", 6)
Expand Down

0 comments on commit 1d86f3d

Please sign in to comment.