Skip to content

Commit

Permalink
Added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
donutloop committed Nov 27, 2016
1 parent 12525fa commit 64a4623
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
8 changes: 4 additions & 4 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ type Route struct {
}

//BadRouteError creates error for a bad route
type BadRouteError struct {
type badRouteError struct {
r *Route
s string
}

func newBadRouteError(r *Route, s string) *BadRouteError {
return &BadRouteError{
func newBadRouteError(r *Route, s string) *badRouteError {
return &badRouteError{
r: r,
s: s,
}
}

func (bre BadRouteError) Error() string {
func (bre badRouteError) Error() string {
return fmt.Sprintf("Route -> Method: %s Path: %s Error: %s", bre.r.methodName, bre.r.path, bre.s)
}

Expand Down
20 changes: 20 additions & 0 deletions route_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mux

import (
"strings"
"testing"
)

func TestNewBadRouteError(t *testing.T) {

r := &Route{
methodName: "GET",
path: "/api/user",
}

err := newBadRouteError(r, "Something went wrong")

if err == nil || !strings.Contains(err.Error(), "Something went wrong") {
t.Errorf("Bad error message (%v)", err.Error())
}
}
18 changes: 18 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mux

import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -232,3 +233,20 @@ func testRoute(rt routeTest) (int, bool) {

return res.Code, true
}

func TestHasErrors(t *testing.T) {
routeA := &Route{
err: errors.New("Bad route"),
}
routeB := &Route{
err: errors.New("Bad method"),
}

r := &Router{}
r.routes = map[string][]*Route{}
r.routes[http.MethodGet] = append(r.routes[http.MethodGet], routeA, routeB)

if ok, errors := r.HasErrors(); !ok || 0 == len(errors) {
t.Errorf("Has no errros (Status is %v, How many errors ? %v)", ok, len(errors))
}
}

0 comments on commit 64a4623

Please sign in to comment.