| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"github.com/davecgh/go-spew/spew" |
| 8 |
"github.com/google/gofuzz" |
| 9 |
"github.com/json-iterator/go" |
| 10 |
"reflect" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
) |
| 14 |
|
| 15 |
var testCases []interface{} |
| 16 |
var asymmetricTestCases [][2]interface{} |
| 17 |
|
| 18 |
type selectedSymmetricCase struct { |
| 19 |
testCase interface{} |
| 20 |
} |
| 21 |
|
| 22 |
func Test_symmetric(t *testing.T) { |
| 23 |
for _, testCase := range testCases { |
| 24 |
selectedSymmetricCase, found := testCase.(selectedSymmetricCase) |
| 25 |
if found { |
| 26 |
testCases = []interface{}{selectedSymmetricCase.testCase} |
| 27 |
break |
| 28 |
} |
| 29 |
} |
| 30 |
for _, testCase := range testCases { |
| 31 |
valType := reflect.TypeOf(testCase).Elem() |
| 32 |
t.Run(valType.String(), func(t *testing.T) { |
| 33 |
fz := fuzz.New().MaxDepth(10).NilChance(0.3) |
| 34 |
for i := 0; i < 100; i++ { |
| 35 |
beforePtrVal := reflect.New(valType) |
| 36 |
beforePtr := beforePtrVal.Interface() |
| 37 |
fz.Fuzz(beforePtr) |
| 38 |
before := beforePtrVal.Elem().Interface() |
| 39 |
|
| 40 |
jbStd, err := json.Marshal(before) |
| 41 |
if err != nil { |
| 42 |
t.Fatalf("failed to marshal with stdlib: %v", err) |
| 43 |
} |
| 44 |
if len(strings.TrimSpace(string(jbStd))) == 0 { |
| 45 |
t.Fatal("stdlib marshal produced empty result and no error") |
| 46 |
} |
| 47 |
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) |
| 48 |
if err != nil { |
| 49 |
t.Fatalf("failed to marshal with jsoniter: %v", err) |
| 50 |
} |
| 51 |
if len(strings.TrimSpace(string(jbIter))) == 0 { |
| 52 |
t.Fatal("jsoniter marshal produced empty result and no error") |
| 53 |
} |
| 54 |
if string(jbStd) != string(jbIter) { |
| 55 |
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", |
| 56 |
indent(jbStd, " "), indent(jbIter, " "), dump(before)) |
| 57 |
} |
| 58 |
|
| 59 |
afterStdPtrVal := reflect.New(valType) |
| 60 |
afterStdPtr := afterStdPtrVal.Interface() |
| 61 |
err = json.Unmarshal(jbIter, afterStdPtr) |
| 62 |
if err != nil { |
| 63 |
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", |
| 64 |
err, indent(jbIter, " ")) |
| 65 |
} |
| 66 |
afterIterPtrVal := reflect.New(valType) |
| 67 |
afterIterPtr := afterIterPtrVal.Interface() |
| 68 |
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, afterIterPtr) |
| 69 |
if err != nil { |
| 70 |
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", |
| 71 |
err, indent(jbIter, " ")) |
| 72 |
} |
| 73 |
afterStd := afterStdPtrVal.Elem().Interface() |
| 74 |
afterIter := afterIterPtrVal.Elem().Interface() |
| 75 |
if fingerprint(afterStd) != fingerprint(afterIter) { |
| 76 |
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", |
| 77 |
dump(afterStd), dump(afterIter), indent(jbIter, " ")) |
| 78 |
} |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func Test_asymmetric(t *testing.T) { |
| 85 |
for _, testCase := range asymmetricTestCases { |
| 86 |
fromType := reflect.TypeOf(testCase[0]).Elem() |
| 87 |
toType := reflect.TypeOf(testCase[1]).Elem() |
| 88 |
fz := fuzz.New().MaxDepth(10).NilChance(0.3) |
| 89 |
for i := 0; i < 100; i++ { |
| 90 |
beforePtrVal := reflect.New(fromType) |
| 91 |
beforePtr := beforePtrVal.Interface() |
| 92 |
fz.Fuzz(beforePtr) |
| 93 |
before := beforePtrVal.Elem().Interface() |
| 94 |
|
| 95 |
jbStd, err := json.Marshal(before) |
| 96 |
if err != nil { |
| 97 |
t.Fatalf("failed to marshal with stdlib: %v", err) |
| 98 |
} |
| 99 |
if len(strings.TrimSpace(string(jbStd))) == 0 { |
| 100 |
t.Fatal("stdlib marshal produced empty result and no error") |
| 101 |
} |
| 102 |
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before) |
| 103 |
if err != nil { |
| 104 |
t.Fatalf("failed to marshal with jsoniter: %v", err) |
| 105 |
} |
| 106 |
if len(strings.TrimSpace(string(jbIter))) == 0 { |
| 107 |
t.Fatal("jsoniter marshal produced empty result and no error") |
| 108 |
} |
| 109 |
if string(jbStd) != string(jbIter) { |
| 110 |
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", |
| 111 |
indent(jbStd, " "), indent(jbIter, " "), dump(before)) |
| 112 |
} |
| 113 |
|
| 114 |
afterStdPtrVal := reflect.New(toType) |
| 115 |
afterStdPtr := afterStdPtrVal.Interface() |
| 116 |
err = json.Unmarshal(jbIter, afterStdPtr) |
| 117 |
if err != nil { |
| 118 |
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", |
| 119 |
err, indent(jbIter, " ")) |
| 120 |
} |
| 121 |
afterIterPtrVal := reflect.New(toType) |
| 122 |
afterIterPtr := afterIterPtrVal.Interface() |
| 123 |
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, afterIterPtr) |
| 124 |
if err != nil { |
| 125 |
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", |
| 126 |
err, indent(jbIter, " ")) |
| 127 |
} |
| 128 |
afterStd := afterStdPtrVal.Elem().Interface() |
| 129 |
afterIter := afterIterPtrVal.Elem().Interface() |
| 130 |
if fingerprint(afterStd) != fingerprint(afterIter) { |
| 131 |
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", |
| 132 |
dump(afterStd), dump(afterIter), indent(jbIter, " ")) |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
const indentStr = "> " |
| 139 |
|
| 140 |
func fingerprint(obj interface{}) string { |
| 141 |
c := spew.ConfigState{ |
| 142 |
SortKeys: true, |
| 143 |
SpewKeys: true, |
| 144 |
} |
| 145 |
return c.Sprintf("%v", obj) |
| 146 |
} |
| 147 |
|
| 148 |
func dump(obj interface{}) string { |
| 149 |
cfg := spew.ConfigState{ |
| 150 |
Indent: indentStr, |
| 151 |
} |
| 152 |
return cfg.Sdump(obj) |
| 153 |
} |
| 154 |
|
| 155 |
func indent(src []byte, prefix string) string { |
| 156 |
var buf bytes.Buffer |
| 157 |
err := json.Indent(&buf, src, prefix, indentStr) |
| 158 |
if err != nil { |
| 159 |
return fmt.Sprintf("!!! %v", err) |
| 160 |
} |
| 161 |
return buf.String() |
| 162 |
} |