text_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 text
  5. import (
  6. "fmt"
  7. "math"
  8. "strings"
  9. "testing"
  10. "unicode/utf8"
  11. "github.com/google/go-cmp/cmp"
  12. "github.com/google/go-cmp/cmp/cmpopts"
  13. "google.golang.org/protobuf/internal/detrand"
  14. "google.golang.org/protobuf/internal/flags"
  15. "google.golang.org/protobuf/reflect/protoreflect"
  16. )
  17. // Disable detrand to enable direct comparisons on outputs.
  18. func init() { detrand.Disable() }
  19. var S = fmt.Sprintf
  20. var V = ValueOf
  21. var ID = func(n protoreflect.Name) Value { return V(n) }
  22. type Lst = []Value
  23. type Msg = [][2]Value
  24. func Test(t *testing.T) {
  25. const space = " \n\r\t"
  26. tests := []struct {
  27. in string
  28. wantVal Value
  29. wantOut string
  30. wantOutBracket string
  31. wantOutASCII string
  32. wantOutIndent string
  33. wantErr string
  34. }{{
  35. in: "",
  36. wantVal: V(Msg{}),
  37. wantOutIndent: "",
  38. }, {
  39. in: S("%s# hello%s", space, space),
  40. wantVal: V(Msg{}),
  41. }, {
  42. in: S("%s# hello\rfoo:bar", space),
  43. wantVal: V(Msg{}),
  44. }, {
  45. // Comments only extend until the newline.
  46. in: S("%s# hello\nfoo:bar", space),
  47. wantVal: V(Msg{{ID("foo"), ID("bar")}}),
  48. wantOut: "foo:bar",
  49. wantOutIndent: "foo: bar\n",
  50. }, {
  51. // NUL is an invalid whitespace since C++ uses C-strings.
  52. in: "\x00",
  53. wantErr: `invalid "\x00" as identifier`,
  54. }, {
  55. in: "foo:0",
  56. wantVal: V(Msg{{ID("foo"), V(uint32(0))}}),
  57. wantOut: "foo:0",
  58. }, {
  59. in: S("%sfoo%s:0", space, space),
  60. wantVal: V(Msg{{ID("foo"), V(uint32(0))}}),
  61. }, {
  62. in: "foo bar:0",
  63. wantErr: `expected ':' after message key`,
  64. }, {
  65. in: "[foo]:0",
  66. wantVal: V(Msg{{V("foo"), V(uint32(0))}}),
  67. wantOut: "[foo]:0",
  68. wantOutIndent: "[foo]: 0\n",
  69. }, {
  70. in: S("%s[%sfoo%s]%s:0", space, space, space, space),
  71. wantVal: V(Msg{{V("foo"), V(uint32(0))}}),
  72. }, {
  73. in: "[proto.package.name]:0",
  74. wantVal: V(Msg{{V("proto.package.name"), V(uint32(0))}}),
  75. wantOut: "[proto.package.name]:0",
  76. wantOutIndent: "[proto.package.name]: 0\n",
  77. }, {
  78. in: S("%s[%sproto.package.name%s]%s:0", space, space, space, space),
  79. wantVal: V(Msg{{V("proto.package.name"), V(uint32(0))}}),
  80. }, {
  81. in: "['sub.domain.com\x2fpath\x2fto\x2fproto.package.name']:0",
  82. wantVal: V(Msg{{V("sub.domain.com/path/to/proto.package.name"), V(uint32(0))}}),
  83. wantOut: "[sub.domain.com/path/to/proto.package.name]:0",
  84. wantOutIndent: "[sub.domain.com/path/to/proto.package.name]: 0\n",
  85. }, {
  86. in: "[\"sub.domain.com\x2fpath\x2fto\x2fproto.package.name\"]:0",
  87. wantVal: V(Msg{{V("sub.domain.com/path/to/proto.package.name"), V(uint32(0))}}),
  88. }, {
  89. in: S("%s[%s'sub.domain.com\x2fpath\x2fto\x2fproto.package.name'%s]%s:0", space, space, space, space),
  90. wantVal: V(Msg{{V("sub.domain.com/path/to/proto.package.name"), V(uint32(0))}}),
  91. }, {
  92. in: S("%s[%s\"sub.domain.com\x2fpath\x2fto\x2fproto.package.name\"%s]%s:0", space, space, space, space),
  93. wantVal: V(Msg{{V("sub.domain.com/path/to/proto.package.name"), V(uint32(0))}}),
  94. }, {
  95. in: `['http://example.com/path/to/proto.package.name']:0`,
  96. wantVal: V(Msg{{V("http://example.com/path/to/proto.package.name"), V(uint32(0))}}),
  97. wantOut: `["http://example.com/path/to/proto.package.name"]:0`,
  98. wantOutIndent: `["http://example.com/path/to/proto.package.name"]: 0` + "\n",
  99. }, {
  100. in: "[proto.package.name:0",
  101. wantErr: `invalid character ':', expected ']' at end of extension name`,
  102. }, {
  103. in: "[proto.package name]:0",
  104. wantErr: `invalid character 'n', expected ']' at end of extension name`,
  105. }, {
  106. in: `["proto.package" "name"]:0`,
  107. wantErr: `invalid character '"', expected ']' at end of extension name`,
  108. }, {
  109. in: `["\z"]`,
  110. wantErr: `invalid escape code "\\z" in string`,
  111. }, {
  112. in: "[$]",
  113. wantErr: `invalid "$" as identifier`,
  114. }, {
  115. in: `[proto.package.]:0`,
  116. wantErr: `invalid "proto.package." as identifier`,
  117. }, {
  118. in: `[/proto.package]:0`,
  119. wantErr: `invalid "/proto.package" as identifier`,
  120. }, {
  121. in: `[proto.package/]:0`,
  122. wantErr: `invalid "proto.package/" as identifier`,
  123. }, {
  124. // This parses fine, but should result in a error later since no
  125. // type name in proto will ever be just a number.
  126. in: "[20]:0",
  127. wantVal: V(Msg{{V("20"), V(uint32(0))}}),
  128. wantOut: "[20]:0",
  129. }, {
  130. in: "20:0",
  131. wantVal: V(Msg{{V(uint32(20)), V(uint32(0))}}),
  132. wantOut: "20:0",
  133. }, {
  134. in: "0x20:0",
  135. wantVal: V(Msg{{V(uint32(0x20)), V(uint32(0))}}),
  136. wantOut: "32:0",
  137. }, {
  138. in: "020:0",
  139. wantVal: V(Msg{{V(uint32(020)), V(uint32(0))}}),
  140. wantOut: "16:0",
  141. }, {
  142. in: "-20:0",
  143. wantErr: `invalid "-20" as identifier`,
  144. }, {
  145. in: `foo:true bar:"s" baz:{} qux:[] wib:id`,
  146. wantVal: V(Msg{
  147. {ID("foo"), V(true)},
  148. {ID("bar"), V("s")},
  149. {ID("baz"), V(Msg{})},
  150. {ID("qux"), V(Lst{})},
  151. {ID("wib"), ID("id")},
  152. }),
  153. wantOut: `foo:true bar:"s" baz:{} qux:[] wib:id`,
  154. wantOutIndent: "foo: true\nbar: \"s\"\nbaz: {}\nqux: []\nwib: id\n",
  155. }, {
  156. in: S(`%sfoo%s:%strue%s %sbar%s:%s"s"%s %sbaz%s:%s<>%s %squx%s:%s[]%s %swib%s:%sid%s`,
  157. space, space, space, space, space, space, space, space, space, space, space, space, space, space, space, space, space, space, space, space),
  158. wantVal: V(Msg{
  159. {ID("foo"), V(true)},
  160. {ID("bar"), V("s")},
  161. {ID("baz"), V(Msg{})},
  162. {ID("qux"), V(Lst{})},
  163. {ID("wib"), ID("id")},
  164. }),
  165. }, {
  166. in: `foo:true;`,
  167. wantVal: V(Msg{{ID("foo"), V(true)}}),
  168. wantOut: "foo:true",
  169. wantOutIndent: "foo: true\n",
  170. }, {
  171. in: `foo:true,`,
  172. wantVal: V(Msg{{ID("foo"), V(true)}}),
  173. }, {
  174. in: `foo:bar;,`,
  175. wantErr: `invalid "," as identifier`,
  176. }, {
  177. in: `foo:bar,;`,
  178. wantErr: `invalid ";" as identifier`,
  179. }, {
  180. in: `footrue`,
  181. wantErr: `unexpected EOF`,
  182. }, {
  183. in: `foo true`,
  184. wantErr: `expected ':' after message key`,
  185. }, {
  186. in: `foo"s"`,
  187. wantErr: `expected ':' after message key`,
  188. }, {
  189. in: `foo "s"`,
  190. wantErr: `expected ':' after message key`,
  191. }, {
  192. in: `foo{}`,
  193. wantVal: V(Msg{{ID("foo"), V(Msg{})}}),
  194. wantOut: "foo:{}",
  195. wantOutBracket: "foo:<>",
  196. wantOutIndent: "foo: {}\n",
  197. }, {
  198. in: `foo {}`,
  199. wantVal: V(Msg{{ID("foo"), V(Msg{})}}),
  200. }, {
  201. in: `foo<>`,
  202. wantVal: V(Msg{{ID("foo"), V(Msg{})}}),
  203. }, {
  204. in: `foo <>`,
  205. wantVal: V(Msg{{ID("foo"), V(Msg{})}}),
  206. }, {
  207. in: `foo[]`,
  208. wantErr: `expected ':' after message key`,
  209. }, {
  210. in: `foo []`,
  211. wantErr: `expected ':' after message key`,
  212. }, {
  213. in: `foo:truebar:true`,
  214. wantErr: `invalid ":" as identifier`,
  215. }, {
  216. in: `foo:"s"bar:true`,
  217. wantVal: V(Msg{{ID("foo"), V("s")}, {ID("bar"), V(true)}}),
  218. wantOut: `foo:"s" bar:true`,
  219. wantOutIndent: "foo: \"s\"\nbar: true\n",
  220. }, {
  221. in: `foo:0bar:true`,
  222. wantErr: `invalid "0bar" as number or bool`,
  223. }, {
  224. in: `foo:{}bar:true`,
  225. wantVal: V(Msg{{ID("foo"), V(Msg{})}, {ID("bar"), V(true)}}),
  226. wantOut: "foo:{} bar:true",
  227. wantOutBracket: "foo:<> bar:true",
  228. wantOutIndent: "foo: {}\nbar: true\n",
  229. }, {
  230. in: `foo:[]bar:true`,
  231. wantVal: V(Msg{{ID("foo"), V(Lst{})}, {ID("bar"), V(true)}}),
  232. }, {
  233. in: `foo{bar:true}`,
  234. wantVal: V(Msg{{ID("foo"), V(Msg{{ID("bar"), V(true)}})}}),
  235. wantOut: "foo:{bar:true}",
  236. wantOutBracket: "foo:<bar:true>",
  237. wantOutIndent: "foo: {\n\tbar: true\n}\n",
  238. }, {
  239. in: `foo<bar:true>`,
  240. wantVal: V(Msg{{ID("foo"), V(Msg{{ID("bar"), V(true)}})}}),
  241. }, {
  242. in: `foo{bar:true,}`,
  243. wantVal: V(Msg{{ID("foo"), V(Msg{{ID("bar"), V(true)}})}}),
  244. }, {
  245. in: `foo{bar:true;}`,
  246. wantVal: V(Msg{{ID("foo"), V(Msg{{ID("bar"), V(true)}})}}),
  247. }, {
  248. in: `foo{`,
  249. wantErr: `unexpected EOF`,
  250. }, {
  251. in: `foo{ `,
  252. wantErr: `unexpected EOF`,
  253. }, {
  254. in: `foo{[`,
  255. wantErr: `unexpected EOF`,
  256. }, {
  257. in: `foo{[ `,
  258. wantErr: `unexpected EOF`,
  259. }, {
  260. in: `foo{bar:true,;}`,
  261. wantErr: `invalid ";" as identifier`,
  262. }, {
  263. in: `foo{bar:true;,}`,
  264. wantErr: `invalid "," as identifier`,
  265. }, {
  266. in: `foo<bar:{}>`,
  267. wantVal: V(Msg{{ID("foo"), V(Msg{{ID("bar"), V(Msg{})}})}}),
  268. wantOut: "foo:{bar:{}}",
  269. wantOutBracket: "foo:<bar:<>>",
  270. wantOutIndent: "foo: {\n\tbar: {}\n}\n",
  271. }, {
  272. in: `foo<bar:{>`,
  273. wantErr: `invalid character '>', expected '}' at end of message`,
  274. }, {
  275. in: `foo<bar:{}`,
  276. wantErr: `unexpected EOF`,
  277. }, {
  278. in: `arr:[]`,
  279. wantVal: V(Msg{{ID("arr"), V(Lst{})}}),
  280. wantOut: "arr:[]",
  281. wantOutBracket: "arr:[]",
  282. wantOutIndent: "arr: []\n",
  283. }, {
  284. in: `arr:[,]`,
  285. wantErr: `invalid "," as number or bool`,
  286. }, {
  287. in: `arr:[0 0]`,
  288. wantErr: `invalid character '0', expected ']' at end of list`,
  289. }, {
  290. in: `arr:["foo" "bar"]`,
  291. wantVal: V(Msg{{ID("arr"), V(Lst{V("foobar")})}}),
  292. wantOut: `arr:["foobar"]`,
  293. wantOutBracket: `arr:["foobar"]`,
  294. wantOutIndent: "arr: [\n\t\"foobar\"\n]\n",
  295. }, {
  296. in: `arr:[0,]`,
  297. wantErr: `invalid "]" as number or bool`,
  298. }, {
  299. in: `arr:[true,0,"",id,[],{}]`,
  300. wantVal: V(Msg{{ID("arr"), V(Lst{
  301. V(true), V(uint32(0)), V(""), ID("id"), V(Lst{}), V(Msg{}),
  302. })}}),
  303. wantOut: `arr:[true,0,"",id,[],{}]`,
  304. wantOutBracket: `arr:[true,0,"",id,[],<>]`,
  305. wantOutIndent: "arr: [\n\ttrue,\n\t0,\n\t\"\",\n\tid,\n\t[],\n\t{}\n]\n",
  306. }, {
  307. in: S(`arr:[%strue%s,%s0%s,%s""%s,%sid%s,%s[]%s,%s{}%s]`,
  308. space, space, space, space, space, space, space, space, space, space, space, space),
  309. wantVal: V(Msg{{ID("arr"), V(Lst{
  310. V(true), V(uint32(0)), V(""), ID("id"), V(Lst{}), V(Msg{}),
  311. })}}),
  312. }, {
  313. in: `arr:[`,
  314. wantErr: `unexpected EOF`,
  315. }, {
  316. in: `{`,
  317. wantErr: `invalid "{" as identifier`,
  318. }, {
  319. in: `<`,
  320. wantErr: `invalid "<" as identifier`,
  321. }, {
  322. in: `[`,
  323. wantErr: "unexpected EOF",
  324. }, {
  325. in: `}`,
  326. wantErr: "1 bytes of unconsumed input",
  327. }, {
  328. in: `>`,
  329. wantErr: "1 bytes of unconsumed input",
  330. }, {
  331. in: `]`,
  332. wantErr: `invalid "]" as identifier`,
  333. }, {
  334. in: `str: "'"`,
  335. wantVal: V(Msg{{ID("str"), V(`'`)}}),
  336. wantOut: `str:"'"`,
  337. }, {
  338. in: `str: '"'`,
  339. wantVal: V(Msg{{ID("str"), V(`"`)}}),
  340. wantOut: `str:"\""`,
  341. }, {
  342. // String that has as few escaped characters as possible.
  343. in: `str: ` + func() string {
  344. var b []byte
  345. for i := 0; i < utf8.RuneSelf; i++ {
  346. switch i {
  347. case 0, '\\', '\n', '\'': // these must be escaped, so ignore them
  348. default:
  349. b = append(b, byte(i))
  350. }
  351. }
  352. return "'" + string(b) + "'"
  353. }(),
  354. wantVal: V(Msg{{ID("str"), V("\x01\x02\x03\x04\x05\x06\a\b\t\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f")}}),
  355. wantOut: `str:"\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` + "`abcdefghijklmnopqrstuvwxyz{|}~\x7f\"",
  356. wantOutASCII: `str:"\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_` + "`abcdefghijklmnopqrstuvwxyz{|}~\x7f\"",
  357. }, {
  358. in: "str: '\xde\xad\xbe\xef'",
  359. wantErr: "invalid UTF-8 detected",
  360. }, {
  361. // Valid UTF-8 wire encoding, but sub-optimal encoding.
  362. in: "str: '\xc0\x80'",
  363. wantErr: "invalid UTF-8 detected",
  364. }, {
  365. // Valid UTF-8 wire encoding, but invalid rune (surrogate pair).
  366. in: "str: '\xed\xa0\x80'",
  367. wantErr: "invalid UTF-8 detected",
  368. }, {
  369. // Valid UTF-8 wire encoding, but invalid rune (above max rune).
  370. in: "str: '\xf7\xbf\xbf\xbf'",
  371. wantErr: "invalid UTF-8 detected",
  372. }, {
  373. // Valid UTF-8 wire encoding of the RuneError rune.
  374. in: "str: '\xef\xbf\xbd'",
  375. wantVal: V(Msg{{ID("str"), V(string(utf8.RuneError))}}),
  376. wantOut: `str:"` + string(utf8.RuneError) + `"`,
  377. wantOutASCII: `str:"\ufffd"`,
  378. }, {
  379. in: "str: 'hello\u1234world'",
  380. wantVal: V(Msg{{ID("str"), V("hello\u1234world")}}),
  381. wantOut: "str:\"hello\u1234world\"",
  382. wantOutASCII: `str:"hello\u1234world"`,
  383. }, {
  384. in: `str: '\"\'\\\?\a\b\n\r\t\v\f\1\12\123\xA\xaB\x12\uAb8f\U0010FFFF'`,
  385. wantVal: V(Msg{{ID("str"), V("\"'\\?\a\b\n\r\t\v\f\x01\nS\n\xab\x12\uab8f\U0010ffff")}}),
  386. wantOut: `str:"\"'\\?\x07\x08\n\r\t\x0b\x0c\x01\nS\n\xab\x12` + "\uab8f\U0010ffff" + `"`,
  387. wantOutASCII: `str:"\"'\\?\x07\x08\n\r\t\x0b\x0c\x01\nS\n\xab\x12\uab8f\U0010ffff"`,
  388. }, {
  389. in: `str: '`,
  390. wantErr: `unexpected EOF`,
  391. }, {
  392. in: `str: '\`,
  393. wantErr: `unexpected EOF`,
  394. }, {
  395. in: `str: '\'`,
  396. wantErr: `unexpected EOF`,
  397. }, {
  398. in: `str: '\8'`,
  399. wantErr: `invalid escape code "\\8" in string`,
  400. }, {
  401. in: `str: '\1x'`,
  402. wantVal: V(Msg{{ID("str"), V("\001x")}}),
  403. wantOut: `str:"\x01x"`,
  404. wantOutASCII: `str:"\x01x"`,
  405. }, {
  406. in: `str: '\12x'`,
  407. wantVal: V(Msg{{ID("str"), V("\012x")}}),
  408. wantOut: `str:"\nx"`,
  409. wantOutASCII: `str:"\nx"`,
  410. }, {
  411. in: `str: '\123x'`,
  412. wantVal: V(Msg{{ID("str"), V("\123x")}}),
  413. wantOut: `str:"Sx"`,
  414. wantOutASCII: `str:"Sx"`,
  415. }, {
  416. in: `str: '\1234x'`,
  417. wantVal: V(Msg{{ID("str"), V("\1234x")}}),
  418. wantOut: `str:"S4x"`,
  419. wantOutASCII: `str:"S4x"`,
  420. }, {
  421. in: `str: '\1'`,
  422. wantVal: V(Msg{{ID("str"), V("\001")}}),
  423. wantOut: `str:"\x01"`,
  424. wantOutASCII: `str:"\x01"`,
  425. }, {
  426. in: `str: '\12'`,
  427. wantVal: V(Msg{{ID("str"), V("\012")}}),
  428. wantOut: `str:"\n"`,
  429. wantOutASCII: `str:"\n"`,
  430. }, {
  431. in: `str: '\123'`,
  432. wantVal: V(Msg{{ID("str"), V("\123")}}),
  433. wantOut: `str:"S"`,
  434. wantOutASCII: `str:"S"`,
  435. }, {
  436. in: `str: '\1234'`,
  437. wantVal: V(Msg{{ID("str"), V("\1234")}}),
  438. wantOut: `str:"S4"`,
  439. wantOutASCII: `str:"S4"`,
  440. }, {
  441. in: `str: '\377'`,
  442. wantVal: V(Msg{{ID("str"), V("\377")}}),
  443. wantOut: `str:"\xff"`,
  444. wantOutASCII: `str:"\xff"`,
  445. }, {
  446. // Overflow octal escape.
  447. in: `str: '\400'`,
  448. wantErr: `invalid octal escape code "\\400" in string`,
  449. }, {
  450. in: `str: '\xfx'`,
  451. wantVal: V(Msg{{ID("str"), V("\x0fx")}}),
  452. wantOut: `str:"\x0fx"`,
  453. wantOutASCII: `str:"\x0fx"`,
  454. }, {
  455. in: `str: '\xffx'`,
  456. wantVal: V(Msg{{ID("str"), V("\xffx")}}),
  457. wantOut: `str:"\xffx"`,
  458. wantOutASCII: `str:"\xffx"`,
  459. }, {
  460. in: `str: '\xfffx'`,
  461. wantVal: V(Msg{{ID("str"), V("\xfffx")}}),
  462. wantOut: `str:"\xfffx"`,
  463. wantOutASCII: `str:"\xfffx"`,
  464. }, {
  465. in: `str: '\xf'`,
  466. wantVal: V(Msg{{ID("str"), V("\x0f")}}),
  467. wantOut: `str:"\x0f"`,
  468. wantOutASCII: `str:"\x0f"`,
  469. }, {
  470. in: `str: '\xff'`,
  471. wantVal: V(Msg{{ID("str"), V("\xff")}}),
  472. wantOut: `str:"\xff"`,
  473. wantOutASCII: `str:"\xff"`,
  474. }, {
  475. in: `str: '\xfff'`,
  476. wantVal: V(Msg{{ID("str"), V("\xfff")}}),
  477. wantOut: `str:"\xfff"`,
  478. wantOutASCII: `str:"\xfff"`,
  479. }, {
  480. in: `str: '\xz'`,
  481. wantErr: `invalid hex escape code "\\x" in string`,
  482. }, {
  483. in: `str: '\uPo'`,
  484. wantErr: `unexpected EOF`,
  485. }, {
  486. in: `str: '\uPoo'`,
  487. wantErr: `invalid Unicode escape code "\\uPoo'" in string`,
  488. }, {
  489. in: `str: '\uPoop'`,
  490. wantErr: `invalid Unicode escape code "\\uPoop" in string`,
  491. }, {
  492. // Unmatched surrogate pair.
  493. in: `str: '\uDEAD'`,
  494. wantErr: `unexpected EOF`, // trying to reader other half
  495. }, {
  496. // Surrogate pair with invalid other half.
  497. in: `str: '\uDEAD\u0000'`,
  498. wantErr: `invalid Unicode escape code "\\u0000" in string`,
  499. }, {
  500. // Properly matched surrogate pair.
  501. in: `str: '\uD800\uDEAD'`,
  502. wantVal: V(Msg{{ID("str"), V("𐊭")}}),
  503. wantOut: `str:"𐊭"`,
  504. wantOutASCII: `str:"\U000102ad"`,
  505. }, {
  506. // Overflow on Unicode rune.
  507. in: `str: '\U00110000'`,
  508. wantErr: `invalid Unicode escape code "\\U00110000" in string`,
  509. }, {
  510. in: `str: '\z'`,
  511. wantErr: `invalid escape code "\\z" in string`,
  512. }, {
  513. // Strings cannot have NUL literal since C-style strings forbid them.
  514. in: "str: '\x00'",
  515. wantErr: `invalid character '\x00' in string`,
  516. }, {
  517. // Strings cannot have newline literal. The C++ permits them if an
  518. // option is specified to allow them. In Go, we always forbid them.
  519. in: "str: '\n'",
  520. wantErr: `invalid character '\n' in string`,
  521. }, {
  522. in: "name: \"My name is \"\n\"elsewhere\"",
  523. wantVal: V(Msg{{ID("name"), V("My name is elsewhere")}}),
  524. wantOut: `name:"My name is elsewhere"`,
  525. wantOutASCII: `name:"My name is elsewhere"`,
  526. }, {
  527. in: "name: 'My name is '\n'elsewhere'",
  528. wantVal: V(Msg{{ID("name"), V("My name is elsewhere")}}),
  529. }, {
  530. in: "name: 'My name is '\n\"elsewhere\"",
  531. wantVal: V(Msg{{ID("name"), V("My name is elsewhere")}}),
  532. }, {
  533. in: "name: \"My name is \"\n'elsewhere'",
  534. wantVal: V(Msg{{ID("name"), V("My name is elsewhere")}}),
  535. }, {
  536. in: "name: \"My \"'name '\"is \"\n'elsewhere'",
  537. wantVal: V(Msg{{ID("name"), V("My name is elsewhere")}}),
  538. }, {
  539. in: `crazy:"x'"'\""\''"'z"`,
  540. wantVal: V(Msg{{ID("crazy"), V(`x'""''z`)}}),
  541. }, {
  542. in: `nums: [t,T,true,True,TRUE,f,F,false,False,FALSE]`,
  543. wantVal: V(Msg{{ID("nums"), V(Lst{
  544. V(true),
  545. ID("T"),
  546. V(true),
  547. V(true),
  548. ID("TRUE"),
  549. V(false),
  550. ID("F"),
  551. V(false),
  552. V(false),
  553. ID("FALSE"),
  554. })}}),
  555. wantOut: "nums:[true,T,true,true,TRUE,false,F,false,false,FALSE]",
  556. wantOutIndent: "nums: [\n\ttrue,\n\tT,\n\ttrue,\n\ttrue,\n\tTRUE,\n\tfalse,\n\tF,\n\tfalse,\n\tfalse,\n\tFALSE\n]\n",
  557. }, {
  558. in: `nums: [nan,inf,-inf,NaN,NAN,Inf,INF]`,
  559. wantVal: V(Msg{{ID("nums"), V(Lst{
  560. V(math.NaN()),
  561. V(math.Inf(+1)),
  562. V(math.Inf(-1)),
  563. ID("NaN"),
  564. ID("NAN"),
  565. ID("Inf"),
  566. ID("INF"),
  567. })}}),
  568. wantOut: "nums:[nan,inf,-inf,NaN,NAN,Inf,INF]",
  569. wantOutIndent: "nums: [\n\tnan,\n\tinf,\n\t-inf,\n\tNaN,\n\tNAN,\n\tInf,\n\tINF\n]\n",
  570. }, {
  571. // C++ permits this, but we currently reject this.
  572. in: `num: -nan`,
  573. wantErr: `invalid "-nan" as number or bool`,
  574. }, {
  575. in: `nums: [0,-0,-9876543210,9876543210,0x0,0x0123456789abcdef,-0x0123456789abcdef,01234567,-01234567]`,
  576. wantVal: V(Msg{{ID("nums"), V(Lst{
  577. V(uint32(0)),
  578. V(int32(-0)),
  579. V(int64(-9876543210)),
  580. V(uint64(9876543210)),
  581. V(uint32(0x0)),
  582. V(uint64(0x0123456789abcdef)),
  583. V(int64(-0x0123456789abcdef)),
  584. V(uint64(01234567)),
  585. V(int64(-01234567)),
  586. })}}),
  587. wantOut: "nums:[0,0,-9876543210,9876543210,0,81985529216486895,-81985529216486895,342391,-342391]",
  588. wantOutIndent: "nums: [\n\t0,\n\t0,\n\t-9876543210,\n\t9876543210,\n\t0,\n\t81985529216486895,\n\t-81985529216486895,\n\t342391,\n\t-342391\n]\n",
  589. }, {
  590. in: `nums: [0.,0f,1f,10f,-0f,-1f,-10f,1.0,0.1e-3,1.5e+5,1e10,.0]`,
  591. wantVal: V(Msg{{ID("nums"), V(Lst{
  592. V(0.0),
  593. V(0.0),
  594. V(1.0),
  595. V(10.0),
  596. V(-0.0),
  597. V(-1.0),
  598. V(-10.0),
  599. V(1.0),
  600. V(0.1e-3),
  601. V(1.5e+5),
  602. V(1.0e+10),
  603. V(0.0),
  604. })}}),
  605. wantOut: "nums:[0,0,1,10,0,-1,-10,1,0.0001,150000,1e+10,0]",
  606. wantOutIndent: "nums: [\n\t0,\n\t0,\n\t1,\n\t10,\n\t0,\n\t-1,\n\t-10,\n\t1,\n\t0.0001,\n\t150000,\n\t1e+10,\n\t0\n]\n",
  607. }, {
  608. in: `nums: [0xbeefbeef,0xbeefbeefbeefbeef]`,
  609. wantVal: V(Msg{{ID("nums"), func() Value {
  610. if flags.ProtoLegacy {
  611. return V(Lst{V(int32(-1091584273)), V(int64(-4688318750159552785))})
  612. } else {
  613. return V(Lst{V(uint32(0xbeefbeef)), V(uint64(0xbeefbeefbeefbeef))})
  614. }
  615. }()}}),
  616. }, {
  617. in: `num: +0`,
  618. wantErr: `invalid "+0" as number or bool`,
  619. }, {
  620. in: `num: 01.1234`,
  621. wantErr: `invalid "01.1234" as number or bool`,
  622. }, {
  623. in: `num: 0x`,
  624. wantErr: `invalid "0x" as number or bool`,
  625. }, {
  626. in: `num: 0xX`,
  627. wantErr: `invalid "0xX" as number or bool`,
  628. }, {
  629. in: `num: 0800`,
  630. wantErr: `invalid "0800" as number or bool`,
  631. }, {
  632. in: `num: true.`,
  633. wantErr: `invalid "true." as number or bool`,
  634. }, {
  635. in: `num: .`,
  636. wantErr: `parsing ".": invalid syntax`,
  637. }, {
  638. in: `num: -.`,
  639. wantErr: `parsing "-.": invalid syntax`,
  640. }, {
  641. in: `num: 1e10000`,
  642. wantErr: `parsing "1e10000": value out of range`,
  643. }, {
  644. in: `num: 99999999999999999999`,
  645. wantErr: `parsing "99999999999999999999": value out of range`,
  646. }, {
  647. in: `num: -99999999999999999999`,
  648. wantErr: `parsing "-99999999999999999999": value out of range`,
  649. }, {
  650. in: "x: -",
  651. wantErr: `syntax error (line 1:5)`,
  652. }, {
  653. in: "x:[\"💩\"x",
  654. wantErr: `syntax error (line 1:7)`,
  655. }, {
  656. in: "x:\n\n[\"🔥🔥🔥\"x",
  657. wantErr: `syntax error (line 3:7)`,
  658. }, {
  659. in: "x:[\"👍🏻👍🏿\"x",
  660. wantErr: `syntax error (line 1:10)`, // multi-rune emojis; could be column:8
  661. }, {
  662. in: `
  663. firstName : "John",
  664. lastName : "Smith" ,
  665. isAlive : true,
  666. age : 27,
  667. address { # missing colon is okay for messages
  668. streetAddress : "21 2nd Street" ,
  669. city : "New York" ,
  670. state : "NY" ,
  671. postalCode : "10021-3100" ; # trailing semicolon is okay
  672. },
  673. phoneNumbers : [ {
  674. type : "home" ,
  675. number : "212 555-1234"
  676. } , {
  677. type : "office" ,
  678. number : "646 555-4567"
  679. } , {
  680. type : "mobile" ,
  681. number : "123 456-7890" , # trailing comma is okay
  682. } ],
  683. children : [] ,
  684. spouse : null`,
  685. wantVal: V(Msg{
  686. {ID("firstName"), V("John")},
  687. {ID("lastName"), V("Smith")},
  688. {ID("isAlive"), V(true)},
  689. {ID("age"), V(27.0)},
  690. {ID("address"), V(Msg{
  691. {ID("streetAddress"), V("21 2nd Street")},
  692. {ID("city"), V("New York")},
  693. {ID("state"), V("NY")},
  694. {ID("postalCode"), V("10021-3100")},
  695. })},
  696. {ID("phoneNumbers"), V([]Value{
  697. V(Msg{
  698. {ID("type"), V("home")},
  699. {ID("number"), V("212 555-1234")},
  700. }),
  701. V(Msg{
  702. {ID("type"), V("office")},
  703. {ID("number"), V("646 555-4567")},
  704. }),
  705. V(Msg{
  706. {ID("type"), V("mobile")},
  707. {ID("number"), V("123 456-7890")},
  708. }),
  709. })},
  710. {ID("children"), V([]Value{})},
  711. {ID("spouse"), V(protoreflect.Name("null"))},
  712. }),
  713. wantOut: `firstName:"John" lastName:"Smith" isAlive:true age:27 address:{streetAddress:"21 2nd Street" city:"New York" state:"NY" postalCode:"10021-3100"} phoneNumbers:[{type:"home" number:"212 555-1234"},{type:"office" number:"646 555-4567"},{type:"mobile" number:"123 456-7890"}] children:[] spouse:null`,
  714. wantOutBracket: `firstName:"John" lastName:"Smith" isAlive:true age:27 address:<streetAddress:"21 2nd Street" city:"New York" state:"NY" postalCode:"10021-3100"> phoneNumbers:[<type:"home" number:"212 555-1234">,<type:"office" number:"646 555-4567">,<type:"mobile" number:"123 456-7890">] children:[] spouse:null`,
  715. wantOutIndent: `firstName: "John"
  716. lastName: "Smith"
  717. isAlive: true
  718. age: 27
  719. address: {
  720. streetAddress: "21 2nd Street"
  721. city: "New York"
  722. state: "NY"
  723. postalCode: "10021-3100"
  724. }
  725. phoneNumbers: [
  726. {
  727. type: "home"
  728. number: "212 555-1234"
  729. },
  730. {
  731. type: "office"
  732. number: "646 555-4567"
  733. },
  734. {
  735. type: "mobile"
  736. number: "123 456-7890"
  737. }
  738. ]
  739. children: []
  740. spouse: null
  741. `,
  742. }}
  743. opts := cmp.Options{
  744. cmpopts.EquateEmpty(),
  745. // Transform composites (List and Message).
  746. cmp.FilterValues(func(x, y Value) bool {
  747. return (x.Type() == List && y.Type() == List) || (x.Type() == Message && y.Type() == Message)
  748. }, cmp.Transformer("", func(v Value) interface{} {
  749. if v.Type() == List {
  750. return v.List()
  751. } else {
  752. return v.Message()
  753. }
  754. })),
  755. // Compare scalars (Bool, Int, Uint, Float, String, Name).
  756. cmp.FilterValues(func(x, y Value) bool {
  757. return !(x.Type() == List && y.Type() == List) && !(x.Type() == Message && y.Type() == Message)
  758. }, cmp.Comparer(func(x, y Value) bool {
  759. if x.Type() == List || x.Type() == Message || y.Type() == List || y.Type() == Message {
  760. return false
  761. }
  762. // Ensure golden value is always in x variable.
  763. if len(x.raw) > 0 {
  764. x, y = y, x
  765. }
  766. switch x.Type() {
  767. case Bool:
  768. want, _ := x.Bool()
  769. got, ok := y.Bool()
  770. return got == want && ok
  771. case Int:
  772. want, _ := x.Int(true)
  773. got, ok := y.Int(want < math.MinInt32 || math.MaxInt32 < want)
  774. return got == want && ok
  775. case Uint:
  776. want, _ := x.Uint(true)
  777. got, ok := y.Uint(math.MaxUint32 < want)
  778. return got == want && ok
  779. case Float32, Float64:
  780. want, _ := x.Float(true)
  781. got, ok := y.Float(math.MaxFloat32 < math.Abs(want))
  782. if math.IsNaN(got) || math.IsNaN(want) {
  783. return math.IsNaN(got) == math.IsNaN(want)
  784. }
  785. return got == want && ok
  786. case Name:
  787. want, _ := x.Name()
  788. got, ok := y.Name()
  789. return got == want && ok
  790. default:
  791. return x.String() == y.String()
  792. }
  793. })),
  794. }
  795. for _, tt := range tests {
  796. t.Run("", func(t *testing.T) {
  797. if tt.in != "" || tt.wantVal.Type() != 0 || tt.wantErr != "" {
  798. gotVal, err := Unmarshal([]byte(tt.in))
  799. if err == nil {
  800. if tt.wantErr != "" {
  801. t.Errorf("Unmarshal(): got nil error, want %v", tt.wantErr)
  802. }
  803. } else {
  804. if tt.wantErr == "" {
  805. t.Errorf("Unmarshal(): got %v, want nil error", err)
  806. } else if !strings.Contains(err.Error(), tt.wantErr) {
  807. t.Errorf("Unmarshal(): got %v, want %v", err, tt.wantErr)
  808. }
  809. }
  810. if diff := cmp.Diff(gotVal, tt.wantVal, opts); diff != "" {
  811. t.Errorf("Unmarshal(): output mismatch (-got +want):\n%s", diff)
  812. }
  813. }
  814. if tt.wantOut != "" {
  815. gotOut, err := Marshal(tt.wantVal, "", [2]byte{0, 0}, false)
  816. if err != nil {
  817. t.Errorf("Marshal(): got %v, want nil error", err)
  818. }
  819. if string(gotOut) != tt.wantOut {
  820. t.Errorf("Marshal():\ngot: %s\nwant: %s", gotOut, tt.wantOut)
  821. }
  822. }
  823. if tt.wantOutBracket != "" {
  824. gotOut, err := Marshal(tt.wantVal, "", [2]byte{'<', '>'}, false)
  825. if err != nil {
  826. t.Errorf("Marshal(Bracket): got %v, want nil error", err)
  827. }
  828. if string(gotOut) != tt.wantOutBracket {
  829. t.Errorf("Marshal(Bracket):\ngot: %s\nwant: %s", gotOut, tt.wantOutBracket)
  830. }
  831. }
  832. if tt.wantOutASCII != "" {
  833. gotOut, err := Marshal(tt.wantVal, "", [2]byte{0, 0}, true)
  834. if err != nil {
  835. t.Errorf("Marshal(ASCII): got %v, want nil error", err)
  836. }
  837. if string(gotOut) != tt.wantOutASCII {
  838. t.Errorf("Marshal(ASCII):\ngot: %s\nwant: %s", gotOut, tt.wantOutASCII)
  839. }
  840. }
  841. if tt.wantOutIndent != "" {
  842. gotOut, err := Marshal(tt.wantVal, "\t", [2]byte{0, 0}, false)
  843. if err != nil {
  844. t.Errorf("Marshal(Indent): got %v, want nil error", err)
  845. }
  846. if string(gotOut) != tt.wantOutIndent {
  847. t.Errorf("Marshal(Indent):\ngot: %s\nwant: %s", gotOut, tt.wantOutIndent)
  848. }
  849. }
  850. })
  851. }
  852. }