1
0
mirror of https://github.com/json-iterator/go.git synced 2025-11-26 22:40:13 +02:00

consolidate mor tests

This commit is contained in:
Tao Wen
2018-02-13 23:49:40 +08:00
parent 761ce8cce2
commit 8fa357ab7b
33 changed files with 1132 additions and 1663 deletions

View File

@@ -71,5 +71,19 @@ func init() {
Map map[string]string
})(nil),
(*[]uint8)(nil),
(*[]GeoLocation)(nil),
)
}
type GeoLocation struct {
Id string `json:"id,omitempty" db:"id"`
}
func (p *GeoLocation) MarshalJSON() ([]byte, error) {
return []byte(`{}`), nil
}
func (p *GeoLocation) UnmarshalJSON(input []byte) error {
p.Id = "hello"
return nil
}

View File

@@ -59,6 +59,7 @@ func init() {
(*SameLevel2BothTagged)(nil),
(*SameLevel2NoTags)(nil),
(*SameLevel2Tagged)(nil),
(*EmbeddedPtr)(nil),
)
}
@@ -218,3 +219,15 @@ type SameLevel2Tagged struct {
SameLevel2TaggedE1
SameLevel2TaggedE2
}
type EmbeddedPtrO1 struct {
O1F string
}
type EmbeddedPtrOption struct {
O1 *EmbeddedPtrO1
}
type EmbeddedPtr struct {
EmbeddedPtrOption `json:","`
}

View File

@@ -1,6 +1,7 @@
package test
func init() {
// TODO: fix this
//testCases = append(testCases,
// (*struct {
// Upper bool `json:"M"`

View File

@@ -142,6 +142,9 @@ func init() {
B string `json:"b,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
})(nil),
(*struct {
Field bool `json:",omitempty,string"`
})(nil),
)
}

View File

@@ -1,5 +1,7 @@
package test
import "time"
func init() {
testCases = append(testCases,
(*struct1Alias)(nil),
@@ -246,6 +248,13 @@ func init() {
(*struct {
F struct3
})(nil),
(*struct {
TF1 struct {
F1 withTime
F2 *withTime
}
})(nil),
(*DeeplyNested)(nil),
)
}
@@ -274,4 +283,28 @@ type struct3 struct {
F1 stringAlias
F2 stringAlias
F3 stringAlias
}
}
type withTime struct {
time.Time
}
func (t *withTime) UnmarshalJSON(b []byte) error {
return nil
}
func (t withTime) MarshalJSON() ([]byte, error) {
return []byte(`"fake"`), nil
}
type YetYetAnotherObject struct {
Field string
}
type YetAnotherObject struct {
Field *YetYetAnotherObject
}
type AnotherObject struct {
Field *YetAnotherObject
}
type DeeplyNested struct {
Me *AnotherObject
}

View File

@@ -18,53 +18,55 @@ var asymmetricTestCases [][2]interface{}
func Test_symmetric(t *testing.T) {
for _, testCase := range testCases {
valType := reflect.TypeOf(testCase).Elem()
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 100; i++ {
beforePtrVal := reflect.New(valType)
beforePtr := beforePtrVal.Interface()
fz.Fuzz(beforePtr)
before := beforePtrVal.Elem().Interface()
t.Run(valType.String(), func(t *testing.T) {
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 100; i++ {
beforePtrVal := reflect.New(valType)
beforePtr := beforePtrVal.Interface()
fz.Fuzz(beforePtr)
before := beforePtrVal.Elem().Interface()
jbStd, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal with stdlib: %v", err)
}
if len(strings.TrimSpace(string(jbStd))) == 0 {
t.Fatal("stdlib marshal produced empty result and no error")
}
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal with jsoniter: %v", err)
}
if len(strings.TrimSpace(string(jbIter))) == 0 {
t.Fatal("jsoniter marshal produced empty result and no error")
}
if string(jbStd) != string(jbIter) {
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
jbStd, err := json.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal with stdlib: %v", err)
}
if len(strings.TrimSpace(string(jbStd))) == 0 {
t.Fatal("stdlib marshal produced empty result and no error")
}
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
if err != nil {
t.Fatalf("failed to marshal with jsoniter: %v", err)
}
if len(strings.TrimSpace(string(jbIter))) == 0 {
t.Fatal("jsoniter marshal produced empty result and no error")
}
if string(jbStd) != string(jbIter) {
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
indent(jbStd, " "), indent(jbIter, " "), dump(before))
}
afterStdPtrVal := reflect.New(valType)
afterStdPtr := afterStdPtrVal.Interface()
err = json.Unmarshal(jbIter, afterStdPtr)
if err != nil {
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
err, indent(jbIter, " "))
afterStdPtrVal := reflect.New(valType)
afterStdPtr := afterStdPtrVal.Interface()
err = json.Unmarshal(jbIter, afterStdPtr)
if err != nil {
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
err, indent(jbIter, " "))
}
afterIterPtrVal := reflect.New(valType)
afterIterPtr := afterIterPtrVal.Interface()
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, afterIterPtr)
if err != nil {
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
err, indent(jbIter, " "))
}
afterStd := afterStdPtrVal.Elem().Interface()
afterIter := afterIterPtrVal.Elem().Interface()
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
afterIterPtrVal := reflect.New(valType)
afterIterPtr := afterIterPtrVal.Interface()
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, afterIterPtr)
if err != nil {
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
err, indent(jbIter, " "))
}
afterStd := afterStdPtrVal.Elem().Interface()
afterIter := afterIterPtrVal.Elem().Interface()
if fingerprint(afterStd) != fingerprint(afterIter) {
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
dump(afterStd), dump(afterIter), indent(jbIter, " "))
}
}
})
}
}