Skip to content

Commit

Permalink
👔 up: test,base - update some test and base func
Browse files Browse the repository at this point in the history
- upgrade deps to latest version
  • Loading branch information
inhere committed Feb 4, 2024
1 parent 66e926c commit ac5dfa1
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 15 deletions.
29 changes: 28 additions & 1 deletion basefn/basefn.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,41 @@ func MustOK(err error) {
}
}

// Must if error is not empty, will panic
// Must return like (v, error). will panic on error, otherwise return v.
//
// Usage:
//
// // old
// v, err := fn()
// if err != nil {
// panic(err)
// }
//
// // new
// v := goutil.Must(fn())
func Must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}

// MustIgnore for return like (v, error). Ignore return v and will panic on error.
//
// Useful for io, file operation func: (n int, err error)
//
// Usage:
//
// // old
// _, err := fn()
// if err != nil {
// panic(err)
// }
//
// // new
// basefn.MustIgnore(fn())
func MustIgnore(_ any, err error) { PanicErr(err) }

// ErrOnFail return input error on cond is false, otherwise return nil
func ErrOnFail(cond bool, err error) error {
return OrError(cond, err)
Expand Down
9 changes: 9 additions & 0 deletions basefn/basefn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@ func TestPanicIf(t *testing.T) {
}

func TestPanicErr(t *testing.T) {
basefn.MustOK(nil)
basefn.PanicErr(nil)
assert.Panics(t, func() {
basefn.PanicErr(errors.New("a error"))
})

// must ignore
assert.NotPanics(t, func() {
basefn.MustIgnore(nil, nil)
})
assert.Panics(t, func() {
basefn.MustIgnore(nil, errors.New("a error"))
})
}

func TestPanicf(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.19

require (
github.com/gookit/color v1.5.4
golang.org/x/sync v0.5.0
golang.org/x/sys v0.15.0
golang.org/x/term v0.15.0
golang.org/x/sync v0.6.0
golang.org/x/sys v0.16.0
golang.org/x/term v0.16.0
golang.org/x/text v0.14.0
)

Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
27 changes: 23 additions & 4 deletions testutil/httpmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewHttpRequest(method, path string, data *MD) *http.Request {
return NewHTTPRequest(method, path, data)
}

// NewHTTPRequest for http testing
// NewHTTPRequest quick create request for http testing
// Usage:
//
// req := NewHttpRequest("GET", "/path", nil)
Expand Down Expand Up @@ -130,6 +130,21 @@ func (r *EchoReply) ContentType() string {
return r.Headers["Content-Type"].(string)
}

// EchoServer for testing http request.
type EchoServer struct {
*httptest.Server
}

// HostAddr get host address. eg: 127.0.0.1:8999
func (s *EchoServer) HostAddr() string {
return s.Listener.Addr().String()
}

// HTTPHost get http host address. eg: http://127.0.0.1:8999
func (s *EchoServer) HTTPHost() string {
return "http://" + s.HostAddr()
}

// NewEchoServer create an echo server for testing.
//
// Usage on testing:
Expand All @@ -140,7 +155,7 @@ func (r *EchoReply) ContentType() string {
// // create server
// s := testutil.NewEchoServer()
// defer s.Close()
// testSrvAddr = "http://" + s.Listener.Addr().String()
// testSrvAddr = s.HTTPHost()
// fmt.Println("Test server listen on:", testSrvAddr)
//
// m.Run()
Expand All @@ -150,8 +165,8 @@ func (r *EchoReply) ContentType() string {
// res := http.Get(testSrvAddr)
// rpl := testutil.ParseRespToReply(res)
// // assert ...
func NewEchoServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
func NewEchoServer() *EchoServer {
hs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Server", "goutil/echo-server")
w.WriteHeader(http.StatusOK)
Expand All @@ -164,6 +179,10 @@ func NewEchoServer() *httptest.Server {
_, _ = w.Write([]byte(`{"error": "encode error"}`))
}
}))

return &EchoServer{
Server: hs,
}
}

// BuildEchoReply build reply body data
Expand Down
2 changes: 1 addition & 1 deletion testutil/httpmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestMain(m *testing.M) {
s := testutil.NewEchoServer()
defer s.Close()

testSrvAddr = "http://" + s.Listener.Addr().String()
testSrvAddr = s.HTTPHost()
fmt.Println("server addr:", testSrvAddr)

m.Run()
Expand Down

0 comments on commit ac5dfa1

Please sign in to comment.