1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-12 22:47:42 +02:00

consolidate more tests

This commit is contained in:
Tao Wen
2018-02-14 10:13:34 +08:00
parent a8708bca85
commit 477be43d00
15 changed files with 329 additions and 913 deletions

View File

@ -199,3 +199,28 @@ func TestDecodeErrorType(t *testing.T) {
should.Nil(jsoniter.Unmarshal([]byte("null"), &err))
should.NotNil(jsoniter.Unmarshal([]byte("123"), &err))
}
func Test_decode_slash(t *testing.T) {
should := require.New(t)
var obj interface{}
should.NotNil(json.Unmarshal([]byte("\\"), &obj))
should.NotNil(jsoniter.UnmarshalFromString("\\", &obj))
}
func Test_NilInput(t *testing.T) {
var jb []byte // nil
var out string
err := jsoniter.Unmarshal(jb, &out)
if err == nil {
t.Errorf("Expected error")
}
}
func Test_EmptyInput(t *testing.T) {
jb := []byte("")
var out string
err := jsoniter.Unmarshal(jb, &out)
if err == nil {
t.Errorf("Expected error")
}
}

View File

@ -0,0 +1,88 @@
package test
import (
"testing"
"github.com/json-iterator/go"
"encoding/json"
"unicode/utf8"
)
func init() {
marshalCases = append(marshalCases,
`>`,
`"数字山谷"`,
"he\u2029\u2028he",
)
for i := 0; i < utf8.RuneSelf; i++ {
marshalCases = append(marshalCases, string([]byte{byte(i)}))
}
}
func Test_read_string(t *testing.T) {
badInputs := []string{
``,
`"`,
`"\"`,
`"\\\"`,
"\"\n\"",
`"\U0001f64f"`,
`"\uD83D\u00"`,
}
for i := 0; i < 32; i++ {
// control characters are invalid
badInputs = append(badInputs, string([]byte{'"', byte(i), '"'}))
}
for _, input := range badInputs {
testReadString(t, input, "", true, "json.Unmarshal", json.Unmarshal)
testReadString(t, input, "", true, "jsoniter.Unmarshal", jsoniter.Unmarshal)
testReadString(t, input, "", true, "jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
}
goodInputs := []struct {
input string
expectValue string
}{
{`""`, ""},
{`"a"`, "a"},
{`null`, ""},
{`"Iñtërnâtiônàlizætiøn,💝🐹🌇⛔"`, "Iñtërnâtiônàlizætiøn,💝🐹🌇⛔"},
{`"\uD83D"`, string([]byte{239, 191, 189})},
{`"\uD83D\\"`, string([]byte{239, 191, 189, '\\'})},
{`"\uD83D\ub000"`, string([]byte{239, 191, 189, 235, 128, 128})},
{`"\uD83D\ude04"`, "😄"},
{`"\uDEADBEEF"`, string([]byte{239, 191, 189, 66, 69, 69, 70})},
{`"hel\"lo"`, `hel"lo`},
{`"hel\\\/lo"`, `hel\/lo`},
{`"hel\\blo"`, `hel\blo`},
{`"hel\\\blo"`, "hel\\\blo"},
{`"hel\\nlo"`, `hel\nlo`},
{`"hel\\\nlo"`, "hel\\\nlo"},
{`"hel\\tlo"`, `hel\tlo`},
{`"hel\\flo"`, `hel\flo`},
{`"hel\\\flo"`, "hel\\\flo"},
{`"hel\\\rlo"`, "hel\\\rlo"},
{`"hel\\\tlo"`, "hel\\\tlo"},
{`"\u4e2d\u6587"`, "中文"},
{`"\ud83d\udc4a"`, "\xf0\x9f\x91\x8a"},
}
for _, tc := range goodInputs {
testReadString(t, tc.input, tc.expectValue, false, "json.Unmarshal", json.Unmarshal)
testReadString(t, tc.input, tc.expectValue, false, "jsoniter.Unmarshal", jsoniter.Unmarshal)
testReadString(t, tc.input, tc.expectValue, false, "jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
}
}
func testReadString(t *testing.T, input string, expectValue string, expectError bool, marshalerName string, marshaler func([]byte, interface{}) error) {
var value string
err := marshaler([]byte(input), &value)
if expectError != (err != nil) {
t.Errorf("%q: %s: expected error %v, got %v", input, marshalerName, expectError, err)
return
}
if value != expectValue {
t.Errorf("%q: %s: expected %q, got %q", input, marshalerName, expectValue, value)
return
}
}

View File

@ -49,6 +49,14 @@ func init() {
Field2 json.RawMessage
})(nil),
input: `{"field1": "hello", "field2":[1,2,3]}`,
}, unmarshalCase{
ptr: (*struct {
a int
b <-chan int
C int
d *time.Timer
})(nil),
input: `{"a": 444, "b":"bad", "C":256, "d":{"not":"a timer"}}`,
})
marshalCases = append(marshalCases,
struct {
@ -135,6 +143,17 @@ func init() {
Field1 *string
Field2 *string
}{Field2: pString("world")},
struct {
a int
b <-chan int
C int
d *time.Timer
}{
a: 42,
b: make(<-chan int, 10),
C: 21,
d: time.NewTimer(10 * time.Second),
},
)
}