PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/proto/text_test.go

https://code.google.com/p/goprotobuf/
Go | 232 lines | 177 code | 16 blank | 39 comment | 33 complexity | fd2ef7cdf01ed91defadc5ef0db631b6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 Google Inc. 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. "bytes"
  34. "strings"
  35. "testing"
  36. "code.google.com/p/goprotobuf/proto"
  37. pb "./testdata"
  38. )
  39. func newTestMessage() *pb.MyMessage {
  40. msg := &pb.MyMessage{
  41. Count: proto.Int32(42),
  42. Name: proto.String("Dave"),
  43. Quote: proto.String(`"I didn't want to go."`),
  44. Pet: []string{"bunny", "kitty", "horsey"},
  45. Inner: &pb.InnerMessage{
  46. Host: proto.String("footrest.syd"),
  47. Port: proto.Int32(7001),
  48. Connected: proto.Bool(true),
  49. },
  50. Others: []*pb.OtherMessage{
  51. {
  52. Key: proto.Int64(0xdeadbeef),
  53. Value: []byte{1, 65, 7, 12},
  54. },
  55. {
  56. Weight: proto.Float32(6.022),
  57. Inner: &pb.InnerMessage{
  58. Host: proto.String("lesha.mtv"),
  59. Port: proto.Int32(8002),
  60. },
  61. },
  62. },
  63. Bikeshed: pb.NewMyMessage_Color(pb.MyMessage_BLUE),
  64. Somegroup: &pb.MyMessage_SomeGroup{
  65. GroupField: proto.Int32(8),
  66. },
  67. // One normally wouldn't do this.
  68. // This is an undeclared tag 13, as a varint (wire type 0) with value 4.
  69. XXX_unrecognized: []byte{13<<3 | 0, 4},
  70. }
  71. ext := &pb.Ext{
  72. Data: proto.String("Big gobs for big rats"),
  73. }
  74. if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
  75. panic(err)
  76. }
  77. // Add an unknown extension. We marshal a pb.Ext, and fake the ID.
  78. b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
  79. if err != nil {
  80. panic(err)
  81. }
  82. b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...)
  83. proto.SetRawExtension(msg, 201, b)
  84. // Extensions can be plain fields, too, so let's test that.
  85. b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19)
  86. proto.SetRawExtension(msg, 202, b)
  87. return msg
  88. }
  89. const text = `count: 42
  90. name: "Dave"
  91. quote: "\"I didn't want to go.\""
  92. pet: "bunny"
  93. pet: "kitty"
  94. pet: "horsey"
  95. inner: <
  96. host: "footrest.syd"
  97. port: 7001
  98. connected: true
  99. >
  100. others: <
  101. key: 3735928559
  102. value: "\001A\007\014"
  103. >
  104. others: <
  105. weight: 6.022
  106. inner: <
  107. host: "lesha.mtv"
  108. port: 8002
  109. >
  110. >
  111. bikeshed: BLUE
  112. SomeGroup {
  113. group_field: 8
  114. }
  115. /* 2 unknown bytes */
  116. tag13: 4
  117. [testdata.Ext.more]: <
  118. data: "Big gobs for big rats"
  119. >
  120. /* 13 unknown bytes */
  121. tag201: "\t3G skiing"
  122. /* 3 unknown bytes */
  123. tag202: 19
  124. `
  125. func TestMarshalTextFull(t *testing.T) {
  126. buf := new(bytes.Buffer)
  127. proto.MarshalText(buf, newTestMessage())
  128. s := buf.String()
  129. if s != text {
  130. t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, text)
  131. }
  132. }
  133. func compact(src string) string {
  134. // s/[ \n]+/ /g; s/ $//;
  135. dst := make([]byte, len(src))
  136. space, comment := false, false
  137. j := 0
  138. for i := 0; i < len(src); i++ {
  139. if strings.HasPrefix(src[i:], "/*") {
  140. comment = true
  141. i++
  142. continue
  143. }
  144. if comment && strings.HasPrefix(src[i:], "*/") {
  145. comment = false
  146. i++
  147. continue
  148. }
  149. if comment {
  150. continue
  151. }
  152. c := src[i]
  153. if c == ' ' || c == '\n' {
  154. space = true
  155. continue
  156. }
  157. if j > 0 && (dst[j-1] == ':' || dst[j-1] == '<' || dst[j-1] == '{') {
  158. space = false
  159. }
  160. if c == '{' {
  161. space = false
  162. }
  163. if space {
  164. dst[j] = ' '
  165. j++
  166. space = false
  167. }
  168. dst[j] = c
  169. j++
  170. }
  171. if space {
  172. dst[j] = ' '
  173. j++
  174. }
  175. return string(dst[0:j])
  176. }
  177. var compactText = compact(text)
  178. func TestCompactText(t *testing.T) {
  179. s := proto.CompactTextString(newTestMessage())
  180. if s != compactText {
  181. t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v\n===\n", s, compactText)
  182. }
  183. }
  184. func TestStringEscaping(t *testing.T) {
  185. testCases := []struct {
  186. in *pb.Strings
  187. out string
  188. }{
  189. {
  190. // Test data from C++ test (TextFormatTest.StringEscape).
  191. // Single divergence: we don't escape apostrophes.
  192. &pb.Strings{StringField: proto.String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces")},
  193. "string_field: \"\\\"A string with ' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"\n",
  194. },
  195. {
  196. // Test data from the same C++ test.
  197. &pb.Strings{StringField: proto.String("\350\260\267\346\255\214")},
  198. "string_field: \"\\350\\260\\267\\346\\255\\214\"\n",
  199. },
  200. }
  201. for i, tc := range testCases {
  202. var buf bytes.Buffer
  203. proto.MarshalText(&buf, tc.in)
  204. if s := buf.String(); s != tc.out {
  205. t.Errorf("#%d: Got:\n%s\nExpected:\n%s\n", i, s, tc.out)
  206. }
  207. }
  208. }
  209. func TestNonPtrMessage(t *testing.T) {
  210. // Ensure we don't panic when we pass a non-pointer to MarshalText.
  211. var buf bytes.Buffer
  212. proto.MarshalText(&buf, pb.MyMessage{})
  213. if s := buf.String(); s != "<struct-by-value>" {
  214. t.Errorf("got: %q, want %q", s, "<struct-by-value>")
  215. }
  216. }