github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/reflect2/test/map_key_eface_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  	"github.com/v2pro/plz/reflect2"
     6  	"github.com/v2pro/plz/test/must"
     7  )
     8  
     9  func Test_map_key_eface(t *testing.T) {
    10  	var pEFace = func(val interface{}) interface{} {
    11  		return &val
    12  	}
    13  	var pInt = func(val int) *int {
    14  		return &val
    15  	}
    16  	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
    17  		obj := map[interface{}]int{}
    18  		valType := api.TypeOf(obj).(reflect2.MapType)
    19  		valType.SetIndex(&obj, pEFace(2), pInt(4))
    20  		valType.SetIndex(&obj, pEFace(3), pInt(9))
    21  		valType.SetIndex(&obj, pEFace(nil), pInt(9))
    22  		return obj
    23  	}))
    24  	t.Run("GetIndex", testOp(func(api reflect2.API) interface{} {
    25  		obj := map[interface{}]int{3: 9, 2: 4}
    26  		valType := api.TypeOf(obj).(reflect2.MapType)
    27  		return []interface{}{
    28  			valType.GetIndex(&obj, pEFace(3)),
    29  			valType.GetIndex(&obj, pEFace(0)),
    30  			valType.GetIndex(&obj, pEFace(nil)),
    31  			valType.GetIndex(&obj, pEFace("")),
    32  		}
    33  	}))
    34  	t.Run("Iterate", testOp(func(api reflect2.API) interface{} {
    35  		obj := map[interface{}]int{2: 4}
    36  		valType := api.TypeOf(obj).(reflect2.MapType)
    37  		iter := valType.Iterate(obj)
    38  		must.Pass(iter.HasNext(), "api", api)
    39  		key1, elem1 := iter.Next()
    40  		must.Pass(!iter.HasNext(), "api", api)
    41  		return []interface{}{key1, elem1}
    42  	}))
    43  }