package maputils import ( "sort" "strconv" "testing" ) func TestGetValueByPath(t *testing.T) { testMap := map[string]any{ "foo1": map[string]any{ "foo1_sub": []any{ "value1", 2, }, }, } value, err := GetValueByPath(testMap, "foo1.foo1_sub.[0]") if err != nil { t.Fatal(err) } if value.(string) != "value1" { t.Fatal("值不一致") } value, err = GetValueByPath(testMap, "foo1.foo1_sub.[1]") if err != nil { t.Fatal(err) } if value.(int) != 2 { t.Fatal("值不一致") } } func TestGetValueByKey(t *testing.T) { testMap := map[string]any{ "foo1": map[string]any{ "foo1_sub": []any{ "value1", 2, }, }, } foo1Map, ok := GetValueByKey[string, map[string]any](testMap, "foo1") if !ok { t.Fatal("取foo1值失败") } foo1Sub, ok := GetValueByKey[string, []any](foo1Map, "foo1_sub") if !ok { t.Fatal("取foo1Sub值失败") } if foo1Sub[0].(string) != "value1" { t.Fatal("值不一致") } if foo1Sub[1].(int) != 2 { t.Fatal("值不一致") } } func TestSort(t *testing.T) { testMap := map[int]any{ 1: "test1", 2: "test2", 3: "test3", 4: "test4", 5: "test5", 6: "test6", 7: "test7", 8: "test8", 9: "test9", } Sort(testMap, func(keys []int) { sort.Sort(sort.IntSlice(keys)) }).Range(func(index int, key int, value any) { if index != key-1 { t.Fatal("key顺序错误") } if value != "test"+strconv.Itoa(key) { t.Fatal("key对应的value不正确") } }) }