You've already forked json-iterator
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:
44
type_tests/map_key_test.go
Normal file
44
type_tests/map_key_test.go
Normal 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{}
|
24
type_tests/struct_field_case_test.go
Normal file
24
type_tests/struct_field_case_test.go
Normal 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),
|
||||
},
|
||||
}...)
|
||||
}
|
@ -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)
|
||||
|
69
type_tests/text_marshaler_struct_test.go
Normal file
69
type_tests/text_marshaler_struct_test.go
Normal 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
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user