clone_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2011 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package proto_test
  32. import (
  33. "testing"
  34. "github.com/golang/protobuf/proto"
  35. pb "github.com/golang/protobuf/proto/testdata"
  36. )
  37. var cloneTestMessage = &pb.MyMessage{
  38. Count: proto.Int32(42),
  39. Name: proto.String("Dave"),
  40. Pet: []string{"bunny", "kitty", "horsey"},
  41. Inner: &pb.InnerMessage{
  42. Host: proto.String("niles"),
  43. Port: proto.Int32(9099),
  44. Connected: proto.Bool(true),
  45. },
  46. Others: []*pb.OtherMessage{
  47. {
  48. Value: []byte("some bytes"),
  49. },
  50. },
  51. Somegroup: &pb.MyMessage_SomeGroup{
  52. GroupField: proto.Int32(6),
  53. },
  54. RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
  55. }
  56. func init() {
  57. ext := &pb.Ext{
  58. Data: proto.String("extension"),
  59. }
  60. if err := proto.SetExtension(cloneTestMessage, pb.E_Ext_More, ext); err != nil {
  61. panic("SetExtension: " + err.Error())
  62. }
  63. }
  64. func TestClone(t *testing.T) {
  65. m := proto.Clone(cloneTestMessage).(*pb.MyMessage)
  66. if !proto.Equal(m, cloneTestMessage) {
  67. t.Errorf("Clone(%v) = %v", cloneTestMessage, m)
  68. }
  69. // Verify it was a deep copy.
  70. *m.Inner.Port++
  71. if proto.Equal(m, cloneTestMessage) {
  72. t.Error("Mutating clone changed the original")
  73. }
  74. // Byte fields and repeated fields should be copied.
  75. if &m.Pet[0] == &cloneTestMessage.Pet[0] {
  76. t.Error("Pet: repeated field not copied")
  77. }
  78. if &m.Others[0] == &cloneTestMessage.Others[0] {
  79. t.Error("Others: repeated field not copied")
  80. }
  81. if &m.Others[0].Value[0] == &cloneTestMessage.Others[0].Value[0] {
  82. t.Error("Others[0].Value: bytes field not copied")
  83. }
  84. if &m.RepBytes[0] == &cloneTestMessage.RepBytes[0] {
  85. t.Error("RepBytes: repeated field not copied")
  86. }
  87. if &m.RepBytes[0][0] == &cloneTestMessage.RepBytes[0][0] {
  88. t.Error("RepBytes[0]: bytes field not copied")
  89. }
  90. }
  91. func TestCloneNil(t *testing.T) {
  92. var m *pb.MyMessage
  93. if c := proto.Clone(m); !proto.Equal(m, c) {
  94. t.Errorf("Clone(%v) = %v", m, c)
  95. }
  96. }
  97. var mergeTests = []struct {
  98. src, dst, want proto.Message
  99. }{
  100. {
  101. src: &pb.MyMessage{
  102. Count: proto.Int32(42),
  103. },
  104. dst: &pb.MyMessage{
  105. Name: proto.String("Dave"),
  106. },
  107. want: &pb.MyMessage{
  108. Count: proto.Int32(42),
  109. Name: proto.String("Dave"),
  110. },
  111. },
  112. {
  113. src: &pb.MyMessage{
  114. Inner: &pb.InnerMessage{
  115. Host: proto.String("hey"),
  116. Connected: proto.Bool(true),
  117. },
  118. Pet: []string{"horsey"},
  119. Others: []*pb.OtherMessage{
  120. {
  121. Value: []byte("some bytes"),
  122. },
  123. },
  124. },
  125. dst: &pb.MyMessage{
  126. Inner: &pb.InnerMessage{
  127. Host: proto.String("niles"),
  128. Port: proto.Int32(9099),
  129. },
  130. Pet: []string{"bunny", "kitty"},
  131. Others: []*pb.OtherMessage{
  132. {
  133. Key: proto.Int64(31415926535),
  134. },
  135. {
  136. // Explicitly test a src=nil field
  137. Inner: nil,
  138. },
  139. },
  140. },
  141. want: &pb.MyMessage{
  142. Inner: &pb.InnerMessage{
  143. Host: proto.String("hey"),
  144. Connected: proto.Bool(true),
  145. Port: proto.Int32(9099),
  146. },
  147. Pet: []string{"bunny", "kitty", "horsey"},
  148. Others: []*pb.OtherMessage{
  149. {
  150. Key: proto.Int64(31415926535),
  151. },
  152. {},
  153. {
  154. Value: []byte("some bytes"),
  155. },
  156. },
  157. },
  158. },
  159. {
  160. src: &pb.MyMessage{
  161. RepBytes: [][]byte{[]byte("wow")},
  162. },
  163. dst: &pb.MyMessage{
  164. Somegroup: &pb.MyMessage_SomeGroup{
  165. GroupField: proto.Int32(6),
  166. },
  167. RepBytes: [][]byte{[]byte("sham")},
  168. },
  169. want: &pb.MyMessage{
  170. Somegroup: &pb.MyMessage_SomeGroup{
  171. GroupField: proto.Int32(6),
  172. },
  173. RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
  174. },
  175. },
  176. // Check that a scalar bytes field replaces rather than appends.
  177. {
  178. src: &pb.OtherMessage{Value: []byte("foo")},
  179. dst: &pb.OtherMessage{Value: []byte("bar")},
  180. want: &pb.OtherMessage{Value: []byte("foo")},
  181. },
  182. {
  183. src: &pb.MessageWithMap{
  184. NameMapping: map[int32]string{6: "Nigel"},
  185. MsgMapping: map[int64]*pb.FloatingPoint{
  186. 0x4001: &pb.FloatingPoint{F: proto.Float64(2.0)},
  187. },
  188. ByteMapping: map[bool][]byte{true: []byte("wowsa")},
  189. },
  190. dst: &pb.MessageWithMap{
  191. NameMapping: map[int32]string{
  192. 6: "Bruce", // should be overwritten
  193. 7: "Andrew",
  194. },
  195. },
  196. want: &pb.MessageWithMap{
  197. NameMapping: map[int32]string{
  198. 6: "Nigel",
  199. 7: "Andrew",
  200. },
  201. MsgMapping: map[int64]*pb.FloatingPoint{
  202. 0x4001: &pb.FloatingPoint{F: proto.Float64(2.0)},
  203. },
  204. ByteMapping: map[bool][]byte{true: []byte("wowsa")},
  205. },
  206. },
  207. }
  208. func TestMerge(t *testing.T) {
  209. for _, m := range mergeTests {
  210. got := proto.Clone(m.dst)
  211. proto.Merge(got, m.src)
  212. if !proto.Equal(got, m.want) {
  213. t.Errorf("Merge(%v, %v)\n got %v\nwant %v\n", m.dst, m.src, got, m.want)
  214. }
  215. }
  216. }