Skip to content

Commit

Permalink
✅ test: add more unit test cases for sys, test, reflects
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 11, 2023
1 parent 4768e7d commit 197c22d
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 16 deletions.
6 changes: 6 additions & 0 deletions errorx/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func TestErrorR_usage(t *testing.T) {
assert.False(t, err.IsSuc())
assert.True(t, err.IsFail())
assert.NotEmpty(t, err.String())

err = errorx.Failf(1301, "fail %s", "msg")
assert.Eq(t, 1301, err.Code())
assert.False(t, err.IsSuc())
}

func TestErrMap_usage(t *testing.T) {
Expand All @@ -43,6 +47,7 @@ func TestErrMap_usage(t *testing.T) {
em["err1"] = errorx.Raw("this is error1")
assert.False(t, em.IsEmpty())
assert.NotEmpty(t, em.Error())
assert.Err(t, em.ErrorOrNil())
}

func TestErrors_usage(t *testing.T) {
Expand All @@ -54,4 +59,5 @@ func TestErrors_usage(t *testing.T) {
es = append(es, errorx.Raw("this is error1"))
assert.False(t, es.IsEmpty())
assert.NotEmpty(t, es.Error())
assert.Err(t, es.ErrorOrNil())
}
4 changes: 2 additions & 2 deletions errorx/errorx.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (e *ErrorX) Unwrap() error {
return e.prev
}

// Format error
// Format error, will output stack information.
func (e *ErrorX) Format(s fmt.State, verb rune) {
// format current error: only output on have msg
if len(e.msg) > 0 {
Expand Down Expand Up @@ -103,7 +103,7 @@ func (e *ErrorX) GoString() string {
return buf.String()
}

// Error to string, not contains stack information.
// Error msg string, not contains stack information.
func (e *ErrorX) Error() string {
var buf bytes.Buffer
e.writeMsgTo(&buf)
Expand Down
23 changes: 22 additions & 1 deletion errorx/errorx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestWithPrev_errorx_l2(t *testing.T) {
err1 := returnXErrL2("first error message")
assert.Err(t, err1)

err2 := errorx.WithPrev(err1, "second error message")
err2 := errorx.WithPrevf(err1, "second error %s", "message")
assert.Err(t, err2)
assert.True(t, errorx.Has(err2, err1))
assert.True(t, errorx.Is(err2, err1))
Expand Down Expand Up @@ -243,6 +243,27 @@ func TestWrapf(t *testing.T) {
fmt.Println(err.Error())
}

func TestErrorX_Format(t *testing.T) {
err := errorx.New("first error message")
err = errorx.Wrap(err, "second error message")
err = errorx.Wrap(err, "third error message")
assert.Err(t, err)
fmt.Println(err)

ex, ok := errorx.ToErrorX(err)
assert.True(t, ok)

s := ex.String()
// fmt.Println(s)
assert.StrContains(t, s, "third error message")
assert.StrContains(t, s, "Previous: second error message")
assert.StrContains(t, s, "Previous: first error message")

err = ex.Cause()
assert.Err(t, err)
assert.Eq(t, "first error message", err.Error())
}

type MyError struct {
Msg string
}
Expand Down
6 changes: 6 additions & 0 deletions errorx/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func Unwrap(err error) error {
// Previous alias of Unwrap()
func Previous(err error) error { return Unwrap(err) }

// IsErrorX check
func IsErrorX(err error) (ok bool) {
_, ok = err.(*ErrorX)
return
}

// ToErrorX convert check
func ToErrorX(err error) (ex *ErrorX, ok bool) {
ex, ok = err.(*ErrorX)
Expand Down
23 changes: 15 additions & 8 deletions reflects/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@ package reflects

import "reflect"

// BKind base data kind type
type BKind uint
// BKind base data kind type, alias of reflect.Kind
//
// Diff with reflect.Kind:
// - Int contains all intX types
// - Uint contains all uintX types
// - Float contains all floatX types
// - Array for array and slice types
// - Complex contains all complexX types
type BKind = reflect.Kind

// base kinds
const (
// Int for all intX types
Int = BKind(reflect.Int)
Int = reflect.Int
// Uint for all uintX types
Uint = BKind(reflect.Uint)
Uint = reflect.Uint
// Float for all floatX types
Float = BKind(reflect.Float32)
Float = reflect.Float32
// Array for array,slice types
Array = BKind(reflect.Array)
Array = reflect.Array
// Complex for all complexX types
Complex = BKind(reflect.Complex64)
Complex = reflect.Complex64
)

// ToBaseKind convert reflect.Kind to base kind
Expand All @@ -39,7 +46,7 @@ func ToBKind(kind reflect.Kind) BKind {
return Array
default:
// like: string, map, struct, ptr, func, interface ...
return BKind(kind)
return kind
}
}

Expand Down
10 changes: 8 additions & 2 deletions reflects/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func TestValueOf(t *testing.T) {
assert.Eq(t, uint64(23), rv.Uint())
assert.Eq(t, int64(23), rv.Int())
assert.False(t, rv.HasChild())
assert.Eq(t, reflects.Uint, rv.Type().BaseKind())

rv = reflects.ValueOf(reflect.ValueOf("abc"))
assert.Eq(t, "abc", rv.String())
assert.Eq(t, "abc", rv.Elem().String())
assert.Eq(t, reflect.String, rv.Type().BaseKind())

rv = reflects.ValueOf("abc")
assert.Panics(t, func() {
Expand All @@ -38,7 +44,7 @@ func TestValue_Indirect(t *testing.T) {
}

rv := reflects.ValueOf(&user{Age: 23})
assert.Eq(t, reflects.BKind(reflect.Ptr), rv.BKind())
assert.Eq(t, reflect.Ptr, rv.BKind())
assert.False(t, rv.HasChild())

rv1 := reflects.Elem(rv.Value)
Expand All @@ -51,7 +57,7 @@ func TestValue_Indirect(t *testing.T) {

rv = rv.Indirect()
assert.True(t, rv.HasChild())
assert.Eq(t, reflects.BKind(reflect.Struct), rv.BKind())
assert.Eq(t, reflect.Struct, rv.BKind())

rv1 = reflects.Elem(reflect.ValueOf("abc"))
assert.Eq(t, reflect.String, rv1.Kind())
Expand Down
2 changes: 2 additions & 0 deletions stdio/stdio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func TestNewIOReader(t *testing.T) {
}

func TestWriteBytes(t *testing.T) {
stdio.WriteByte('a')
stdio.WritelnBytes([]byte("bc "))
stdio.WriteBytes([]byte("hi,"))
stdio.WriteString("inhere.")
stdio.Writeln("welcome")
Expand Down
2 changes: 1 addition & 1 deletion sysutil/sysutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestBasic_usage(t *testing.T) {
assert.NotEmpty(t, sysutil.BinDir())
assert.NotEmpty(t, sysutil.BinDir())
assert.NotEmpty(t, sysutil.BinName())
assert.NotEmpty(t, sysutil.BinFile())
}

Expand Down
6 changes: 4 additions & 2 deletions testutil/httpmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func BuildEchoReply(r *http.Request) *EchoReply {
if method == "POST" || method == "PUT" || method == "PATCH" {
// get form data
_ = r.ParseForm()
form = stringsMapToAnyMap(r.Form)
form = stringsMapToAnyMap(r.PostForm)

// get form files
if r.MultipartForm != nil {
Expand All @@ -199,7 +199,9 @@ func BuildEchoReply(r *http.Request) *EchoReply {
}

// get body data
if r.Body != nil {
if len(r.PostForm) > 0 {
bodyStr = r.PostForm.Encode()
} else if r.Body != nil {
// defer r.Body.Close()
body, _ := io.ReadAll(r.Body)
bodyStr = string(body)
Expand Down

0 comments on commit 197c22d

Please sign in to comment.