| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085 |
- // Copyright 2019 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package json_test
- import (
- "strings"
- "testing"
- "unicode/utf8"
- "github.com/golang/protobuf/v2/internal/encoding/json"
- )
- type R struct {
- // T is expected Type returned from calling Decoder.ReadNext.
- T json.Type
- // E is expected error substring from calling Decoder.ReadNext if set.
- E string
- // V is expected value from calling
- // Value.{Bool()|Float()|Int()|Uint()|String()} depending on type.
- V interface{}
- // VE is expected error substring from calling
- // Value.{Bool()|Float()|Int()|Uint()|String()} depending on type if set.
- VE string
- }
- func TestDecoder(t *testing.T) {
- const space = " \n\r\t"
- tests := []struct {
- input string
- // want is a list of expected values returned from calling
- // Decoder.ReadNext. An item makes the test code invoke
- // Decoder.ReadNext and compare against R.T and R.E. For Bool,
- // Number and String tokens, it invokes the corresponding getter method
- // and compares the returned value against R.V or R.VE if it returned an
- // error.
- want []R
- }{
- {
- input: ``,
- want: []R{{T: json.EOF}},
- },
- {
- input: space,
- want: []R{{T: json.EOF}},
- },
- {
- // Calling ReadNext after EOF will keep returning EOF for
- // succeeding ReadNext calls.
- input: space,
- want: []R{
- {T: json.EOF},
- {T: json.EOF},
- {T: json.EOF},
- },
- },
- // JSON literals.
- {
- input: space + `null` + space,
- want: []R{
- {T: json.Null},
- {T: json.EOF},
- },
- },
- {
- input: space + `true` + space,
- want: []R{
- {T: json.Bool, V: true},
- {T: json.EOF},
- },
- },
- {
- input: space + `false` + space,
- want: []R{
- {T: json.Bool, V: false},
- {T: json.EOF},
- },
- },
- {
- // Error returned will produce the same error again.
- input: space + `foo` + space,
- want: []R{
- {E: `invalid value foo`},
- {E: `invalid value foo`},
- },
- },
- // JSON strings.
- {
- input: space + `""` + space,
- want: []R{
- {T: json.String, V: ""},
- {T: json.EOF},
- },
- },
- {
- input: space + `"hello"` + space,
- want: []R{
- {T: json.String, V: "hello"},
- {T: json.EOF},
- },
- },
- {
- input: `"hello`,
- want: []R{{E: `unexpected EOF`}},
- },
- {
- input: "\"\x00\"",
- want: []R{{E: `invalid character '\x00' in string`}},
- },
- {
- input: "\"\u0031\u0032\"",
- want: []R{
- {T: json.String, V: "12"},
- {T: json.EOF},
- },
- },
- {
- // Invalid UTF-8 error is returned in ReadString instead of ReadNext.
- input: "\"\xff\"",
- want: []R{
- {T: json.String, E: `invalid UTF-8 detected`, V: string("\xff")},
- {T: json.EOF},
- },
- },
- {
- input: `"` + string(utf8.RuneError) + `"`,
- want: []R{
- {T: json.String, V: string(utf8.RuneError)},
- {T: json.EOF},
- },
- },
- {
- input: `"\uFFFD"`,
- want: []R{
- {T: json.String, V: string(utf8.RuneError)},
- {T: json.EOF},
- },
- },
- {
- input: `"\x"`,
- want: []R{{E: `invalid escape code "\\x" in string`}},
- },
- {
- input: `"\uXXXX"`,
- want: []R{{E: `invalid escape code "\\uXXXX" in string`}},
- },
- {
- input: `"\uDEAD"`, // unmatched surrogate pair
- want: []R{{E: `unexpected EOF`}},
- },
- {
- input: `"\uDEAD\uBEEF"`, // invalid surrogate half
- want: []R{{E: `invalid escape code "\\uBEEF" in string`}},
- },
- {
- input: `"\uD800\udead"`, // valid surrogate pair
- want: []R{
- {T: json.String, V: `𐊭`},
- {T: json.EOF},
- },
- },
- {
- input: `"\u0000\"\\\/\b\f\n\r\t"`,
- want: []R{
- {T: json.String, V: "\u0000\"\\/\b\f\n\r\t"},
- {T: json.EOF},
- },
- },
- // Invalid JSON numbers.
- {
- input: `-`,
- want: []R{{E: `invalid number -`}},
- },
- {
- input: `+0`,
- want: []R{{E: `invalid value +0`}},
- },
- {
- input: `-+`,
- want: []R{{E: `invalid number -+`}},
- },
- {
- input: `0.`,
- want: []R{{E: `invalid number 0.`}},
- },
- {
- input: `.1`,
- want: []R{{E: `invalid value .1`}},
- },
- {
- input: `1.0.1`,
- want: []R{{E: `invalid number 1.0.1`}},
- },
- {
- input: `1..1`,
- want: []R{{E: `invalid number 1..1`}},
- },
- {
- input: `-1-2`,
- want: []R{{E: `invalid number -1-2`}},
- },
- {
- input: `01`,
- want: []R{{E: `invalid number 01`}},
- },
- {
- input: `1e`,
- want: []R{{E: `invalid number 1e`}},
- },
- {
- input: `1e1.2`,
- want: []R{{E: `invalid number 1e1.2`}},
- },
- {
- input: `1Ee`,
- want: []R{{E: `invalid number 1Ee`}},
- },
- {
- input: `1.e1`,
- want: []R{{E: `invalid number 1.e1`}},
- },
- {
- input: `1.e+`,
- want: []R{{E: `invalid number 1.e+`}},
- },
- {
- input: `1e+-2`,
- want: []R{{E: `invalid number 1e+-2`}},
- },
- {
- input: `1e--2`,
- want: []R{{E: `invalid number 1e--2`}},
- },
- {
- input: `1.0true`,
- want: []R{{E: `invalid number 1.0true`}},
- },
- // JSON numbers as floating point.
- {
- input: space + `0.0` + space,
- want: []R{
- {T: json.Number, V: float32(0)},
- {T: json.EOF},
- },
- },
- {
- input: space + `0` + space,
- want: []R{
- {T: json.Number, V: float32(0)},
- {T: json.EOF},
- },
- },
- {
- input: space + `-0` + space,
- want: []R{
- {T: json.Number, V: float32(0)},
- {T: json.EOF},
- },
- },
- {
- input: `-1.02`,
- want: []R{
- {T: json.Number, V: float32(-1.02)},
- {T: json.EOF},
- },
- },
- {
- input: `1.020000`,
- want: []R{
- {T: json.Number, V: float32(1.02)},
- {T: json.EOF},
- },
- },
- {
- input: `-1.0e0`,
- want: []R{
- {T: json.Number, V: float32(-1)},
- {T: json.EOF},
- },
- },
- {
- input: `1.0e-000`,
- want: []R{
- {T: json.Number, V: float32(1)},
- {T: json.EOF},
- },
- },
- {
- input: `1e+00`,
- want: []R{
- {T: json.Number, V: float32(1)},
- {T: json.EOF},
- },
- },
- {
- input: `1.02e3`,
- want: []R{
- {T: json.Number, V: float32(1.02e3)},
- {T: json.EOF},
- },
- },
- {
- input: `-1.02E03`,
- want: []R{
- {T: json.Number, V: float32(-1.02e3)},
- {T: json.EOF},
- },
- },
- {
- input: `1.0200e+3`,
- want: []R{
- {T: json.Number, V: float32(1.02e3)},
- {T: json.EOF},
- },
- },
- {
- input: `-1.0200E+03`,
- want: []R{
- {T: json.Number, V: float32(-1.02e3)},
- {T: json.EOF},
- },
- },
- {
- input: `1.0200e-3`,
- want: []R{
- {T: json.Number, V: float32(1.02e-3)},
- {T: json.EOF},
- },
- },
- {
- input: `-1.0200E-03`,
- want: []R{
- {T: json.Number, V: float32(-1.02e-3)},
- {T: json.EOF},
- },
- },
- {
- // Exceeds max float32 limit, but should be ok for float64.
- input: `3.4e39`,
- want: []R{
- {T: json.Number, V: float64(3.4e39)},
- {T: json.EOF},
- },
- },
- {
- // Exceeds max float32 limit.
- input: `3.4e39`,
- want: []R{
- {T: json.Number, V: float32(0), VE: `value out of range`},
- {T: json.EOF},
- },
- },
- {
- // Less than negative max float32 limit.
- input: `-3.4e39`,
- want: []R{
- {T: json.Number, V: float32(0), VE: `value out of range`},
- {T: json.EOF},
- },
- },
- {
- // Exceeds max float64 limit.
- input: `1.79e+309`,
- want: []R{
- {T: json.Number, V: float64(0), VE: `value out of range`},
- {T: json.EOF},
- },
- },
- {
- // Less than negative max float64 limit.
- input: `-1.79e+309`,
- want: []R{
- {T: json.Number, V: float64(0), VE: `value out of range`},
- {T: json.EOF},
- },
- },
- // JSON numbers as signed integers.
- {
- input: space + `0` + space,
- want: []R{
- {T: json.Number, V: int32(0)},
- {T: json.EOF},
- },
- },
- {
- input: space + `-0` + space,
- want: []R{
- {T: json.Number, V: int32(0)},
- {T: json.EOF},
- },
- },
- {
- // Fractional part equals 0 is ok.
- input: `1.00000`,
- want: []R{
- {T: json.Number, V: int32(1)},
- {T: json.EOF},
- },
- },
- {
- // Fractional part not equals 0 returns error.
- input: `1.0000000001`,
- want: []R{
- {T: json.Number, V: int32(0), VE: `cannot convert 1.0000000001 to integer`},
- {T: json.EOF},
- },
- },
- {
- input: `0e0`,
- want: []R{
- {T: json.Number, V: int32(0)},
- {T: json.EOF},
- },
- },
- {
- input: `0.0E0`,
- want: []R{
- {T: json.Number, V: int32(0)},
- {T: json.EOF},
- },
- },
- {
- input: `0.0E10`,
- want: []R{
- {T: json.Number, V: int32(0)},
- {T: json.EOF},
- },
- },
- {
- input: `-1`,
- want: []R{
- {T: json.Number, V: int32(-1)},
- {T: json.EOF},
- },
- },
- {
- input: `1.0e+0`,
- want: []R{
- {T: json.Number, V: int32(1)},
- {T: json.EOF},
- },
- },
- {
- input: `-1E-0`,
- want: []R{
- {T: json.Number, V: int32(-1)},
- {T: json.EOF},
- },
- },
- {
- input: `1E1`,
- want: []R{
- {T: json.Number, V: int32(10)},
- {T: json.EOF},
- },
- },
- {
- input: `-100.00e-02`,
- want: []R{
- {T: json.Number, V: int32(-1)},
- {T: json.EOF},
- },
- },
- {
- input: `0.1200E+02`,
- want: []R{
- {T: json.Number, V: int64(12)},
- {T: json.EOF},
- },
- },
- {
- input: `0.012e2`,
- want: []R{
- {T: json.Number, V: int32(0), VE: `cannot convert 0.012e2 to integer`},
- {T: json.EOF},
- },
- },
- {
- input: `12e-2`,
- want: []R{
- {T: json.Number, V: int32(0), VE: `cannot convert 12e-2 to integer`},
- {T: json.EOF},
- },
- },
- {
- // Exceeds math.MaxInt32.
- input: `2147483648`,
- want: []R{
- {T: json.Number, V: int32(0), VE: `value out of range`},
- {T: json.EOF},
- },
- },
- {
- // Exceeds math.MinInt32.
- input: `-2147483649`,
- want: []R{
- {T: json.Number, V: int32(0), VE: `value out of range`},
- {T: json.EOF},
- },
- },
- {
- // Exceeds math.MaxInt32, but ok for int64.
- input: `2147483648`,
- want: []R{
- {T: json.Number, V: int64(2147483648)},
- {T: json.EOF},
- },
- },
- {
- // Exceeds math.MinInt32, but ok for int64.
- input: `-2147483649`,
- want: []R{
- {T: json.Number, V: int64(-2147483649)},
- {T: json.EOF},
- },
- },
- {
- // Exceeds math.MaxInt64.
- input: `9223372036854775808`,
- want: []R{
- {T: json.Number, V: int64(0), VE: `value out of range`},
- {T: json.EOF},
- },
- },
- {
- // Exceeds math.MinInt64.
- input: `-9223372036854775809`,
- want: []R{
- {T: json.Number, V: int64(0), VE: `value out of range`},
- {T: json.EOF},
- },
- },
- // JSON numbers as unsigned integers.
- {
- input: space + `0` + space,
- want: []R{
- {T: json.Number, V: uint32(0)},
- {T: json.EOF},
- },
- },
- {
- input: space + `-0` + space,
- want: []R{
- {T: json.Number, V: uint32(0)},
- {T: json.EOF},
- },
- },
- {
- input: `-1`,
- want: []R{
- {T: json.Number, V: uint32(0), VE: `invalid syntax`},
- {T: json.EOF},
- },
- },
- {
- // Exceeds math.MaxUint32.
- input: `4294967296`,
- want: []R{
- {T: json.Number, V: uint32(0), VE: `value out of range`},
- {T: json.EOF},
- },
- },
- {
- // Exceeds math.MaxUint64.
- input: `18446744073709551616`,
- want: []R{
- {T: json.Number, V: uint64(0), VE: `value out of range`},
- {T: json.EOF},
- },
- },
- // JSON sequence of values.
- {
- input: `true null`,
- want: []R{
- {T: json.Bool, V: true},
- {E: `unexpected value null`},
- },
- },
- {
- input: "null false",
- want: []R{
- {T: json.Null},
- {E: `unexpected value false`},
- },
- },
- {
- input: `true,false`,
- want: []R{
- {T: json.Bool, V: true},
- {E: `unexpected character ,`},
- },
- },
- {
- input: `47"hello"`,
- want: []R{
- {T: json.Number, V: int32(47)},
- {E: `unexpected value "hello"`},
- },
- },
- {
- input: `47 "hello"`,
- want: []R{
- {T: json.Number, V: int32(47)},
- {E: `unexpected value "hello"`},
- },
- },
- {
- input: `true 42`,
- want: []R{
- {T: json.Bool, V: true},
- {E: `unexpected value 42`},
- },
- },
- // JSON arrays.
- {
- input: space + `[]` + space,
- want: []R{
- {T: json.StartArray},
- {T: json.EndArray},
- {T: json.EOF},
- },
- },
- {
- input: space + `[` + space + `]` + space,
- want: []R{
- {T: json.StartArray},
- {T: json.EndArray},
- {T: json.EOF},
- },
- },
- {
- input: space + `[` + space,
- want: []R{
- {T: json.StartArray},
- {E: `unexpected EOF`},
- },
- },
- {
- input: space + `]` + space,
- want: []R{{E: `unexpected character ]`}},
- },
- {
- input: `[null,true,false, 1e1, "hello" ]`,
- want: []R{
- {T: json.StartArray},
- {T: json.Null},
- {T: json.Bool, V: true},
- {T: json.Bool, V: false},
- {T: json.Number, V: int32(10)},
- {T: json.String, V: "hello"},
- {T: json.EndArray},
- {T: json.EOF},
- },
- },
- {
- input: `[` + space + `true` + space + `,` + space + `"hello"` + space + `]`,
- want: []R{
- {T: json.StartArray},
- {T: json.Bool, V: true},
- {T: json.String, V: "hello"},
- {T: json.EndArray},
- {T: json.EOF},
- },
- },
- {
- input: `[` + space + `true` + space + `,` + space + `]`,
- want: []R{
- {T: json.StartArray},
- {T: json.Bool, V: true},
- {E: `unexpected character ]`},
- },
- },
- {
- input: `[` + space + `false` + space + `]`,
- want: []R{
- {T: json.StartArray},
- {T: json.Bool, V: false},
- {T: json.EndArray},
- {T: json.EOF},
- },
- },
- {
- input: `[` + space + `1` + space + `0` + space + `]`,
- want: []R{
- {T: json.StartArray},
- {T: json.Number, V: int64(1)},
- {E: `unexpected value 0`},
- },
- },
- {
- input: `[null`,
- want: []R{
- {T: json.StartArray},
- {T: json.Null},
- {E: `unexpected EOF`},
- },
- },
- {
- input: `[foo]`,
- want: []R{
- {T: json.StartArray},
- {E: `invalid value foo`},
- },
- },
- {
- input: `[{}, "hello", [true, false], null]`,
- want: []R{
- {T: json.StartArray},
- {T: json.StartObject},
- {T: json.EndObject},
- {T: json.String, V: "hello"},
- {T: json.StartArray},
- {T: json.Bool, V: true},
- {T: json.Bool, V: false},
- {T: json.EndArray},
- {T: json.Null},
- {T: json.EndArray},
- {T: json.EOF},
- },
- },
- {
- input: `[{ ]`,
- want: []R{
- {T: json.StartArray},
- {T: json.StartObject},
- {E: `unexpected character ]`},
- },
- },
- {
- input: `[[ ]`,
- want: []R{
- {T: json.StartArray},
- {T: json.StartArray},
- {T: json.EndArray},
- {E: `unexpected EOF`},
- },
- },
- {
- input: `[,]`,
- want: []R{
- {T: json.StartArray},
- {E: `unexpected character ,`},
- },
- },
- {
- input: `[true "hello"]`,
- want: []R{
- {T: json.StartArray},
- {T: json.Bool, V: true},
- {E: `unexpected value "hello"`},
- },
- },
- {
- input: `[] null`,
- want: []R{
- {T: json.StartArray},
- {T: json.EndArray},
- {E: `unexpected value null`},
- },
- },
- {
- input: `true []`,
- want: []R{
- {T: json.Bool, V: true},
- {E: `unexpected character [`},
- },
- },
- // JSON objects.
- {
- input: space + `{}` + space,
- want: []R{
- {T: json.StartObject},
- {T: json.EndObject},
- {T: json.EOF},
- },
- },
- {
- input: space + `{` + space + `}` + space,
- want: []R{
- {T: json.StartObject},
- {T: json.EndObject},
- {T: json.EOF},
- },
- },
- {
- input: space + `{` + space,
- want: []R{
- {T: json.StartObject},
- {E: `unexpected EOF`},
- },
- },
- {
- input: space + `}` + space,
- want: []R{{E: `unexpected character }`}},
- },
- {
- input: `{` + space + `null` + space + `}`,
- want: []R{
- {T: json.StartObject},
- {E: `unexpected value null`},
- },
- },
- {
- input: `{[]}`,
- want: []R{
- {T: json.StartObject},
- {E: `unexpected character [`},
- },
- },
- {
- input: `{,}`,
- want: []R{
- {T: json.StartObject},
- {E: `unexpected character ,`},
- },
- },
- {
- input: `{"345678"}`,
- want: []R{
- {T: json.StartObject},
- {E: `unexpected character }, missing ":" after object name`},
- },
- },
- {
- input: `{` + space + `"hello"` + space + `:` + space + `"world"` + space + `}`,
- want: []R{
- {T: json.StartObject},
- {T: json.Name, V: "hello"},
- {T: json.String, V: "world"},
- {T: json.EndObject},
- {T: json.EOF},
- },
- },
- {
- input: `{"hello" "world"}`,
- want: []R{
- {T: json.StartObject},
- {E: `unexpected character ", missing ":" after object name`},
- },
- },
- {
- input: `{"hello":`,
- want: []R{
- {T: json.StartObject},
- {T: json.Name, V: "hello"},
- {E: `unexpected EOF`},
- },
- },
- {
- input: `{"hello":"world"`,
- want: []R{
- {T: json.StartObject},
- {T: json.Name, V: "hello"},
- {T: json.String, V: "world"},
- {E: `unexpected EOF`},
- },
- },
- {
- input: `{"hello":"world",`,
- want: []R{
- {T: json.StartObject},
- {T: json.Name, V: "hello"},
- {T: json.String, V: "world"},
- {E: `unexpected EOF`},
- },
- },
- {
- input: `{"34":"89",}`,
- want: []R{
- {T: json.StartObject},
- {T: json.Name, V: "34"},
- {T: json.String, V: "89"},
- {E: `syntax error (line 1:12): unexpected character }`},
- },
- },
- {
- input: `{
- "number": 123e2,
- "bool" : false,
- "object": {"string": "world"},
- "null" : null,
- "array" : [1.01, "hello", true],
- "string": "hello"
- }`,
- want: []R{
- {T: json.StartObject},
- {T: json.Name, V: "number"},
- {T: json.Number, V: int32(12300)},
- {T: json.Name, V: "bool"},
- {T: json.Bool, V: false},
- {T: json.Name, V: "object"},
- {T: json.StartObject},
- {T: json.Name, V: "string"},
- {T: json.String, V: "world"},
- {T: json.EndObject},
- {T: json.Name, V: "null"},
- {T: json.Null},
- {T: json.Name, V: "array"},
- {T: json.StartArray},
- {T: json.Number, V: float32(1.01)},
- {T: json.String, V: "hello"},
- {T: json.Bool, V: true},
- {T: json.EndArray},
- {T: json.Name, V: "string"},
- {T: json.String, V: "hello"},
- {T: json.EndObject},
- {T: json.EOF},
- },
- },
- {
- input: `[
- {"object": {"number": 47}},
- ["list"],
- null
- ]`,
- want: []R{
- {T: json.StartArray},
- {T: json.StartObject},
- {T: json.Name, V: "object"},
- {T: json.StartObject},
- {T: json.Name, V: "number"},
- {T: json.Number, V: uint32(47)},
- {T: json.EndObject},
- {T: json.EndObject},
- {T: json.StartArray},
- {T: json.String, V: "list"},
- {T: json.EndArray},
- {T: json.Null},
- {T: json.EndArray},
- {T: json.EOF},
- },
- },
- // Tests for line and column info.
- {
- input: `12345678 x`,
- want: []R{
- {T: json.Number, V: int64(12345678)},
- {E: `syntax error (line 1:10): invalid value x`},
- },
- },
- {
- input: "\ntrue\n x",
- want: []R{
- {T: json.Bool, V: true},
- {E: `syntax error (line 3:4): invalid value x`},
- },
- },
- {
- input: `"💩"x`,
- want: []R{
- {T: json.String, V: "💩"},
- {E: `syntax error (line 1:4): invalid value x`},
- },
- },
- {
- input: "\n\n[\"🔥🔥🔥\"x",
- want: []R{
- {T: json.StartArray},
- {T: json.String, V: "🔥🔥🔥"},
- {E: `syntax error (line 3:7): invalid value x`},
- },
- },
- {
- // Multi-rune emojis.
- input: `["👍🏻👍🏿"x`,
- want: []R{
- {T: json.StartArray},
- {T: json.String, V: "👍🏻👍🏿"},
- {E: `syntax error (line 1:8): invalid value x`},
- },
- },
- {
- input: `{
- "45678":-1
- }`,
- want: []R{
- {T: json.StartObject},
- {T: json.Name, V: "45678"},
- {T: json.Number, V: uint64(1), VE: "error (line 2:11)"},
- },
- },
- }
- for _, tc := range tests {
- tc := tc
- t.Run("", func(t *testing.T) {
- dec := json.NewDecoder([]byte(tc.input))
- for i, want := range tc.want {
- value, err := dec.ReadNext()
- if err != nil {
- if want.E == "" {
- t.Errorf("input: %v\nReadNext() got unexpected error: %v", tc.input, err)
- } else if !strings.Contains(err.Error(), want.E) {
- t.Errorf("input: %v\nReadNext() got %q, want %q", tc.input, err, want.E)
- }
- } else {
- if want.E != "" {
- t.Errorf("input: %v\nReadNext() got nil error, want %q", tc.input, want.E)
- }
- }
- token := value.Type()
- if token != want.T {
- t.Errorf("input: %v\nReadNext() got %v, want %v", tc.input, token, want.T)
- break
- }
- checkValue(t, value, i, want)
- }
- })
- }
- }
- func checkValue(t *testing.T, value json.Value, wantIdx int, want R) {
- var got interface{}
- var err error
- switch value.Type() {
- case json.Bool:
- got, err = value.Bool()
- case json.String:
- got = value.String()
- case json.Name:
- got, err = value.Name()
- case json.Number:
- switch want.V.(type) {
- case float32:
- got, err = value.Float(32)
- got = float32(got.(float64))
- case float64:
- got, err = value.Float(64)
- case int32:
- got, err = value.Int(32)
- got = int32(got.(int64))
- case int64:
- got, err = value.Int(64)
- case uint32:
- got, err = value.Uint(32)
- got = uint32(got.(uint64))
- case uint64:
- got, err = value.Uint(64)
- }
- default:
- return
- }
- if err != nil {
- if want.VE == "" {
- t.Errorf("want%d: %v got unexpected error: %v", wantIdx, value, err)
- } else if !strings.Contains(err.Error(), want.VE) {
- t.Errorf("want#%d: %v got %q, want %q", wantIdx, value, err, want.VE)
- }
- return
- } else {
- if want.VE != "" {
- t.Errorf("want#%d: %v got nil error, want %q", wantIdx, value, want.VE)
- return
- }
- }
- if got != want.V {
- t.Errorf("want#%d: %v got %v, want %v", wantIdx, value, got, want.V)
- }
- }
|