text_parser_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. {
  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. {
  54. in: `count:42 name:""`,
  55. out: &MyMessage{
  56. Count: Int32(42),
  57. Name: String(""),
  58. },
  59. },
  60. // Quoted string concatenation
  61. {
  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. {
  70. in: `inner: < host: "\0" >` + "\n",
  71. error: `line 1.15: invalid quoted string "\0"`,
  72. },
  73. // Number too large for int64
  74. {
  75. in: "count: 123456789012345678901",
  76. error: "line 1.7: invalid int32: 123456789012345678901",
  77. },
  78. // Number too large for int32
  79. {
  80. in: "count: 1234567890123",
  81. error: "line 1.7: invalid int32: 1234567890123",
  82. },
  83. // Number too large for float32
  84. {
  85. in: "others:< weight: 12345678901234567890123456789012345678901234567890 >",
  86. error: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890",
  87. },
  88. // Number posing as a quoted string
  89. {
  90. in: `inner: < host: 12 >` + "\n",
  91. error: `line 1.15: invalid string: 12`,
  92. },
  93. // Quoted string posing as int32
  94. {
  95. in: `count: "12"`,
  96. error: `line 1.7: invalid int32: "12"`,
  97. },
  98. // Quoted string posing a float32
  99. {
  100. in: `others:< weight: "17.4" >`,
  101. error: `line 1.17: invalid float32: "17.4"`,
  102. },
  103. // Enum
  104. {
  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. {
  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. {
  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. {
  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. {
  144. in: `name "Dave"`,
  145. error: `line 1.5: expected ':', found "\"Dave\""`,
  146. },
  147. // Missing colon for int32 field
  148. {
  149. in: `count 42`,
  150. error: `line 1.6: expected ':', found "42"`,
  151. },
  152. // Missing required field
  153. {
  154. in: ``,
  155. error: `line 1.0: message test_proto.MyMessage missing required field "count"`,
  156. },
  157. // Repeated non-repeated field
  158. {
  159. in: `name: "Rob" name: "Russ"`,
  160. error: `line 1.12: non-repeated field "name" was repeated`,
  161. },
  162. // Group
  163. {
  164. in: `count: 17 SomeGroup { group_field: 12 }`,
  165. out: &MyMessage{
  166. Count: Int32(17),
  167. Somegroup: &MyMessage_SomeGroup{
  168. GroupField: Int32(12),
  169. },
  170. },
  171. },
  172. // Big all-in-one
  173. {
  174. in: "count:42 # Meaning\n" +
  175. `name:"Dave" ` +
  176. `quote:"\"I didn't want to go.\"" ` +
  177. `pet:"bunny" ` +
  178. `pet:"kitty" ` +
  179. `pet:"horsey" ` +
  180. `inner:<` +
  181. ` host:"footrest.syd" ` +
  182. ` port:7001 ` +
  183. ` connected:true ` +
  184. `> ` +
  185. `others:<` +
  186. ` key:3735928559 ` +
  187. ` value:"\x01A\a\f" ` +
  188. `> ` +
  189. `others:<` +
  190. " weight:58.9 # Atomic weight of Co\n" +
  191. ` inner:<` +
  192. ` host:"lesha.mtv" ` +
  193. ` port:8002 ` +
  194. ` >` +
  195. `>`,
  196. out: &MyMessage{
  197. Count: Int32(42),
  198. Name: String("Dave"),
  199. Quote: String(`"I didn't want to go."`),
  200. Pet: []string{"bunny", "kitty", "horsey"},
  201. Inner: &InnerMessage{
  202. Host: String("footrest.syd"),
  203. Port: Int32(7001),
  204. Connected: Bool(true),
  205. },
  206. Others: []*OtherMessage{
  207. &OtherMessage{
  208. Key: Int64(3735928559),
  209. Value: []byte{0x1, 'A', '\a', '\f'},
  210. },
  211. &OtherMessage{
  212. Weight: Float32(58.9),
  213. Inner: &InnerMessage{
  214. Host: String("lesha.mtv"),
  215. Port: Int32(8002),
  216. },
  217. },
  218. },
  219. },
  220. },
  221. }
  222. func TestUnmarshalText(t *testing.T) {
  223. for i, test := range unMarshalTextTests {
  224. pb := new(MyMessage)
  225. err := UnmarshalText(test.in, pb)
  226. if test.error == "" {
  227. // We don't expect failure.
  228. if err != nil {
  229. t.Errorf("Test %d: Unexpected error: %v", i, err)
  230. } else if !reflect.DeepEqual(pb, test.out) {
  231. t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
  232. i, pb, test.out)
  233. }
  234. } else {
  235. // We do expect failure.
  236. if err == nil {
  237. t.Errorf("Test %d: Didn't get expected error: %v", i, test.error)
  238. } else if err.Error() != test.error {
  239. t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v",
  240. i, err, test.error)
  241. }
  242. }
  243. }
  244. }
  245. // Regression test; this caused a panic.
  246. func TestRepeatedEnum(t *testing.T) {
  247. pb := new(RepeatedEnum)
  248. if err := UnmarshalText("color: RED", pb); err != nil {
  249. t.Fatal(err)
  250. }
  251. exp := &RepeatedEnum{
  252. Color: []RepeatedEnum_Color{RepeatedEnum_RED},
  253. }
  254. if !reflect.DeepEqual(pb, exp) {
  255. t.Errorf("Incorrect populated \nHave: %v\nWant: %v", pb, exp)
  256. }
  257. }
  258. var benchInput string
  259. func init() {
  260. benchInput = "count: 4\n"
  261. for i := 0; i < 1000; i++ {
  262. benchInput += "pet: \"fido\"\n"
  263. }
  264. // Check it is valid input.
  265. pb := new(MyMessage)
  266. err := UnmarshalText(benchInput, pb)
  267. if err != nil {
  268. panic("Bad benchmark input: " + err.Error())
  269. }
  270. }
  271. func BenchmarkUnmarshalText(b *testing.B) {
  272. pb := new(MyMessage)
  273. for i := 0; i < b.N; i++ {
  274. UnmarshalText(benchInput, pb)
  275. }
  276. b.SetBytes(int64(len(benchInput)))
  277. }