github.com/gogf/gf@v1.16.9/container/gvar/gvar_z_unit_map_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gvar_test 8 9 import ( 10 "github.com/gogf/gf/container/gvar" 11 "github.com/gogf/gf/frame/g" 12 "github.com/gogf/gf/test/gtest" 13 "testing" 14 ) 15 16 func TestVar_Map(t *testing.T) { 17 gtest.C(t, func(t *gtest.T) { 18 m := g.Map{ 19 "k1": "v1", 20 "k2": "v2", 21 } 22 objOne := gvar.New(m, true) 23 t.Assert(objOne.Map()["k1"], m["k1"]) 24 t.Assert(objOne.Map()["k2"], m["k2"]) 25 }) 26 } 27 28 func TestVar_MapToMap(t *testing.T) { 29 // map[int]int -> map[string]string 30 // empty original map. 31 gtest.C(t, func(t *gtest.T) { 32 m1 := g.MapIntInt{} 33 m2 := g.MapStrStr{} 34 t.Assert(gvar.New(m1).MapToMap(&m2), nil) 35 t.Assert(len(m1), len(m2)) 36 }) 37 // map[int]int -> map[string]string 38 gtest.C(t, func(t *gtest.T) { 39 m1 := g.MapIntInt{ 40 1: 100, 41 2: 200, 42 } 43 m2 := g.MapStrStr{} 44 t.Assert(gvar.New(m1).MapToMap(&m2), nil) 45 t.Assert(m2["1"], m1[1]) 46 t.Assert(m2["2"], m1[2]) 47 }) 48 // map[string]interface{} -> map[string]string 49 gtest.C(t, func(t *gtest.T) { 50 m1 := g.Map{ 51 "k1": "v1", 52 "k2": "v2", 53 } 54 m2 := g.MapStrStr{} 55 t.Assert(gvar.New(m1).MapToMap(&m2), nil) 56 t.Assert(m2["k1"], m1["k1"]) 57 t.Assert(m2["k2"], m1["k2"]) 58 }) 59 // map[string]string -> map[string]interface{} 60 gtest.C(t, func(t *gtest.T) { 61 m1 := g.MapStrStr{ 62 "k1": "v1", 63 "k2": "v2", 64 } 65 m2 := g.Map{} 66 t.Assert(gvar.New(m1).MapToMap(&m2), nil) 67 t.Assert(m2["k1"], m1["k1"]) 68 t.Assert(m2["k2"], m1["k2"]) 69 }) 70 // map[string]interface{} -> map[interface{}]interface{} 71 gtest.C(t, func(t *gtest.T) { 72 m1 := g.MapStrStr{ 73 "k1": "v1", 74 "k2": "v2", 75 } 76 m2 := g.MapAnyAny{} 77 t.Assert(gvar.New(m1).MapToMap(&m2), nil) 78 t.Assert(m2["k1"], m1["k1"]) 79 t.Assert(m2["k2"], m1["k2"]) 80 }) 81 }