Skip to content

Commit

Permalink
Merge pull request #119 from adamvduke/adamvduke/use-t-dot-run
Browse files Browse the repository at this point in the history
set testcase names and use t.Run(name, func(){...})
  • Loading branch information
bmf-san authored Dec 26, 2024
2 parents d0e7e93 + 69d6ff1 commit 1f031b4
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 150 deletions.
13 changes: 10 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,37 @@ func TestGetParam(t *testing.T) {
ngCtx := context.WithValue(context.Background(), ParamsKey, "not a []Param")

cases := []struct {
name string
actual string
expected string
}{
{
name: "id_param",
actual: GetParam(ctx, "id"),
expected: "123",
},
{
name: "name_param",
actual: GetParam(ctx, "name"),
expected: "john",
},
{
name: "not_exist_param",
actual: GetParam(ctx, "not-exist-key"),
expected: "",
},
{
name: "param_value_wrong_type",
actual: GetParam(ngCtx, "ng ctx"),
expected: "",
},
}

for _, c := range cases {
if c.actual != c.expected {
t.Errorf("actual:%v expected:%v", c.actual, c.expected)
}
t.Run(c.name, func(t *testing.T) {
if c.actual != c.expected {
t.Errorf("actual:%v expected:%v", c.actual, c.expected)
}
})
}
}
143 changes: 72 additions & 71 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ func TestNewRouter(t *testing.T) {
}
}

type routerTest struct {
path string
method string
code int
body string
}

func (tc routerTest) name() string {
return fmt.Sprintf("%s_%s_%d", tc.method, tc.path, tc.code)
}

func TestRouterMiddleware(t *testing.T) {
r := NewRouter()

Expand All @@ -34,12 +45,7 @@ func TestRouterMiddleware(t *testing.T) {
fmt.Fprintf(w, "/middlewares\n")
}))

cases := []struct {
path string
method string
code int
body string
}{
cases := []routerTest{
{
path: "/globalmiddleware",
method: http.MethodGet,
Expand All @@ -61,22 +67,25 @@ func TestRouterMiddleware(t *testing.T) {
}

for _, c := range cases {
req := httptest.NewRequest(c.method, c.path, nil)
rec := httptest.NewRecorder()

r.ServeHTTP(rec, req)

if rec.Code != c.code {
t.Errorf("actual: %v expected: %v\n", rec.Code, c.code)
}

recBody, _ := io.ReadAll(rec.Body)
body := string(recBody)
if body != c.body {
t.Errorf("actual: %v expected: %v\n", body, c.body)
}
t.Run(c.name(), func(t *testing.T) {
req := httptest.NewRequest(c.method, c.path, nil)
rec := httptest.NewRecorder()

r.ServeHTTP(rec, req)

if rec.Code != c.code {
t.Errorf("actual: %v expected: %v\n", rec.Code, c.code)
}

recBody, _ := io.ReadAll(rec.Body)
body := string(recBody)
if body != c.body {
t.Errorf("actual: %v expected: %v\n", body, c.body)
}
})
}
}

func TestRouter(t *testing.T) {
r := NewRouter()

Expand Down Expand Up @@ -142,12 +151,7 @@ func TestRouter(t *testing.T) {
fmt.Fprintf(w, "/%v", id)
}))

cases := []struct {
path string
method string
code int
body string
}{
cases := []routerTest{
{
path: "/",
method: http.MethodGet,
Expand Down Expand Up @@ -247,20 +251,22 @@ func TestRouter(t *testing.T) {
}

for _, c := range cases {
req := httptest.NewRequest(c.method, c.path, nil)
rec := httptest.NewRecorder()

r.ServeHTTP(rec, req)

if rec.Code != c.code {
t.Errorf("actual: %v expected: %v\n", rec.Code, c.code)
}

recBody, _ := io.ReadAll(rec.Body)
body := string(recBody)
if body != c.body {
t.Errorf("actual: %v expected: %v\n", body, c.body)
}
t.Run(c.name(), func(t *testing.T) {
req := httptest.NewRequest(c.method, c.path, nil)
rec := httptest.NewRecorder()

r.ServeHTTP(rec, req)

if rec.Code != c.code {
t.Errorf("actual: %v expected: %v\n", rec.Code, c.code)
}

recBody, _ := io.ReadAll(rec.Body)
body := string(recBody)
if body != c.body {
t.Errorf("actual: %v expected: %v\n", body, c.body)
}
})
}
}

Expand All @@ -269,11 +275,7 @@ func TestDefaultErrorHandler(t *testing.T) {
r.Methods(http.MethodGet).Handler(`/defaulterrorhandler`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
r.Methods(http.MethodGet).Handler(`/methodnotallowed`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))

cases := []struct {
path string
method string
code int
}{
cases := []routerTest{
{
path: "/",
method: http.MethodGet,
Expand All @@ -287,14 +289,16 @@ func TestDefaultErrorHandler(t *testing.T) {
}

for _, c := range cases {
req := httptest.NewRequest(c.method, c.path, nil)
rec := httptest.NewRecorder()
t.Run(c.name(), func(t *testing.T) {
req := httptest.NewRequest(c.method, c.path, nil)
rec := httptest.NewRecorder()

r.ServeHTTP(rec, req)
r.ServeHTTP(rec, req)

if rec.Code != c.code {
t.Errorf("actual: %v expected: %v\n", rec.Code, c.code)
}
if rec.Code != c.code {
t.Errorf("actual: %v expected: %v\n", rec.Code, c.code)
}
})
}
}

Expand All @@ -311,12 +315,7 @@ func TestCustomErrorHandler(t *testing.T) {
r.Methods(http.MethodGet).Handler(`/custommethodnotfound`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
r.Methods(http.MethodGet).Handler(`/custommethodnotallowed`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))

cases := []struct {
path string
method string
code int
body string
}{
cases := []routerTest{
{
path: "/",
method: http.MethodGet,
Expand All @@ -332,20 +331,22 @@ func TestCustomErrorHandler(t *testing.T) {
}

for _, c := range cases {
req := httptest.NewRequest(c.method, c.path, nil)
rec := httptest.NewRecorder()

r.ServeHTTP(rec, req)

if rec.Code != c.code {
t.Errorf("actual: %v expected: %v\n", rec.Code, c.code)
}

recBody, _ := io.ReadAll(rec.Body)
body := string(recBody)
if body != c.body {
t.Errorf("actual: %v expected: %v\n", body, c.body)
}
t.Run(c.name(), func(t *testing.T) {
req := httptest.NewRequest(c.method, c.path, nil)
rec := httptest.NewRecorder()

r.ServeHTTP(rec, req)

if rec.Code != c.code {
t.Errorf("actual: %v expected: %v\n", rec.Code, c.code)
}

recBody, _ := io.ReadAll(rec.Body)
body := string(recBody)
if body != c.body {
t.Errorf("actual: %v expected: %v\n", body, c.body)
}
})
}
}

Expand Down
Loading

0 comments on commit 1f031b4

Please sign in to comment.