clone_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  36. pb "github.com/golang/protobuf/proto/testdata"
  37. )
  38. var cloneTestMessage = &pb.MyMessage{
  39. Count: proto.Int32(42),
  40. Name: proto.String("Dave"),
  41. Pet: []string{"bunny", "kitty", "horsey"},
  42. Inner: &pb.InnerMessage{
  43. Host: proto.String("niles"),
  44. Port: proto.Int32(9099),
  45. Connected: proto.Bool(true),
  46. },
  47. Others: []*pb.OtherMessage{
  48. {
  49. Value: []byte("some bytes"),
  50. },
  51. },
  52. Somegroup: &pb.MyMessage_SomeGroup{
  53. GroupField: proto.Int32(6),
  54. },
  55. RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
  56. }
  57. func init() {
  58. ext := &pb.Ext{
  59. Data: proto.String("extension"),
  60. }
  61. if err := proto.SetExtension(cloneTestMessage, pb.E_Ext_More, ext); err != nil {
  62. panic("SetExtension: " + err.Error())
  63. }
  64. }
  65. func TestClone(t *testing.T) {
  66. m := proto.Clone(cloneTestMessage).(*pb.MyMessage)
  67. if !proto.Equal(m, cloneTestMessage) {
  68. t.Errorf("Clone(%v) = %v", cloneTestMessage, m)
  69. }
  70. // Verify it was a deep copy.
  71. *m.Inner.Port++
  72. if proto.Equal(m, cloneTestMessage) {
  73. t.Error("Mutating clone changed the original")
  74. }
  75. // Byte fields and repeated fields should be copied.
  76. if &m.Pet[0] == &cloneTestMessage.Pet[0] {
  77. t.Error("Pet: repeated field not copied")
  78. }
  79. if &m.Others[0] == &cloneTestMessage.Others[0] {
  80. t.Error("Others: repeated field not copied")
  81. }
  82. if &m.Others[0].Value[0] == &cloneTestMessage.Others[0].Value[0] {
  83. t.Error("Others[0].Value: bytes field not copied")
  84. }
  85. if &m.RepBytes[0] == &cloneTestMessage.RepBytes[0] {
  86. t.Error("RepBytes: repeated field not copied")
  87. }
  88. if &m.RepBytes[0][0] == &cloneTestMessage.RepBytes[0][0] {
  89. t.Error("RepBytes[0]: bytes field not copied")
  90. }
  91. }
  92. func TestCloneNil(t *testing.T) {
  93. var m *pb.MyMessage
  94. if c := proto.Clone(m); !proto.Equal(m, c) {
  95. t.Errorf("Clone(%v) = %v", m, c)
  96. }
  97. }
  98. var mergeTests = []struct {
  99. src, dst, want proto.Message
  100. }{
  101. {
  102. src: &pb.MyMessage{
  103. Count: proto.Int32(42),
  104. },
  105. dst: &pb.MyMessage{
  106. Name: proto.String("Dave"),
  107. },
  108. want: &pb.MyMessage{
  109. Count: proto.Int32(42),
  110. Name: proto.String("Dave"),
  111. },
  112. },
  113. {
  114. src: &pb.MyMessage{
  115. Inner: &pb.InnerMessage{
  116. Host: proto.String("hey"),
  117. Connected: proto.Bool(true),
  118. },
  119. Pet: []string{"horsey"},
  120. Others: []*pb.OtherMessage{
  121. {
  122. Value: []byte("some bytes"),
  123. },
  124. },
  125. },
  126. dst: &pb.MyMessage{
  127. Inner: &pb.InnerMessage{
  128. Host: proto.String("niles"),
  129. Port: proto.Int32(9099),
  130. },
  131. Pet: []string{"bunny", "kitty"},
  132. Others: []*pb.OtherMessage{
  133. {
  134. Key: proto.Int64(31415926535),
  135. },
  136. {
  137. // Explicitly test a src=nil field
  138. Inner: nil,
  139. },
  140. },
  141. },
  142. want: &pb.MyMessage{
  143. Inner: &pb.InnerMessage{
  144. Host: proto.String("hey"),
  145. Connected: proto.Bool(true),
  146. Port: proto.Int32(9099),
  147. },
  148. Pet: []string{"bunny", "kitty", "horsey"},
  149. Others: []*pb.OtherMessage{
  150. {
  151. Key: proto.Int64(31415926535),
  152. },
  153. {},
  154. {
  155. Value: []byte("some bytes"),
  156. },
  157. },
  158. },
  159. },
  160. {
  161. src: &pb.MyMessage{
  162. RepBytes: [][]byte{[]byte("wow")},
  163. },
  164. dst: &pb.MyMessage{
  165. Somegroup: &pb.MyMessage_SomeGroup{
  166. GroupField: proto.Int32(6),
  167. },
  168. RepBytes: [][]byte{[]byte("sham")},
  169. },
  170. want: &pb.MyMessage{
  171. Somegroup: &pb.MyMessage_SomeGroup{
  172. GroupField: proto.Int32(6),
  173. },
  174. RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
  175. },
  176. },
  177. // Check that a scalar bytes field replaces rather than appends.
  178. {
  179. src: &pb.OtherMessage{Value: []byte("foo")},
  180. dst: &pb.OtherMessage{Value: []byte("bar")},
  181. want: &pb.OtherMessage{Value: []byte("foo")},
  182. },
  183. {
  184. src: &pb.MessageWithMap{
  185. NameMapping: map[int32]string{6: "Nigel"},
  186. MsgMapping: map[int64]*pb.FloatingPoint{
  187. 0x4001: &pb.FloatingPoint{F: proto.Float64(2.0)},
  188. },
  189. ByteMapping: map[bool][]byte{true: []byte("wowsa")},
  190. },
  191. dst: &pb.MessageWithMap{
  192. NameMapping: map[int32]string{
  193. 6: "Bruce", // should be overwritten
  194. 7: "Andrew",
  195. },
  196. },
  197. want: &pb.MessageWithMap{
  198. NameMapping: map[int32]string{
  199. 6: "Nigel",
  200. 7: "Andrew",
  201. },
  202. MsgMapping: map[int64]*pb.FloatingPoint{
  203. 0x4001: &pb.FloatingPoint{F: proto.Float64(2.0)},
  204. },
  205. ByteMapping: map[bool][]byte{true: []byte("wowsa")},
  206. },
  207. },
  208. // proto3 shouldn't merge zero values,
  209. // in the same way that proto2 shouldn't merge nils.
  210. {
  211. src: &proto3pb.Message{
  212. Name: "Aaron",
  213. Data: []byte(""), // zero value, but not nil
  214. },
  215. dst: &proto3pb.Message{
  216. HeightInCm: 176,
  217. Data: []byte("texas!"),
  218. },
  219. want: &proto3pb.Message{
  220. Name: "Aaron",
  221. HeightInCm: 176,
  222. Data: []byte("texas!"),
  223. },
  224. },
  225. // Oneof fields should merge by assignment.
  226. {
  227. src: &pb.Communique{
  228. Union: &pb.Communique_Number{41},
  229. },
  230. dst: &pb.Communique{
  231. Union: &pb.Communique_Name{"Bobby Tables"},
  232. },
  233. want: &pb.Communique{
  234. Union: &pb.Communique_Number{41},
  235. },
  236. },
  237. // Oneof nil is the same as not set.
  238. {
  239. src: &pb.Communique{},
  240. dst: &pb.Communique{
  241. Union: &pb.Communique_Name{"Bobby Tables"},
  242. },
  243. want: &pb.Communique{
  244. Union: &pb.Communique_Name{"Bobby Tables"},
  245. },
  246. },
  247. }
  248. func TestMerge(t *testing.T) {
  249. for _, m := range mergeTests {
  250. got := proto.Clone(m.dst)
  251. proto.Merge(got, m.src)
  252. if !proto.Equal(got, m.want) {
  253. t.Errorf("Merge(%v, %v)\n got %v\nwant %v\n", m.dst, m.src, got, m.want)
  254. }
  255. }
  256. }