clone_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2011 The Go Authors. All rights reserved.
  4. // http://code.google.com/p/goprotobuf/
  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. "code.google.com/p/goprotobuf/proto"
  35. pb "./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. }
  75. func TestCloneNil(t *testing.T) {
  76. var m *pb.MyMessage
  77. if c := proto.Clone(m); !proto.Equal(m, c) {
  78. t.Errorf("Clone(%v) = %v", m, c)
  79. }
  80. }
  81. var mergeTests = []struct {
  82. src, dst, want proto.Message
  83. }{
  84. {
  85. src: &pb.MyMessage{
  86. Count: proto.Int32(42),
  87. },
  88. dst: &pb.MyMessage{
  89. Name: proto.String("Dave"),
  90. },
  91. want: &pb.MyMessage{
  92. Count: proto.Int32(42),
  93. Name: proto.String("Dave"),
  94. },
  95. },
  96. {
  97. src: &pb.MyMessage{
  98. Inner: &pb.InnerMessage{
  99. Host: proto.String("hey"),
  100. Connected: proto.Bool(true),
  101. },
  102. Pet: []string{"horsey"},
  103. Others: []*pb.OtherMessage{
  104. {
  105. Value: []byte("some bytes"),
  106. },
  107. },
  108. },
  109. dst: &pb.MyMessage{
  110. Inner: &pb.InnerMessage{
  111. Host: proto.String("niles"),
  112. Port: proto.Int32(9099),
  113. },
  114. Pet: []string{"bunny", "kitty"},
  115. Others: []*pb.OtherMessage{
  116. {
  117. Key: proto.Int64(31415926535),
  118. },
  119. {
  120. // Explicitly test a src=nil field
  121. Inner: nil,
  122. },
  123. },
  124. },
  125. want: &pb.MyMessage{
  126. Inner: &pb.InnerMessage{
  127. Host: proto.String("hey"),
  128. Connected: proto.Bool(true),
  129. Port: proto.Int32(9099),
  130. },
  131. Pet: []string{"bunny", "kitty", "horsey"},
  132. Others: []*pb.OtherMessage{
  133. {
  134. Key: proto.Int64(31415926535),
  135. },
  136. {},
  137. {
  138. Value: []byte("some bytes"),
  139. },
  140. },
  141. },
  142. },
  143. {
  144. src: &pb.MyMessage{
  145. RepBytes: [][]byte{[]byte("wow")},
  146. },
  147. dst: &pb.MyMessage{
  148. Somegroup: &pb.MyMessage_SomeGroup{
  149. GroupField: proto.Int32(6),
  150. },
  151. RepBytes: [][]byte{[]byte("sham")},
  152. },
  153. want: &pb.MyMessage{
  154. Somegroup: &pb.MyMessage_SomeGroup{
  155. GroupField: proto.Int32(6),
  156. },
  157. RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
  158. },
  159. },
  160. }
  161. func TestMerge(t *testing.T) {
  162. for _, m := range mergeTests {
  163. got := proto.Clone(m.dst)
  164. proto.Merge(got, m.src)
  165. if !proto.Equal(got, m.want) {
  166. t.Errorf("Merge(%v, %v)\n got %v\nwant %v\n", m.dst, m.src, got, m.want)
  167. }
  168. }
  169. }