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.
153 lines
3.7 KiB
Go
153 lines
3.7 KiB
Go
1 year ago
|
package narr_test
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"git.noahlan.cn/noahlan/ntool/narr"
|
||
|
"git.noahlan.cn/noahlan/ntool/ntest/assert"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestToInt64s(t *testing.T) {
|
||
|
is := assert.New(t)
|
||
|
|
||
|
ints, err := narr.ToInt64s([]string{"1", "2"})
|
||
|
is.Nil(err)
|
||
|
is.Eq("[]int64{1, 2}", fmt.Sprintf("%#v", ints))
|
||
|
|
||
|
ints = narr.MustToInt64s([]string{"1", "2"})
|
||
|
is.Eq("[]int64{1, 2}", fmt.Sprintf("%#v", ints))
|
||
|
|
||
|
ints = narr.MustToInt64s([]any{"1", "2"})
|
||
|
is.Eq("[]int64{1, 2}", fmt.Sprintf("%#v", ints))
|
||
|
|
||
|
ints = narr.SliceToInt64s([]any{"1", "2"})
|
||
|
is.Eq("[]int64{1, 2}", fmt.Sprintf("%#v", ints))
|
||
|
|
||
|
_, err = narr.ToInt64s([]string{"a", "b"})
|
||
|
is.Err(err)
|
||
|
}
|
||
|
|
||
|
func TestToStrings(t *testing.T) {
|
||
|
is := assert.New(t)
|
||
|
|
||
|
ss, err := narr.ToStrings([]int{1, 2})
|
||
|
is.Nil(err)
|
||
|
is.Eq(`[]string{"1", "2"}`, fmt.Sprintf("%#v", ss))
|
||
|
|
||
|
ss = narr.MustToStrings([]int{1, 2})
|
||
|
is.Eq(`[]string{"1", "2"}`, fmt.Sprintf("%#v", ss))
|
||
|
|
||
|
ss = narr.MustToStrings([]any{1, 2})
|
||
|
is.Eq(`[]string{"1", "2"}`, fmt.Sprintf("%#v", ss))
|
||
|
|
||
|
ss = narr.SliceToStrings([]any{1, 2})
|
||
|
is.Eq(`[]string{"1", "2"}`, fmt.Sprintf("%#v", ss))
|
||
|
|
||
|
as := narr.StringsToSlice([]string{"1", "2"})
|
||
|
is.Eq(`[]interface {}{"1", "2"}`, fmt.Sprintf("%#v", as))
|
||
|
|
||
|
ss, err = narr.ToStrings("b")
|
||
|
is.Nil(err)
|
||
|
is.Eq(`[]string{"b"}`, fmt.Sprintf("%#v", ss))
|
||
|
|
||
|
_, err = narr.ToStrings([]any{[]int{1}, nil})
|
||
|
is.Err(err)
|
||
|
}
|
||
|
|
||
|
func TestStringsToString(t *testing.T) {
|
||
|
is := assert.New(t)
|
||
|
|
||
|
is.Eq("a,b", narr.JoinStrings(",", []string{"a", "b"}...))
|
||
|
is.Eq("a,b", narr.JoinStrings(",", []string{"a", "b"}...))
|
||
|
is.Eq("a,b", narr.JoinStrings(",", "a", "b"))
|
||
|
}
|
||
|
|
||
|
func TestAnyToString(t *testing.T) {
|
||
|
is := assert.New(t)
|
||
|
arr := [2]string{"a", "b"}
|
||
|
|
||
|
is.Eq("", narr.AnyToString(nil))
|
||
|
is.Eq("[]", narr.AnyToString([]string{}))
|
||
|
is.Eq("[a, b]", narr.AnyToString(arr))
|
||
|
is.Eq("[a, b]", narr.AnyToString([]string{"a", "b"}))
|
||
|
is.Eq("", narr.AnyToString("invalid"))
|
||
|
}
|
||
|
|
||
|
func TestSliceToString(t *testing.T) {
|
||
|
is := assert.New(t)
|
||
|
|
||
|
is.Eq("[]", narr.SliceToString(nil))
|
||
|
is.Eq("[a,b]", narr.SliceToString("a", "b"))
|
||
|
}
|
||
|
|
||
|
func TestStringsToInts(t *testing.T) {
|
||
|
is := assert.New(t)
|
||
|
|
||
|
ints, err := narr.StringsToInts([]string{"1", "2"})
|
||
|
is.Nil(err)
|
||
|
is.Eq("[]int{1, 2}", fmt.Sprintf("%#v", ints))
|
||
|
|
||
|
_, err = narr.StringsToInts([]string{"a", "b"})
|
||
|
is.Err(err)
|
||
|
|
||
|
ints = narr.StringsAsInts([]string{"1", "2"})
|
||
|
is.Eq("[]int{1, 2}", fmt.Sprintf("%#v", ints))
|
||
|
is.Nil(narr.StringsAsInts([]string{"abc"}))
|
||
|
}
|
||
|
|
||
|
func TestConvType(t *testing.T) {
|
||
|
is := assert.New(t)
|
||
|
|
||
|
// []string => []int
|
||
|
arr, err := narr.ConvType([]string{"1", "2"}, 1)
|
||
|
is.Nil(err)
|
||
|
is.Eq("[]int{1, 2}", fmt.Sprintf("%#v", arr))
|
||
|
|
||
|
// []int => []string
|
||
|
arr1, err := narr.ConvType([]int{1, 2}, "1")
|
||
|
is.Nil(err)
|
||
|
is.Eq(`[]string{"1", "2"}`, fmt.Sprintf("%#v", arr1))
|
||
|
|
||
|
// not need conv
|
||
|
arr2, err := narr.ConvType([]string{"1", "2"}, "1")
|
||
|
is.Nil(err)
|
||
|
is.Eq(`[]string{"1", "2"}`, fmt.Sprintf("%#v", arr2))
|
||
|
|
||
|
// conv error
|
||
|
arr3, err := narr.ConvType([]string{"ab", "cd"}, 1)
|
||
|
is.Err(err)
|
||
|
is.Nil(arr3)
|
||
|
}
|
||
|
|
||
|
func TestJoinSlice(t *testing.T) {
|
||
|
assert.Eq(t, "", narr.JoinSlice(","))
|
||
|
assert.Eq(t, "", narr.JoinSlice(",", nil))
|
||
|
assert.Eq(t, "a,23,b", narr.JoinSlice(",", "a", 23, "b"))
|
||
|
}
|
||
|
|
||
|
func TestCombineToMap(t *testing.T) {
|
||
|
keys := []string{"key0", "key1"}
|
||
|
|
||
|
mp := narr.CombineToMap(keys, []int{1, 2})
|
||
|
assert.Len(t, mp, 2)
|
||
|
assert.Eq(t, 1, mp["key0"])
|
||
|
assert.Eq(t, 2, mp["key1"])
|
||
|
|
||
|
mp = narr.CombineToMap(keys, []int{1})
|
||
|
assert.Len(t, mp, 1)
|
||
|
assert.Eq(t, 1, mp["key0"])
|
||
|
}
|
||
|
|
||
|
func TestCombineToSMap(t *testing.T) {
|
||
|
keys := []string{"key0", "key1"}
|
||
|
|
||
|
mp := narr.CombineToSMap(keys, []string{"val0", "val1"})
|
||
|
assert.Len(t, mp, 2)
|
||
|
assert.Eq(t, "val0", mp["key0"])
|
||
|
|
||
|
mp = narr.CombineToSMap(keys, []string{"val0"})
|
||
|
assert.Len(t, mp, 2)
|
||
|
assert.Eq(t, "val0", mp["key0"])
|
||
|
assert.Eq(t, "", mp["key1"])
|
||
|
}
|