github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/reflect2/test/map_key_iface_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  type intError int
    10  
    11  func (err intError) Error() string {
    12  	return ""
    13  }
    14  
    15  func Test_map_iface_key(t *testing.T) {
    16  	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
    17  		obj := map[error]int{}
    18  		valType := api.TypeOf(obj).(reflect2.MapType)
    19  		valType.SetIndex(obj, intError(2), 4)
    20  		valType.SetIndex(obj, intError(2), 9)
    21  		valType.SetIndex(obj, nil, 9)
    22  		must.Panic(func() {
    23  			valType.SetIndex(obj, "", 9)
    24  		})
    25  		return obj
    26  	}))
    27  	t.Run("GetIndex", testOp(func(api reflect2.API) interface{} {
    28  		obj := map[error]int{intError(3): 9, intError(2): 4}
    29  		valType := api.TypeOf(obj).(reflect2.MapType)
    30  		must.Panic(func() {
    31  			valType.GetIndex(obj, "")
    32  		})
    33  		return []interface{}{
    34  			valType.GetIndex(obj, intError(3)),
    35  			valType.GetIndex(obj, nil),
    36  		}
    37  	}))
    38  	t.Run("Iterate", testOp(func(api reflect2.API) interface{} {
    39  		obj := map[error]int{intError(2): 4}
    40  		valType := api.TypeOf(obj).(reflect2.MapType)
    41  		iter := valType.Iterate(obj)
    42  		must.Pass(iter.HasNext(), "api", api)
    43  		key1, elem1 := iter.Next()
    44  		must.Pass(!iter.HasNext(), "api", api)
    45  		return []interface{}{key1, elem1}
    46  	}))
    47  }