Skip to content

Commit

Permalink
✅ test: update and add more tests for arr,cli, errorx package
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 24, 2023
1 parent e5a13ce commit 5c2e10b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 13 deletions.
10 changes: 0 additions & 10 deletions arrutil/collection.go
Expand Up @@ -2,7 +2,6 @@ package arrutil

import (
"errors"
"reflect"

"github.com/gookit/goutil/comdef"
"github.com/gookit/goutil/reflects"
Expand Down Expand Up @@ -99,15 +98,6 @@ func TwowaySearch[T any](data []T, item T, fn Comparer[T]) (int, error) {
return -1, ErrElementNotFound
}

// MakeEmptySlice Create a new slice with the elements of the source that satisfy the predicate.
//
// itemType: the type of the elements in the source.
// returns: the new slice.
func MakeEmptySlice(itemType reflect.Type) any {
ret := reflect.MakeSlice(reflect.SliceOf(itemType), 0, 0).Interface()
return ret
}

// CloneSlice Clone a slice.
//
// data: the slice to clone.
Expand Down
4 changes: 3 additions & 1 deletion arrutil/collection_test.go
Expand Up @@ -40,9 +40,11 @@ func TestDifferencesShouldPassed(t *testing.T) {
data := []string{"a", "b", "c"}
result := arrutil.Differences[string](data, []string{"a", "b"}, arrutil.StringEqualsComparer)
assert.Eq(t, []string{"c"}, result)

result = arrutil.Differences[string]([]string{"a", "b"}, data, arrutil.StringEqualsComparer)
assert.Eq(t, []string{"c"}, result)
result = arrutil.Differences[string]([]string{"a", "b", "d"}, data, arrutil.ReflectEqualsComparer[string])

result = arrutil.Diff([]string{"a", "b", "d"}, data, arrutil.ReflectEqualsComparer[string])
assert.Eq(t, 2, len(result))
}

Expand Down
4 changes: 4 additions & 0 deletions arrutil/list_test.go
Expand Up @@ -57,6 +57,10 @@ func TestStrings_methods(t *testing.T) {
assert.Eq(t, "a", ss.First())
assert.Eq(t, "b", ss.Last())

ss = arrutil.Strings{"b", "a"}
ss.Sort()
assert.Eq(t, "a", ss.First())

ss = arrutil.Strings{}
assert.Eq(t, "default1", ss.First("default1"))
assert.Eq(t, "default2", ss.Last("default2"))
Expand Down
3 changes: 3 additions & 0 deletions cliutil/cliutil_test.go
Expand Up @@ -91,6 +91,9 @@ func TestWorkdir(t *testing.T) {
assert.NotEmpty(t, cliutil.BinFile())
assert.NotEmpty(t, cliutil.BinName())
fmt.Println(cliutil.GetTermSize())
// repeat call
w, h := cliutil.GetTermSize()
fmt.Println(w, h)
}

func TestColorPrint(t *testing.T) {
Expand Down
11 changes: 9 additions & 2 deletions cliutil/read_test.go
Expand Up @@ -5,13 +5,13 @@ import (
"os"
"testing"

"github.com/gookit/goutil/byteutil"
"github.com/gookit/goutil/cliutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/goutil/testutil/fakeobj"
)

func TestRead_cases(t *testing.T) {
buf := byteutil.NewBuffer()
buf := fakeobj.NewReader()
cliutil.Input = buf
defer func() {
cliutil.Input = os.Stdin
Expand All @@ -23,6 +23,13 @@ func TestRead_cases(t *testing.T) {
fmt.Println("ans:", ans)
assert.NoError(t, err)
assert.Equal(t, "inhere", ans)

// error
buf.SetErrOnRead()
_, err = cliutil.ReadInput("hi, your name? ")
assert.Error(t, err)
fmt.Println(err)
buf.ErrOnRead = false
})

// test ReadLine
Expand Down
2 changes: 2 additions & 0 deletions errorx/assert_test.go
Expand Up @@ -11,6 +11,7 @@ func TestAssert_methods(t *testing.T) {
// IsFalse
assert.NoErr(t, errorx.IsFalse(false))
assert.Err(t, errorx.IsFalse(true))
assert.Err(t, errorx.IsFalse(true, "error msg"))

// IsTrue
assert.NoErr(t, errorx.IsTrue(true))
Expand All @@ -25,4 +26,5 @@ func TestAssert_methods(t *testing.T) {
// NotIn
assert.NoErr(t, errorx.NotIn(4, []int{1, 2, 3}))
assert.Err(t, errorx.NotIn(1, []int{1, 2, 3}))
assert.Err(t, errorx.NotIn(1, []int{1, 2, 3}, "error msg"))
}
1 change: 1 addition & 0 deletions errorx/errors_test.go
Expand Up @@ -61,4 +61,5 @@ func TestErrors_usage(t *testing.T) {
assert.False(t, es.IsEmpty())
assert.NotEmpty(t, es.Error())
assert.Err(t, es.ErrorOrNil())
assert.Err(t, es.First())
}

0 comments on commit 5c2e10b

Please sign in to comment.