github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf.
     6  
     7  package gvar_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/container/gvar"
    13  	"github.com/wangyougui/gf/v2/frame/g"
    14  	"github.com/wangyougui/gf/v2/test/gtest"
    15  )
    16  
    17  func TestVar_Map(t *testing.T) {
    18  	gtest.C(t, func(t *gtest.T) {
    19  		m := g.Map{
    20  			"k1": "v1",
    21  			"k2": "v2",
    22  		}
    23  		objOne := gvar.New(m, true)
    24  		t.Assert(objOne.Map()["k1"], m["k1"])
    25  		t.Assert(objOne.Map()["k2"], m["k2"])
    26  	})
    27  }
    28  
    29  func TestVar_MapToMap(t *testing.T) {
    30  	// map[int]int -> map[string]string
    31  	// empty original map.
    32  	gtest.C(t, func(t *gtest.T) {
    33  		m1 := g.MapIntInt{}
    34  		m2 := g.MapStrStr{}
    35  		t.Assert(gvar.New(m1).MapToMap(&m2), nil)
    36  		t.Assert(len(m1), len(m2))
    37  	})
    38  	// map[int]int -> map[string]string
    39  	gtest.C(t, func(t *gtest.T) {
    40  		m1 := g.MapIntInt{
    41  			1: 100,
    42  			2: 200,
    43  		}
    44  		m2 := g.MapStrStr{}
    45  		t.Assert(gvar.New(m1).MapToMap(&m2), nil)
    46  		t.Assert(m2["1"], m1[1])
    47  		t.Assert(m2["2"], m1[2])
    48  	})
    49  	// map[string]interface{} -> map[string]string
    50  	gtest.C(t, func(t *gtest.T) {
    51  		m1 := g.Map{
    52  			"k1": "v1",
    53  			"k2": "v2",
    54  		}
    55  		m2 := g.MapStrStr{}
    56  		t.Assert(gvar.New(m1).MapToMap(&m2), nil)
    57  		t.Assert(m2["k1"], m1["k1"])
    58  		t.Assert(m2["k2"], m1["k2"])
    59  	})
    60  	// map[string]string -> map[string]interface{}
    61  	gtest.C(t, func(t *gtest.T) {
    62  		m1 := g.MapStrStr{
    63  			"k1": "v1",
    64  			"k2": "v2",
    65  		}
    66  		m2 := g.Map{}
    67  		t.Assert(gvar.New(m1).MapToMap(&m2), nil)
    68  		t.Assert(m2["k1"], m1["k1"])
    69  		t.Assert(m2["k2"], m1["k2"])
    70  	})
    71  	// map[string]interface{} -> map[interface{}]interface{}
    72  	gtest.C(t, func(t *gtest.T) {
    73  		m1 := g.MapStrStr{
    74  			"k1": "v1",
    75  			"k2": "v2",
    76  		}
    77  		m2 := g.MapAnyAny{}
    78  		t.Assert(gvar.New(m1).MapToMap(&m2), nil)
    79  		t.Assert(m2["k1"], m1["k1"])
    80  		t.Assert(m2["k2"], m1["k2"])
    81  	})
    82  }
    83  
    84  func TestVar_MapStrVar(t *testing.T) {
    85  	gtest.C(t, func(t *gtest.T) {
    86  		m := g.Map{
    87  			"k1": "v1",
    88  			"k2": "v2",
    89  		}
    90  		objOne := gvar.New(m, true)
    91  		t.Assert(objOne.MapStrVar(), "{\"k1\":\"v1\",\"k2\":\"v2\"}")
    92  
    93  		objEmpty := gvar.New(g.Map{})
    94  		t.Assert(objEmpty.MapStrVar(), "")
    95  	})
    96  }