map_elem_struct_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package tests
  2. import (
  3. "github.com/modern-go/reflect2"
  4. "testing"
  5. "time"
  6. )
  7. func Test_map_elem_struct(t *testing.T) {
  8. var pInt = func(val int) *int {
  9. return &val
  10. }
  11. t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
  12. obj := map[int]time.Time{}
  13. valType := api.TypeOf(obj).(reflect2.MapType)
  14. valType.SetIndex(&obj, pInt(2), &time.Time{})
  15. valType.SetIndex(&obj, pInt(3), &time.Time{})
  16. return obj
  17. }))
  18. t.Run("SetIndex single ptr struct", testOp(func(api reflect2.API) interface{} {
  19. type TestObject struct {
  20. Field1 *int
  21. }
  22. obj := map[int]TestObject{}
  23. valType := api.TypeOf(obj).(reflect2.MapType)
  24. valType.SetIndex(&obj, pInt(2), &TestObject{})
  25. valType.SetIndex(&obj, pInt(3), &TestObject{})
  26. return obj
  27. }))
  28. t.Run("SetIndex single map struct", testOp(func(api reflect2.API) interface{} {
  29. type TestObject struct {
  30. Field1 map[int]int
  31. }
  32. obj := map[int]TestObject{}
  33. valType := api.TypeOf(obj).(reflect2.MapType)
  34. valType.SetIndex(&obj, pInt(2), &TestObject{})
  35. valType.SetIndex(&obj, pInt(3), &TestObject{})
  36. return obj
  37. }))
  38. t.Run("SetIndex single chan struct", testOp(func(api reflect2.API) interface{} {
  39. type TestObject struct {
  40. Field1 chan int
  41. }
  42. obj := map[int]TestObject{}
  43. valType := api.TypeOf(obj).(reflect2.MapType)
  44. valType.SetIndex(&obj, pInt(2), &TestObject{})
  45. valType.SetIndex(&obj, pInt(3), &TestObject{})
  46. return obj
  47. }))
  48. t.Run("SetIndex single func struct", testOp(func(api reflect2.API) interface{} {
  49. type TestObject struct {
  50. Field1 func()
  51. }
  52. obj := map[int]TestObject{}
  53. valType := api.TypeOf(obj).(reflect2.MapType)
  54. valType.SetIndex(&obj, pInt(2), &TestObject{})
  55. valType.SetIndex(&obj, pInt(3), &TestObject{})
  56. return obj
  57. }))
  58. }