text_parser_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. . "goprotobuf.googlecode.com/hg/proto"
  34. . "./testdata/_obj/test_proto"
  35. "reflect"
  36. "testing"
  37. )
  38. type UnmarshalTextTest struct {
  39. in string
  40. error string // if "", no error expected
  41. out *MyMessage
  42. }
  43. var unMarshalTextTests = []UnmarshalTextTest{
  44. // Basic
  45. UnmarshalTextTest{
  46. in: " count:42\n name:\"Dave\" ",
  47. out: &MyMessage{
  48. Count: Int32(42),
  49. Name: String("Dave"),
  50. },
  51. },
  52. // Empty quoted string
  53. UnmarshalTextTest{
  54. in: `count:42 name:""`,
  55. out: &MyMessage{
  56. Count: Int32(42),
  57. Name: String(""),
  58. },
  59. },
  60. // Quoted string concatenation
  61. UnmarshalTextTest{
  62. in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`,
  63. out: &MyMessage{
  64. Count: Int32(42),
  65. Name: String("My name is elsewhere"),
  66. },
  67. },
  68. // Bad quoted string
  69. UnmarshalTextTest{
  70. in: `inner: < host: "\0" >` + "\n",
  71. error: `line 1.15: invalid quoted string "\0"`,
  72. },
  73. // Number too large for int64
  74. UnmarshalTextTest{
  75. in: "count: 123456789012345678901",
  76. error: "line 1.7: invalid int32: 123456789012345678901",
  77. },
  78. // Number too large for int32
  79. UnmarshalTextTest{
  80. in: "count: 1234567890123",
  81. error: "line 1.7: invalid int32: 1234567890123",
  82. },
  83. // Number too large for float32
  84. UnmarshalTextTest{
  85. in: "others:< weight: 12345678901234567890123456789012345678901234567890 >",
  86. error: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890",
  87. },
  88. // Number posing as a quoted string
  89. UnmarshalTextTest{
  90. in: `inner: < host: 12 >` + "\n",
  91. error: `line 1.15: invalid string: 12`,
  92. },
  93. // Quoted string posing as int32
  94. UnmarshalTextTest{
  95. in: `count: "12"`,
  96. error: `line 1.7: invalid int32: "12"`,
  97. },
  98. // Quoted string posing a float32
  99. UnmarshalTextTest{
  100. in: `others:< weight: "17.4" >`,
  101. error: `line 1.17: invalid float32: "17.4"`,
  102. },
  103. // Enum
  104. UnmarshalTextTest{
  105. in: `count:42 bikeshed: BLUE`,
  106. out: &MyMessage{
  107. Count: Int32(42),
  108. Bikeshed: NewMyMessage_Color(MyMessage_BLUE),
  109. },
  110. },
  111. // Repeated field
  112. UnmarshalTextTest{
  113. in: `count:42 pet: "horsey" pet:"bunny"`,
  114. out: &MyMessage{
  115. Count: Int32(42),
  116. Pet: []string{"horsey", "bunny"},
  117. },
  118. },
  119. // Missing colon for inner message
  120. UnmarshalTextTest{
  121. in: `count:42 inner < host: "cauchy.syd" >`,
  122. out: &MyMessage{
  123. Count: Int32(42),
  124. Inner: &InnerMessage{
  125. Host: String("cauchy.syd"),
  126. },
  127. },
  128. },
  129. // Missing colon for string field
  130. UnmarshalTextTest{
  131. in: `name "Dave"`,
  132. error: `line 1.5: expected ':', found "\"Dave\""`,
  133. },
  134. // Missing colon for int32 field
  135. UnmarshalTextTest{
  136. in: `count 42`,
  137. error: `line 1.6: expected ':', found "42"`,
  138. },
  139. // Missing required field
  140. UnmarshalTextTest{
  141. in: ``,
  142. error: `line 1.0: message test_proto.MyMessage missing required field "count"`,
  143. },
  144. // Repeated non-repeated field
  145. UnmarshalTextTest{
  146. in: `name: "Rob" name: "Russ"`,
  147. error: `line 1.12: non-repeated field "name" was repeated`,
  148. },
  149. // Big all-in-one
  150. UnmarshalTextTest{
  151. in: "count:42 # Meaning\n" +
  152. `name:"Dave" ` +
  153. `quote:"\"I didn't want to go.\"" ` +
  154. `pet:"bunny" ` +
  155. `pet:"kitty" ` +
  156. `pet:"horsey" ` +
  157. `inner:<` +
  158. ` host:"footrest.syd" ` +
  159. ` port:7001 ` +
  160. ` connected:true ` +
  161. `> ` +
  162. `others:<` +
  163. ` key:3735928559 ` +
  164. ` value:"\x01A\a\f" ` +
  165. `> ` +
  166. `others:<` +
  167. " weight:58.9 # Atomic weight of Co\n" +
  168. ` inner:<` +
  169. ` host:"lesha.mtv" ` +
  170. ` port:8002 ` +
  171. ` >` +
  172. `>`,
  173. out: &MyMessage{
  174. Count: Int32(42),
  175. Name: String("Dave"),
  176. Quote: String(`"I didn't want to go."`),
  177. Pet: []string{"bunny", "kitty", "horsey"},
  178. Inner: &InnerMessage{
  179. Host: String("footrest.syd"),
  180. Port: Int32(7001),
  181. Connected: Bool(true),
  182. },
  183. Others: []*OtherMessage{
  184. &OtherMessage{
  185. Key: Int64(3735928559),
  186. Value: []byte{0x1, 'A', '\a', '\f'},
  187. },
  188. &OtherMessage{
  189. Weight: Float32(58.9),
  190. Inner: &InnerMessage{
  191. Host: String("lesha.mtv"),
  192. Port: Int32(8002),
  193. },
  194. },
  195. },
  196. },
  197. },
  198. }
  199. func TestUnmarshalText(t *testing.T) {
  200. for i, test := range unMarshalTextTests {
  201. pb := new(MyMessage)
  202. err := UnmarshalText(test.in, pb)
  203. if test.error == "" {
  204. // We don't expect failure.
  205. if err != nil {
  206. t.Errorf("Test %d: Unexpected error: %v", i, err)
  207. } else if !reflect.DeepEqual(pb, test.out) {
  208. t.Errorf("Test %d: Incorrect populated \n"+
  209. "Have: %v\nWant: %v",
  210. i, CompactTextString(pb), CompactTextString(test.out))
  211. }
  212. } else {
  213. // We do expect failure.
  214. if err == nil {
  215. t.Errorf("Test %d: Didn't get expected error: %v", i, test.error)
  216. } else if err.String() != test.error {
  217. t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v",
  218. i, err.String(), test.error)
  219. }
  220. }
  221. }
  222. }