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