decode_test.go 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. "strings"
  7. "testing"
  8. "unicode/utf8"
  9. "github.com/golang/protobuf/v2/internal/encoding/json"
  10. )
  11. type R struct {
  12. // T is expected Type returned from calling Decoder.ReadNext.
  13. T json.Type
  14. // E is expected error substring from calling Decoder.ReadNext if set.
  15. E string
  16. // V is expected value from calling
  17. // Value.{Bool()|Float()|Int()|Uint()|String()} depending on type.
  18. V interface{}
  19. // VE is expected error substring from calling
  20. // Value.{Bool()|Float()|Int()|Uint()|String()} depending on type if set.
  21. VE string
  22. }
  23. func TestDecoder(t *testing.T) {
  24. const space = " \n\r\t"
  25. tests := []struct {
  26. input string
  27. // want is a list of expected values returned from calling
  28. // Decoder.ReadNext. An item makes the test code invoke
  29. // Decoder.ReadNext and compare against R.T and R.E. For Bool,
  30. // Number and String tokens, it invokes the corresponding getter method
  31. // and compares the returned value against R.V or R.VE if it returned an
  32. // error.
  33. want []R
  34. }{
  35. {
  36. input: ``,
  37. want: []R{{T: json.EOF}},
  38. },
  39. {
  40. input: space,
  41. want: []R{{T: json.EOF}},
  42. },
  43. {
  44. // Calling ReadNext after EOF will keep returning EOF for
  45. // succeeding ReadNext calls.
  46. input: space,
  47. want: []R{
  48. {T: json.EOF},
  49. {T: json.EOF},
  50. {T: json.EOF},
  51. },
  52. },
  53. // JSON literals.
  54. {
  55. input: space + `null` + space,
  56. want: []R{
  57. {T: json.Null},
  58. {T: json.EOF},
  59. },
  60. },
  61. {
  62. input: space + `true` + space,
  63. want: []R{
  64. {T: json.Bool, V: true},
  65. {T: json.EOF},
  66. },
  67. },
  68. {
  69. input: space + `false` + space,
  70. want: []R{
  71. {T: json.Bool, V: false},
  72. {T: json.EOF},
  73. },
  74. },
  75. {
  76. // Error returned will produce the same error again.
  77. input: space + `foo` + space,
  78. want: []R{
  79. {E: `invalid value foo`},
  80. {E: `invalid value foo`},
  81. },
  82. },
  83. // JSON strings.
  84. {
  85. input: space + `""` + space,
  86. want: []R{
  87. {T: json.String, V: ""},
  88. {T: json.EOF},
  89. },
  90. },
  91. {
  92. input: space + `"hello"` + space,
  93. want: []R{
  94. {T: json.String, V: "hello"},
  95. {T: json.EOF},
  96. },
  97. },
  98. {
  99. input: `"hello`,
  100. want: []R{{E: `unexpected EOF`}},
  101. },
  102. {
  103. input: "\"\x00\"",
  104. want: []R{{E: `invalid character '\x00' in string`}},
  105. },
  106. {
  107. input: "\"\u0031\u0032\"",
  108. want: []R{
  109. {T: json.String, V: "12"},
  110. {T: json.EOF},
  111. },
  112. },
  113. {
  114. // Invalid UTF-8 error is returned in ReadString instead of ReadNext.
  115. input: "\"\xff\"",
  116. want: []R{
  117. {T: json.String, E: `invalid UTF-8 detected`, V: string("\xff")},
  118. {T: json.EOF},
  119. },
  120. },
  121. {
  122. input: `"` + string(utf8.RuneError) + `"`,
  123. want: []R{
  124. {T: json.String, V: string(utf8.RuneError)},
  125. {T: json.EOF},
  126. },
  127. },
  128. {
  129. input: `"\uFFFD"`,
  130. want: []R{
  131. {T: json.String, V: string(utf8.RuneError)},
  132. {T: json.EOF},
  133. },
  134. },
  135. {
  136. input: `"\x"`,
  137. want: []R{{E: `invalid escape code "\\x" in string`}},
  138. },
  139. {
  140. input: `"\uXXXX"`,
  141. want: []R{{E: `invalid escape code "\\uXXXX" in string`}},
  142. },
  143. {
  144. input: `"\uDEAD"`, // unmatched surrogate pair
  145. want: []R{{E: `unexpected EOF`}},
  146. },
  147. {
  148. input: `"\uDEAD\uBEEF"`, // invalid surrogate half
  149. want: []R{{E: `invalid escape code "\\uBEEF" in string`}},
  150. },
  151. {
  152. input: `"\uD800\udead"`, // valid surrogate pair
  153. want: []R{
  154. {T: json.String, V: `𐊭`},
  155. {T: json.EOF},
  156. },
  157. },
  158. {
  159. input: `"\u0000\"\\\/\b\f\n\r\t"`,
  160. want: []R{
  161. {T: json.String, V: "\u0000\"\\/\b\f\n\r\t"},
  162. {T: json.EOF},
  163. },
  164. },
  165. // Invalid JSON numbers.
  166. {
  167. input: `-`,
  168. want: []R{{E: `invalid number -`}},
  169. },
  170. {
  171. input: `+0`,
  172. want: []R{{E: `invalid value +0`}},
  173. },
  174. {
  175. input: `-+`,
  176. want: []R{{E: `invalid number -+`}},
  177. },
  178. {
  179. input: `0.`,
  180. want: []R{{E: `invalid number 0.`}},
  181. },
  182. {
  183. input: `.1`,
  184. want: []R{{E: `invalid value .1`}},
  185. },
  186. {
  187. input: `1.0.1`,
  188. want: []R{{E: `invalid number 1.0.1`}},
  189. },
  190. {
  191. input: `1..1`,
  192. want: []R{{E: `invalid number 1..1`}},
  193. },
  194. {
  195. input: `-1-2`,
  196. want: []R{{E: `invalid number -1-2`}},
  197. },
  198. {
  199. input: `01`,
  200. want: []R{{E: `invalid number 01`}},
  201. },
  202. {
  203. input: `1e`,
  204. want: []R{{E: `invalid number 1e`}},
  205. },
  206. {
  207. input: `1e1.2`,
  208. want: []R{{E: `invalid number 1e1.2`}},
  209. },
  210. {
  211. input: `1Ee`,
  212. want: []R{{E: `invalid number 1Ee`}},
  213. },
  214. {
  215. input: `1.e1`,
  216. want: []R{{E: `invalid number 1.e1`}},
  217. },
  218. {
  219. input: `1.e+`,
  220. want: []R{{E: `invalid number 1.e+`}},
  221. },
  222. {
  223. input: `1e+-2`,
  224. want: []R{{E: `invalid number 1e+-2`}},
  225. },
  226. {
  227. input: `1e--2`,
  228. want: []R{{E: `invalid number 1e--2`}},
  229. },
  230. {
  231. input: `1.0true`,
  232. want: []R{{E: `invalid number 1.0true`}},
  233. },
  234. // JSON numbers as floating point.
  235. {
  236. input: space + `0.0` + space,
  237. want: []R{
  238. {T: json.Number, V: float32(0)},
  239. {T: json.EOF},
  240. },
  241. },
  242. {
  243. input: space + `0` + space,
  244. want: []R{
  245. {T: json.Number, V: float32(0)},
  246. {T: json.EOF},
  247. },
  248. },
  249. {
  250. input: space + `-0` + space,
  251. want: []R{
  252. {T: json.Number, V: float32(0)},
  253. {T: json.EOF},
  254. },
  255. },
  256. {
  257. input: `-1.02`,
  258. want: []R{
  259. {T: json.Number, V: float32(-1.02)},
  260. {T: json.EOF},
  261. },
  262. },
  263. {
  264. input: `1.020000`,
  265. want: []R{
  266. {T: json.Number, V: float32(1.02)},
  267. {T: json.EOF},
  268. },
  269. },
  270. {
  271. input: `-1.0e0`,
  272. want: []R{
  273. {T: json.Number, V: float32(-1)},
  274. {T: json.EOF},
  275. },
  276. },
  277. {
  278. input: `1.0e-000`,
  279. want: []R{
  280. {T: json.Number, V: float32(1)},
  281. {T: json.EOF},
  282. },
  283. },
  284. {
  285. input: `1e+00`,
  286. want: []R{
  287. {T: json.Number, V: float32(1)},
  288. {T: json.EOF},
  289. },
  290. },
  291. {
  292. input: `1.02e3`,
  293. want: []R{
  294. {T: json.Number, V: float32(1.02e3)},
  295. {T: json.EOF},
  296. },
  297. },
  298. {
  299. input: `-1.02E03`,
  300. want: []R{
  301. {T: json.Number, V: float32(-1.02e3)},
  302. {T: json.EOF},
  303. },
  304. },
  305. {
  306. input: `1.0200e+3`,
  307. want: []R{
  308. {T: json.Number, V: float32(1.02e3)},
  309. {T: json.EOF},
  310. },
  311. },
  312. {
  313. input: `-1.0200E+03`,
  314. want: []R{
  315. {T: json.Number, V: float32(-1.02e3)},
  316. {T: json.EOF},
  317. },
  318. },
  319. {
  320. input: `1.0200e-3`,
  321. want: []R{
  322. {T: json.Number, V: float32(1.02e-3)},
  323. {T: json.EOF},
  324. },
  325. },
  326. {
  327. input: `-1.0200E-03`,
  328. want: []R{
  329. {T: json.Number, V: float32(-1.02e-3)},
  330. {T: json.EOF},
  331. },
  332. },
  333. {
  334. // Exceeds max float32 limit, but should be ok for float64.
  335. input: `3.4e39`,
  336. want: []R{
  337. {T: json.Number, V: float64(3.4e39)},
  338. {T: json.EOF},
  339. },
  340. },
  341. {
  342. // Exceeds max float32 limit.
  343. input: `3.4e39`,
  344. want: []R{
  345. {T: json.Number, V: float32(0), VE: `value out of range`},
  346. {T: json.EOF},
  347. },
  348. },
  349. {
  350. // Less than negative max float32 limit.
  351. input: `-3.4e39`,
  352. want: []R{
  353. {T: json.Number, V: float32(0), VE: `value out of range`},
  354. {T: json.EOF},
  355. },
  356. },
  357. {
  358. // Exceeds max float64 limit.
  359. input: `1.79e+309`,
  360. want: []R{
  361. {T: json.Number, V: float64(0), VE: `value out of range`},
  362. {T: json.EOF},
  363. },
  364. },
  365. {
  366. // Less than negative max float64 limit.
  367. input: `-1.79e+309`,
  368. want: []R{
  369. {T: json.Number, V: float64(0), VE: `value out of range`},
  370. {T: json.EOF},
  371. },
  372. },
  373. // JSON numbers as signed integers.
  374. {
  375. input: space + `0` + space,
  376. want: []R{
  377. {T: json.Number, V: int32(0)},
  378. {T: json.EOF},
  379. },
  380. },
  381. {
  382. input: space + `-0` + space,
  383. want: []R{
  384. {T: json.Number, V: int32(0)},
  385. {T: json.EOF},
  386. },
  387. },
  388. {
  389. // Fractional part equals 0 is ok.
  390. input: `1.00000`,
  391. want: []R{
  392. {T: json.Number, V: int32(1)},
  393. {T: json.EOF},
  394. },
  395. },
  396. {
  397. // Fractional part not equals 0 returns error.
  398. input: `1.0000000001`,
  399. want: []R{
  400. {T: json.Number, V: int32(0), VE: `cannot convert 1.0000000001 to integer`},
  401. {T: json.EOF},
  402. },
  403. },
  404. {
  405. input: `0e0`,
  406. want: []R{
  407. {T: json.Number, V: int32(0)},
  408. {T: json.EOF},
  409. },
  410. },
  411. {
  412. input: `0.0E0`,
  413. want: []R{
  414. {T: json.Number, V: int32(0)},
  415. {T: json.EOF},
  416. },
  417. },
  418. {
  419. input: `0.0E10`,
  420. want: []R{
  421. {T: json.Number, V: int32(0)},
  422. {T: json.EOF},
  423. },
  424. },
  425. {
  426. input: `-1`,
  427. want: []R{
  428. {T: json.Number, V: int32(-1)},
  429. {T: json.EOF},
  430. },
  431. },
  432. {
  433. input: `1.0e+0`,
  434. want: []R{
  435. {T: json.Number, V: int32(1)},
  436. {T: json.EOF},
  437. },
  438. },
  439. {
  440. input: `-1E-0`,
  441. want: []R{
  442. {T: json.Number, V: int32(-1)},
  443. {T: json.EOF},
  444. },
  445. },
  446. {
  447. input: `1E1`,
  448. want: []R{
  449. {T: json.Number, V: int32(10)},
  450. {T: json.EOF},
  451. },
  452. },
  453. {
  454. input: `-100.00e-02`,
  455. want: []R{
  456. {T: json.Number, V: int32(-1)},
  457. {T: json.EOF},
  458. },
  459. },
  460. {
  461. input: `0.1200E+02`,
  462. want: []R{
  463. {T: json.Number, V: int64(12)},
  464. {T: json.EOF},
  465. },
  466. },
  467. {
  468. input: `0.012e2`,
  469. want: []R{
  470. {T: json.Number, V: int32(0), VE: `cannot convert 0.012e2 to integer`},
  471. {T: json.EOF},
  472. },
  473. },
  474. {
  475. input: `12e-2`,
  476. want: []R{
  477. {T: json.Number, V: int32(0), VE: `cannot convert 12e-2 to integer`},
  478. {T: json.EOF},
  479. },
  480. },
  481. {
  482. // Exceeds math.MaxInt32.
  483. input: `2147483648`,
  484. want: []R{
  485. {T: json.Number, V: int32(0), VE: `value out of range`},
  486. {T: json.EOF},
  487. },
  488. },
  489. {
  490. // Exceeds math.MinInt32.
  491. input: `-2147483649`,
  492. want: []R{
  493. {T: json.Number, V: int32(0), VE: `value out of range`},
  494. {T: json.EOF},
  495. },
  496. },
  497. {
  498. // Exceeds math.MaxInt32, but ok for int64.
  499. input: `2147483648`,
  500. want: []R{
  501. {T: json.Number, V: int64(2147483648)},
  502. {T: json.EOF},
  503. },
  504. },
  505. {
  506. // Exceeds math.MinInt32, but ok for int64.
  507. input: `-2147483649`,
  508. want: []R{
  509. {T: json.Number, V: int64(-2147483649)},
  510. {T: json.EOF},
  511. },
  512. },
  513. {
  514. // Exceeds math.MaxInt64.
  515. input: `9223372036854775808`,
  516. want: []R{
  517. {T: json.Number, V: int64(0), VE: `value out of range`},
  518. {T: json.EOF},
  519. },
  520. },
  521. {
  522. // Exceeds math.MinInt64.
  523. input: `-9223372036854775809`,
  524. want: []R{
  525. {T: json.Number, V: int64(0), VE: `value out of range`},
  526. {T: json.EOF},
  527. },
  528. },
  529. // JSON numbers as unsigned integers.
  530. {
  531. input: space + `0` + space,
  532. want: []R{
  533. {T: json.Number, V: uint32(0)},
  534. {T: json.EOF},
  535. },
  536. },
  537. {
  538. input: space + `-0` + space,
  539. want: []R{
  540. {T: json.Number, V: uint32(0)},
  541. {T: json.EOF},
  542. },
  543. },
  544. {
  545. input: `-1`,
  546. want: []R{
  547. {T: json.Number, V: uint32(0), VE: `invalid syntax`},
  548. {T: json.EOF},
  549. },
  550. },
  551. {
  552. // Exceeds math.MaxUint32.
  553. input: `4294967296`,
  554. want: []R{
  555. {T: json.Number, V: uint32(0), VE: `value out of range`},
  556. {T: json.EOF},
  557. },
  558. },
  559. {
  560. // Exceeds math.MaxUint64.
  561. input: `18446744073709551616`,
  562. want: []R{
  563. {T: json.Number, V: uint64(0), VE: `value out of range`},
  564. {T: json.EOF},
  565. },
  566. },
  567. // JSON sequence of values.
  568. {
  569. input: `true null`,
  570. want: []R{
  571. {T: json.Bool, V: true},
  572. {E: `unexpected value null`},
  573. },
  574. },
  575. {
  576. input: "null false",
  577. want: []R{
  578. {T: json.Null},
  579. {E: `unexpected value false`},
  580. },
  581. },
  582. {
  583. input: `true,false`,
  584. want: []R{
  585. {T: json.Bool, V: true},
  586. {E: `unexpected character ,`},
  587. },
  588. },
  589. {
  590. input: `47"hello"`,
  591. want: []R{
  592. {T: json.Number, V: int32(47)},
  593. {E: `unexpected value "hello"`},
  594. },
  595. },
  596. {
  597. input: `47 "hello"`,
  598. want: []R{
  599. {T: json.Number, V: int32(47)},
  600. {E: `unexpected value "hello"`},
  601. },
  602. },
  603. {
  604. input: `true 42`,
  605. want: []R{
  606. {T: json.Bool, V: true},
  607. {E: `unexpected value 42`},
  608. },
  609. },
  610. // JSON arrays.
  611. {
  612. input: space + `[]` + space,
  613. want: []R{
  614. {T: json.StartArray},
  615. {T: json.EndArray},
  616. {T: json.EOF},
  617. },
  618. },
  619. {
  620. input: space + `[` + space + `]` + space,
  621. want: []R{
  622. {T: json.StartArray},
  623. {T: json.EndArray},
  624. {T: json.EOF},
  625. },
  626. },
  627. {
  628. input: space + `[` + space,
  629. want: []R{
  630. {T: json.StartArray},
  631. {E: `unexpected EOF`},
  632. },
  633. },
  634. {
  635. input: space + `]` + space,
  636. want: []R{{E: `unexpected character ]`}},
  637. },
  638. {
  639. input: `[null,true,false, 1e1, "hello" ]`,
  640. want: []R{
  641. {T: json.StartArray},
  642. {T: json.Null},
  643. {T: json.Bool, V: true},
  644. {T: json.Bool, V: false},
  645. {T: json.Number, V: int32(10)},
  646. {T: json.String, V: "hello"},
  647. {T: json.EndArray},
  648. {T: json.EOF},
  649. },
  650. },
  651. {
  652. input: `[` + space + `true` + space + `,` + space + `"hello"` + space + `]`,
  653. want: []R{
  654. {T: json.StartArray},
  655. {T: json.Bool, V: true},
  656. {T: json.String, V: "hello"},
  657. {T: json.EndArray},
  658. {T: json.EOF},
  659. },
  660. },
  661. {
  662. input: `[` + space + `true` + space + `,` + space + `]`,
  663. want: []R{
  664. {T: json.StartArray},
  665. {T: json.Bool, V: true},
  666. {E: `unexpected character ]`},
  667. },
  668. },
  669. {
  670. input: `[` + space + `false` + space + `]`,
  671. want: []R{
  672. {T: json.StartArray},
  673. {T: json.Bool, V: false},
  674. {T: json.EndArray},
  675. {T: json.EOF},
  676. },
  677. },
  678. {
  679. input: `[` + space + `1` + space + `0` + space + `]`,
  680. want: []R{
  681. {T: json.StartArray},
  682. {T: json.Number, V: int64(1)},
  683. {E: `unexpected value 0`},
  684. },
  685. },
  686. {
  687. input: `[null`,
  688. want: []R{
  689. {T: json.StartArray},
  690. {T: json.Null},
  691. {E: `unexpected EOF`},
  692. },
  693. },
  694. {
  695. input: `[foo]`,
  696. want: []R{
  697. {T: json.StartArray},
  698. {E: `invalid value foo`},
  699. },
  700. },
  701. {
  702. input: `[{}, "hello", [true, false], null]`,
  703. want: []R{
  704. {T: json.StartArray},
  705. {T: json.StartObject},
  706. {T: json.EndObject},
  707. {T: json.String, V: "hello"},
  708. {T: json.StartArray},
  709. {T: json.Bool, V: true},
  710. {T: json.Bool, V: false},
  711. {T: json.EndArray},
  712. {T: json.Null},
  713. {T: json.EndArray},
  714. {T: json.EOF},
  715. },
  716. },
  717. {
  718. input: `[{ ]`,
  719. want: []R{
  720. {T: json.StartArray},
  721. {T: json.StartObject},
  722. {E: `unexpected character ]`},
  723. },
  724. },
  725. {
  726. input: `[[ ]`,
  727. want: []R{
  728. {T: json.StartArray},
  729. {T: json.StartArray},
  730. {T: json.EndArray},
  731. {E: `unexpected EOF`},
  732. },
  733. },
  734. {
  735. input: `[,]`,
  736. want: []R{
  737. {T: json.StartArray},
  738. {E: `unexpected character ,`},
  739. },
  740. },
  741. {
  742. input: `[true "hello"]`,
  743. want: []R{
  744. {T: json.StartArray},
  745. {T: json.Bool, V: true},
  746. {E: `unexpected value "hello"`},
  747. },
  748. },
  749. {
  750. input: `[] null`,
  751. want: []R{
  752. {T: json.StartArray},
  753. {T: json.EndArray},
  754. {E: `unexpected value null`},
  755. },
  756. },
  757. {
  758. input: `true []`,
  759. want: []R{
  760. {T: json.Bool, V: true},
  761. {E: `unexpected character [`},
  762. },
  763. },
  764. // JSON objects.
  765. {
  766. input: space + `{}` + space,
  767. want: []R{
  768. {T: json.StartObject},
  769. {T: json.EndObject},
  770. {T: json.EOF},
  771. },
  772. },
  773. {
  774. input: space + `{` + space + `}` + space,
  775. want: []R{
  776. {T: json.StartObject},
  777. {T: json.EndObject},
  778. {T: json.EOF},
  779. },
  780. },
  781. {
  782. input: space + `{` + space,
  783. want: []R{
  784. {T: json.StartObject},
  785. {E: `unexpected EOF`},
  786. },
  787. },
  788. {
  789. input: space + `}` + space,
  790. want: []R{{E: `unexpected character }`}},
  791. },
  792. {
  793. input: `{` + space + `null` + space + `}`,
  794. want: []R{
  795. {T: json.StartObject},
  796. {E: `unexpected value null`},
  797. },
  798. },
  799. {
  800. input: `{[]}`,
  801. want: []R{
  802. {T: json.StartObject},
  803. {E: `unexpected character [`},
  804. },
  805. },
  806. {
  807. input: `{,}`,
  808. want: []R{
  809. {T: json.StartObject},
  810. {E: `unexpected character ,`},
  811. },
  812. },
  813. {
  814. input: `{"345678"}`,
  815. want: []R{
  816. {T: json.StartObject},
  817. {E: `unexpected character }, missing ":" after object name`},
  818. },
  819. },
  820. {
  821. input: `{` + space + `"hello"` + space + `:` + space + `"world"` + space + `}`,
  822. want: []R{
  823. {T: json.StartObject},
  824. {T: json.Name, V: "hello"},
  825. {T: json.String, V: "world"},
  826. {T: json.EndObject},
  827. {T: json.EOF},
  828. },
  829. },
  830. {
  831. input: `{"hello" "world"}`,
  832. want: []R{
  833. {T: json.StartObject},
  834. {E: `unexpected character ", missing ":" after object name`},
  835. },
  836. },
  837. {
  838. input: `{"hello":`,
  839. want: []R{
  840. {T: json.StartObject},
  841. {T: json.Name, V: "hello"},
  842. {E: `unexpected EOF`},
  843. },
  844. },
  845. {
  846. input: `{"hello":"world"`,
  847. want: []R{
  848. {T: json.StartObject},
  849. {T: json.Name, V: "hello"},
  850. {T: json.String, V: "world"},
  851. {E: `unexpected EOF`},
  852. },
  853. },
  854. {
  855. input: `{"hello":"world",`,
  856. want: []R{
  857. {T: json.StartObject},
  858. {T: json.Name, V: "hello"},
  859. {T: json.String, V: "world"},
  860. {E: `unexpected EOF`},
  861. },
  862. },
  863. {
  864. input: `{"34":"89",}`,
  865. want: []R{
  866. {T: json.StartObject},
  867. {T: json.Name, V: "34"},
  868. {T: json.String, V: "89"},
  869. {E: `syntax error (line 1:12): unexpected character }`},
  870. },
  871. },
  872. {
  873. input: `{
  874. "number": 123e2,
  875. "bool" : false,
  876. "object": {"string": "world"},
  877. "null" : null,
  878. "array" : [1.01, "hello", true],
  879. "string": "hello"
  880. }`,
  881. want: []R{
  882. {T: json.StartObject},
  883. {T: json.Name, V: "number"},
  884. {T: json.Number, V: int32(12300)},
  885. {T: json.Name, V: "bool"},
  886. {T: json.Bool, V: false},
  887. {T: json.Name, V: "object"},
  888. {T: json.StartObject},
  889. {T: json.Name, V: "string"},
  890. {T: json.String, V: "world"},
  891. {T: json.EndObject},
  892. {T: json.Name, V: "null"},
  893. {T: json.Null},
  894. {T: json.Name, V: "array"},
  895. {T: json.StartArray},
  896. {T: json.Number, V: float32(1.01)},
  897. {T: json.String, V: "hello"},
  898. {T: json.Bool, V: true},
  899. {T: json.EndArray},
  900. {T: json.Name, V: "string"},
  901. {T: json.String, V: "hello"},
  902. {T: json.EndObject},
  903. {T: json.EOF},
  904. },
  905. },
  906. {
  907. input: `[
  908. {"object": {"number": 47}},
  909. ["list"],
  910. null
  911. ]`,
  912. want: []R{
  913. {T: json.StartArray},
  914. {T: json.StartObject},
  915. {T: json.Name, V: "object"},
  916. {T: json.StartObject},
  917. {T: json.Name, V: "number"},
  918. {T: json.Number, V: uint32(47)},
  919. {T: json.EndObject},
  920. {T: json.EndObject},
  921. {T: json.StartArray},
  922. {T: json.String, V: "list"},
  923. {T: json.EndArray},
  924. {T: json.Null},
  925. {T: json.EndArray},
  926. {T: json.EOF},
  927. },
  928. },
  929. // Tests for line and column info.
  930. {
  931. input: `12345678 x`,
  932. want: []R{
  933. {T: json.Number, V: int64(12345678)},
  934. {E: `syntax error (line 1:10): invalid value x`},
  935. },
  936. },
  937. {
  938. input: "\ntrue\n x",
  939. want: []R{
  940. {T: json.Bool, V: true},
  941. {E: `syntax error (line 3:4): invalid value x`},
  942. },
  943. },
  944. {
  945. input: `"💩"x`,
  946. want: []R{
  947. {T: json.String, V: "💩"},
  948. {E: `syntax error (line 1:4): invalid value x`},
  949. },
  950. },
  951. {
  952. input: "\n\n[\"🔥🔥🔥\"x",
  953. want: []R{
  954. {T: json.StartArray},
  955. {T: json.String, V: "🔥🔥🔥"},
  956. {E: `syntax error (line 3:7): invalid value x`},
  957. },
  958. },
  959. {
  960. // Multi-rune emojis.
  961. input: `["👍🏻👍🏿"x`,
  962. want: []R{
  963. {T: json.StartArray},
  964. {T: json.String, V: "👍🏻👍🏿"},
  965. {E: `syntax error (line 1:8): invalid value x`},
  966. },
  967. },
  968. {
  969. input: `{
  970. "45678":-1
  971. }`,
  972. want: []R{
  973. {T: json.StartObject},
  974. {T: json.Name, V: "45678"},
  975. {T: json.Number, V: uint64(1), VE: "error (line 2:11)"},
  976. },
  977. },
  978. }
  979. for _, tc := range tests {
  980. tc := tc
  981. t.Run("", func(t *testing.T) {
  982. dec := json.NewDecoder([]byte(tc.input))
  983. for i, want := range tc.want {
  984. value, err := dec.ReadNext()
  985. if err != nil {
  986. if want.E == "" {
  987. t.Errorf("input: %v\nReadNext() got unexpected error: %v", tc.input, err)
  988. } else if !strings.Contains(err.Error(), want.E) {
  989. t.Errorf("input: %v\nReadNext() got %q, want %q", tc.input, err, want.E)
  990. }
  991. } else {
  992. if want.E != "" {
  993. t.Errorf("input: %v\nReadNext() got nil error, want %q", tc.input, want.E)
  994. }
  995. }
  996. token := value.Type()
  997. if token != want.T {
  998. t.Errorf("input: %v\nReadNext() got %v, want %v", tc.input, token, want.T)
  999. break
  1000. }
  1001. checkValue(t, value, i, want)
  1002. }
  1003. })
  1004. }
  1005. }
  1006. func checkValue(t *testing.T, value json.Value, wantIdx int, want R) {
  1007. var got interface{}
  1008. var err error
  1009. switch value.Type() {
  1010. case json.Bool:
  1011. got, err = value.Bool()
  1012. case json.String:
  1013. got = value.String()
  1014. case json.Name:
  1015. got, err = value.Name()
  1016. case json.Number:
  1017. switch want.V.(type) {
  1018. case float32:
  1019. got, err = value.Float(32)
  1020. got = float32(got.(float64))
  1021. case float64:
  1022. got, err = value.Float(64)
  1023. case int32:
  1024. got, err = value.Int(32)
  1025. got = int32(got.(int64))
  1026. case int64:
  1027. got, err = value.Int(64)
  1028. case uint32:
  1029. got, err = value.Uint(32)
  1030. got = uint32(got.(uint64))
  1031. case uint64:
  1032. got, err = value.Uint(64)
  1033. }
  1034. default:
  1035. return
  1036. }
  1037. if err != nil {
  1038. if want.VE == "" {
  1039. t.Errorf("want%d: %v got unexpected error: %v", wantIdx, value, err)
  1040. } else if !strings.Contains(err.Error(), want.VE) {
  1041. t.Errorf("want#%d: %v got %q, want %q", wantIdx, value, err, want.VE)
  1042. }
  1043. return
  1044. } else {
  1045. if want.VE != "" {
  1046. t.Errorf("want#%d: %v got nil error, want %q", wantIdx, value, want.VE)
  1047. return
  1048. }
  1049. }
  1050. if got != want.V {
  1051. t.Errorf("want#%d: %v got %v, want %v", wantIdx, value, got, want.V)
  1052. }
  1053. }