encode_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package textpb_test
  5. import (
  6. "math"
  7. "strings"
  8. "testing"
  9. protoV1 "github.com/golang/protobuf/proto"
  10. "github.com/golang/protobuf/v2/encoding/textpb"
  11. "github.com/golang/protobuf/v2/internal/detrand"
  12. "github.com/golang/protobuf/v2/internal/encoding/pack"
  13. "github.com/golang/protobuf/v2/internal/impl"
  14. "github.com/golang/protobuf/v2/internal/scalar"
  15. "github.com/golang/protobuf/v2/proto"
  16. "github.com/google/go-cmp/cmp"
  17. "github.com/google/go-cmp/cmp/cmpopts"
  18. // The legacy package must be imported prior to use of any legacy messages.
  19. // TODO: Remove this when protoV1 registers these hooks for you.
  20. _ "github.com/golang/protobuf/v2/internal/legacy"
  21. "github.com/golang/protobuf/v2/encoding/textpb/testprotos/pb2"
  22. "github.com/golang/protobuf/v2/encoding/textpb/testprotos/pb3"
  23. )
  24. func init() {
  25. // Disable detrand to enable direct comparisons on outputs.
  26. detrand.Disable()
  27. }
  28. func M(m interface{}) proto.Message {
  29. return impl.Export{}.MessageOf(m).Interface()
  30. }
  31. // splitLines is a cmpopts.Option for comparing strings with line breaks.
  32. var splitLines = cmpopts.AcyclicTransformer("SplitLines", func(s string) []string {
  33. return strings.Split(s, "\n")
  34. })
  35. func pb2Enum(i int32) *pb2.Enum {
  36. p := new(pb2.Enum)
  37. *p = pb2.Enum(i)
  38. return p
  39. }
  40. func pb2Enums_NestedEnum(i int32) *pb2.Enums_NestedEnum {
  41. p := new(pb2.Enums_NestedEnum)
  42. *p = pb2.Enums_NestedEnum(i)
  43. return p
  44. }
  45. func TestMarshal(t *testing.T) {
  46. tests := []struct {
  47. desc string
  48. input protoV1.Message
  49. want string
  50. wantErr bool
  51. }{{
  52. desc: "proto2 optional scalar fields not set",
  53. input: &pb2.Scalars{},
  54. want: "\n",
  55. }, {
  56. desc: "proto3 scalar fields not set",
  57. input: &pb3.Scalars{},
  58. want: "\n",
  59. }, {
  60. desc: "proto2 optional scalar fields set to zero values",
  61. input: &pb2.Scalars{
  62. OptBool: scalar.Bool(false),
  63. OptInt32: scalar.Int32(0),
  64. OptInt64: scalar.Int64(0),
  65. OptUint32: scalar.Uint32(0),
  66. OptUint64: scalar.Uint64(0),
  67. OptSint32: scalar.Int32(0),
  68. OptSint64: scalar.Int64(0),
  69. OptFixed32: scalar.Uint32(0),
  70. OptFixed64: scalar.Uint64(0),
  71. OptSfixed32: scalar.Int32(0),
  72. OptSfixed64: scalar.Int64(0),
  73. OptFloat: scalar.Float32(0),
  74. OptDouble: scalar.Float64(0),
  75. OptBytes: []byte{},
  76. OptString: scalar.String(""),
  77. },
  78. want: `opt_bool: false
  79. opt_int32: 0
  80. opt_int64: 0
  81. opt_uint32: 0
  82. opt_uint64: 0
  83. opt_sint32: 0
  84. opt_sint64: 0
  85. opt_fixed32: 0
  86. opt_fixed64: 0
  87. opt_sfixed32: 0
  88. opt_sfixed64: 0
  89. opt_float: 0
  90. opt_double: 0
  91. opt_bytes: ""
  92. opt_string: ""
  93. `,
  94. }, {
  95. desc: "proto3 scalar fields set to zero values",
  96. input: &pb3.Scalars{
  97. SBool: false,
  98. SInt32: 0,
  99. SInt64: 0,
  100. SUint32: 0,
  101. SUint64: 0,
  102. SSint32: 0,
  103. SSint64: 0,
  104. SFixed32: 0,
  105. SFixed64: 0,
  106. SSfixed32: 0,
  107. SSfixed64: 0,
  108. SFloat: 0,
  109. SDouble: 0,
  110. SBytes: []byte{},
  111. SString: "",
  112. },
  113. want: "\n",
  114. }, {
  115. desc: "proto2 optional scalar fields set to some values",
  116. input: &pb2.Scalars{
  117. OptBool: scalar.Bool(true),
  118. OptInt32: scalar.Int32(0xff),
  119. OptInt64: scalar.Int64(0xdeadbeef),
  120. OptUint32: scalar.Uint32(47),
  121. OptUint64: scalar.Uint64(0xdeadbeef),
  122. OptSint32: scalar.Int32(-1001),
  123. OptSint64: scalar.Int64(-0xffff),
  124. OptFixed64: scalar.Uint64(64),
  125. OptSfixed32: scalar.Int32(-32),
  126. // TODO: Update encoder to output same decimals.
  127. OptFloat: scalar.Float32(1.02),
  128. OptDouble: scalar.Float64(1.23e100),
  129. // TODO: Update encoder to not output UTF8 for bytes.
  130. OptBytes: []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
  131. OptString: scalar.String("谷歌"),
  132. },
  133. want: `opt_bool: true
  134. opt_int32: 255
  135. opt_int64: 3735928559
  136. opt_uint32: 47
  137. opt_uint64: 3735928559
  138. opt_sint32: -1001
  139. opt_sint64: -65535
  140. opt_fixed64: 64
  141. opt_sfixed32: -32
  142. opt_float: 1.0199999809265137
  143. opt_double: 1.23e+100
  144. opt_bytes: "谷歌"
  145. opt_string: "谷歌"
  146. `,
  147. }, {
  148. desc: "float32 nan",
  149. input: &pb3.Scalars{
  150. SFloat: float32(math.NaN()),
  151. },
  152. want: "s_float: nan\n",
  153. }, {
  154. desc: "float32 positive infinity",
  155. input: &pb3.Scalars{
  156. SFloat: float32(math.Inf(1)),
  157. },
  158. want: "s_float: inf\n",
  159. }, {
  160. desc: "float32 negative infinity",
  161. input: &pb3.Scalars{
  162. SFloat: float32(math.Inf(-1)),
  163. },
  164. want: "s_float: -inf\n",
  165. }, {
  166. desc: "float64 nan",
  167. input: &pb3.Scalars{
  168. SDouble: math.NaN(),
  169. },
  170. want: "s_double: nan\n",
  171. }, {
  172. desc: "float64 positive infinity",
  173. input: &pb3.Scalars{
  174. SDouble: math.Inf(1),
  175. },
  176. want: "s_double: inf\n",
  177. }, {
  178. desc: "float64 negative infinity",
  179. input: &pb3.Scalars{
  180. SDouble: math.Inf(-1),
  181. },
  182. want: "s_double: -inf\n",
  183. }, {
  184. desc: "proto2 bytes set to empty string",
  185. input: &pb2.Scalars{
  186. OptBytes: []byte(""),
  187. },
  188. want: "opt_bytes: \"\"\n",
  189. }, {
  190. desc: "proto3 bytes set to empty string",
  191. input: &pb3.Scalars{
  192. SBytes: []byte(""),
  193. },
  194. want: "\n",
  195. }, {
  196. desc: "proto2 enum not set",
  197. input: &pb2.Enums{},
  198. want: "\n",
  199. }, {
  200. desc: "proto2 enum set to zero value",
  201. input: &pb2.Enums{
  202. OptEnum: pb2.Enum_UNKNOWN.Enum(),
  203. OptNestedEnum: pb2Enums_NestedEnum(0),
  204. },
  205. want: `opt_enum: UNKNOWN
  206. opt_nested_enum: 0
  207. `,
  208. }, {
  209. desc: "proto2 enum",
  210. input: &pb2.Enums{
  211. OptEnum: pb2.Enum_FIRST.Enum(),
  212. OptNestedEnum: pb2.Enums_UNO.Enum(),
  213. },
  214. want: `opt_enum: FIRST
  215. opt_nested_enum: UNO
  216. `,
  217. }, {
  218. desc: "proto2 enum set to numeric values",
  219. input: &pb2.Enums{
  220. OptEnum: pb2Enum(1),
  221. OptNestedEnum: pb2Enums_NestedEnum(2),
  222. },
  223. want: `opt_enum: FIRST
  224. opt_nested_enum: DOS
  225. `,
  226. }, {
  227. desc: "proto2 enum set to unnamed numeric values",
  228. input: &pb2.Enums{
  229. OptEnum: pb2Enum(101),
  230. OptNestedEnum: pb2Enums_NestedEnum(-101),
  231. },
  232. want: `opt_enum: 101
  233. opt_nested_enum: -101
  234. `,
  235. }, {
  236. desc: "proto3 enum not set",
  237. input: &pb3.Enums{},
  238. want: "\n",
  239. }, {
  240. desc: "proto3 enum set to zero value",
  241. input: &pb3.Enums{
  242. SEnum: pb3.Enum_ZERO,
  243. SNestedEnum: pb3.Enums_CERO,
  244. },
  245. want: "\n",
  246. }, {
  247. desc: "proto3 enum",
  248. input: &pb3.Enums{
  249. SEnum: pb3.Enum_ONE,
  250. SNestedEnum: pb3.Enums_DIEZ,
  251. },
  252. want: `s_enum: ONE
  253. s_nested_enum: DIEZ
  254. `,
  255. }, {
  256. desc: "proto3 enum set to numeric values",
  257. input: &pb3.Enums{
  258. SEnum: 2,
  259. SNestedEnum: 1,
  260. },
  261. want: `s_enum: TWO
  262. s_nested_enum: UNO
  263. `,
  264. }, {
  265. desc: "proto3 enum set to unnamed numeric values",
  266. input: &pb3.Enums{
  267. SEnum: -47,
  268. SNestedEnum: 47,
  269. },
  270. want: `s_enum: -47
  271. s_nested_enum: 47
  272. `,
  273. }, {
  274. desc: "proto2 nested message not set",
  275. input: &pb2.Nests{},
  276. want: "\n",
  277. }, {
  278. desc: "proto2 nested message set to empty",
  279. input: &pb2.Nests{
  280. OptNested: &pb2.Nested{},
  281. Optgroup: &pb2.Nests_OptGroup{},
  282. },
  283. want: `opt_nested: {}
  284. optgroup: {}
  285. `,
  286. }, {
  287. desc: "proto2 nested messages",
  288. input: &pb2.Nests{
  289. OptNested: &pb2.Nested{
  290. OptString: scalar.String("nested message"),
  291. OptNested: &pb2.Nested{
  292. OptString: scalar.String("another nested message"),
  293. },
  294. },
  295. },
  296. want: `opt_nested: {
  297. opt_string: "nested message"
  298. opt_nested: {
  299. opt_string: "another nested message"
  300. }
  301. }
  302. `,
  303. }, {
  304. desc: "proto2 group fields",
  305. input: &pb2.Nests{
  306. Optgroup: &pb2.Nests_OptGroup{
  307. OptBool: scalar.Bool(true),
  308. OptString: scalar.String("inside a group"),
  309. OptNested: &pb2.Nested{
  310. OptString: scalar.String("nested message inside a group"),
  311. },
  312. Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{
  313. OptEnum: pb2.Enum_TENTH.Enum(),
  314. },
  315. },
  316. },
  317. want: `optgroup: {
  318. opt_bool: true
  319. opt_string: "inside a group"
  320. opt_nested: {
  321. opt_string: "nested message inside a group"
  322. }
  323. optnestedgroup: {
  324. opt_enum: TENTH
  325. }
  326. }
  327. `,
  328. }, {
  329. desc: "proto3 nested message not set",
  330. input: &pb3.Nests{},
  331. want: "\n",
  332. }, {
  333. desc: "proto3 nested message",
  334. input: &pb3.Nests{
  335. SNested: &pb3.Nested{
  336. SString: "nested message",
  337. SNested: &pb3.Nested{
  338. SString: "another nested message",
  339. },
  340. },
  341. },
  342. want: `s_nested: {
  343. s_string: "nested message"
  344. s_nested: {
  345. s_string: "another nested message"
  346. }
  347. }
  348. `,
  349. }, {
  350. desc: "oneof fields",
  351. input: &pb2.Oneofs{},
  352. want: "\n",
  353. }, {
  354. desc: "oneof field set to empty string",
  355. input: &pb2.Oneofs{
  356. Union: &pb2.Oneofs_Str{},
  357. },
  358. want: "str: \"\"\n",
  359. }, {
  360. desc: "oneof field set to string",
  361. input: &pb2.Oneofs{
  362. Union: &pb2.Oneofs_Str{
  363. Str: "hello",
  364. },
  365. },
  366. want: "str: \"hello\"\n",
  367. }, {
  368. desc: "oneof field set to empty message",
  369. input: &pb2.Oneofs{
  370. Union: &pb2.Oneofs_Msg{
  371. Msg: &pb2.Nested{},
  372. },
  373. },
  374. want: "msg: {}\n",
  375. }, {
  376. desc: "oneof field set to message",
  377. input: &pb2.Oneofs{
  378. Union: &pb2.Oneofs_Msg{
  379. Msg: &pb2.Nested{
  380. OptString: scalar.String("nested message"),
  381. },
  382. },
  383. },
  384. want: `msg: {
  385. opt_string: "nested message"
  386. }
  387. `,
  388. }, {
  389. desc: "repeated not set",
  390. input: &pb2.Repeats{},
  391. want: "\n",
  392. }, {
  393. desc: "repeated set to empty slices",
  394. input: &pb2.Repeats{
  395. RptBool: []bool{},
  396. RptInt32: []int32{},
  397. RptInt64: []int64{},
  398. RptUint32: []uint32{},
  399. RptUint64: []uint64{},
  400. RptFloat: []float32{},
  401. RptDouble: []float64{},
  402. RptBytes: [][]byte{},
  403. },
  404. want: "\n",
  405. }, {
  406. desc: "repeated set to some values",
  407. input: &pb2.Repeats{
  408. RptBool: []bool{true, false, true, true},
  409. RptInt32: []int32{1, 6, 0, 0},
  410. RptInt64: []int64{-64, 47},
  411. RptUint32: []uint32{0xff, 0xffff},
  412. RptUint64: []uint64{0xdeadbeef},
  413. // TODO: add float32 examples.
  414. RptDouble: []float64{math.NaN(), math.Inf(1), math.Inf(-1), 1.23e-308},
  415. RptString: []string{"hello", "世界"},
  416. RptBytes: [][]byte{
  417. []byte("hello"),
  418. []byte("\xe4\xb8\x96\xe7\x95\x8c"),
  419. },
  420. },
  421. want: `rpt_bool: true
  422. rpt_bool: false
  423. rpt_bool: true
  424. rpt_bool: true
  425. rpt_int32: 1
  426. rpt_int32: 6
  427. rpt_int32: 0
  428. rpt_int32: 0
  429. rpt_int64: -64
  430. rpt_int64: 47
  431. rpt_uint32: 255
  432. rpt_uint32: 65535
  433. rpt_uint64: 3735928559
  434. rpt_double: nan
  435. rpt_double: inf
  436. rpt_double: -inf
  437. rpt_double: 1.23e-308
  438. rpt_string: "hello"
  439. rpt_string: "世界"
  440. rpt_bytes: "hello"
  441. rpt_bytes: "世界"
  442. `,
  443. }, {
  444. desc: "repeated enum",
  445. input: &pb2.Enums{
  446. RptEnum: []pb2.Enum{pb2.Enum_FIRST, 2, pb2.Enum_TENTH, 42},
  447. RptNestedEnum: []pb2.Enums_NestedEnum{2, 47, 10},
  448. },
  449. want: `rpt_enum: FIRST
  450. rpt_enum: SECOND
  451. rpt_enum: TENTH
  452. rpt_enum: 42
  453. rpt_nested_enum: DOS
  454. rpt_nested_enum: 47
  455. rpt_nested_enum: DIEZ
  456. `,
  457. }, {
  458. desc: "repeated nested message set to empty",
  459. input: &pb2.Nests{
  460. RptNested: []*pb2.Nested{},
  461. Rptgroup: []*pb2.Nests_RptGroup{},
  462. },
  463. want: "\n",
  464. }, {
  465. desc: "repeated nested messages",
  466. input: &pb2.Nests{
  467. RptNested: []*pb2.Nested{
  468. {
  469. OptString: scalar.String("repeat nested one"),
  470. },
  471. {
  472. OptString: scalar.String("repeat nested two"),
  473. OptNested: &pb2.Nested{
  474. OptString: scalar.String("inside repeat nested two"),
  475. },
  476. },
  477. {},
  478. },
  479. },
  480. want: `rpt_nested: {
  481. opt_string: "repeat nested one"
  482. }
  483. rpt_nested: {
  484. opt_string: "repeat nested two"
  485. opt_nested: {
  486. opt_string: "inside repeat nested two"
  487. }
  488. }
  489. rpt_nested: {}
  490. `,
  491. }, {
  492. desc: "repeated group fields",
  493. input: &pb2.Nests{
  494. Rptgroup: []*pb2.Nests_RptGroup{
  495. {
  496. RptBool: []bool{true, false},
  497. },
  498. {},
  499. },
  500. },
  501. want: `rptgroup: {
  502. rpt_bool: true
  503. rpt_bool: false
  504. }
  505. rptgroup: {}
  506. `,
  507. }, {
  508. desc: "map fields empty",
  509. input: &pb2.Maps{},
  510. want: "\n",
  511. }, {
  512. desc: "map fields set to empty maps",
  513. input: &pb2.Maps{
  514. Int32ToStr: map[int32]string{},
  515. Sfixed64ToBool: map[int64]bool{},
  516. BoolToUint32: map[bool]uint32{},
  517. Uint64ToEnum: map[uint64]pb2.Enum{},
  518. StrToNested: map[string]*pb2.Nested{},
  519. StrToOneofs: map[string]*pb2.Oneofs{},
  520. },
  521. want: "\n",
  522. }, {
  523. desc: "map fields 1",
  524. input: &pb2.Maps{
  525. Int32ToStr: map[int32]string{
  526. -101: "-101",
  527. 0xff: "0xff",
  528. 0: "zero",
  529. },
  530. Sfixed64ToBool: map[int64]bool{
  531. 0xcafe: true,
  532. 0: false,
  533. },
  534. BoolToUint32: map[bool]uint32{
  535. true: 42,
  536. false: 101,
  537. },
  538. },
  539. want: `int32_to_str: {
  540. key: -101
  541. value: "-101"
  542. }
  543. int32_to_str: {
  544. key: 0
  545. value: "zero"
  546. }
  547. int32_to_str: {
  548. key: 255
  549. value: "0xff"
  550. }
  551. sfixed64_to_bool: {
  552. key: 0
  553. value: false
  554. }
  555. sfixed64_to_bool: {
  556. key: 51966
  557. value: true
  558. }
  559. bool_to_uint32: {
  560. key: false
  561. value: 101
  562. }
  563. bool_to_uint32: {
  564. key: true
  565. value: 42
  566. }
  567. `,
  568. }, {
  569. desc: "map fields 2",
  570. input: &pb2.Maps{
  571. Uint64ToEnum: map[uint64]pb2.Enum{
  572. 1: pb2.Enum_FIRST,
  573. 2: pb2.Enum_SECOND,
  574. 10: pb2.Enum_TENTH,
  575. },
  576. },
  577. want: `uint64_to_enum: {
  578. key: 1
  579. value: FIRST
  580. }
  581. uint64_to_enum: {
  582. key: 2
  583. value: SECOND
  584. }
  585. uint64_to_enum: {
  586. key: 10
  587. value: TENTH
  588. }
  589. `,
  590. }, {
  591. desc: "map fields 3",
  592. input: &pb2.Maps{
  593. StrToNested: map[string]*pb2.Nested{
  594. "nested_one": &pb2.Nested{
  595. OptString: scalar.String("nested in a map"),
  596. },
  597. },
  598. },
  599. want: `str_to_nested: {
  600. key: "nested_one"
  601. value: {
  602. opt_string: "nested in a map"
  603. }
  604. }
  605. `,
  606. }, {
  607. desc: "map fields 4",
  608. input: &pb2.Maps{
  609. StrToOneofs: map[string]*pb2.Oneofs{
  610. "string": &pb2.Oneofs{
  611. Union: &pb2.Oneofs_Str{
  612. Str: "hello",
  613. },
  614. },
  615. "nested": &pb2.Oneofs{
  616. Union: &pb2.Oneofs_Msg{
  617. Msg: &pb2.Nested{
  618. OptString: scalar.String("nested oneof in map field value"),
  619. },
  620. },
  621. },
  622. },
  623. },
  624. want: `str_to_oneofs: {
  625. key: "nested"
  626. value: {
  627. msg: {
  628. opt_string: "nested oneof in map field value"
  629. }
  630. }
  631. }
  632. str_to_oneofs: {
  633. key: "string"
  634. value: {
  635. str: "hello"
  636. }
  637. }
  638. `,
  639. }, {
  640. desc: "proto2 required fields not set",
  641. input: &pb2.Requireds{},
  642. want: "\n",
  643. wantErr: true,
  644. }, {
  645. desc: "proto2 required fields partially set",
  646. input: &pb2.Requireds{
  647. ReqBool: scalar.Bool(false),
  648. ReqFixed32: scalar.Uint32(47),
  649. ReqSfixed64: scalar.Int64(0xbeefcafe),
  650. ReqDouble: scalar.Float64(math.NaN()),
  651. ReqString: scalar.String("hello"),
  652. ReqEnum: pb2.Enum_FIRST.Enum(),
  653. },
  654. want: `req_bool: false
  655. req_fixed32: 47
  656. req_sfixed64: 3203386110
  657. req_double: nan
  658. req_string: "hello"
  659. req_enum: FIRST
  660. `,
  661. wantErr: true,
  662. }, {
  663. desc: "proto2 required fields all set",
  664. input: &pb2.Requireds{
  665. ReqBool: scalar.Bool(false),
  666. ReqFixed32: scalar.Uint32(0),
  667. ReqFixed64: scalar.Uint64(0),
  668. ReqSfixed32: scalar.Int32(0),
  669. ReqSfixed64: scalar.Int64(0),
  670. ReqFloat: scalar.Float32(0),
  671. ReqDouble: scalar.Float64(0),
  672. ReqString: scalar.String(""),
  673. ReqEnum: pb2.Enum_UNKNOWN.Enum(),
  674. ReqBytes: []byte{},
  675. ReqNested: &pb2.Nested{},
  676. },
  677. want: `req_bool: false
  678. req_fixed32: 0
  679. req_fixed64: 0
  680. req_sfixed32: 0
  681. req_sfixed64: 0
  682. req_float: 0
  683. req_double: 0
  684. req_string: ""
  685. req_bytes: ""
  686. req_enum: UNKNOWN
  687. req_nested: {}
  688. `,
  689. }, {
  690. desc: "indirect required field",
  691. input: &pb2.IndirectRequired{
  692. OptNested: &pb2.NestedWithRequired{},
  693. },
  694. want: "opt_nested: {}\n",
  695. wantErr: true,
  696. }, {
  697. desc: "indirect required field in empty repeated",
  698. input: &pb2.IndirectRequired{
  699. RptNested: []*pb2.NestedWithRequired{},
  700. },
  701. want: "\n",
  702. }, {
  703. desc: "indirect required field in repeated",
  704. input: &pb2.IndirectRequired{
  705. RptNested: []*pb2.NestedWithRequired{
  706. &pb2.NestedWithRequired{},
  707. },
  708. },
  709. want: "rpt_nested: {}\n",
  710. wantErr: true,
  711. }, {
  712. desc: "indirect required field in empty map",
  713. input: &pb2.IndirectRequired{
  714. StrToNested: map[string]*pb2.NestedWithRequired{},
  715. },
  716. want: "\n",
  717. }, {
  718. desc: "indirect required field in map",
  719. input: &pb2.IndirectRequired{
  720. StrToNested: map[string]*pb2.NestedWithRequired{
  721. "fail": &pb2.NestedWithRequired{},
  722. },
  723. },
  724. want: `str_to_nested: {
  725. key: "fail"
  726. value: {}
  727. }
  728. `,
  729. wantErr: true,
  730. }, {
  731. desc: "unknown varint and fixed types",
  732. input: &pb2.Scalars{
  733. OptString: scalar.String("this message contains unknown fields"),
  734. XXX_unrecognized: pack.Message{
  735. pack.Tag{101, pack.VarintType}, pack.Bool(true),
  736. pack.Tag{102, pack.VarintType}, pack.Varint(0xff),
  737. pack.Tag{103, pack.Fixed32Type}, pack.Uint32(47),
  738. pack.Tag{104, pack.Fixed64Type}, pack.Int64(0xdeadbeef),
  739. }.Marshal(),
  740. },
  741. want: `opt_string: "this message contains unknown fields"
  742. 101: 1
  743. 102: 255
  744. 103: 47
  745. 104: 3735928559
  746. `,
  747. }, {
  748. desc: "unknown length-delimited",
  749. input: &pb2.Scalars{
  750. XXX_unrecognized: pack.Message{
  751. pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false)},
  752. pack.Tag{102, pack.BytesType}, pack.String("hello world"),
  753. pack.Tag{103, pack.BytesType}, pack.Bytes("\xe4\xb8\x96\xe7\x95\x8c"),
  754. }.Marshal(),
  755. },
  756. want: `101: "\x01\x00"
  757. 102: "hello world"
  758. 103: "世界"
  759. `,
  760. }, {
  761. desc: "unknown group type",
  762. input: &pb2.Scalars{
  763. XXX_unrecognized: pack.Message{
  764. pack.Tag{101, pack.StartGroupType}, pack.Tag{101, pack.EndGroupType},
  765. pack.Tag{102, pack.StartGroupType},
  766. pack.Tag{101, pack.VarintType}, pack.Bool(false),
  767. pack.Tag{102, pack.BytesType}, pack.String("inside a group"),
  768. pack.Tag{102, pack.EndGroupType},
  769. }.Marshal(),
  770. },
  771. want: `101: {}
  772. 102: {
  773. 101: 0
  774. 102: "inside a group"
  775. }
  776. `,
  777. }, {
  778. desc: "unknown unpack repeated field",
  779. input: &pb2.Scalars{
  780. XXX_unrecognized: pack.Message{
  781. pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false), pack.Bool(true)},
  782. pack.Tag{102, pack.BytesType}, pack.String("hello"),
  783. pack.Tag{101, pack.VarintType}, pack.Bool(true),
  784. pack.Tag{102, pack.BytesType}, pack.String("世界"),
  785. }.Marshal(),
  786. },
  787. want: `101: "\x01\x00\x01"
  788. 101: 1
  789. 102: "hello"
  790. 102: "世界"
  791. `,
  792. }}
  793. for _, tt := range tests {
  794. tt := tt
  795. t.Run(tt.desc, func(t *testing.T) {
  796. t.Parallel()
  797. b, err := textpb.Marshal(M(tt.input))
  798. if err != nil && !tt.wantErr {
  799. t.Errorf("Marshal() returned error: %v\n\n", err)
  800. }
  801. if err == nil && tt.wantErr {
  802. t.Error("Marshal() got nil error, want error\n\n")
  803. }
  804. got := string(b)
  805. if tt.want != "" && got != tt.want {
  806. t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
  807. if diff := cmp.Diff(tt.want, got, splitLines); diff != "" {
  808. t.Errorf("Marshal() diff -want +got\n%v\n", diff)
  809. }
  810. }
  811. })
  812. }
  813. }