diff --git a/env/env_test.go b/env/env_test.go index 7398100..995f95b 100644 --- a/env/env_test.go +++ b/env/env_test.go @@ -2,33 +2,19 @@ package env import ( "cmp" + "github.com/stretchr/testify/assert" "reflect" - "slices" "testing" ) func test[X comparable](t *testing.T, wantType string, got X, want X) { - gotType := reflect.TypeOf(got).String() - if gotType != wantType { - t.Errorf("got type %s, want %s", gotType, wantType) - return - } - - if got != want { - t.Errorf("got %v, wanted %v", got, want) - } + assert.Equal(t, wantType, reflect.TypeOf(got).String()) + assert.Equal(t, want, got) } func testSlice[X ~[]E, E cmp.Ordered](t *testing.T, wantType string, got X, want X) { - gotType := reflect.TypeOf(got).String() - if gotType != wantType { - t.Errorf("got type %s, want %s", gotType, wantType) - return - } - - if slices.Compare(got, want) != 0 { - t.Errorf("got %v, wanted %v", got, want) - } + assert.Equal(t, wantType, reflect.TypeOf(got).String()) + assert.Equal(t, want, got) } // String diff --git a/http/http_test.go b/http/http_test.go index 56e229b..9e50e10 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -1,6 +1,9 @@ package http -import "testing" +import ( + "github.com/stretchr/testify/assert" + "testing" +) type User struct { ID int `json:"id"` @@ -35,20 +38,13 @@ func TestGet(t *testing.T) { todo := Todo{} err := Get("https://dummyjson.com/todos/1", &todo) - if err != nil { - t.Error(err) - } - - if todo.ID != 1 { - t.Error("todo id not equal 1") - } + assert.Nil(t, err) + assert.Equal(t, todo.ID, 1) } func TestNilVGet(t *testing.T) { err := Get("https://dummyjson.com/todos/1", nil) - if err != nil { - t.Error(err) - } + assert.Nil(t, err) } func TestGetWithToken(t *testing.T) { @@ -59,25 +55,15 @@ func TestGetWithToken(t *testing.T) { } err := Post("https://dummyjson.com/auth/login", &body, &user) - if err != nil { - t.Error(err) - } - - if user.ID != 15 { - t.Error("user id not equal 15") - } + assert.Nil(t, err) + assert.Equal(t, user.ID, 15) token := user.Token user = User{} err = GetWithToken("https://dummyjson.com/auth/me", token, &user) - if err != nil { - t.Error(err) - } - - if user.ID != 15 { - t.Error("user id not equal 15") - } + assert.Nil(t, err) + assert.Equal(t, user.ID, 15) } func TestPost(t *testing.T) { @@ -89,19 +75,8 @@ func TestPost(t *testing.T) { } err := Post("https://dummyjson.com/todos/add", &body, &todo) - if err != nil { - t.Error(err) - } - - if todo.Todo != "Test" { - t.Error("todo name not equal Test") - } - - if !todo.Completed { - t.Error("todo completed not equal true") - } - - if todo.UserID != 5 { - t.Error("todo user id not equal 5") - } + assert.Nil(t, err) + assert.Equal(t, todo.Todo, "Test") + assert.True(t, todo.Completed) + assert.Equal(t, todo.UserID, 5) } diff --git a/output/output_test.go b/output/output_test.go index 595b203..cf45dca 100644 --- a/output/output_test.go +++ b/output/output_test.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "github.com/goccy/go-json" + "github.com/stretchr/testify/assert" "github.com/valyala/fasthttp" "github.com/valyala/fasthttp/fasthttputil" "testing" @@ -45,10 +46,7 @@ func TestCORSOptions(t *testing.T) { CORSOptions(ctx) }) - contentType := string(resp.Header.Peek("Content-Type")) - if contentType != `text/html` { - t.Errorf("content-type: %s", contentType) - } + assert.Equal(t, string(resp.Header.Peek("Content-Type")), "text/html") } func TestOutputJson(t *testing.T) { @@ -56,10 +54,7 @@ func TestOutputJson(t *testing.T) { OutputJson(ctx, 200, out{Code: 123, Message: "test"}) }) - body := string(resp.Body()) - if body != `{"code":123,"message":"test"}` { - t.Errorf("body: %s", body) - } + assert.Equal(t, string(resp.Body()), `{"code":123,"message":"test"}`) } func TestJsonNoIndent(t *testing.T) { @@ -67,10 +62,7 @@ func TestJsonNoIndent(t *testing.T) { JsonNoIndent(ctx, 200, out{Code: 123, Message: "test"}) }) - body := string(resp.Body()) - if body != `{"code":123,"message":"test"}` { - t.Errorf("body: %s", body) - } + assert.Equal(t, string(resp.Body()), `{"code":123,"message":"test"}`) } func TestJsonMessageResult(t *testing.T) { @@ -78,10 +70,7 @@ func TestJsonMessageResult(t *testing.T) { JsonMessageResult(ctx, 200, "test") }) - body := string(resp.Body()) - if body != `{"code":200,"message":"test"}` { - t.Errorf("body: %s", body) - } + assert.Equal(t, string(resp.Body()), `{"code":200,"message":"test"}`) } func getResult() []byte { @@ -101,10 +90,7 @@ func TestFprint(t *testing.T) { } }) - body := string(resp.Body()) - if body != `{"code":200,"message":"test"}` { - t.Errorf("body: %s", body) - } + assert.Equal(t, string(resp.Body()), `{"code":200,"message":"test"}`) } func TestWrite(t *testing.T) { @@ -116,10 +102,7 @@ func TestWrite(t *testing.T) { } }) - body := string(resp.Body()) - if body != `{"code":200,"message":"test"}` { - t.Errorf("body: %s", body) - } + assert.Equal(t, string(resp.Body()), `{"code":200,"message":"test"}`) } func BenchmarkFprint(b *testing.B) { diff --git a/validator/validator_test.go b/validator/validator_test.go index f0168ec..7250aca 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -1,68 +1,48 @@ package validator -import "testing" +import ( + "github.com/stretchr/testify/assert" + "testing" +) func TestIs(t *testing.T) { - if !Is("email@example.com", "required,email") { - t.Error("Expected true, got false") - } + assert.True(t, Is("email@example.com", "required,email")) } func TestInSlice(t *testing.T) { - if !InSlice("ABC", []string{"ABC", "DEF"}) { - t.Error("Expected true, got false") - } - - if InSlice("NOO", []string{"ABC", "DEF"}) { - t.Error("Expected false, got true") - } + assert.True(t, InSlice("ABC", []string{"ABC", "DEF"})) + assert.False(t, InSlice("NOO", []string{"ABC", "DEF"})) } func TestIsExchange(t *testing.T) { - if !IsExchange("binance") { - t.Error("Expected true, got false") - } + assert.True(t, IsExchange("binance")) } func TestIsExchangeWith(t *testing.T) { - if !IsExchangeWith("myexchange", []string{"myexchange"}) { - t.Error("Expected true, got false") - } + assert.True(t, IsExchangeWith("myexchange", []string{"myexchange"})) } func TestIsExchangeWithInit(t *testing.T) { InitExchangeList([]string{"ownexchange"}) - if !IsExchange("ownexchange") { - t.Error("Expected true, got false") - } + assert.True(t, IsExchange("ownexchange")) } func TestIsSymbol(t *testing.T) { - if !IsSymbol("BTC/USDT") { - t.Error("Expected true, got false") - } + assert.True(t, IsSymbol("BTC/USDT")) } func TestIsSymbolWith(t *testing.T) { - if !IsSymbolWith("BTC-USDT", "-") { - t.Error("Expected true, got false") - } + assert.True(t, IsSymbolWith("BTC-USDT", "-")) } func TestIsBuyerMakerStr(t *testing.T) { - if !IsBuyerMaker("buy") { - t.Error("Expected true, got false") - } + assert.True(t, IsBuyerMaker("buy")) } func TestIsBuyerMakerOC(t *testing.T) { - if !IsBuyerMakerOC(2.0, 3.0) { - t.Error("Expected true, got false") - } + assert.True(t, IsBuyerMakerOC(2.0, 3.0)) } func TestIsEmpty(t *testing.T) { - if !IsEmpty(" ") { - t.Error("Expected true, got false") - } + assert.True(t, IsEmpty(" ")) }