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

support asymmetric tests

This commit is contained in:
Tao Wen
2018-02-13 20:25:27 +08:00
parent 6fded6eb5f
commit 0ed9de94f2
24 changed files with 200 additions and 1593 deletions

View File

@ -0,0 +1,44 @@
package test
import (
"strings"
"encoding"
)
func init() {
testCases = append(testCases,
(*map[stringKeyType]string)(nil),
(*map[structKeyType]string)(nil),
)
}
type stringKeyType string
func (k stringKeyType) MarshalText() ([]byte, error) {
return []byte("MANUAL__" + k), nil
}
func (k *stringKeyType) UnmarshalText(text []byte) error {
*k = stringKeyType(strings.TrimPrefix(string(text), "MANUAL__"))
return nil
}
var _ encoding.TextMarshaler = stringKeyType("")
var _ encoding.TextUnmarshaler = new(stringKeyType)
type structKeyType struct {
X string
}
func (k structKeyType) MarshalText() ([]byte, error) {
return []byte("MANUAL__" + k.X), nil
}
func (k *structKeyType) UnmarshalText(text []byte) error {
k.X = strings.TrimPrefix(string(text), "MANUAL__")
return nil
}
var _ encoding.TextMarshaler = structKeyType{}
var _ encoding.TextUnmarshaler = &structKeyType{}

View File

@ -0,0 +1,24 @@
package test
func init() {
asymmetricTestCases = append(asymmetricTestCases, [][2]interface{}{
{
(*struct {
Field string
})(nil),
(*struct {
FIELD string
})(nil),
},
{
(*struct {
F1 string
F2 string
F3 string
})(nil),
(*struct {
F1 string
})(nil),
},
}...)
}

View File

@ -7,6 +7,12 @@ import (
"encoding/base64"
)
func init() {
testCases = append(testCases,
(*StringTextMarshaler)(nil),
)
}
// StringTextMarshaler TEST ONLY
type StringTextMarshaler string
@ -48,4 +54,4 @@ func (m *StringTextMarshaler) UnmarshalText(text []byte) error {
}
var _ encoding.TextMarshaler = *new(StringTextMarshaler)
var _ encoding.TextUnmarshaler = new(StringTextMarshaler)
var _ encoding.TextUnmarshaler = new(StringTextMarshaler)

View File

@ -0,0 +1,69 @@
package test
import (
"bytes"
"encoding/base64"
"strings"
"encoding"
)
func init() {
testCases = append(testCases,
(*structTextMarshaler)(nil),
(*structTextMarshalerAlias)(nil),
(*struct {
S string
M structTextMarshaler
I int8
})(nil),
(*struct {
S string
M structTextMarshalerAlias
I int8
})(nil),
)
}
type structTextMarshaler struct {
X string
}
func (m structTextMarshaler) encode(str string) string {
buf := bytes.Buffer{}
b64 := base64.NewEncoder(base64.StdEncoding, &buf)
if _, err := b64.Write([]byte(str)); err != nil {
panic(err)
}
if err := b64.Close(); err != nil {
panic(err)
}
return buf.String()
}
func (m structTextMarshaler) decode(str string) string {
if len(str) == 0 {
return ""
}
b64 := base64.NewDecoder(base64.StdEncoding, strings.NewReader(str))
bs := make([]byte, len(str))
if n, err := b64.Read(bs); err != nil {
panic(err)
} else {
bs = bs[:n]
}
return string(bs)
}
func (m structTextMarshaler) MarshalText() ([]byte, error) {
return []byte(`MANUAL__` + m.encode(m.X)), nil
}
func (m *structTextMarshaler) UnmarshalText(text []byte) error {
m.X = m.decode(strings.TrimPrefix(string(text), "MANUAL__"))
return nil
}
var _ encoding.TextMarshaler = structTextMarshaler{}
var _ encoding.TextUnmarshaler = &structTextMarshaler{}
type structTextMarshalerAlias structTextMarshaler

View File

@ -13,8 +13,9 @@ import (
)
var testCases []interface{}
var asymmetricTestCases [][2]interface{}
func Test(t *testing.T) {
func Test_symmetric(t *testing.T) {
for _, testCase := range testCases {
valType := reflect.TypeOf(testCase).Elem()
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
@ -67,6 +68,60 @@ func Test(t *testing.T) {
}
}
func Test_asymmetric(t *testing.T) {
for _, testCase := range asymmetricTestCases {
fromType := reflect.TypeOf(testCase[0]).Elem()
toType := reflect.TypeOf(testCase[1]).Elem()
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
for i := 0; i < 100; i++ {
beforePtrVal := reflect.New(fromType)
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))
}
afterStdPtrVal := reflect.New(toType)
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(toType)
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, " "))
}
}
}
}
const indentStr = "> "
func fingerprint(obj interface{}) string {