Skip to content

Commit

Permalink
✅ test: arr,reflects - add more unit cases
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 24, 2023
1 parent a0ac609 commit f0dea38
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 17 deletions.
4 changes: 1 addition & 3 deletions arrutil/collection.go
Expand Up @@ -104,9 +104,7 @@ func TwowaySearch[T any](data []T, item T, fn Comparer[T]) (int, error) {
// returns: the cloned slice.
func CloneSlice[T any](data []T) []T {
nt := make([]T, 0, len(data))
for _, v := range data {
nt = append(nt, v)
}
nt = append(nt, data...)
return nt
}

Expand Down
4 changes: 2 additions & 2 deletions cliutil/cmdline/builder.go
Expand Up @@ -55,13 +55,13 @@ func (b *LineBuilder) WriteString(a string) (int, error) {
if pos := strings.IndexByte(a, '"'); pos > -1 {
quote = '\''
// fix: a = `--pretty=format:"one two three"`
if pos > 0 && '"' == a[len(a)-1] {
if pos > 0 && a[len(a)-1] == '"' {
quote = 0
}
} else if pos := strings.IndexByte(a, '\''); pos > -1 {
quote = '"'
// fix: a = "--pretty=format:'one two three'"
if pos > 0 && '\'' == a[len(a)-1] {
if pos > 0 && a[len(a)-1] == '\'' {
quote = 0
}
} else if a == "" || strings.ContainsRune(a, ' ') {
Expand Down
14 changes: 4 additions & 10 deletions reflects/type.go
Expand Up @@ -55,6 +55,8 @@ type Type interface {
reflect.Type
// BaseKind value
BaseKind() BKind
// RealType returns a ptr type's real type. otherwise, will return self.
RealType() reflect.Type
// SafeElem returns a type's element type. otherwise, will return self.
SafeElem() reflect.Type
}
Expand All @@ -81,18 +83,10 @@ func (t *xType) BaseKind() BKind {

// RealType returns a ptr type's real type. otherwise, will return self.
func (t *xType) RealType() reflect.Type {
if t.Kind() == reflect.Pointer {
return t.Type.Elem()
}
return t.Type
return TypeReal(t.Type)
}

// SafeElem returns the array, slice, chan, map type's element type. otherwise, will return self.
func (t *xType) SafeElem() reflect.Type {
switch t.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
return t.Type.Elem()
default:
return t.Type
}
return TypeElem(t.Type)
}
15 changes: 15 additions & 0 deletions reflects/type_test.go
Expand Up @@ -22,4 +22,19 @@ func TestTypeOf(t *testing.T) {

assert.Eq(t, reflect.Int64, rt.Kind())
assert.Eq(t, reflects.Int, rt.BaseKind())

assert.Eq(t, reflect.Int64, rt.RealType().Kind())
assert.Eq(t, reflect.Int64, rt.SafeElem().Kind())

s := new(string)
*s = "abc"
rt = reflects.TypeOf(s)
assert.Eq(t, reflect.Pointer, rt.Kind())
assert.Eq(t, reflect.String, rt.RealType().Kind())
assert.Eq(t, reflect.Pointer, rt.SafeElem().Kind())

ss := []string{"abc"}
rt = reflects.TypeOf(ss)
assert.Eq(t, reflect.Slice, rt.Kind())
assert.Eq(t, reflect.String, rt.SafeElem().Kind())
}
3 changes: 1 addition & 2 deletions reflects/value.go
Expand Up @@ -31,7 +31,7 @@ func ValueOf(v any) Value {

// Indirect value. alias of the reflect.Indirect()
func (v Value) Indirect() Value {
if v.Kind() != reflect.Ptr {
if v.Kind() != reflect.Pointer {
return v
}

Expand All @@ -48,7 +48,6 @@ func (v Value) Indirect() Value {
func (v Value) Elem() Value {
if v.Kind() == reflect.Pointer || v.Kind() == reflect.Interface {
elem := v.Value.Elem()

return Value{
Value: elem,
baseKind: ToBKind(elem.Kind()),
Expand Down
7 changes: 7 additions & 0 deletions reflects/value_test.go
Expand Up @@ -22,12 +22,19 @@ func TestValueOf(t *testing.T) {
assert.Eq(t, int64(23), rv.Int())
assert.False(t, rv.HasChild())
assert.Eq(t, reflects.Uint, rv.Type().BaseKind())
assert.Eq(t, reflects.Uint, rv.Indirect().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())

// pointer
s := new(string)
*s = "abc"
rv = reflects.ValueOf(s)
assert.Eq(t, reflect.String, rv.Elem().Kind())

rv = reflects.ValueOf("abc")
assert.Panics(t, func() {
rv.Int()
Expand Down
29 changes: 29 additions & 0 deletions testutil/fakeobj/io.go
Expand Up @@ -6,6 +6,35 @@ import (
"github.com/gookit/goutil/byteutil"
)

// IOWriter only implements the io.Writer
type IOWriter struct {
Buf []byte
// ErrOnWrite return error on write, useful for testing
ErrOnWrite bool
}

// NewIOWriter instance
func NewIOWriter() *IOWriter {
return &IOWriter{
Buf: make([]byte, 0, 1024),
}
}

// Write implements
func (w *IOWriter) Write(p []byte) (n int, err error) {
if w.ErrOnWrite {
return 0, errors.New("fake write error")
}

w.Buf = append(w.Buf, p...)
return len(p), nil
}

// Reset the buffer
func (w *IOWriter) Reset() {
w.Buf = w.Buf[:0]
}

// Reader implements the io.Reader
type Reader struct {
byteutil.Buffer
Expand Down
14 changes: 14 additions & 0 deletions testutil/fakeobj/io_test.go
Expand Up @@ -7,6 +7,20 @@ import (
"github.com/gookit/goutil/testutil/fakeobj"
)

func TestIOWriter(t *testing.T) {
w := fakeobj.NewIOWriter()

m, err := w.Write([]byte("hello"))
assert.Eq(t, 5, m)
assert.NoErr(t, err)
assert.Eq(t, "hello", string(w.Buf))

w.Reset()
w.ErrOnWrite = true
_, err = w.Write([]byte("hello"))
assert.Err(t, err)
}

func TestNewReader(t *testing.T) {
tr := fakeobj.NewReader()
tr.Write([]byte("hello"))
Expand Down

0 comments on commit f0dea38

Please sign in to comment.