Skip to content

Commit

Permalink
✅ up(sys,env,map): add more tests case for util func
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 6, 2023
1 parent 67cbae1 commit 392536c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
34 changes: 34 additions & 0 deletions envutil/envutil_test.go
Expand Up @@ -3,6 +3,7 @@ package envutil
import (
"testing"

"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)
Expand Down Expand Up @@ -63,3 +64,36 @@ func TestParseEnvValue(t *testing.T) {
is.Eq("abc/def_val", ParseEnvValue(rVal))
})
}

func TestSetEnvs(t *testing.T) {
envMp := map[string]string{
"FirstEnv": "abc",
"SecondEnv": "def",
}
keys := maputil.Keys(envMp)
for key := range envMp {
assert.Empty(t, Getenv(key))
}

// SetEnvs
SetEnvs(maputil.SMap(envMp).ToKVPairs()...)
for key, val := range envMp {
assert.Eq(t, val, Getenv(key))
}

UnsetEnvs(keys...)
for key := range envMp {
assert.Empty(t, Getenv(key))
}

// SetEnvMap
SetEnvMap(envMp)
for key, val := range envMp {
assert.Eq(t, val, Getenv(key))
}

UnsetEnvs(keys...)
for key := range envMp {
assert.Empty(t, Getenv(key))
}
}
9 changes: 9 additions & 0 deletions maputil/smap.go
Expand Up @@ -111,6 +111,15 @@ func (m SMap) Values() []string {
return ss
}

// ToKVPairs slice convert. eg: {k1:v1,k2:v2} => {k1,v1,k2,v2}
func (m SMap) ToKVPairs() []string {
pairs := make([]string, 0, len(m)*2)
for k, v := range m {
pairs = append(pairs, k, v)
}
return pairs
}

// String data to string
func (m SMap) String() string {
return ToString2(m)
Expand Down
13 changes: 13 additions & 0 deletions maputil/smap_test.go
@@ -1,6 +1,7 @@
package maputil_test

import (
"fmt"
"testing"

"github.com/gookit/goutil/maputil"
Expand Down Expand Up @@ -47,3 +48,15 @@ func TestSMap_usage(t *testing.T) {
assert.Eq(t, "", mp.Str("notExists"))
assert.Empty(t, mp.Ints("notExists"))
}

func TestSMap_ToKVPairs(t *testing.T) {
mp := maputil.SMap{
"k1": "23",
"k2": "ab",
}
arr := mp.ToKVPairs()
assert.Len(t, arr, 4)
str := fmt.Sprint(arr)
assert.StrContains(t, str, "k1 23")
assert.StrContains(t, str, "k2 ab")
}
3 changes: 2 additions & 1 deletion sysutil/sysenv_test.go
Expand Up @@ -13,12 +13,13 @@ import (
func TestSysenv_common(t *testing.T) {
ss := sysutil.EnvPaths()
assert.NotEmpty(t, ss)

assert.NotEmpty(t, sysutil.Environ())

ss = sysutil.SearchPath("go", 3)
assert.NotEmpty(t, ss)
// dump.P(ss)
ss = sysutil.SearchPath("o", 3)
assert.NotEmpty(t, ss)
}

func TestCurrentShell(t *testing.T) {
Expand Down

0 comments on commit 392536c

Please sign in to comment.