example_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package tests
  2. import (
  3. "testing"
  4. "time"
  5. "unsafe"
  6. "github.com/stretchr/testify/assert"
  7. "go-common/app/tool/gengo/cmd/deepcopy-gen/examples/api/v1"
  8. "go-common/app/tool/gengo/cmd/deepcopy-gen/examples/model"
  9. xtime "go-common/library/time"
  10. )
  11. func TestCopyFromAllBuitin(t *testing.T) {
  12. base := &model.MemberBase{
  13. Mid: 1,
  14. Name: "name",
  15. Sex: 1,
  16. Face: "aaa.jpg",
  17. Sign: "sign",
  18. Rank: 10000,
  19. Birthday: xtime.Time(time.Now().Unix()),
  20. }
  21. reply := new(v1.BaseInfoReply)
  22. reply.DeepCopyFromMemberBase(base)
  23. assert.Equal(t, base.Mid, reply.Mid)
  24. assert.Equal(t, base.Name, reply.Name)
  25. assert.Equal(t, base.Sex, reply.Sex)
  26. assert.Equal(t, base.Face, reply.Face)
  27. assert.Equal(t, base.Sign, reply.Sign)
  28. assert.Equal(t, base.Rank, reply.Rank)
  29. assert.Equal(t, base.Birthday, reply.Birthday)
  30. copied := reply.DeepCopyAsMemberBase()
  31. assert.Equal(t, base, copied)
  32. }
  33. func TestCopyFromMap(t *testing.T) {
  34. names := &model.Names{
  35. Names: map[int64]string{
  36. 1: "1",
  37. 2: "2",
  38. },
  39. }
  40. reply := new(v1.NamesReply)
  41. reply.DeepCopyFromNames(names)
  42. assert.Equal(t, names.Names[1], reply.Names[1])
  43. assert.Equal(t, names.Names[2], reply.Names[2])
  44. assert.Equal(t, unsafe.Pointer(&names.Names), unsafe.Pointer(&names.Names))
  45. assert.Equal(t, unsafe.Pointer(&reply.Names), unsafe.Pointer(&reply.Names))
  46. assert.NotEqual(t, unsafe.Pointer(&names.Names), unsafe.Pointer(&reply.Names))
  47. names.Names[3] = "3"
  48. assert.Contains(t, names.Names, int64(3))
  49. assert.NotContains(t, reply.Names, int64(3))
  50. }
  51. func TestCopyFromSlice(t *testing.T) {
  52. mids := &model.Mids{
  53. Mids: []int64{1, 2},
  54. }
  55. reply := new(v1.MidsReply)
  56. reply.DeepCopyFromMids(mids)
  57. assert.Equal(t, mids.Mids[0], reply.Mids[0])
  58. assert.Equal(t, mids.Mids[1], reply.Mids[1])
  59. assert.Equal(t, unsafe.Pointer(&mids.Mids), unsafe.Pointer(&mids.Mids))
  60. assert.Equal(t, unsafe.Pointer(&reply.Mids), unsafe.Pointer(&reply.Mids))
  61. assert.NotEqual(t, unsafe.Pointer(&mids.Mids), unsafe.Pointer(&reply.Mids))
  62. mids.Mids = append(mids.Mids, 3)
  63. assert.Equal(t, mids.Mids[2], int64(3))
  64. assert.NotContains(t, reply.Mids, int64(3))
  65. }