You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nmap/convert_test.go

101 lines
2.1 KiB
Go

package nmap_test
import (
"fmt"
"git.noahlan.cn/noahlan/ntool/nmap"
"git.noahlan.cn/noahlan/ntool/ntest/assert"
"testing"
)
func TestKeyToLower(t *testing.T) {
src := map[string]string{"A": "v0"}
ret := nmap.KeyToLower(src)
assert.Contains(t, ret, "a")
assert.NotContains(t, ret, "A")
}
func TestToStringMap(t *testing.T) {
src := map[string]any{"a": "v0", "b": 23}
ret := nmap.ToStringMap(src)
assert.Eq(t, ret["a"], "v0")
assert.Eq(t, ret["b"], "23")
keys := []string{"key0", "key1"}
mp := nmap.CombineToSMap(keys, []string{"val0", "val1"})
assert.Len(t, mp, 2)
assert.Eq(t, "val0", mp.Str("key0"))
}
func TestToAnyMap(t *testing.T) {
src := map[string]string{"a": "v0", "b": "23"}
mp := nmap.ToAnyMap(src)
assert.Len(t, mp, 2)
assert.Eq(t, "v0", mp["a"])
src1 := map[string]any{"a": "v0", "b": "23"}
mp = nmap.ToAnyMap(src1)
assert.Len(t, mp, 2)
assert.Eq(t, "v0", mp["a"])
_, err := nmap.TryAnyMap(123)
assert.Err(t, err)
}
func TestHTTPQueryString(t *testing.T) {
src := map[string]any{"a": "v0", "b": 23}
str := nmap.HTTPQueryString(src)
fmt.Println(str)
assert.Contains(t, str, "b=23")
assert.Contains(t, str, "a=v0")
}
func TestToString2(t *testing.T) {
src := map[string]any{"a": "v0", "b": 23}
s := nmap.ToString2(src)
assert.Contains(t, s, "b:23")
assert.Contains(t, s, "a:v0")
}
func TestToString(t *testing.T) {
src := map[string]any{"a": "v0", "b": 23}
s := nmap.ToString(src)
//dump.P(s)
assert.Contains(t, s, "b:23")
assert.Contains(t, s, "a:v0")
s = nmap.ToString(nil)
assert.Eq(t, "", s)
s = nmap.ToString(map[string]any{})
assert.Eq(t, "{}", s)
s = nmap.ToString(map[string]any{"": nil})
assert.Eq(t, "{:}", s)
}
func TestFlatten(t *testing.T) {
data := map[string]any{
"name": "inhere",
"age": 234,
"top": map[string]any{
"sub0": "val0",
"sub1": []string{"val1-0", "val1-1"},
},
}
mp := nmap.Flatten(data)
assert.ContainsKeys(t, mp, []string{"age", "name", "top.sub0", "top.sub1[0]", "top.sub1[1]"})
assert.Nil(t, nmap.Flatten(nil))
assert.NotPanics(t, func() {
nmap.FlatWithFunc(nil, nil)
})
}