Improved code quality
This commit is contained in:
parent
4c4775c071
commit
ad033d4315
10 changed files with 131 additions and 120 deletions
|
@ -1,23 +1,14 @@
|
|||
package router_test
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.akyoto.dev/go/assert"
|
||||
"git.akyoto.dev/go/router"
|
||||
)
|
||||
|
||||
type route struct {
|
||||
method string
|
||||
path string
|
||||
}
|
||||
|
||||
func TestHello(t *testing.T) {
|
||||
func TestStatic(t *testing.T) {
|
||||
router := router.New[string]()
|
||||
|
||||
router.Add("GET", "/hello", "Hello")
|
||||
router.Add("GET", "/world", "World")
|
||||
|
||||
|
@ -34,9 +25,8 @@ func TestHello(t *testing.T) {
|
|||
assert.Equal(t, data, "")
|
||||
}
|
||||
|
||||
func TestParam(t *testing.T) {
|
||||
func TestParameter(t *testing.T) {
|
||||
router := router.New[string]()
|
||||
|
||||
router.Add("GET", "/blog/:slug", "Blog post")
|
||||
router.Add("GET", "/blog/:slug/comments/:id", "Comment")
|
||||
|
||||
|
@ -57,7 +47,6 @@ func TestParam(t *testing.T) {
|
|||
|
||||
func TestWildcard(t *testing.T) {
|
||||
router := router.New[string]()
|
||||
|
||||
router.Add("GET", "/", "Front page")
|
||||
router.Add("GET", "/:slug", "Blog post")
|
||||
router.Add("GET", "/users/:id", "Parameter")
|
||||
|
@ -86,52 +75,40 @@ func TestWildcard(t *testing.T) {
|
|||
assert.Equal(t, data, "Wildcard")
|
||||
}
|
||||
|
||||
func TestMethods(t *testing.T) {
|
||||
router := router.New[string]()
|
||||
methods := []string{
|
||||
"GET",
|
||||
"POST",
|
||||
"DELETE",
|
||||
"PUT",
|
||||
"PATCH",
|
||||
"HEAD",
|
||||
"CONNECT",
|
||||
"TRACE",
|
||||
"OPTIONS",
|
||||
}
|
||||
|
||||
for _, method := range methods {
|
||||
router.Add(method, "/", method)
|
||||
}
|
||||
|
||||
for _, method := range methods {
|
||||
data, _ := router.Lookup(method, "/")
|
||||
assert.Equal(t, data, method)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGitHub(t *testing.T) {
|
||||
tree := router.New[string]()
|
||||
router := router.New[string]()
|
||||
routes := loadRoutes("testdata/github.txt")
|
||||
|
||||
for _, route := range routes {
|
||||
tree.Add(route.method, route.path, "octocat")
|
||||
router.Add(route.method, route.path, "octocat")
|
||||
}
|
||||
|
||||
for _, route := range routes {
|
||||
data, _ := tree.Lookup(route.method, route.path)
|
||||
data, _ := router.Lookup(route.method, route.path)
|
||||
assert.Equal(t, data, "octocat")
|
||||
}
|
||||
}
|
||||
|
||||
func loadRoutes(fileName string) []route {
|
||||
var routes []route
|
||||
|
||||
for line := range linesInFile(fileName) {
|
||||
parts := strings.Split(line, " ")
|
||||
routes = append(routes, route{
|
||||
method: parts[0],
|
||||
path: parts[1],
|
||||
})
|
||||
}
|
||||
|
||||
return routes
|
||||
}
|
||||
|
||||
func linesInFile(fileName string) <-chan string {
|
||||
lines := make(chan string)
|
||||
|
||||
go func() {
|
||||
defer close(lines)
|
||||
file, err := os.Open(fileName)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
for scanner.Scan() {
|
||||
lines <- strings.TrimSpace(scanner.Text())
|
||||
}
|
||||
}()
|
||||
|
||||
return lines
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue