Skip to content

Commit

Permalink
👔 up(str,byte): update some util func and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 8, 2023
1 parent 57a853d commit 531fa6b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
5 changes: 5 additions & 0 deletions basefn/basefunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func TestPanicf(t *testing.T) {
assert.Panics(t, func() {
basefn.Panicf("hi %s", "inhere")
})
assert.Panics(t, func() {
basefn.MustOK(errors.New("a error"))
})
}

func TestErrOnFail(t *testing.T) {
Expand All @@ -29,6 +32,8 @@ func TestErrOnFail(t *testing.T) {
func TestOrValue(t *testing.T) {
assert.Eq(t, "ab", basefn.OrValue(true, "ab", "dc"))
assert.Eq(t, "dc", basefn.OrValue(false, "ab", "dc"))
assert.Eq(t, 1, basefn.FirstOr([]int{1, 2}, 3))
assert.Eq(t, 3, basefn.FirstOr(nil, 3))
}

func TestOrReturn(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions byteutil/byteutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ func TestStrOrErr(t *testing.T) {
assert.Err(t, err)
assert.Eq(t, "", str)
}

func TestMd5(t *testing.T) {
assert.NotEmpty(t, byteutil.Md5("abc"))
assert.NotEmpty(t, byteutil.Md5([]int{12, 34}))
}
18 changes: 18 additions & 0 deletions byteutil/pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package byteutil_test

import (
"testing"

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

func TestNewChanPool(t *testing.T) {
p := byteutil.NewChanPool(10, 8, 8)

assert.Equal(t, 8, p.Width())
assert.Equal(t, 8, p.WidthCap())

p.Put([]byte("abc"))
assert.Equal(t, []byte("abc"), p.Get())
}
8 changes: 4 additions & 4 deletions cliutil/cliutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ func TestParseLine(t *testing.T) {

// exception line string.
args = cliutil.ParseLine(`./app top sub -a ddd --xx msg"`)
dump.P(args)
// dump.P(args)
assert.Len(t, args, 7)
assert.Eq(t, "msg", args[6])
assert.Eq(t, "msg\"", args[6])

args = cliutil.StringToOSArgs(`./app top sub -a ddd --xx "msg "text"`)
dump.P(args)
// dump.P(args)
assert.Len(t, args, 7)
assert.Eq(t, "msg text", args[6])
assert.Eq(t, "msg \"text", args[6])
}

func TestWorkdir(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions strutil/strutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ func Valid(ss ...string) string {
//
// strings.NewReplacer("old1", "new1", "old2", "new2").Replace(str)
func Replaces(str string, pairs map[string]string) string {
return NewReplacer(pairs).Replace(str)
}

// NewReplacer instance
func NewReplacer(pairs map[string]string) *strings.Replacer {
ss := make([]string, len(pairs)*2)
for old, newVal := range pairs {
ss = append(ss, old, newVal)
}

return strings.NewReplacer(ss...).Replace(str)
return strings.NewReplacer(ss...)
}

// PrettyJSON get pretty Json string
Expand Down

0 comments on commit 531fa6b

Please sign in to comment.