Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Commit

Permalink
Cleanup unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed May 5, 2020
1 parent d063760 commit 6a13d0f
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 503 deletions.
65 changes: 16 additions & 49 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"

Expand All @@ -31,25 +30,19 @@ func TestContext_SetContentType(t *testing.T) {
for _, test := range tests {
ctx := newContext(httptest.NewRecorder(), nil)
ctx.SetContentType(test)
if ctx.Response.Header().Get("Content-Type") != test {
t.Errorf("expected content type %q, got %q", test, ctx.Response.Header().Get("Content-Type"))
}
assert.Equal(t, test, ctx.Response.Header().Get("Content-Type"))
}
}

func TestContext_SetContentTypeHTML(t *testing.T) {
ctx := newContext(httptest.NewRecorder(), nil)
ctx.SetContentTypeHTML()
if ctx.Response.Header().Get("Content-Type") != "text/html; charset=utf-8" {
t.Errorf("expected content type %q, got %q", "text/html; charset=utf-8", ctx.Response.Header().Get("Content-Type"))
}
assert.Equal(t, "text/html; charset=utf-8", ctx.Response.Header().Get("Content-Type"))
}
func TestContext_SetContentTypeText(t *testing.T) {
ctx := newContext(httptest.NewRecorder(), nil)
ctx.SetContentTypeText()
if ctx.Response.Header().Get("Content-Type") != "text/plain; charset=utf-8" {
t.Errorf("expected content type %q, got %q", "text/plain; charset=utf-8", ctx.Response.Header().Get("Content-Type"))
}
assert.Equal(t, "text/plain; charset=utf-8", ctx.Response.Header().Get("Content-Type"))
}
func TestContext_SetContentTypeJSON(t *testing.T) {
ctx := newContext(httptest.NewRecorder(), nil)
Expand All @@ -72,9 +65,7 @@ func TestContext_Write(t *testing.T) {
w := httptest.NewRecorder()
ctx := newContext(w, nil)
ctx.Write(test)
if !bytes.Equal(w.Body.Bytes(), test) {
t.Errorf("expected body %q, got %q", test, w.Body.Bytes())
}
assert.Equal(t, string(test), w.Body.String())
}
}
func TestContext_WriteString(t *testing.T) {
Expand All @@ -87,28 +78,22 @@ func TestContext_WriteString(t *testing.T) {
w := httptest.NewRecorder()
ctx := newContext(w, nil)
ctx.WriteString(test)
if w.Body.String() != test {
t.Errorf("expected body %q, got %q", test, w.Body.String())
}
assert.Equal(t, test, w.Body.String())
}
}

func TestContext_NotFound(t *testing.T) {
w := httptest.NewRecorder()
ctx := newContext(w, nil)
ctx.NotFound()
if w.Code != http.StatusNotFound {
t.Errorf("expected status code %d, got %d", http.StatusNotFound, w.Code)
}
assert.Equal(t, http.StatusNotFound, w.Code)
}

func TestContext_Redirect(t *testing.T) {
w := httptest.NewRecorder()
ctx := newContext(w, httptest.NewRequest(http.MethodGet, "/", nil))
ctx.Redirect("/redirect", http.StatusPermanentRedirect)
if w.Code != http.StatusPermanentRedirect {
t.Errorf("expected status code %d, got %d", http.StatusPermanentRedirect, w.Code)
}
assert.Equal(t, http.StatusPermanentRedirect, w.Code)
}
func TestContext_Error(t *testing.T) {
tests := []struct {
Expand All @@ -123,12 +108,8 @@ func TestContext_Error(t *testing.T) {
w := httptest.NewRecorder()
ctx := newContext(w, nil)
ctx.Error(test.msg, test.code)
if w.Body.String() != fmt.Sprintln(test.msg) {
t.Errorf("expected body %q, got %q", fmt.Sprintln(test.msg), w.Body.String())
}
if w.Code != test.code {
t.Errorf("expected status code %d, got %d", test.code, w.Code)
}
assert.Equal(t, fmt.Sprintln(test.msg), w.Body.String())
assert.Equal(t, test.code, w.Code)
}
}

Expand All @@ -148,9 +129,7 @@ func TestContext_WithValue(t *testing.T) {
}

for key, val := range values {
if !reflect.DeepEqual(val, ctx.Value(key)) {
t.Errorf("expected the value of %v: %v, got %v", key, val, ctx.Value(key))
}
assert.Equal(t, val, ctx.Value(key))
}
}

Expand Down Expand Up @@ -183,9 +162,7 @@ func TestIsMethod(t *testing.T) {
}
for _, test := range tests {
ctx := newContext(nil, httptest.NewRequest(test.method, "/", nil))
if !test.f(ctx, test.method) {
t.Errorf("failed to determine request method")
}
assert.True(t, test.f(ctx, test.method), "failed to determine request method")
}
}

Expand All @@ -211,12 +188,8 @@ func TestContext_SetCookie(t *testing.T) {
cookie := &http.Cookie{Name: "foo", Value: "bar"}
ctx.SetCookie(cookie)
actual := w.Result().Cookies()[0]
if cookie.Name != actual.Name {
t.Errorf("expected cookie name %s, got %s", cookie.Name, actual.Name)
}
if cookie.Value != actual.Value {
t.Errorf("expected cookie value %s, got %s", cookie.Value, actual.Value)
}
assert.Equal(t, cookie.Name, actual.Name)
assert.Equal(t, cookie.Value, actual.Value)
}

func TestContext_WriteHeader(t *testing.T) {
Expand All @@ -225,9 +198,7 @@ func TestContext_WriteHeader(t *testing.T) {
w := httptest.NewRecorder()
ctx := newContext(w, nil)
ctx.WriteHeader(code)
if w.Code != code {
t.Errorf("expected status code %d, got %d", code, w.Code)
}
assert.Equal(t, code, w.Code)
}
}

Expand All @@ -244,9 +215,7 @@ func TestContext_IsAJAX(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("X-Requested-With", test.value)
ctx := newContext(nil, req)
if ctx.IsAJAX() != test.expected {
t.Errorf("expected IsAJAX %t, got %t", test.expected, ctx.IsAJAX())
}
assert.Equal(t, test.expected, ctx.IsAJAX())
}
}

Expand All @@ -255,9 +224,7 @@ func TestContext_GetHeader(t *testing.T) {
req.Header.Set("foo", "bar")
ctx := newContext(nil, req)
for _, name := range []string{"foo", "fizz"} {
if req.Header.Get(name) != ctx.GetHeader(name) {
t.Errorf("expected header %s: %q, got %q", name, req.Header.Get(name), ctx.GetHeader(name))
}
assert.Equal(t, req.Header.Get(name), ctx.GetHeader(name))
}
}

Expand Down
10 changes: 4 additions & 6 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"errors"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewError(t *testing.T) {
Expand All @@ -21,11 +23,7 @@ func TestNewError(t *testing.T) {

for _, test := range tests {
err := NewError(test.code, errors.New(test.msg))
if err.Code != test.code {
t.Errorf("expected error code %d, got %d", test.code, err.Code)
}
if err.Error() != test.msg {
t.Errorf("expected error message %s, got %s", test.msg, err.Error())
}
assert.Equal(t, test.code, err.Code)
assert.Equal(t, test.msg, err.Error())
}
}
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/clevergo/clevergo

go 1.7

require (
github.com/docker/docker v1.13.1
github.com/stretchr/testify v1.5.1
)
require github.com/stretchr/testify v1.5.1
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo=
github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
55 changes: 25 additions & 30 deletions params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ func TestParams_Int(t *testing.T) {
"param3": 1,
}
for name, value := range tests {
if val, err := ps.Int(name); err != nil || val != value {
t.Errorf("Wrong value for %s: Got %d; Want %d", name, val, value)
}
}
if val, err := ps.Int("noKey"); err == nil {
t.Errorf("Expected an error for not found key; got %d", val)
val, err := ps.Int(name)
assert.Nil(t, err)
assert.Equal(t, value, val)
}
_, err := ps.Int("noKey")
assert.NotNil(t, err)
}

func TestParams_Int64(t *testing.T) {
Expand All @@ -82,13 +81,12 @@ func TestParams_Int64(t *testing.T) {
"param3": 1,
}
for name, value := range tests {
if val, err := ps.Int64(name); err != nil || val != value {
t.Errorf("Wrong value for %s: Got %d; Want %d", name, val, value)
}
}
if val, err := ps.Int64("noKey"); err == nil {
t.Errorf("Expected an error for not found key; got %d", val)
val, err := ps.Int64(name)
assert.Nil(t, err)
assert.Equal(t, value, val)
}
_, err := ps.Int64("noKey")
assert.NotNil(t, err)
}

func TestParams_Uint64(t *testing.T) {
Expand All @@ -101,13 +99,12 @@ func TestParams_Uint64(t *testing.T) {
"param2": 1,
}
for name, value := range tests {
if val, err := ps.Uint64(name); err != nil || val != value {
t.Errorf("Wrong value for %s: Got %d; Want %d", name, val, value)
}
}
if val, err := ps.Uint64("noKey"); err == nil {
t.Errorf("Expected an error for not found key; got %d", val)
val, err := ps.Uint64(name)
assert.Nil(t, err)
assert.Equal(t, value, val)
}
_, err := ps.Uint64("noKey")
assert.NotNil(t, err)
}

func TestParams_Float(t *testing.T) {
Expand All @@ -122,13 +119,12 @@ func TestParams_Float(t *testing.T) {
"param3": 1.9,
}
for name, value := range tests {
if val, err := ps.Float64(name); err != nil || val != value {
t.Errorf("Wrong value for %s: Got %f; Want %f", name, val, value)
}
}
if val, err := ps.Float64("noKey"); err == nil {
t.Errorf("Expected an error for not found key; got %f", val)
val, err := ps.Float64(name)
assert.Nil(t, err)
assert.Equal(t, value, val)
}
_, err := ps.Float64("noKey")
assert.NotNil(t, err)
}

func TestParams_Bool(t *testing.T) {
Expand Down Expand Up @@ -161,11 +157,10 @@ func TestParams_Bool(t *testing.T) {
"param12": false,
}
for name, value := range tests {
if val, err := ps.Bool(name); err != nil || val != value {
t.Errorf("Wrong value for %s: Got %t; Want %t", name, val, value)
}
}
if val, err := ps.Bool("noKey"); err == nil {
t.Errorf("Expected an error for not found key; got %t", val)
val, err := ps.Bool(name)
assert.Nil(t, err)
assert.Equal(t, value, val)
}
_, err := ps.Bool("noKey")
assert.NotNil(t, err)
}
22 changes: 7 additions & 15 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package clevergo
import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

type cleanPathTest struct {
Expand Down Expand Up @@ -67,12 +69,8 @@ var cleanTests = []cleanPathTest{

func TestPathClean(t *testing.T) {
for _, test := range cleanTests {
if s := CleanPath(test.path); s != test.result {
t.Errorf("CleanPath(%q) = %q, want %q", test.path, s, test.result)
}
if s := CleanPath(test.result); s != test.result {
t.Errorf("CleanPath(%q) = %q, want %q", test.result, s, test.result)
}
assert.Equal(t, test.result, CleanPath(test.path))
assert.Equal(t, test.result, CleanPath(test.result))
}
}

Expand All @@ -83,9 +81,7 @@ func TestPathCleanMallocs(t *testing.T) {

for _, test := range cleanTests {
allocs := testing.AllocsPerRun(100, func() { CleanPath(test.result) })
if allocs > 0 {
t.Errorf("CleanPath(%q): %v allocs, want zero", test.result, allocs)
}
assert.Equal(t, float64(0), allocs)
}
}

Expand Down Expand Up @@ -125,12 +121,8 @@ func TestPathCleanLong(t *testing.T) {
cleanTests := genLongPaths()

for _, test := range cleanTests {
if s := CleanPath(test.path); s != test.result {
t.Errorf("CleanPath(%q) = %q, want %q", test.path, s, test.result)
}
if s := CleanPath(test.result); s != test.result {
t.Errorf("CleanPath(%q) = %q, want %q", test.result, s, test.result)
}
assert.Equal(t, test.result, CleanPath(test.path))
assert.Equal(t, test.result, CleanPath(test.result))
}
}

Expand Down
Loading

0 comments on commit 6a13d0f

Please sign in to comment.