text_parser_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  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. "math"
  34. "testing"
  35. . "github.com/golang/protobuf/proto"
  36. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  37. . "github.com/golang/protobuf/proto/test_proto"
  38. )
  39. type UnmarshalTextTest struct {
  40. in string
  41. err string // if "", no error expected
  42. out *MyMessage
  43. }
  44. func buildExtStructTest(text string) UnmarshalTextTest {
  45. msg := &MyMessage{
  46. Count: Int32(42),
  47. }
  48. SetExtension(msg, E_Ext_More, &Ext{
  49. Data: String("Hello, world!"),
  50. })
  51. return UnmarshalTextTest{in: text, out: msg}
  52. }
  53. func buildExtDataTest(text string) UnmarshalTextTest {
  54. msg := &MyMessage{
  55. Count: Int32(42),
  56. }
  57. SetExtension(msg, E_Ext_Text, String("Hello, world!"))
  58. SetExtension(msg, E_Ext_Number, Int32(1729))
  59. return UnmarshalTextTest{in: text, out: msg}
  60. }
  61. func buildExtRepStringTest(text string) UnmarshalTextTest {
  62. msg := &MyMessage{
  63. Count: Int32(42),
  64. }
  65. if err := SetExtension(msg, E_Greeting, []string{"bula", "hola"}); err != nil {
  66. panic(err)
  67. }
  68. return UnmarshalTextTest{in: text, out: msg}
  69. }
  70. var unMarshalTextTests = []UnmarshalTextTest{
  71. // Basic
  72. {
  73. in: " count:42\n name:\"Dave\" ",
  74. out: &MyMessage{
  75. Count: Int32(42),
  76. Name: String("Dave"),
  77. },
  78. },
  79. // Empty quoted string
  80. {
  81. in: `count:42 name:""`,
  82. out: &MyMessage{
  83. Count: Int32(42),
  84. Name: String(""),
  85. },
  86. },
  87. // Quoted string concatenation with double quotes
  88. {
  89. in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`,
  90. out: &MyMessage{
  91. Count: Int32(42),
  92. Name: String("My name is elsewhere"),
  93. },
  94. },
  95. // Quoted string concatenation with single quotes
  96. {
  97. in: "count:42 name: 'My name is '\n'elsewhere'",
  98. out: &MyMessage{
  99. Count: Int32(42),
  100. Name: String("My name is elsewhere"),
  101. },
  102. },
  103. // Quoted string concatenations with mixed quotes
  104. {
  105. in: "count:42 name: 'My name is '\n\"elsewhere\"",
  106. out: &MyMessage{
  107. Count: Int32(42),
  108. Name: String("My name is elsewhere"),
  109. },
  110. },
  111. {
  112. in: "count:42 name: \"My name is \"\n'elsewhere'",
  113. out: &MyMessage{
  114. Count: Int32(42),
  115. Name: String("My name is elsewhere"),
  116. },
  117. },
  118. // Quoted string with escaped apostrophe
  119. {
  120. in: `count:42 name: "HOLIDAY - New Year\'s Day"`,
  121. out: &MyMessage{
  122. Count: Int32(42),
  123. Name: String("HOLIDAY - New Year's Day"),
  124. },
  125. },
  126. // Quoted string with single quote
  127. {
  128. in: `count:42 name: 'Roger "The Ramster" Ramjet'`,
  129. out: &MyMessage{
  130. Count: Int32(42),
  131. Name: String(`Roger "The Ramster" Ramjet`),
  132. },
  133. },
  134. // Quoted string with all the accepted special characters from the C++ test
  135. {
  136. in: `count:42 name: ` + "\"\\\"A string with \\' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"",
  137. out: &MyMessage{
  138. Count: Int32(42),
  139. Name: String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces"),
  140. },
  141. },
  142. // Quoted string with quoted backslash
  143. {
  144. in: `count:42 name: "\\'xyz"`,
  145. out: &MyMessage{
  146. Count: Int32(42),
  147. Name: String(`\'xyz`),
  148. },
  149. },
  150. // Quoted string with UTF-8 bytes.
  151. {
  152. in: "count:42 name: '\303\277\302\201\x00\xAB\xCD\xEF'",
  153. out: &MyMessage{
  154. Count: Int32(42),
  155. Name: String("\303\277\302\201\x00\xAB\xCD\xEF"),
  156. },
  157. },
  158. // Quoted string with unicode escapes.
  159. {
  160. in: `count: 42 name: "\u0047\U00000047\uffff\U0010ffff"`,
  161. out: &MyMessage{
  162. Count: Int32(42),
  163. Name: String("GG\uffff\U0010ffff"),
  164. },
  165. },
  166. // Bad quoted string
  167. {
  168. in: `inner: < host: "\0" >` + "\n",
  169. err: `line 1.15: invalid quoted string "\0": \0 requires 2 following digits`,
  170. },
  171. // Bad \u escape
  172. {
  173. in: `count: 42 name: "\u000"`,
  174. err: `line 1.16: invalid quoted string "\u000": \u requires 4 following digits`,
  175. },
  176. // Bad \U escape
  177. {
  178. in: `count: 42 name: "\U0000000"`,
  179. err: `line 1.16: invalid quoted string "\U0000000": \U requires 8 following digits`,
  180. },
  181. // Bad \U escape
  182. {
  183. in: `count: 42 name: "\xxx"`,
  184. err: `line 1.16: invalid quoted string "\xxx": \xxx contains non-hexadecimal digits`,
  185. },
  186. // Number too large for int64
  187. {
  188. in: "count: 1 others { key: 123456789012345678901 }",
  189. err: "line 1.23: invalid int64: 123456789012345678901",
  190. },
  191. // Number too large for int32
  192. {
  193. in: "count: 1234567890123",
  194. err: "line 1.7: invalid int32: 1234567890123",
  195. },
  196. // Number in hexadecimal
  197. {
  198. in: "count: 0x2beef",
  199. out: &MyMessage{
  200. Count: Int32(0x2beef),
  201. },
  202. },
  203. // Number in octal
  204. {
  205. in: "count: 024601",
  206. out: &MyMessage{
  207. Count: Int32(024601),
  208. },
  209. },
  210. // Floating point number with "f" suffix
  211. {
  212. in: "count: 4 others:< weight: 17.0f >",
  213. out: &MyMessage{
  214. Count: Int32(4),
  215. Others: []*OtherMessage{
  216. {
  217. Weight: Float32(17),
  218. },
  219. },
  220. },
  221. },
  222. // Floating point positive infinity
  223. {
  224. in: "count: 4 bigfloat: inf",
  225. out: &MyMessage{
  226. Count: Int32(4),
  227. Bigfloat: Float64(math.Inf(1)),
  228. },
  229. },
  230. // Floating point negative infinity
  231. {
  232. in: "count: 4 bigfloat: -inf",
  233. out: &MyMessage{
  234. Count: Int32(4),
  235. Bigfloat: Float64(math.Inf(-1)),
  236. },
  237. },
  238. // Number too large for float32
  239. {
  240. in: "others:< weight: 12345678901234567890123456789012345678901234567890 >",
  241. err: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890",
  242. },
  243. // Number posing as a quoted string
  244. {
  245. in: `inner: < host: 12 >` + "\n",
  246. err: `line 1.15: invalid string: 12`,
  247. },
  248. // Quoted string posing as int32
  249. {
  250. in: `count: "12"`,
  251. err: `line 1.7: invalid int32: "12"`,
  252. },
  253. // Quoted string posing a float32
  254. {
  255. in: `others:< weight: "17.4" >`,
  256. err: `line 1.17: invalid float32: "17.4"`,
  257. },
  258. // unclosed bracket doesn't cause infinite loop
  259. {
  260. in: `[`,
  261. err: `line 1.0: unclosed type_url or extension name`,
  262. },
  263. // Enum
  264. {
  265. in: `count:42 bikeshed: BLUE`,
  266. out: &MyMessage{
  267. Count: Int32(42),
  268. Bikeshed: MyMessage_BLUE.Enum(),
  269. },
  270. },
  271. // Repeated field
  272. {
  273. in: `count:42 pet: "horsey" pet:"bunny"`,
  274. out: &MyMessage{
  275. Count: Int32(42),
  276. Pet: []string{"horsey", "bunny"},
  277. },
  278. },
  279. // Repeated field with list notation
  280. {
  281. in: `count:42 pet: ["horsey", "bunny"]`,
  282. out: &MyMessage{
  283. Count: Int32(42),
  284. Pet: []string{"horsey", "bunny"},
  285. },
  286. },
  287. // Repeated message with/without colon and <>/{}
  288. {
  289. in: `count:42 others:{} others{} others:<> others:{}`,
  290. out: &MyMessage{
  291. Count: Int32(42),
  292. Others: []*OtherMessage{
  293. {},
  294. {},
  295. {},
  296. {},
  297. },
  298. },
  299. },
  300. // Missing colon for inner message
  301. {
  302. in: `count:42 inner < host: "cauchy.syd" >`,
  303. out: &MyMessage{
  304. Count: Int32(42),
  305. Inner: &InnerMessage{
  306. Host: String("cauchy.syd"),
  307. },
  308. },
  309. },
  310. // Missing colon for string field
  311. {
  312. in: `name "Dave"`,
  313. err: `line 1.5: expected ':', found "\"Dave\""`,
  314. },
  315. // Missing colon for int32 field
  316. {
  317. in: `count 42`,
  318. err: `line 1.6: expected ':', found "42"`,
  319. },
  320. // Missing required field
  321. {
  322. in: `name: "Pawel"`,
  323. err: `proto: required field "test_proto.MyMessage.count" not set`,
  324. out: &MyMessage{
  325. Name: String("Pawel"),
  326. },
  327. },
  328. // Missing required field in a required submessage
  329. {
  330. in: `count: 42 we_must_go_deeper < leo_finally_won_an_oscar <> >`,
  331. err: `proto: required field "test_proto.InnerMessage.host" not set`,
  332. out: &MyMessage{
  333. Count: Int32(42),
  334. WeMustGoDeeper: &RequiredInnerMessage{LeoFinallyWonAnOscar: &InnerMessage{}},
  335. },
  336. },
  337. // Repeated non-repeated field
  338. {
  339. in: `name: "Rob" name: "Russ"`,
  340. err: `line 1.12: non-repeated field "name" was repeated`,
  341. },
  342. // Group
  343. {
  344. in: `count: 17 SomeGroup { group_field: 12 }`,
  345. out: &MyMessage{
  346. Count: Int32(17),
  347. Somegroup: &MyMessage_SomeGroup{
  348. GroupField: Int32(12),
  349. },
  350. },
  351. },
  352. // Semicolon between fields
  353. {
  354. in: `count:3;name:"Calvin"`,
  355. out: &MyMessage{
  356. Count: Int32(3),
  357. Name: String("Calvin"),
  358. },
  359. },
  360. // Comma between fields
  361. {
  362. in: `count:4,name:"Ezekiel"`,
  363. out: &MyMessage{
  364. Count: Int32(4),
  365. Name: String("Ezekiel"),
  366. },
  367. },
  368. // Boolean false
  369. {
  370. in: `count:42 inner { host: "example.com" connected: false }`,
  371. out: &MyMessage{
  372. Count: Int32(42),
  373. Inner: &InnerMessage{
  374. Host: String("example.com"),
  375. Connected: Bool(false),
  376. },
  377. },
  378. },
  379. // Boolean true
  380. {
  381. in: `count:42 inner { host: "example.com" connected: true }`,
  382. out: &MyMessage{
  383. Count: Int32(42),
  384. Inner: &InnerMessage{
  385. Host: String("example.com"),
  386. Connected: Bool(true),
  387. },
  388. },
  389. },
  390. // Boolean 0
  391. {
  392. in: `count:42 inner { host: "example.com" connected: 0 }`,
  393. out: &MyMessage{
  394. Count: Int32(42),
  395. Inner: &InnerMessage{
  396. Host: String("example.com"),
  397. Connected: Bool(false),
  398. },
  399. },
  400. },
  401. // Boolean 1
  402. {
  403. in: `count:42 inner { host: "example.com" connected: 1 }`,
  404. out: &MyMessage{
  405. Count: Int32(42),
  406. Inner: &InnerMessage{
  407. Host: String("example.com"),
  408. Connected: Bool(true),
  409. },
  410. },
  411. },
  412. // Boolean f
  413. {
  414. in: `count:42 inner { host: "example.com" connected: f }`,
  415. out: &MyMessage{
  416. Count: Int32(42),
  417. Inner: &InnerMessage{
  418. Host: String("example.com"),
  419. Connected: Bool(false),
  420. },
  421. },
  422. },
  423. // Boolean t
  424. {
  425. in: `count:42 inner { host: "example.com" connected: t }`,
  426. out: &MyMessage{
  427. Count: Int32(42),
  428. Inner: &InnerMessage{
  429. Host: String("example.com"),
  430. Connected: Bool(true),
  431. },
  432. },
  433. },
  434. // Boolean False
  435. {
  436. in: `count:42 inner { host: "example.com" connected: False }`,
  437. out: &MyMessage{
  438. Count: Int32(42),
  439. Inner: &InnerMessage{
  440. Host: String("example.com"),
  441. Connected: Bool(false),
  442. },
  443. },
  444. },
  445. // Boolean True
  446. {
  447. in: `count:42 inner { host: "example.com" connected: True }`,
  448. out: &MyMessage{
  449. Count: Int32(42),
  450. Inner: &InnerMessage{
  451. Host: String("example.com"),
  452. Connected: Bool(true),
  453. },
  454. },
  455. },
  456. // Extension
  457. buildExtStructTest(`count: 42 [test_proto.Ext.more]:<data:"Hello, world!" >`),
  458. buildExtStructTest(`count: 42 [test_proto.Ext.more] {data:"Hello, world!"}`),
  459. buildExtDataTest(`count: 42 [test_proto.Ext.text]:"Hello, world!" [test_proto.Ext.number]:1729`),
  460. buildExtRepStringTest(`count: 42 [test_proto.greeting]:"bula" [test_proto.greeting]:"hola"`),
  461. // Big all-in-one
  462. {
  463. in: "count:42 # Meaning\n" +
  464. `name:"Dave" ` +
  465. `quote:"\"I didn't want to go.\"" ` +
  466. `pet:"bunny" ` +
  467. `pet:"kitty" ` +
  468. `pet:"horsey" ` +
  469. `inner:<` +
  470. ` host:"footrest.syd" ` +
  471. ` port:7001 ` +
  472. ` connected:true ` +
  473. `> ` +
  474. `others:<` +
  475. ` key:3735928559 ` +
  476. ` value:"\x01A\a\f" ` +
  477. `> ` +
  478. `others:<` +
  479. " weight:58.9 # Atomic weight of Co\n" +
  480. ` inner:<` +
  481. ` host:"lesha.mtv" ` +
  482. ` port:8002 ` +
  483. ` >` +
  484. `>`,
  485. out: &MyMessage{
  486. Count: Int32(42),
  487. Name: String("Dave"),
  488. Quote: String(`"I didn't want to go."`),
  489. Pet: []string{"bunny", "kitty", "horsey"},
  490. Inner: &InnerMessage{
  491. Host: String("footrest.syd"),
  492. Port: Int32(7001),
  493. Connected: Bool(true),
  494. },
  495. Others: []*OtherMessage{
  496. {
  497. Key: Int64(3735928559),
  498. Value: []byte{0x1, 'A', '\a', '\f'},
  499. },
  500. {
  501. Weight: Float32(58.9),
  502. Inner: &InnerMessage{
  503. Host: String("lesha.mtv"),
  504. Port: Int32(8002),
  505. },
  506. },
  507. },
  508. },
  509. },
  510. }
  511. func TestUnmarshalText(t *testing.T) {
  512. for i, test := range unMarshalTextTests {
  513. pb := new(MyMessage)
  514. err := UnmarshalText(test.in, pb)
  515. if test.err == "" {
  516. // We don't expect failure.
  517. if err != nil {
  518. t.Errorf("Test %d: Unexpected error: %v", i, err)
  519. } else if !Equal(pb, test.out) {
  520. t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
  521. i, pb, test.out)
  522. }
  523. } else {
  524. // We do expect failure.
  525. if err == nil {
  526. t.Errorf("Test %d: Didn't get expected error: %v", i, test.err)
  527. } else if err.Error() != test.err {
  528. t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v",
  529. i, err.Error(), test.err)
  530. } else if _, ok := err.(*RequiredNotSetError); ok && test.out != nil && !Equal(pb, test.out) {
  531. t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
  532. i, pb, test.out)
  533. }
  534. }
  535. }
  536. }
  537. func TestUnmarshalTextCustomMessage(t *testing.T) {
  538. msg := &textMessage{}
  539. if err := UnmarshalText("custom", msg); err != nil {
  540. t.Errorf("Unexpected error from custom unmarshal: %v", err)
  541. }
  542. if UnmarshalText("not custom", msg) == nil {
  543. t.Errorf("Didn't get expected error from custom unmarshal")
  544. }
  545. }
  546. // Regression test; this caused a panic.
  547. func TestRepeatedEnum(t *testing.T) {
  548. pb := new(RepeatedEnum)
  549. if err := UnmarshalText("color: RED", pb); err != nil {
  550. t.Fatal(err)
  551. }
  552. exp := &RepeatedEnum{
  553. Color: []RepeatedEnum_Color{RepeatedEnum_RED},
  554. }
  555. if !Equal(pb, exp) {
  556. t.Errorf("Incorrect populated \nHave: %v\nWant: %v", pb, exp)
  557. }
  558. }
  559. func TestProto3TextParsing(t *testing.T) {
  560. m := new(proto3pb.Message)
  561. const in = `name: "Wallace" true_scotsman: true`
  562. want := &proto3pb.Message{
  563. Name: "Wallace",
  564. TrueScotsman: true,
  565. }
  566. if err := UnmarshalText(in, m); err != nil {
  567. t.Fatal(err)
  568. }
  569. if !Equal(m, want) {
  570. t.Errorf("\n got %v\nwant %v", m, want)
  571. }
  572. }
  573. func TestMapParsing(t *testing.T) {
  574. m := new(MessageWithMap)
  575. const in = `name_mapping:<key:1234 value:"Feist"> name_mapping:<key:1 value:"Beatles">` +
  576. `msg_mapping:<key:-4, value:<f: 2.0>,>` + // separating commas are okay
  577. `msg_mapping<key:-2 value<f: 4.0>>` + // no colon after "value"
  578. `msg_mapping:<value:<f: 5.0>>` + // omitted key
  579. `msg_mapping:<key:1>` + // omitted value
  580. `byte_mapping:<key:true value:"so be it">` +
  581. `byte_mapping:<>` // omitted key and value
  582. want := &MessageWithMap{
  583. NameMapping: map[int32]string{
  584. 1: "Beatles",
  585. 1234: "Feist",
  586. },
  587. MsgMapping: map[int64]*FloatingPoint{
  588. -4: {F: Float64(2.0)},
  589. -2: {F: Float64(4.0)},
  590. 0: {F: Float64(5.0)},
  591. 1: nil,
  592. },
  593. ByteMapping: map[bool][]byte{
  594. false: nil,
  595. true: []byte("so be it"),
  596. },
  597. }
  598. if err := UnmarshalText(in, m); err != nil {
  599. t.Fatal(err)
  600. }
  601. if !Equal(m, want) {
  602. t.Errorf("\n got %v\nwant %v", m, want)
  603. }
  604. }
  605. func TestOneofParsing(t *testing.T) {
  606. const in = `name:"Shrek"`
  607. m := new(Communique)
  608. want := &Communique{Union: &Communique_Name{"Shrek"}}
  609. if err := UnmarshalText(in, m); err != nil {
  610. t.Fatal(err)
  611. }
  612. if !Equal(m, want) {
  613. t.Errorf("\n got %v\nwant %v", m, want)
  614. }
  615. const inOverwrite = `name:"Shrek" number:42`
  616. m = new(Communique)
  617. testErr := "line 1.13: field 'number' would overwrite already parsed oneof 'Union'"
  618. if err := UnmarshalText(inOverwrite, m); err == nil {
  619. t.Errorf("TestOneofParsing: Didn't get expected error: %v", testErr)
  620. } else if err.Error() != testErr {
  621. t.Errorf("TestOneofParsing: Incorrect error.\nHave: %v\nWant: %v",
  622. err.Error(), testErr)
  623. }
  624. }
  625. var benchInput string
  626. func init() {
  627. benchInput = "count: 4\n"
  628. for i := 0; i < 1000; i++ {
  629. benchInput += "pet: \"fido\"\n"
  630. }
  631. // Check it is valid input.
  632. pb := new(MyMessage)
  633. err := UnmarshalText(benchInput, pb)
  634. if err != nil {
  635. panic("Bad benchmark input: " + err.Error())
  636. }
  637. }
  638. func BenchmarkUnmarshalText(b *testing.B) {
  639. pb := new(MyMessage)
  640. for i := 0; i < b.N; i++ {
  641. UnmarshalText(benchInput, pb)
  642. }
  643. b.SetBytes(int64(len(benchInput)))
  644. }