Improved test coverage
This commit is contained in:
parent
7070897e70
commit
bd91077f83
@ -48,7 +48,8 @@ PASS: TestParameter
|
||||
PASS: TestWildcard
|
||||
PASS: TestMethods
|
||||
PASS: TestGitHub
|
||||
coverage: 76.9% of statements
|
||||
PASS: TestOverwrite
|
||||
coverage: 82.5% of statements
|
||||
```
|
||||
|
||||
## Benchmarks
|
||||
|
@ -20,25 +20,36 @@ func TestStatic(t *testing.T) {
|
||||
assert.Equal(t, len(params), 0)
|
||||
assert.Equal(t, data, "World")
|
||||
|
||||
data, params = r.Lookup("GET", "/404")
|
||||
assert.Equal(t, len(params), 0)
|
||||
assert.Equal(t, data, "")
|
||||
notFound := []string{
|
||||
"",
|
||||
"?",
|
||||
"/404",
|
||||
"/hell",
|
||||
"/hall",
|
||||
"/helloo",
|
||||
}
|
||||
|
||||
for _, path := range notFound {
|
||||
data, params = r.Lookup("GET", path)
|
||||
assert.Equal(t, len(params), 0)
|
||||
assert.Equal(t, data, "")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParameter(t *testing.T) {
|
||||
r := router.New[string]()
|
||||
r.Add("GET", "/blog/:slug", "Blog post")
|
||||
r.Add("GET", "/blog/:slug/comments/:id", "Comment")
|
||||
r.Add("GET", "/blog/:post", "Blog post")
|
||||
r.Add("GET", "/blog/:post/comments/:id", "Comment")
|
||||
|
||||
data, params := r.Lookup("GET", "/blog/hello-world")
|
||||
assert.Equal(t, len(params), 1)
|
||||
assert.Equal(t, params[0].Key, "slug")
|
||||
assert.Equal(t, params[0].Key, "post")
|
||||
assert.Equal(t, params[0].Value, "hello-world")
|
||||
assert.Equal(t, data, "Blog post")
|
||||
|
||||
data, params = r.Lookup("GET", "/blog/hello-world/comments/123")
|
||||
assert.Equal(t, len(params), 2)
|
||||
assert.Equal(t, params[0].Key, "slug")
|
||||
assert.Equal(t, params[0].Key, "post")
|
||||
assert.Equal(t, params[0].Value, "hello-world")
|
||||
assert.Equal(t, params[1].Key, "id")
|
||||
assert.Equal(t, params[1].Value, "123")
|
||||
@ -48,8 +59,9 @@ func TestParameter(t *testing.T) {
|
||||
func TestWildcard(t *testing.T) {
|
||||
r := router.New[string]()
|
||||
r.Add("GET", "/", "Front page")
|
||||
r.Add("GET", "/:slug", "Blog post")
|
||||
r.Add("GET", "/:post", "Blog post")
|
||||
r.Add("GET", "/users/:id", "Parameter")
|
||||
r.Add("GET", "/images/static", "Static")
|
||||
r.Add("GET", "/images/*path", "Wildcard")
|
||||
|
||||
data, params := r.Lookup("GET", "/")
|
||||
@ -58,7 +70,7 @@ func TestWildcard(t *testing.T) {
|
||||
|
||||
data, params = r.Lookup("GET", "/blog-post")
|
||||
assert.Equal(t, len(params), 1)
|
||||
assert.Equal(t, params[0].Key, "slug")
|
||||
assert.Equal(t, params[0].Key, "post")
|
||||
assert.Equal(t, params[0].Value, "blog-post")
|
||||
assert.Equal(t, data, "Blog post")
|
||||
|
||||
@ -68,6 +80,22 @@ func TestWildcard(t *testing.T) {
|
||||
assert.Equal(t, params[0].Value, "42")
|
||||
assert.Equal(t, data, "Parameter")
|
||||
|
||||
data, params = r.Lookup("GET", "/images/static")
|
||||
assert.Equal(t, len(params), 0)
|
||||
assert.Equal(t, data, "Static")
|
||||
|
||||
data, params = r.Lookup("GET", "/images/ste")
|
||||
assert.Equal(t, len(params), 1)
|
||||
assert.Equal(t, params[0].Key, "path")
|
||||
assert.Equal(t, params[0].Value, "ste")
|
||||
assert.Equal(t, data, "Wildcard")
|
||||
|
||||
data, params = r.Lookup("GET", "/images/sta")
|
||||
assert.Equal(t, len(params), 1)
|
||||
assert.Equal(t, params[0].Key, "path")
|
||||
assert.Equal(t, params[0].Value, "sta")
|
||||
assert.Equal(t, data, "Wildcard")
|
||||
|
||||
data, params = r.Lookup("GET", "/images/favicon/256.png")
|
||||
assert.Equal(t, len(params), 1)
|
||||
assert.Equal(t, params[0].Key, "path")
|
||||
@ -111,9 +139,25 @@ func TestGitHub(t *testing.T) {
|
||||
for _, route := range routes {
|
||||
data, _ := r.Lookup(route.method, route.path)
|
||||
assert.Equal(t, data, "octocat")
|
||||
|
||||
data = r.LookupNoAlloc(route.method, route.path, func(string, string) {})
|
||||
assert.Equal(t, data, "octocat")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOverwrite(t *testing.T) {
|
||||
r := router.New[string]()
|
||||
r.Add("GET", "/", "1")
|
||||
r.Add("GET", "/", "2")
|
||||
r.Add("GET", "/", "3")
|
||||
r.Add("GET", "/", "4")
|
||||
r.Add("GET", "/", "5")
|
||||
|
||||
data, params := r.Lookup("GET", "/")
|
||||
assert.Equal(t, len(params), 0)
|
||||
assert.Equal(t, data, "5")
|
||||
}
|
||||
|
||||
func TestMemoryUsage(t *testing.T) {
|
||||
escape := func(a any) {}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user