Skip to content

Commit

Permalink
✅ test: maputil - update some util func and add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 12, 2023
1 parent d075abe commit 5832f25
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
14 changes: 13 additions & 1 deletion maputil/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ func TestHasKey(t *testing.T) {
assert.False(t, maputil.HasKey("abc", "not-exist"))
}

func TestHasOneKey(t *testing.T) {
var mp any = map[string]string{"key0": "val0", "key1": "def"}

ok, key := maputil.HasOneKey(mp, "key0", "not-exist")
assert.True(t, ok)
assert.Eq(t, "key0", key)

ok, key = maputil.HasOneKey("abc", "not-exist")
assert.Nil(t, key)
assert.False(t, ok)
}

func TestHasAllKeys(t *testing.T) {
var mp any = map[string]string{"key0": "val0", "key1": "def"}
ok, noKey := maputil.HasAllKeys(mp, "key0")
Expand All @@ -29,6 +41,6 @@ func TestHasAllKeys(t *testing.T) {
assert.False(t, ok)
assert.Eq(t, "not-exist", noKey)

ok, _ = maputil.HasAllKeys(mp, "invalid-map", "not-exist")
ok, _ = maputil.HasAllKeys("invalid-map", "not-exist")
assert.False(t, ok)
}
11 changes: 11 additions & 0 deletions maputil/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package maputil_test
import (
"fmt"
"net/http"
"reflect"
"testing"

"github.com/gookit/goutil/dump"
Expand Down Expand Up @@ -97,6 +98,16 @@ func TestFlatten(t *testing.T) {
assert.ContainsKeys(t, mp, []string{"age", "name", "top.sub0", "top.sub1[0]", "top.sub1[1]"})
assert.Nil(t, maputil.Flatten(nil))

fmp := make(map[string]string)
maputil.FlatWithFunc(data, func(path string, val reflect.Value) {
fmp[path] = fmt.Sprintf("%v", val.Interface())
})
dump.P(fmp)
assert.Eq(t, "inhere", fmp["name"])
assert.Eq(t, "234", fmp["age"])
assert.Eq(t, "val0", fmp["top.sub0"])
assert.Eq(t, "val1-0", fmp["top.sub1[0]"])

assert.NotPanics(t, func() {
maputil.FlatWithFunc(nil, nil)
})
Expand Down
5 changes: 1 addition & 4 deletions maputil/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ func (d Data) StrSplit(key, sep string) []string {

// StringsByStr value get by key
func (d Data) StringsByStr(key string) []string {
if val, ok := d.GetByPath(key); ok {
return strings.Split(strutil.QuietString(val), ",")
}
return nil
return d.StrSplit(key, ",")
}

// StrMap get map[string]string value
Expand Down
19 changes: 18 additions & 1 deletion maputil/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func TestData_usage(t *testing.T) {
"k4": false,
"k5": map[string]string{"a": "b"},
"anyMp": map[string]any{"b": 23},
"k6": "23,45",
"k7": []string{"ab", "cd"},
}

assert.True(t, mp.Has("k1"))
Expand All @@ -35,6 +37,14 @@ func TestData_usage(t *testing.T) {
assert.Eq(t, "23", mp.Str("k1"))
assert.Eq(t, "ab", mp.Str("k2"))

// Strings
assert.Eq(t, []string{"ab", "cd"}, mp.Strings("k7"))
assert.Nil(t, mp.Strings("k1"))
assert.Nil(t, mp.Strings("notExists"))

// StringsByStr
assert.Eq(t, []string{"23", "45"}, mp.StringsByStr("k6"))

// set
mp.Set("new", "val1")
assert.Eq(t, "val1", mp.Str("new"))
Expand All @@ -53,9 +63,12 @@ func TestData_usage(t *testing.T) {
assert.Eq(t, 23, mp.Default("k1", 10))
assert.Eq(t, 10, mp.Default("notExists", 10))

assert.Nil(t, mp.StringMap("k1"))
assert.Nil(t, mp.StringMap("notExists"))
assert.Eq(t, map[string]string{"a": "b"}, mp.StringMap("k5"))
assert.Eq(t, map[string]string{"a": "b"}, mp.StrMap("k5"))
assert.Eq(t, map[string]string{"b": "23"}, mp.StringMap("anyMp"))

assert.NotEmpty(t, mp.String())
}

func TestData_SetByPath(t *testing.T) {
Expand All @@ -81,6 +94,10 @@ func TestData_SetByPath(t *testing.T) {
sub := mp.Sub("k5")
assert.Eq(t, "v0", sub.Get("a"))
assert.Eq(t, "v2", sub.Get("b"))

// LoadSMap
mp.LoadSMap(map[string]string{"uk2": "val2", "uk3": "val3"})
assert.Eq(t, "val2", mp.Str("uk2"))
}

func TestData_SetByPath_case2(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions maputil/smap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func TestSMap_usage(t *testing.T) {
assert.Eq(t, []string{"1", "2"}, mp.Strings("k4"))
assert.Nil(t, mp.Strings("not-exist"))

// Default
assert.Eq(t, "ab", mp.Default("k2", "abc"))
assert.Eq(t, "abc", mp.Default("notExists", "abc"))

// not exists
assert.False(t, mp.Bool("notExists"))
assert.Eq(t, 0, mp.Int("notExists"))
Expand Down

0 comments on commit 5832f25

Please sign in to comment.