mirror of
https://github.com/json-iterator/go.git
synced 2025-05-13 21:36:29 +02:00
consolidate more tests
This commit is contained in:
parent
e3bc511e5a
commit
64c1c67885
@ -1,71 +1,72 @@
|
|||||||
package jsoniter
|
package any_tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/json-iterator/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
// if must be valid is useless, just drop this test
|
// if must be valid is useless, just drop this test
|
||||||
func Test_must_be_valid(t *testing.T) {
|
func Test_must_be_valid(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
any := Get([]byte("123"))
|
any := jsoniter.Get([]byte("123"))
|
||||||
should.Equal(any.MustBeValid().ToInt(), 123)
|
should.Equal(any.MustBeValid().ToInt(), 123)
|
||||||
|
|
||||||
any = Wrap(int8(10))
|
any = jsoniter.Wrap(int8(10))
|
||||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||||
|
|
||||||
any = Wrap(int16(10))
|
any = jsoniter.Wrap(int16(10))
|
||||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||||
|
|
||||||
any = Wrap(int32(10))
|
any = jsoniter.Wrap(int32(10))
|
||||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||||
|
|
||||||
any = Wrap(int64(10))
|
any = jsoniter.Wrap(int64(10))
|
||||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||||
|
|
||||||
any = Wrap(uint(10))
|
any = jsoniter.Wrap(uint(10))
|
||||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||||
|
|
||||||
any = Wrap(uint8(10))
|
any = jsoniter.Wrap(uint8(10))
|
||||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||||
|
|
||||||
any = Wrap(uint16(10))
|
any = jsoniter.Wrap(uint16(10))
|
||||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||||
|
|
||||||
any = Wrap(uint32(10))
|
any = jsoniter.Wrap(uint32(10))
|
||||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||||
|
|
||||||
any = Wrap(uint64(10))
|
any = jsoniter.Wrap(uint64(10))
|
||||||
should.Equal(any.MustBeValid().ToInt(), 10)
|
should.Equal(any.MustBeValid().ToInt(), 10)
|
||||||
|
|
||||||
any = Wrap(float32(10))
|
any = jsoniter.Wrap(float32(10))
|
||||||
should.Equal(any.MustBeValid().ToFloat64(), float64(10))
|
should.Equal(any.MustBeValid().ToFloat64(), float64(10))
|
||||||
|
|
||||||
any = Wrap(float64(10))
|
any = jsoniter.Wrap(float64(10))
|
||||||
should.Equal(any.MustBeValid().ToFloat64(), float64(10))
|
should.Equal(any.MustBeValid().ToFloat64(), float64(10))
|
||||||
|
|
||||||
any = Wrap(true)
|
any = jsoniter.Wrap(true)
|
||||||
should.Equal(any.MustBeValid().ToFloat64(), float64(1))
|
should.Equal(any.MustBeValid().ToFloat64(), float64(1))
|
||||||
|
|
||||||
any = Wrap(false)
|
any = jsoniter.Wrap(false)
|
||||||
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
||||||
|
|
||||||
any = Wrap(nil)
|
any = jsoniter.Wrap(nil)
|
||||||
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
||||||
|
|
||||||
any = Wrap(struct{ age int }{age: 1})
|
any = jsoniter.Wrap(struct{ age int }{age: 1})
|
||||||
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
||||||
|
|
||||||
any = Wrap(map[string]interface{}{"abc": 1})
|
any = jsoniter.Wrap(map[string]interface{}{"abc": 1})
|
||||||
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
||||||
|
|
||||||
any = Wrap("abc")
|
any = jsoniter.Wrap("abc")
|
||||||
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
||||||
|
|
||||||
any = Wrap([]int{})
|
any = jsoniter.Wrap([]int{})
|
||||||
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
|
||||||
|
|
||||||
any = Wrap([]int{1, 2})
|
any = jsoniter.Wrap([]int{1, 2})
|
||||||
should.Equal(any.MustBeValid().ToFloat64(), float64(1))
|
should.Equal(any.MustBeValid().ToFloat64(), float64(1))
|
||||||
}
|
}
|
@ -1,168 +0,0 @@
|
|||||||
package jsoniter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Test_read_null(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
iter := ParseString(ConfigDefault, `null`)
|
|
||||||
should.True(iter.ReadNil())
|
|
||||||
iter = ParseString(ConfigDefault, `null`)
|
|
||||||
should.Nil(iter.Read())
|
|
||||||
iter = ParseString(ConfigDefault, `navy`)
|
|
||||||
iter.Read()
|
|
||||||
should.True(iter.Error != nil && iter.Error != io.EOF)
|
|
||||||
iter = ParseString(ConfigDefault, `navy`)
|
|
||||||
iter.ReadNil()
|
|
||||||
should.True(iter.Error != nil && iter.Error != io.EOF)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_write_null(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
stream := NewStream(ConfigDefault, buf, 4096)
|
|
||||||
stream.WriteNil()
|
|
||||||
stream.Flush()
|
|
||||||
should.Nil(stream.Error)
|
|
||||||
should.Equal("null", buf.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_encode_null(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
str, err := MarshalToString(nil)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("null", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_null_object_field(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
iter := ParseString(ConfigDefault, `[null,"a"]`)
|
|
||||||
iter.ReadArray()
|
|
||||||
if iter.ReadObject() != "" {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
iter.ReadArray()
|
|
||||||
if iter.ReadString() != "a" {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
type TestObject struct {
|
|
||||||
Field string
|
|
||||||
}
|
|
||||||
objs := []TestObject{}
|
|
||||||
should.Nil(UnmarshalFromString("[null]", &objs))
|
|
||||||
should.Len(objs, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_null_array_element(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
iter := ParseString(ConfigDefault, `[null,"a"]`)
|
|
||||||
should.True(iter.ReadArray())
|
|
||||||
should.True(iter.ReadNil())
|
|
||||||
should.True(iter.ReadArray())
|
|
||||||
should.Equal("a", iter.ReadString())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_null_array(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
arr := []string{}
|
|
||||||
should.Nil(UnmarshalFromString("null", &arr))
|
|
||||||
should.Nil(arr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_null_map(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
arr := map[string]string{}
|
|
||||||
should.Nil(UnmarshalFromString("null", &arr))
|
|
||||||
should.Nil(arr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_null_string(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
iter := ParseString(ConfigDefault, `[null,"a"]`)
|
|
||||||
should.True(iter.ReadArray())
|
|
||||||
should.Equal("", iter.ReadString())
|
|
||||||
should.True(iter.ReadArray())
|
|
||||||
should.Equal("a", iter.ReadString())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_null_skip(t *testing.T) {
|
|
||||||
iter := ParseString(ConfigDefault, `[null,"a"]`)
|
|
||||||
iter.ReadArray()
|
|
||||||
iter.Skip()
|
|
||||||
iter.ReadArray()
|
|
||||||
if iter.ReadString() != "a" {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_encode_nil_map(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
type Ttest map[string]string
|
|
||||||
var obj1 Ttest
|
|
||||||
output, err := json.Marshal(obj1)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("null", string(output))
|
|
||||||
output, err = json.Marshal(&obj1)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("null", string(output))
|
|
||||||
output, err = Marshal(obj1)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("null", string(output))
|
|
||||||
output, err = Marshal(&obj1)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("null", string(output))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_encode_nil_array(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
type Ttest []string
|
|
||||||
var obj1 Ttest
|
|
||||||
output, err := json.Marshal(obj1)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("null", string(output))
|
|
||||||
output, err = json.Marshal(&obj1)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("null", string(output))
|
|
||||||
output, err = Marshal(obj1)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("null", string(output))
|
|
||||||
output, err = Marshal(&obj1)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("null", string(output))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_nil_num(t *testing.T) {
|
|
||||||
type TestData struct {
|
|
||||||
Field int `json:"field"`
|
|
||||||
}
|
|
||||||
should := require.New(t)
|
|
||||||
|
|
||||||
data1 := []byte(`{"field": 42}`)
|
|
||||||
data2 := []byte(`{"field": null}`)
|
|
||||||
|
|
||||||
// Checking stdlib behavior as well
|
|
||||||
obj2 := TestData{}
|
|
||||||
err := json.Unmarshal(data1, &obj2)
|
|
||||||
should.Equal(nil, err)
|
|
||||||
should.Equal(42, obj2.Field)
|
|
||||||
|
|
||||||
err = json.Unmarshal(data2, &obj2)
|
|
||||||
should.Equal(nil, err)
|
|
||||||
should.Equal(42, obj2.Field)
|
|
||||||
|
|
||||||
obj := TestData{}
|
|
||||||
|
|
||||||
err = Unmarshal(data1, &obj)
|
|
||||||
should.Equal(nil, err)
|
|
||||||
should.Equal(42, obj.Field)
|
|
||||||
|
|
||||||
err = Unmarshal(data2, &obj)
|
|
||||||
should.Equal(nil, err)
|
|
||||||
should.Equal(42, obj.Field)
|
|
||||||
}
|
|
@ -1,9 +1,10 @@
|
|||||||
package jsoniter
|
package misc_tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"github.com/json-iterator/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Level1 struct {
|
type Level1 struct {
|
||||||
@ -15,7 +16,7 @@ type Level2 struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_nested(t *testing.T) {
|
func Test_nested(t *testing.T) {
|
||||||
iter := ParseString(ConfigDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
||||||
l1 := Level1{}
|
l1 := Level1{}
|
||||||
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
||||||
switch l1Field {
|
switch l1Field {
|
||||||
@ -50,7 +51,7 @@ func Test_nested(t *testing.T) {
|
|||||||
|
|
||||||
func Benchmark_jsoniter_nested(b *testing.B) {
|
func Benchmark_jsoniter_nested(b *testing.B) {
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter := ParseString(ConfigDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
||||||
l1 := Level1{}
|
l1 := Level1{}
|
||||||
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
||||||
switch l1Field {
|
switch l1Field {
|
||||||
@ -63,7 +64,7 @@ func Benchmark_jsoniter_nested(b *testing.B) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func readLevel1Hello(iter *Iterator) []Level2 {
|
func readLevel1Hello(iter *jsoniter.Iterator) []Level2 {
|
||||||
l2Array := make([]Level2, 0, 2)
|
l2Array := make([]Level2, 0, 2)
|
||||||
for iter.ReadArray() {
|
for iter.ReadArray() {
|
||||||
l2 := Level2{}
|
l2 := Level2{}
|
81
misc_tests/jsoniter_null_test.go
Normal file
81
misc_tests/jsoniter_null_test.go
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
package misc_tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_read_null(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `null`)
|
||||||
|
should.True(iter.ReadNil())
|
||||||
|
iter = jsoniter.ParseString(jsoniter.ConfigDefault, `null`)
|
||||||
|
should.Nil(iter.Read())
|
||||||
|
iter = jsoniter.ParseString(jsoniter.ConfigDefault, `navy`)
|
||||||
|
iter.Read()
|
||||||
|
should.True(iter.Error != nil && iter.Error != io.EOF)
|
||||||
|
iter = jsoniter.ParseString(jsoniter.ConfigDefault, `navy`)
|
||||||
|
iter.ReadNil()
|
||||||
|
should.True(iter.Error != nil && iter.Error != io.EOF)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_write_null(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
||||||
|
stream.WriteNil()
|
||||||
|
stream.Flush()
|
||||||
|
should.Nil(stream.Error)
|
||||||
|
should.Equal("null", buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_decode_null_object_field(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null,"a"]`)
|
||||||
|
iter.ReadArray()
|
||||||
|
if iter.ReadObject() != "" {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
iter.ReadArray()
|
||||||
|
if iter.ReadString() != "a" {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
type TestObject struct {
|
||||||
|
Field string
|
||||||
|
}
|
||||||
|
objs := []TestObject{}
|
||||||
|
should.Nil(jsoniter.UnmarshalFromString("[null]", &objs))
|
||||||
|
should.Len(objs, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_decode_null_array_element(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null,"a"]`)
|
||||||
|
should.True(iter.ReadArray())
|
||||||
|
should.True(iter.ReadNil())
|
||||||
|
should.True(iter.ReadArray())
|
||||||
|
should.Equal("a", iter.ReadString())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_decode_null_string(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null,"a"]`)
|
||||||
|
should.True(iter.ReadArray())
|
||||||
|
should.Equal("", iter.ReadString())
|
||||||
|
should.True(iter.ReadArray())
|
||||||
|
should.Equal("a", iter.ReadString())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_decode_null_skip(t *testing.T) {
|
||||||
|
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null,"a"]`)
|
||||||
|
iter.ReadArray()
|
||||||
|
iter.Skip()
|
||||||
|
iter.ReadArray()
|
||||||
|
if iter.ReadString() != "a" {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ package test
|
|||||||
import "math/big"
|
import "math/big"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
nilMap := map[string]string(nil)
|
||||||
marshalCases = append(marshalCases,
|
marshalCases = append(marshalCases,
|
||||||
map[string]interface{}{"abc": 1},
|
map[string]interface{}{"abc": 1},
|
||||||
map[string]MyInterface{"hello": MyString("world")},
|
map[string]MyInterface{"hello": MyString("world")},
|
||||||
@ -17,10 +18,15 @@ func init() {
|
|||||||
uint64(2): "a",
|
uint64(2): "a",
|
||||||
uint64(4): "a",
|
uint64(4): "a",
|
||||||
},
|
},
|
||||||
|
nilMap,
|
||||||
|
&nilMap,
|
||||||
)
|
)
|
||||||
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
||||||
ptr: (*map[string]string)(nil),
|
ptr: (*map[string]string)(nil),
|
||||||
input: `{"k\"ey": "val"}`,
|
input: `{"k\"ey": "val"}`,
|
||||||
|
}, unmarshalCase{
|
||||||
|
ptr: (*map[string]string)(nil),
|
||||||
|
input: `null`,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,14 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
marshalCases = append(marshalCases, []interface{}{"hello"})
|
nilSlice := []string(nil)
|
||||||
}
|
marshalCases = append(marshalCases,
|
||||||
|
[]interface{}{"hello"},
|
||||||
|
nilSlice,
|
||||||
|
&nilSlice,
|
||||||
|
)
|
||||||
|
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
||||||
|
ptr: (*[]string)(nil),
|
||||||
|
input: "null",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -11,6 +11,11 @@ func init() {
|
|||||||
Field interface{}
|
Field interface{}
|
||||||
})(nil),
|
})(nil),
|
||||||
input: `{"Field": "hello"}`,
|
input: `{"Field": "hello"}`,
|
||||||
|
}, unmarshalCase{
|
||||||
|
ptr: (*struct {
|
||||||
|
Field int `json:"field"`
|
||||||
|
})(nil),
|
||||||
|
input: `{"field": null}`,
|
||||||
})
|
})
|
||||||
marshalCases = append(marshalCases,
|
marshalCases = append(marshalCases,
|
||||||
struct {
|
struct {
|
||||||
|
@ -15,7 +15,9 @@ type unmarshalCase struct {
|
|||||||
|
|
||||||
var unmarshalCases []unmarshalCase
|
var unmarshalCases []unmarshalCase
|
||||||
|
|
||||||
var marshalCases []interface{}
|
var marshalCases = []interface{}{
|
||||||
|
nil,
|
||||||
|
}
|
||||||
|
|
||||||
func Test_unmarshal(t *testing.T) {
|
func Test_unmarshal(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user