diff options
| author | Eltrac <hi@guhub.cn> | 2025-11-27 11:45:00 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-11-27 11:45:00 +0800 |
| commit | c41a8f36676bca96b3a3c4af7ca6ab96f2ad9fc1 (patch) | |
| tree | 143b7388a766c82a45890e37f1c256dbb0ff67f9 /router_test.go | |
| parent | bb58f6cd1d9cce5b12e2f9a30b1c9a6aaefdd54c (diff) | |
refactor: use standard Go tests
Diffstat (limited to 'router_test.go')
| -rw-r--r-- | router_test.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/router_test.go b/router_test.go new file mode 100644 index 0000000..24a2faa --- /dev/null +++ b/router_test.go @@ -0,0 +1,79 @@ +package growel + +import ( + "testing" +) + +func dummy(c *Context) {} + +func TestRouterStaticMatch(t *testing.T) { + r := NewRouter() + r.Add("GET", "/hello", dummy) + + h, params := r.Find("GET", "/hello") + if h == nil { + t.Fatal("expected handler, got nil") + } + if len(params) != 0 { + t.Fatalf("expected no params, got %v", params) + } +} + +func TestRouterStaticNoMatch(t *testing.T) { + r := NewRouter() + r.Add("GET", "/hello", dummy) + + h, _ := r.Find("GET", "/nothello") + if h != nil { + t.Fatal("expected nil handler, got non-nil") + } +} + +func TestRouterParamMatch(t *testing.T) { + r := NewRouter() + r.Add("GET", "/user/:id", dummy) + + h, params := r.Find("GET", "/user/42") + if h == nil { + t.Fatal("expected handler, got nil") + } + if params["id"] != "42" { + t.Fatalf("expected id=42, got %v", params) + } +} + +func TestRouterParamMismatch(t *testing.T) { + r := NewRouter() + r.Add("GET", "/user/:id", dummy) + + h, _ := r.Find("GET", "/user") + if h != nil { + t.Fatal("expected nil handler, got non-nil") + } +} + +func TestRouterSplitPath(t *testing.T) { + r := NewRouter() + + tests := []struct { + input string + want []string + }{ + {"/", []string{""}}, + {"/hello", []string{"hello"}}, + {"/hello/world", []string{"hello", "world"}}, + {"//hello//world//", []string{"hello", "", "world"}}, + } + + for _, tt := range tests { + got := r.splitPath(tt.input) + if len(got) != len(tt.want) { + t.Fatalf("splitPath(%q) length mismatch: got %v want %v", tt.input, got, tt.want) + } + for i := range got { + if got[i] != tt.want[i] { + t.Fatalf("splitPath(%q) mismatch: got %v want %v", tt.input, got, tt.want) + } + } + } +} |
