text_parser_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // Repeated message with/without colon and <>/{}
  120. UnmarshalTextTest{
  121. in: `count:42 others:{} others{} others:<> others:{}`,
  122. out: &MyMessage{
  123. Count: Int32(42),
  124. Others: []*OtherMessage{
  125. &OtherMessage{},
  126. &OtherMessage{},
  127. &OtherMessage{},
  128. &OtherMessage{},
  129. },
  130. },
  131. },
  132. // Missing colon for inner message
  133. UnmarshalTextTest{
  134. in: `count:42 inner < host: "cauchy.syd" >`,
  135. out: &MyMessage{
  136. Count: Int32(42),
  137. Inner: &InnerMessage{
  138. Host: String("cauchy.syd"),
  139. },
  140. },
  141. },
  142. // Missing colon for string field
  143. UnmarshalTextTest{
  144. in: `name "Dave"`,
  145. error: `line 1.5: expected ':', found "\"Dave\""`,
  146. },
  147. // Missing colon for int32 field
  148. UnmarshalTextTest{
  149. in: `count 42`,
  150. error: `line 1.6: expected ':', found "42"`,
  151. },
  152. // Missing required field
  153. UnmarshalTextTest{
  154. in: ``,
  155. error: `line 1.0: message test_proto.MyMessage missing required field "count"`,
  156. },
  157. // Repeated non-repeated field
  158. UnmarshalTextTest{
  159. in: `name: "Rob" name: "Russ"`,
  160. error: `line 1.12: non-repeated field "name" was repeated`,
  161. },
  162. // Big all-in-one
  163. UnmarshalTextTest{
  164. in: "count:42 # Meaning\n" +
  165. `name:"Dave" ` +
  166. `quote:"\"I didn't want to go.\"" ` +
  167. `pet:"bunny" ` +
  168. `pet:"kitty" ` +
  169. `pet:"horsey" ` +
  170. `inner:<` +
  171. ` host:"footrest.syd" ` +
  172. ` port:7001 ` +
  173. ` connected:true ` +
  174. `> ` +
  175. `others:<` +
  176. ` key:3735928559 ` +
  177. ` value:"\x01A\a\f" ` +
  178. `> ` +
  179. `others:<` +
  180. " weight:58.9 # Atomic weight of Co\n" +
  181. ` inner:<` +
  182. ` host:"lesha.mtv" ` +
  183. ` port:8002 ` +
  184. ` >` +
  185. `>`,
  186. out: &MyMessage{
  187. Count: Int32(42),
  188. Name: String("Dave"),
  189. Quote: String(`"I didn't want to go."`),
  190. Pet: []string{"bunny", "kitty", "horsey"},
  191. Inner: &InnerMessage{
  192. Host: String("footrest.syd"),
  193. Port: Int32(7001),
  194. Connected: Bool(true),
  195. },
  196. Others: []*OtherMessage{
  197. &OtherMessage{
  198. Key: Int64(3735928559),
  199. Value: []byte{0x1, 'A', '\a', '\f'},
  200. },
  201. &OtherMessage{
  202. Weight: Float32(58.9),
  203. Inner: &InnerMessage{
  204. Host: String("lesha.mtv"),
  205. Port: Int32(8002),
  206. },
  207. },
  208. },
  209. },
  210. },
  211. }
  212. func TestUnmarshalText(t *testing.T) {
  213. for i, test := range unMarshalTextTests {
  214. pb := new(MyMessage)
  215. err := UnmarshalText(test.in, pb)
  216. if test.error == "" {
  217. // We don't expect failure.
  218. if err != nil {
  219. t.Errorf("Test %d: Unexpected error: %v", i, err)
  220. } else if !reflect.DeepEqual(pb, test.out) {
  221. t.Errorf("Test %d: Incorrect populated \n"+
  222. "Have: %v\nWant: %v",
  223. i, CompactTextString(pb), CompactTextString(test.out))
  224. }
  225. } else {
  226. // We do expect failure.
  227. if err == nil {
  228. t.Errorf("Test %d: Didn't get expected error: %v", i, test.error)
  229. } else if err.String() != test.error {
  230. t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v",
  231. i, err.String(), test.error)
  232. }
  233. }
  234. }
  235. }