map_elem_struct_test.go 1.6 KB

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