decode_test.go 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. // Copyright 2019 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 json_test
  5. import (
  6. "fmt"
  7. "math"
  8. "strings"
  9. "testing"
  10. "unicode/utf8"
  11. "github.com/google/go-cmp/cmp"
  12. "google.golang.org/protobuf/internal/encoding/json"
  13. )
  14. type R struct {
  15. // E is expected error substring from calling Decoder.Read if set.
  16. E string
  17. // V is one of the checker implementations that validates the token value.
  18. V checker
  19. // P is expected Token.Pos() if set > 0.
  20. P int
  21. // RS is expected result from Token.RawString() if not empty.
  22. RS string
  23. }
  24. // checker defines API for Token validation.
  25. type checker interface {
  26. // check checks and expects for token API call to return and compare
  27. // against implementation-stored value. Returns empty string if success,
  28. // else returns error message describing the error.
  29. check(json.Token) string
  30. }
  31. // checkers that checks the token kind only.
  32. var (
  33. EOF = kindOnly{json.EOF}
  34. Null = kindOnly{json.Null}
  35. ObjectOpen = kindOnly{json.ObjectOpen}
  36. ObjectClose = kindOnly{json.ObjectClose}
  37. ArrayOpen = kindOnly{json.ArrayOpen}
  38. ArrayClose = kindOnly{json.ArrayClose}
  39. )
  40. type kindOnly struct {
  41. want json.Kind
  42. }
  43. func (x kindOnly) check(tok json.Token) string {
  44. if got := tok.Kind(); got != x.want {
  45. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, x.want)
  46. }
  47. return ""
  48. }
  49. type Name struct {
  50. val string
  51. }
  52. func (x Name) check(tok json.Token) string {
  53. if got := tok.Kind(); got != json.Name {
  54. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Name)
  55. }
  56. if got := tok.Name(); got != x.val {
  57. return fmt.Sprintf("Token.Name(): got %v, want %v", got, x.val)
  58. }
  59. return ""
  60. }
  61. type Bool struct {
  62. val bool
  63. }
  64. func (x Bool) check(tok json.Token) string {
  65. if got := tok.Kind(); got != json.Bool {
  66. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Bool)
  67. }
  68. if got := tok.Bool(); got != x.val {
  69. return fmt.Sprintf("Token.Bool(): got %v, want %v", got, x.val)
  70. }
  71. return ""
  72. }
  73. type Str struct {
  74. val string
  75. }
  76. func (x Str) check(tok json.Token) string {
  77. if got := tok.Kind(); got != json.String {
  78. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.String)
  79. }
  80. if got := tok.ParsedString(); got != x.val {
  81. return fmt.Sprintf("Token.ParsedString(): got %v, want %v", got, x.val)
  82. }
  83. return ""
  84. }
  85. type F64 struct {
  86. val float64
  87. }
  88. func (x F64) check(tok json.Token) string {
  89. if got := tok.Kind(); got != json.Number {
  90. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Number)
  91. }
  92. got, ok := tok.Float(64)
  93. if !ok {
  94. return fmt.Sprintf("Token.Float(64): returned not ok")
  95. }
  96. if got != x.val {
  97. return fmt.Sprintf("Token.Float(64): got %v, want %v", got, x.val)
  98. }
  99. return ""
  100. }
  101. type F32 struct {
  102. val float32
  103. }
  104. func (x F32) check(tok json.Token) string {
  105. if got := tok.Kind(); got != json.Number {
  106. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Number)
  107. }
  108. got, ok := tok.Float(32)
  109. if !ok {
  110. return fmt.Sprintf("Token.Float(32): returned not ok")
  111. }
  112. if float32(got) != x.val {
  113. return fmt.Sprintf("Token.Float(32): got %v, want %v", got, x.val)
  114. }
  115. return ""
  116. }
  117. // NotF64 is a checker to validate a Number token where Token.Float(64) returns not ok.
  118. var NotF64 = xf64{}
  119. type xf64 struct{}
  120. func (x xf64) check(tok json.Token) string {
  121. if got := tok.Kind(); got != json.Number {
  122. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Number)
  123. }
  124. _, ok := tok.Float(64)
  125. if ok {
  126. return fmt.Sprintf("Token.Float(64): returned ok")
  127. }
  128. return ""
  129. }
  130. // NotF32 is a checker to validate a Number token where Token.Float(32) returns not ok.
  131. var NotF32 = xf32{}
  132. type xf32 struct{}
  133. func (x xf32) check(tok json.Token) string {
  134. if got := tok.Kind(); got != json.Number {
  135. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Number)
  136. }
  137. _, ok := tok.Float(32)
  138. if ok {
  139. return fmt.Sprintf("Token.Float(32): returned ok")
  140. }
  141. return ""
  142. }
  143. type I64 struct {
  144. val int64
  145. }
  146. func (x I64) check(tok json.Token) string {
  147. if got := tok.Kind(); got != json.Number {
  148. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Number)
  149. }
  150. got, ok := tok.Int(64)
  151. if !ok {
  152. return fmt.Sprintf("Token.Int(64): returned not ok")
  153. }
  154. if got != x.val {
  155. return fmt.Sprintf("Token.Int(64): got %v, want %v", got, x.val)
  156. }
  157. return ""
  158. }
  159. type I32 struct {
  160. val int32
  161. }
  162. func (x I32) check(tok json.Token) string {
  163. if got := tok.Kind(); got != json.Number {
  164. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Number)
  165. }
  166. got, ok := tok.Int(32)
  167. if !ok {
  168. return fmt.Sprintf("Token.Int(32): returned not ok")
  169. }
  170. if int32(got) != x.val {
  171. return fmt.Sprintf("Token.Int(32): got %v, want %v", got, x.val)
  172. }
  173. return ""
  174. }
  175. // NotI64 is a checker to validate a Number token where Token.Int(64) returns not ok.
  176. var NotI64 = xi64{}
  177. type xi64 struct{}
  178. func (x xi64) check(tok json.Token) string {
  179. if got := tok.Kind(); got != json.Number {
  180. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Number)
  181. }
  182. _, ok := tok.Int(64)
  183. if ok {
  184. return fmt.Sprintf("Token.Int(64): returned ok")
  185. }
  186. return ""
  187. }
  188. // NotI32 is a checker to validate a Number token where Token.Int(32) returns not ok.
  189. var NotI32 = xi32{}
  190. type xi32 struct{}
  191. func (x xi32) check(tok json.Token) string {
  192. if got := tok.Kind(); got != json.Number {
  193. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Number)
  194. }
  195. _, ok := tok.Int(32)
  196. if ok {
  197. return fmt.Sprintf("Token.Int(32): returned ok")
  198. }
  199. return ""
  200. }
  201. type Ui64 struct {
  202. val uint64
  203. }
  204. func (x Ui64) check(tok json.Token) string {
  205. if got := tok.Kind(); got != json.Number {
  206. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Number)
  207. }
  208. got, ok := tok.Uint(64)
  209. if !ok {
  210. return fmt.Sprintf("Token.Uint(64): returned not ok")
  211. }
  212. if got != x.val {
  213. return fmt.Sprintf("Token.Uint(64): got %v, want %v", got, x.val)
  214. }
  215. return ""
  216. }
  217. type Ui32 struct {
  218. val uint32
  219. }
  220. func (x Ui32) check(tok json.Token) string {
  221. if got := tok.Kind(); got != json.Number {
  222. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Number)
  223. }
  224. got, ok := tok.Uint(32)
  225. if !ok {
  226. return fmt.Sprintf("Token.Uint(32): returned not ok")
  227. }
  228. if uint32(got) != x.val {
  229. return fmt.Sprintf("Token.Uint(32): got %v, want %v", got, x.val)
  230. }
  231. return ""
  232. }
  233. // NotUi64 is a checker to validate a Number token where Token.Uint(64) returns not ok.
  234. var NotUi64 = xui64{}
  235. type xui64 struct{}
  236. func (x xui64) check(tok json.Token) string {
  237. if got := tok.Kind(); got != json.Number {
  238. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Number)
  239. }
  240. _, ok := tok.Uint(64)
  241. if ok {
  242. return fmt.Sprintf("Token.Uint(64): returned ok")
  243. }
  244. return ""
  245. }
  246. // NotI32 is a checker to validate a Number token where Token.Uint(32) returns not ok.
  247. var NotUi32 = xui32{}
  248. type xui32 struct{}
  249. func (x xui32) check(tok json.Token) string {
  250. if got := tok.Kind(); got != json.Number {
  251. return fmt.Sprintf("Token.Kind(): got %v, want %v", got, json.Number)
  252. }
  253. _, ok := tok.Uint(32)
  254. if ok {
  255. return fmt.Sprintf("Token.Uint(32): returned ok")
  256. }
  257. return ""
  258. }
  259. var errEOF = json.ErrUnexpectedEOF.Error()
  260. func TestDecoder(t *testing.T) {
  261. const space = " \n\r\t"
  262. tests := []struct {
  263. in string
  264. // want is a list of expected values returned from calling
  265. // Decoder.Read. An item makes the test code invoke
  266. // Decoder.Read and compare against R.E for error returned or use R.V to
  267. // validate the returned Token object.
  268. want []R
  269. }{
  270. {
  271. in: ``,
  272. want: []R{{V: EOF}},
  273. },
  274. {
  275. in: space,
  276. want: []R{{V: EOF}},
  277. },
  278. {
  279. // Calling Read after EOF will keep returning EOF for
  280. // succeeding Read calls.
  281. in: space,
  282. want: []R{
  283. {V: EOF},
  284. {V: EOF},
  285. {V: EOF},
  286. },
  287. },
  288. // JSON literals.
  289. {
  290. in: space + `null` + space,
  291. want: []R{
  292. {V: Null, P: len(space), RS: `null`},
  293. {V: EOF},
  294. },
  295. },
  296. {
  297. in: space + `true` + space,
  298. want: []R{
  299. {V: Bool{true}},
  300. {V: EOF},
  301. },
  302. },
  303. {
  304. in: space + `false` + space,
  305. want: []R{
  306. {V: Bool{false}},
  307. {V: EOF},
  308. },
  309. },
  310. {
  311. // Error returned will produce the same error again.
  312. in: space + `foo` + space,
  313. want: []R{
  314. {E: `invalid value foo`},
  315. {E: `invalid value foo`},
  316. },
  317. },
  318. // JSON strings.
  319. {
  320. in: space + `""` + space,
  321. want: []R{
  322. {V: Str{}},
  323. {V: EOF},
  324. },
  325. },
  326. {
  327. in: space + `"hello"` + space,
  328. want: []R{
  329. {V: Str{"hello"}, RS: `"hello"`},
  330. {V: EOF},
  331. },
  332. },
  333. {
  334. in: `"hello`,
  335. want: []R{{E: errEOF}},
  336. },
  337. {
  338. in: "\"\x00\"",
  339. want: []R{{E: `invalid character '\x00' in string`}},
  340. },
  341. {
  342. in: "\"\u0031\u0032\"",
  343. want: []R{
  344. {V: Str{"12"}, RS: "\"\u0031\u0032\""},
  345. {V: EOF},
  346. },
  347. },
  348. {
  349. // Invalid UTF-8 error is returned in ReadString instead of Read.
  350. in: "\"\xff\"",
  351. want: []R{{E: `syntax error (line 1:1): invalid UTF-8 in string`}},
  352. },
  353. {
  354. in: `"` + string(utf8.RuneError) + `"`,
  355. want: []R{
  356. {V: Str{string(utf8.RuneError)}},
  357. {V: EOF},
  358. },
  359. },
  360. {
  361. in: `"\uFFFD"`,
  362. want: []R{
  363. {V: Str{string(utf8.RuneError)}},
  364. {V: EOF},
  365. },
  366. },
  367. {
  368. in: `"\x"`,
  369. want: []R{{E: `invalid escape code "\\x" in string`}},
  370. },
  371. {
  372. in: `"\uXXXX"`,
  373. want: []R{{E: `invalid escape code "\\uXXXX" in string`}},
  374. },
  375. {
  376. in: `"\uDEAD"`, // unmatched surrogate pair
  377. want: []R{{E: errEOF}},
  378. },
  379. {
  380. in: `"\uDEAD\uBEEF"`, // invalid surrogate half
  381. want: []R{{E: `invalid escape code "\\uBEEF" in string`}},
  382. },
  383. {
  384. in: `"\uD800\udead"`, // valid surrogate pair
  385. want: []R{
  386. {V: Str{`𐊭`}},
  387. {V: EOF},
  388. },
  389. },
  390. {
  391. in: `"\u0000\"\\\/\b\f\n\r\t"`,
  392. want: []R{
  393. {V: Str{"\u0000\"\\/\b\f\n\r\t"}},
  394. {V: EOF},
  395. },
  396. },
  397. // Invalid JSON numbers.
  398. {
  399. in: `-`,
  400. want: []R{{E: `invalid value -`}},
  401. },
  402. {
  403. in: `+0`,
  404. want: []R{{E: `invalid value +0`}},
  405. },
  406. {
  407. in: `-+`,
  408. want: []R{{E: `invalid value -+`}},
  409. },
  410. {
  411. in: `0.`,
  412. want: []R{{E: `invalid value 0.`}},
  413. },
  414. {
  415. in: `.1`,
  416. want: []R{{E: `invalid value .1`}},
  417. },
  418. {
  419. in: `1.0.1`,
  420. want: []R{{E: `invalid value 1.0.1`}},
  421. },
  422. {
  423. in: `1..1`,
  424. want: []R{{E: `invalid value 1..1`}},
  425. },
  426. {
  427. in: `-1-2`,
  428. want: []R{{E: `invalid value -1-2`}},
  429. },
  430. {
  431. in: `01`,
  432. want: []R{{E: `invalid value 01`}},
  433. },
  434. {
  435. in: `1e`,
  436. want: []R{{E: `invalid value 1e`}},
  437. },
  438. {
  439. in: `1e1.2`,
  440. want: []R{{E: `invalid value 1e1.2`}},
  441. },
  442. {
  443. in: `1Ee`,
  444. want: []R{{E: `invalid value 1Ee`}},
  445. },
  446. {
  447. in: `1.e1`,
  448. want: []R{{E: `invalid value 1.e1`}},
  449. },
  450. {
  451. in: `1.e+`,
  452. want: []R{{E: `invalid value 1.e+`}},
  453. },
  454. {
  455. in: `1e+-2`,
  456. want: []R{{E: `invalid value 1e+-2`}},
  457. },
  458. {
  459. in: `1e--2`,
  460. want: []R{{E: `invalid value 1e--2`}},
  461. },
  462. {
  463. in: `1.0true`,
  464. want: []R{{E: `invalid value 1.0true`}},
  465. },
  466. // JSON numbers as floating point.
  467. {
  468. in: space + `0.0` + space,
  469. want: []R{
  470. {V: F32{0}, P: len(space), RS: `0.0`},
  471. {V: EOF},
  472. },
  473. },
  474. {
  475. in: space + `0` + space,
  476. want: []R{
  477. {V: F32{0}},
  478. {V: EOF},
  479. },
  480. },
  481. {
  482. in: space + `-0` + space,
  483. want: []R{
  484. {V: F32{float32(math.Copysign(0, -1))}},
  485. {V: EOF},
  486. },
  487. },
  488. {
  489. in: `-0`,
  490. want: []R{
  491. {V: F64{math.Copysign(0, -1)}},
  492. {V: EOF},
  493. },
  494. },
  495. {
  496. in: `-0.0`,
  497. want: []R{
  498. {V: F32{float32(math.Copysign(0, -1))}},
  499. {V: EOF},
  500. },
  501. },
  502. {
  503. in: `-0.0`,
  504. want: []R{
  505. {V: F64{math.Copysign(0, -1)}},
  506. {V: EOF},
  507. },
  508. },
  509. {
  510. in: `-1.02`,
  511. want: []R{
  512. {V: F32{-1.02}},
  513. {V: EOF},
  514. },
  515. },
  516. {
  517. in: `1.020000`,
  518. want: []R{
  519. {V: F32{1.02}},
  520. {V: EOF},
  521. },
  522. },
  523. {
  524. in: `-1.0e0`,
  525. want: []R{
  526. {V: F32{-1}},
  527. {V: EOF},
  528. },
  529. },
  530. {
  531. in: `1.0e-000`,
  532. want: []R{
  533. {V: F32{1}},
  534. {V: EOF},
  535. },
  536. },
  537. {
  538. in: `1e+00`,
  539. want: []R{
  540. {V: F32{1}},
  541. {V: EOF},
  542. },
  543. },
  544. {
  545. in: `1.02e3`,
  546. want: []R{
  547. {V: F32{1.02e3}},
  548. {V: EOF},
  549. },
  550. },
  551. {
  552. in: `-1.02E03`,
  553. want: []R{
  554. {V: F32{-1.02e3}},
  555. {V: EOF},
  556. },
  557. },
  558. {
  559. in: `1.0200e+3`,
  560. want: []R{
  561. {V: F32{1.02e3}},
  562. {V: EOF},
  563. },
  564. },
  565. {
  566. in: `-1.0200E+03`,
  567. want: []R{
  568. {V: F32{-1.02e3}},
  569. {V: EOF},
  570. },
  571. },
  572. {
  573. in: `1.0200e-3`,
  574. want: []R{
  575. {V: F32{1.02e-3}},
  576. {V: EOF},
  577. },
  578. },
  579. {
  580. in: `-1.0200E-03`,
  581. want: []R{
  582. {V: F32{-1.02e-3}},
  583. {V: EOF},
  584. },
  585. },
  586. {
  587. // Exceeds max float32 limit, but should be ok for float64.
  588. in: `3.4e39`,
  589. want: []R{
  590. {V: F64{3.4e39}},
  591. {V: EOF},
  592. },
  593. },
  594. {
  595. // Exceeds max float32 limit.
  596. in: `3.4e39`,
  597. want: []R{
  598. {V: NotF32},
  599. {V: EOF},
  600. },
  601. },
  602. {
  603. // Less than negative max float32 limit.
  604. in: `-3.4e39`,
  605. want: []R{
  606. {V: NotF32},
  607. {V: EOF},
  608. },
  609. },
  610. {
  611. // Exceeds max float64 limit.
  612. in: `1.79e+309`,
  613. want: []R{
  614. {V: NotF64},
  615. {V: EOF},
  616. },
  617. },
  618. {
  619. // Less than negative max float64 limit.
  620. in: `-1.79e+309`,
  621. want: []R{
  622. {V: NotF64},
  623. {V: EOF},
  624. },
  625. },
  626. // JSON numbers as signed integers.
  627. {
  628. in: space + `0` + space,
  629. want: []R{
  630. {V: I32{0}},
  631. {V: EOF},
  632. },
  633. },
  634. {
  635. in: space + `-0` + space,
  636. want: []R{
  637. {V: I32{0}},
  638. {V: EOF},
  639. },
  640. },
  641. {
  642. // Fractional part equals 0 is ok.
  643. in: `1.00000`,
  644. want: []R{
  645. {V: I32{1}},
  646. {V: EOF},
  647. },
  648. },
  649. {
  650. // Fractional part not equals 0 returns error.
  651. in: `1.0000000001`,
  652. want: []R{
  653. {V: NotI32},
  654. {V: EOF},
  655. },
  656. },
  657. {
  658. in: `0e0`,
  659. want: []R{
  660. {V: I32{0}},
  661. {V: EOF},
  662. },
  663. },
  664. {
  665. in: `0.0E0`,
  666. want: []R{
  667. {V: I32{0}},
  668. {V: EOF},
  669. },
  670. },
  671. {
  672. in: `0.0E10`,
  673. want: []R{
  674. {V: I32{0}},
  675. {V: EOF},
  676. },
  677. },
  678. {
  679. in: `-1`,
  680. want: []R{
  681. {V: I32{-1}},
  682. {V: EOF},
  683. },
  684. },
  685. {
  686. in: `1.0e+0`,
  687. want: []R{
  688. {V: I32{1}},
  689. {V: EOF},
  690. },
  691. },
  692. {
  693. in: `-1E-0`,
  694. want: []R{
  695. {V: I32{-1}},
  696. {V: EOF},
  697. },
  698. },
  699. {
  700. in: `1E1`,
  701. want: []R{
  702. {V: I32{10}},
  703. {V: EOF},
  704. },
  705. },
  706. {
  707. in: `-100.00e-02`,
  708. want: []R{
  709. {V: I32{-1}},
  710. {V: EOF},
  711. },
  712. },
  713. {
  714. in: `0.1200E+02`,
  715. want: []R{
  716. {V: I64{12}},
  717. {V: EOF},
  718. },
  719. },
  720. {
  721. in: `0.012e2`,
  722. want: []R{
  723. {V: NotI32},
  724. {V: EOF},
  725. },
  726. },
  727. {
  728. in: `12e-2`,
  729. want: []R{
  730. {V: NotI32},
  731. {V: EOF},
  732. },
  733. },
  734. {
  735. // Exceeds math.MaxInt32.
  736. in: `2147483648`,
  737. want: []R{
  738. {V: NotI32},
  739. {V: EOF},
  740. },
  741. },
  742. {
  743. // Exceeds math.MinInt32.
  744. in: `-2147483649`,
  745. want: []R{
  746. {V: NotI32},
  747. {V: EOF},
  748. },
  749. },
  750. {
  751. // Exceeds math.MaxInt32, but ok for int64.
  752. in: `2147483648`,
  753. want: []R{
  754. {V: I64{2147483648}},
  755. {V: EOF},
  756. },
  757. },
  758. {
  759. // Exceeds math.MinInt32, but ok for int64.
  760. in: `-2147483649`,
  761. want: []R{
  762. {V: I64{-2147483649}},
  763. {V: EOF},
  764. },
  765. },
  766. {
  767. // Exceeds math.MaxInt64.
  768. in: `9223372036854775808`,
  769. want: []R{
  770. {V: NotI64},
  771. {V: EOF},
  772. },
  773. },
  774. {
  775. // Exceeds math.MinInt64.
  776. in: `-9223372036854775809`,
  777. want: []R{
  778. {V: NotI64},
  779. {V: EOF},
  780. },
  781. },
  782. // JSON numbers as unsigned integers.
  783. {
  784. in: space + `0` + space,
  785. want: []R{
  786. {V: Ui32{0}},
  787. {V: EOF},
  788. },
  789. },
  790. {
  791. in: space + `-0` + space,
  792. want: []R{
  793. {V: Ui32{0}},
  794. {V: EOF},
  795. },
  796. },
  797. {
  798. in: `-1`,
  799. want: []R{
  800. {V: NotUi32},
  801. {V: EOF},
  802. },
  803. },
  804. {
  805. // Exceeds math.MaxUint32.
  806. in: `4294967296`,
  807. want: []R{
  808. {V: NotUi32},
  809. {V: EOF},
  810. },
  811. },
  812. {
  813. // Exceeds math.MaxUint64.
  814. in: `18446744073709551616`,
  815. want: []R{
  816. {V: NotUi64},
  817. {V: EOF},
  818. },
  819. },
  820. // JSON sequence of values.
  821. {
  822. in: `true null`,
  823. want: []R{
  824. {V: Bool{true}},
  825. {E: `(line 1:6): unexpected token null`},
  826. },
  827. },
  828. {
  829. in: "null false",
  830. want: []R{
  831. {V: Null},
  832. {E: `unexpected token false`},
  833. },
  834. },
  835. {
  836. in: `true,false`,
  837. want: []R{
  838. {V: Bool{true}},
  839. {E: `unexpected token ,`},
  840. },
  841. },
  842. {
  843. in: `47"hello"`,
  844. want: []R{
  845. {V: I32{47}},
  846. {E: `unexpected token "hello"`},
  847. },
  848. },
  849. {
  850. in: `47 "hello"`,
  851. want: []R{
  852. {V: I32{47}},
  853. {E: `unexpected token "hello"`},
  854. },
  855. },
  856. {
  857. in: `true 42`,
  858. want: []R{
  859. {V: Bool{true}},
  860. {E: `unexpected token 42`},
  861. },
  862. },
  863. // JSON arrays.
  864. {
  865. in: space + `[]` + space,
  866. want: []R{
  867. {V: ArrayOpen},
  868. {V: ArrayClose},
  869. {V: EOF},
  870. },
  871. },
  872. {
  873. in: space + `[` + space + `]` + space,
  874. want: []R{
  875. {V: ArrayOpen, P: len(space), RS: `[`},
  876. {V: ArrayClose},
  877. {V: EOF},
  878. },
  879. },
  880. {
  881. in: space + `[` + space,
  882. want: []R{
  883. {V: ArrayOpen},
  884. {E: errEOF},
  885. },
  886. },
  887. {
  888. in: space + `]` + space,
  889. want: []R{{E: `unexpected token ]`}},
  890. },
  891. {
  892. in: `[null,true,false, 1e1, "hello" ]`,
  893. want: []R{
  894. {V: ArrayOpen},
  895. {V: Null},
  896. {V: Bool{true}},
  897. {V: Bool{false}},
  898. {V: I32{10}},
  899. {V: Str{"hello"}},
  900. {V: ArrayClose},
  901. {V: EOF},
  902. },
  903. },
  904. {
  905. in: `[` + space + `true` + space + `,` + space + `"hello"` + space + `]`,
  906. want: []R{
  907. {V: ArrayOpen},
  908. {V: Bool{true}},
  909. {V: Str{"hello"}},
  910. {V: ArrayClose},
  911. {V: EOF},
  912. },
  913. },
  914. {
  915. in: `[` + space + `true` + space + `,` + space + `]`,
  916. want: []R{
  917. {V: ArrayOpen},
  918. {V: Bool{true}},
  919. {E: `unexpected token ]`},
  920. },
  921. },
  922. {
  923. in: `[` + space + `false` + space + `]`,
  924. want: []R{
  925. {V: ArrayOpen},
  926. {V: Bool{false}},
  927. {V: ArrayClose},
  928. {V: EOF},
  929. },
  930. },
  931. {
  932. in: `[` + space + `1` + space + `0` + space + `]`,
  933. want: []R{
  934. {V: ArrayOpen},
  935. {V: I64{1}},
  936. {E: `unexpected token 0`},
  937. },
  938. },
  939. {
  940. in: `[null`,
  941. want: []R{
  942. {V: ArrayOpen},
  943. {V: Null},
  944. {E: errEOF},
  945. },
  946. },
  947. {
  948. in: `[foo]`,
  949. want: []R{
  950. {V: ArrayOpen},
  951. {E: `invalid value foo`},
  952. },
  953. },
  954. {
  955. in: `[{}, "hello", [true, false], null]`,
  956. want: []R{
  957. {V: ArrayOpen},
  958. {V: ObjectOpen},
  959. {V: ObjectClose},
  960. {V: Str{"hello"}},
  961. {V: ArrayOpen},
  962. {V: Bool{true}},
  963. {V: Bool{false}},
  964. {V: ArrayClose},
  965. {V: Null},
  966. {V: ArrayClose},
  967. {V: EOF},
  968. },
  969. },
  970. {
  971. in: `[{ ]`,
  972. want: []R{
  973. {V: ArrayOpen},
  974. {V: ObjectOpen},
  975. {E: `unexpected token ]`},
  976. },
  977. },
  978. {
  979. in: `[[ ]`,
  980. want: []R{
  981. {V: ArrayOpen},
  982. {V: ArrayOpen},
  983. {V: ArrayClose},
  984. {E: errEOF},
  985. },
  986. },
  987. {
  988. in: `[,]`,
  989. want: []R{
  990. {V: ArrayOpen},
  991. {E: `unexpected token ,`},
  992. },
  993. },
  994. {
  995. in: `[true "hello"]`,
  996. want: []R{
  997. {V: ArrayOpen},
  998. {V: Bool{true}},
  999. {E: `unexpected token "hello"`},
  1000. },
  1001. },
  1002. {
  1003. in: `[] null`,
  1004. want: []R{
  1005. {V: ArrayOpen},
  1006. {V: ArrayClose},
  1007. {E: `unexpected token null`},
  1008. },
  1009. },
  1010. {
  1011. in: `true []`,
  1012. want: []R{
  1013. {V: Bool{true}},
  1014. {E: `unexpected token [`},
  1015. },
  1016. },
  1017. // JSON objects.
  1018. {
  1019. in: space + `{}` + space,
  1020. want: []R{
  1021. {V: ObjectOpen},
  1022. {V: ObjectClose},
  1023. {V: EOF},
  1024. },
  1025. },
  1026. {
  1027. in: space + `{` + space + `}` + space,
  1028. want: []R{
  1029. {V: ObjectOpen},
  1030. {V: ObjectClose},
  1031. {V: EOF},
  1032. },
  1033. },
  1034. {
  1035. in: space + `{` + space,
  1036. want: []R{
  1037. {V: ObjectOpen},
  1038. {E: errEOF},
  1039. },
  1040. },
  1041. {
  1042. in: space + `}` + space,
  1043. want: []R{{E: `unexpected token }`}},
  1044. },
  1045. {
  1046. in: `{` + space + `null` + space + `}`,
  1047. want: []R{
  1048. {V: ObjectOpen},
  1049. {E: `unexpected token null`},
  1050. },
  1051. },
  1052. {
  1053. in: `{[]}`,
  1054. want: []R{
  1055. {V: ObjectOpen},
  1056. {E: `(line 1:2): unexpected token [`},
  1057. },
  1058. },
  1059. {
  1060. in: `{,}`,
  1061. want: []R{
  1062. {V: ObjectOpen},
  1063. {E: `unexpected token ,`},
  1064. },
  1065. },
  1066. {
  1067. in: `{"345678"}`,
  1068. want: []R{
  1069. {V: ObjectOpen},
  1070. {E: `(line 1:10): unexpected character }, missing ":" after field name`},
  1071. },
  1072. },
  1073. {
  1074. in: `{` + space + `"hello"` + space + `:` + space + `"world"` + space + `}`,
  1075. want: []R{
  1076. {V: ObjectOpen},
  1077. {V: Name{"hello"}, P: len(space) + 1, RS: `"hello"`},
  1078. {V: Str{"world"}, RS: `"world"`},
  1079. {V: ObjectClose},
  1080. {V: EOF},
  1081. },
  1082. },
  1083. {
  1084. in: `{"hello" "world"}`,
  1085. want: []R{
  1086. {V: ObjectOpen},
  1087. {E: `(line 1:10): unexpected character ", missing ":" after field name`},
  1088. },
  1089. },
  1090. {
  1091. in: `{"hello":`,
  1092. want: []R{
  1093. {V: ObjectOpen},
  1094. {V: Name{"hello"}},
  1095. {E: errEOF},
  1096. },
  1097. },
  1098. {
  1099. in: `{"hello":"world"`,
  1100. want: []R{
  1101. {V: ObjectOpen},
  1102. {V: Name{"hello"}},
  1103. {V: Str{"world"}},
  1104. {E: errEOF},
  1105. },
  1106. },
  1107. {
  1108. in: `{"hello":"world",`,
  1109. want: []R{
  1110. {V: ObjectOpen},
  1111. {V: Name{"hello"}},
  1112. {V: Str{"world"}},
  1113. {E: errEOF},
  1114. },
  1115. },
  1116. {
  1117. in: `{""`,
  1118. want: []R{
  1119. {V: ObjectOpen},
  1120. {E: errEOF},
  1121. },
  1122. },
  1123. {
  1124. in: `{"34":"89",}`,
  1125. want: []R{
  1126. {V: ObjectOpen},
  1127. {V: Name{"34"}, RS: `"34"`},
  1128. {V: Str{"89"}},
  1129. {E: `syntax error (line 1:12): unexpected token }`},
  1130. },
  1131. },
  1132. {
  1133. in: `{
  1134. "number": 123e2,
  1135. "bool" : false,
  1136. "object": {"string": "world"},
  1137. "null" : null,
  1138. "array" : [1.01, "hello", true],
  1139. "string": "hello"
  1140. }`,
  1141. want: []R{
  1142. {V: ObjectOpen},
  1143. {V: Name{"number"}},
  1144. {V: I32{12300}},
  1145. {V: Name{"bool"}},
  1146. {V: Bool{false}},
  1147. {V: Name{"object"}},
  1148. {V: ObjectOpen},
  1149. {V: Name{"string"}},
  1150. {V: Str{"world"}},
  1151. {V: ObjectClose},
  1152. {V: Name{"null"}},
  1153. {V: Null},
  1154. {V: Name{"array"}},
  1155. {V: ArrayOpen},
  1156. {V: F32{1.01}},
  1157. {V: Str{"hello"}},
  1158. {V: Bool{true}},
  1159. {V: ArrayClose},
  1160. {V: Name{"string"}},
  1161. {V: Str{"hello"}},
  1162. {V: ObjectClose},
  1163. {V: EOF},
  1164. },
  1165. },
  1166. {
  1167. in: `[
  1168. {"object": {"number": 47}},
  1169. ["list"],
  1170. null
  1171. ]`,
  1172. want: []R{
  1173. {V: ArrayOpen},
  1174. {V: ObjectOpen},
  1175. {V: Name{"object"}},
  1176. {V: ObjectOpen},
  1177. {V: Name{"number"}},
  1178. {V: I32{47}},
  1179. {V: ObjectClose},
  1180. {V: ObjectClose},
  1181. {V: ArrayOpen},
  1182. {V: Str{"list"}},
  1183. {V: ArrayClose},
  1184. {V: Null},
  1185. {V: ArrayClose},
  1186. {V: EOF},
  1187. },
  1188. },
  1189. // Tests for line and column info.
  1190. {
  1191. in: `12345678 x`,
  1192. want: []R{
  1193. {V: I64{12345678}},
  1194. {E: `syntax error (line 1:10): invalid value x`},
  1195. },
  1196. },
  1197. {
  1198. in: "\ntrue\n x",
  1199. want: []R{
  1200. {V: Bool{true}},
  1201. {E: `syntax error (line 3:4): invalid value x`},
  1202. },
  1203. },
  1204. {
  1205. in: `"πŸ’©"x`,
  1206. want: []R{
  1207. {V: Str{"πŸ’©"}},
  1208. {E: `syntax error (line 1:4): invalid value x`},
  1209. },
  1210. },
  1211. {
  1212. in: "\n\n[\"πŸ”₯πŸ”₯πŸ”₯\"x",
  1213. want: []R{
  1214. {V: ArrayOpen},
  1215. {V: Str{"πŸ”₯πŸ”₯πŸ”₯"}},
  1216. {E: `syntax error (line 3:7): invalid value x`},
  1217. },
  1218. },
  1219. {
  1220. // Multi-rune emojis.
  1221. in: `["πŸ‘πŸ»πŸ‘πŸΏ"x`,
  1222. want: []R{
  1223. {V: ArrayOpen},
  1224. {V: Str{"πŸ‘πŸ»πŸ‘πŸΏ"}},
  1225. {E: `syntax error (line 1:8): invalid value x`},
  1226. },
  1227. },
  1228. }
  1229. for _, tc := range tests {
  1230. tc := tc
  1231. t.Run("", func(t *testing.T) {
  1232. dec := json.NewDecoder([]byte(tc.in))
  1233. for i, want := range tc.want {
  1234. peekTok, peekErr := dec.Peek()
  1235. tok, err := dec.Read()
  1236. if err != nil {
  1237. if want.E == "" {
  1238. errorf(t, tc.in, "want#%d: Read() got unexpected error: %v", i, err)
  1239. } else if !strings.Contains(err.Error(), want.E) {
  1240. errorf(t, tc.in, "want#%d: Read() got %q, want %q", i, err, want.E)
  1241. }
  1242. return
  1243. }
  1244. if want.E != "" {
  1245. errorf(t, tc.in, "want#%d: Read() got nil error, want %q", i, want.E)
  1246. return
  1247. }
  1248. checkToken(t, tok, i, want, tc.in)
  1249. if !cmp.Equal(tok, peekTok, cmp.Comparer(json.TokenEquals)) {
  1250. errorf(t, tc.in, "want#%d: Peek() %+v != Read() token %+v", i, peekTok, tok)
  1251. }
  1252. if err != peekErr {
  1253. errorf(t, tc.in, "want#%d: Peek() error %v != Read() error %v", i, err, peekErr)
  1254. }
  1255. }
  1256. })
  1257. }
  1258. }
  1259. func checkToken(t *testing.T, tok json.Token, idx int, r R, in string) {
  1260. // Validate Token.Pos() if R.P is set.
  1261. if r.P > 0 {
  1262. got := tok.Pos()
  1263. if got != r.P {
  1264. errorf(t, in, "want#%d: Token.Pos() got %v want %v", idx, got, r.P)
  1265. }
  1266. }
  1267. // Validate Token.RawString if R.RS is set.
  1268. if len(r.RS) > 0 {
  1269. got := tok.RawString()
  1270. if got != r.RS {
  1271. errorf(t, in, "want#%d: Token.RawString() got %v want %v", idx, got, r.P)
  1272. }
  1273. }
  1274. // Skip checking for Token details if r.V is not set.
  1275. if r.V == nil {
  1276. return
  1277. }
  1278. if err := r.V.check(tok); err != "" {
  1279. errorf(t, in, "want#%d: %s", idx, err)
  1280. }
  1281. return
  1282. }
  1283. func errorf(t *testing.T, in string, fmtStr string, args ...interface{}) {
  1284. t.Helper()
  1285. vargs := []interface{}{in}
  1286. for _, arg := range args {
  1287. vargs = append(vargs, arg)
  1288. }
  1289. t.Errorf("input:\n%s\n~end~\n"+fmtStr, vargs...)
  1290. }
  1291. func TestClone(t *testing.T) {
  1292. input := `{"outer":{"str":"hello", "number": 123}}`
  1293. dec := json.NewDecoder([]byte(input))
  1294. // Clone at the start should produce the same reads as the original.
  1295. clone := dec.Clone()
  1296. compareDecoders(t, dec, clone)
  1297. // Advance to inner object, clone and compare again.
  1298. dec.Read() // Read ObjectOpen.
  1299. dec.Read() // Read Name.
  1300. clone = dec.Clone()
  1301. compareDecoders(t, dec, clone)
  1302. }
  1303. func compareDecoders(t *testing.T, d1 *json.Decoder, d2 *json.Decoder) {
  1304. for {
  1305. tok1, err1 := d1.Read()
  1306. tok2, err2 := d2.Read()
  1307. if tok1.Kind() != tok2.Kind() {
  1308. t.Errorf("cloned decoder: got Kind %v, want %v", tok2.Kind(), tok1.Kind())
  1309. }
  1310. if tok1.RawString() != tok2.RawString() {
  1311. t.Errorf("cloned decoder: got RawString %v, want %v", tok2.RawString(), tok1.RawString())
  1312. }
  1313. if err1 != err2 {
  1314. t.Errorf("cloned decoder: got error %v, want %v", err2, err1)
  1315. }
  1316. if tok1.Kind() == json.EOF {
  1317. break
  1318. }
  1319. }
  1320. }