mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
consolidate more tests
This commit is contained in:
parent
658ff9ef15
commit
a8708bca85
@ -1,46 +0,0 @@
|
|||||||
package jsoniter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Test_encode_optional_int_pointer(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
var ptr *int
|
|
||||||
str, err := MarshalToString(ptr)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("null", str)
|
|
||||||
val := 100
|
|
||||||
ptr = &val
|
|
||||||
str, err = MarshalToString(ptr)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal("100", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_struct_with_optional_field(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
type TestObject struct {
|
|
||||||
Field1 *string
|
|
||||||
Field2 *string
|
|
||||||
}
|
|
||||||
obj := TestObject{}
|
|
||||||
UnmarshalFromString(`{"field1": null, "field2": "world"}`, &obj)
|
|
||||||
should.Nil(obj.Field1)
|
|
||||||
should.Equal("world", *obj.Field2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_encode_struct_with_optional_field(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
type TestObject struct {
|
|
||||||
Field1 *string
|
|
||||||
Field2 *string
|
|
||||||
}
|
|
||||||
obj := TestObject{}
|
|
||||||
world := "world"
|
|
||||||
obj.Field2 = &world
|
|
||||||
str, err := MarshalToString(obj)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Contains(str, `"Field1":null`)
|
|
||||||
should.Contains(str, `"Field2":"world"`)
|
|
||||||
}
|
|
@ -1,75 +1,29 @@
|
|||||||
package jsoniter
|
package misc_tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"github.com/json-iterator/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_json_RawMessage(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
var data json.RawMessage
|
|
||||||
should.Nil(Unmarshal([]byte(`[1,2,3]`), &data))
|
|
||||||
should.Equal(`[1,2,3]`, string(data))
|
|
||||||
str, err := MarshalToString(data)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(`[1,2,3]`, str)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_jsoniter_RawMessage(t *testing.T) {
|
func Test_jsoniter_RawMessage(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
var data RawMessage
|
var data jsoniter.RawMessage
|
||||||
should.Nil(Unmarshal([]byte(`[1,2,3]`), &data))
|
should.Nil(jsoniter.Unmarshal([]byte(`[1,2,3]`), &data))
|
||||||
should.Equal(`[1,2,3]`, string(data))
|
should.Equal(`[1,2,3]`, string(data))
|
||||||
str, err := MarshalToString(data)
|
str, err := jsoniter.MarshalToString(data)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(`[1,2,3]`, str)
|
should.Equal(`[1,2,3]`, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_json_RawMessage_in_struct(t *testing.T) {
|
|
||||||
type TestObject struct {
|
|
||||||
Field1 string
|
|
||||||
Field2 json.RawMessage
|
|
||||||
}
|
|
||||||
should := require.New(t)
|
|
||||||
var data TestObject
|
|
||||||
should.Nil(Unmarshal([]byte(`{"field1": "hello", "field2": [1,2,3]}`), &data))
|
|
||||||
should.Equal(` [1,2,3]`, string(data.Field2))
|
|
||||||
should.Equal(`hello`, data.Field1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_decode_map_of_raw_message(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
type RawMap map[string]*json.RawMessage
|
|
||||||
b := []byte("{\"test\":[{\"key\":\"value\"}]}")
|
|
||||||
var rawMap RawMap
|
|
||||||
should.Nil(Unmarshal(b, &rawMap))
|
|
||||||
should.Equal(`[{"key":"value"}]`, string(*rawMap["test"]))
|
|
||||||
type Inner struct {
|
|
||||||
Key string `json:"key"`
|
|
||||||
}
|
|
||||||
var inner []Inner
|
|
||||||
Unmarshal(*rawMap["test"], &inner)
|
|
||||||
should.Equal("value", inner[0].Key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_encode_map_of_raw_message(t *testing.T) {
|
|
||||||
should := require.New(t)
|
|
||||||
type RawMap map[string]*json.RawMessage
|
|
||||||
value := json.RawMessage("[]")
|
|
||||||
rawMap := RawMap{"hello": &value}
|
|
||||||
output, err := MarshalToString(rawMap)
|
|
||||||
should.Nil(err)
|
|
||||||
should.Equal(`{"hello":[]}`, output)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_encode_map_of_jsoniter_raw_message(t *testing.T) {
|
func Test_encode_map_of_jsoniter_raw_message(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
type RawMap map[string]*RawMessage
|
type RawMap map[string]*jsoniter.RawMessage
|
||||||
value := RawMessage("[]")
|
value := jsoniter.RawMessage("[]")
|
||||||
rawMap := RawMap{"hello": &value}
|
rawMap := RawMap{"hello": &value}
|
||||||
output, err := MarshalToString(rawMap)
|
output, err := jsoniter.MarshalToString(rawMap)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(`{"hello":[]}`, output)
|
should.Equal(`{"hello":[]}`, output)
|
||||||
}
|
}
|
||||||
@ -82,8 +36,8 @@ func Test_marshal_invalid_json_raw_message(t *testing.T) {
|
|||||||
|
|
||||||
a := A{}
|
a := A{}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
should.Nil(ConfigCompatibleWithStandardLibrary.Unmarshal(message, &a))
|
should.Nil(jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(message, &a))
|
||||||
aout, aouterr := ConfigCompatibleWithStandardLibrary.Marshal(&a)
|
aout, aouterr := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(&a)
|
||||||
should.Equal(`{"raw":null}`, string(aout))
|
should.Equal(`{"raw":null}`, string(aout))
|
||||||
should.Nil(aouterr)
|
should.Nil(aouterr)
|
||||||
}
|
}
|
||||||
@ -99,14 +53,14 @@ func Test_raw_message_memory_not_copied_issue(t *testing.T) {
|
|||||||
BiddingMax *float32 `json:"bidding_max"`
|
BiddingMax *float32 `json:"bidding_max"`
|
||||||
BiddingMin *float32 `json:"bidding_min"`
|
BiddingMin *float32 `json:"bidding_min"`
|
||||||
BiddingType *string `json:"bidding_type"`
|
BiddingType *string `json:"bidding_type"`
|
||||||
Freq *RawMessage `json:"freq"`
|
Freq *jsoniter.RawMessage `json:"freq"`
|
||||||
Targeting *RawMessage `json:"targeting"`
|
Targeting *jsoniter.RawMessage `json:"targeting"`
|
||||||
Url *RawMessage `json:"url"`
|
Url *jsoniter.RawMessage `json:"url"`
|
||||||
Speed *int `json:"speed" db:"speed"`
|
Speed *int `json:"speed" db:"speed"`
|
||||||
}
|
}
|
||||||
|
|
||||||
obj := &IteratorObject{}
|
obj := &IteratorObject{}
|
||||||
decoder := NewDecoder(strings.NewReader(jsonStream))
|
decoder := jsoniter.NewDecoder(strings.NewReader(jsonStream))
|
||||||
err := decoder.Decode(obj)
|
err := decoder.Decode(obj)
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
@ -1,8 +1,14 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import "math/big"
|
import (
|
||||||
|
"math/big"
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
var pRawMessage = func(val json.RawMessage) *json.RawMessage {
|
||||||
|
return &val
|
||||||
|
}
|
||||||
nilMap := map[string]string(nil)
|
nilMap := map[string]string(nil)
|
||||||
marshalCases = append(marshalCases,
|
marshalCases = append(marshalCases,
|
||||||
map[string]interface{}{"abc": 1},
|
map[string]interface{}{"abc": 1},
|
||||||
@ -20,6 +26,7 @@ func init() {
|
|||||||
},
|
},
|
||||||
nilMap,
|
nilMap,
|
||||||
&nilMap,
|
&nilMap,
|
||||||
|
map[string]*json.RawMessage{"hello":pRawMessage(json.RawMessage("[]"))},
|
||||||
)
|
)
|
||||||
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
||||||
ptr: (*map[string]string)(nil),
|
ptr: (*map[string]string)(nil),
|
||||||
@ -27,6 +34,9 @@ func init() {
|
|||||||
}, unmarshalCase{
|
}, unmarshalCase{
|
||||||
ptr: (*map[string]string)(nil),
|
ptr: (*map[string]string)(nil),
|
||||||
input: `null`,
|
input: `null`,
|
||||||
|
}, unmarshalCase{
|
||||||
|
ptr: (*map[string]*json.RawMessage)(nil),
|
||||||
|
input: "{\"test\":[{\"key\":\"value\"}]}",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,9 @@ func init() {
|
|||||||
var pEFace = func(val interface{}) *interface{} {
|
var pEFace = func(val interface{}) *interface{} {
|
||||||
return &val
|
return &val
|
||||||
}
|
}
|
||||||
|
var pInt = func(val int) *int {
|
||||||
|
return &val
|
||||||
|
}
|
||||||
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
||||||
ptr: (**interface{})(nil),
|
ptr: (**interface{})(nil),
|
||||||
input: `"hello"`,
|
input: `"hello"`,
|
||||||
@ -16,5 +19,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
marshalCases = append(marshalCases,
|
marshalCases = append(marshalCases,
|
||||||
pEFace("hello"),
|
pEFace("hello"),
|
||||||
|
(*int)(nil),
|
||||||
|
pInt(100),
|
||||||
)
|
)
|
||||||
}
|
}
|
@ -6,4 +6,8 @@ func init() {
|
|||||||
marshalCases = append(marshalCases,
|
marshalCases = append(marshalCases,
|
||||||
json.RawMessage("{}"),
|
json.RawMessage("{}"),
|
||||||
)
|
)
|
||||||
|
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
||||||
|
ptr: (*json.RawMessage)(nil),
|
||||||
|
input: `[1,2,3]`,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
var pString = func(val string) *string {
|
||||||
|
return &val
|
||||||
|
}
|
||||||
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
||||||
ptr: (*struct {
|
ptr: (*struct {
|
||||||
Field interface{}
|
Field interface{}
|
||||||
@ -34,6 +37,18 @@ func init() {
|
|||||||
Field1 string
|
Field1 string
|
||||||
})(nil),
|
})(nil),
|
||||||
input: `{"\u0046ield1":"hello"}`,
|
input: `{"\u0046ield1":"hello"}`,
|
||||||
|
}, unmarshalCase{
|
||||||
|
ptr: (*struct {
|
||||||
|
Field1 *string
|
||||||
|
Field2 *string
|
||||||
|
})(nil),
|
||||||
|
input: `{"field1": null, "field2": "world"}`,
|
||||||
|
}, unmarshalCase{
|
||||||
|
ptr: (*struct {
|
||||||
|
Field1 string
|
||||||
|
Field2 json.RawMessage
|
||||||
|
})(nil),
|
||||||
|
input: `{"field1": "hello", "field2":[1,2,3]}`,
|
||||||
})
|
})
|
||||||
marshalCases = append(marshalCases,
|
marshalCases = append(marshalCases,
|
||||||
struct {
|
struct {
|
||||||
@ -116,6 +131,10 @@ func init() {
|
|||||||
MaxAge: 20,
|
MaxAge: 20,
|
||||||
},
|
},
|
||||||
structOrder{},
|
structOrder{},
|
||||||
|
struct {
|
||||||
|
Field1 *string
|
||||||
|
Field2 *string
|
||||||
|
}{Field2: pString("world")},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user