From 7fa780bd5d1574088f1bf5e1b79ddf925b23904a Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Thu, 29 Jun 2017 21:15:46 -0700 Subject: [PATCH 1/5] Uncommit files accidentally added --- output_tests/_json_test.go | 152 ------------- output_tests/_not_done/TODO | 3 - .../_not_done/structs/everything/json_test.go | 152 ------------- .../_not_done/structs/everything/types.go | 204 ------------------ 4 files changed, 511 deletions(-) delete mode 100644 output_tests/_json_test.go delete mode 100644 output_tests/_not_done/TODO delete mode 100644 output_tests/_not_done/structs/everything/json_test.go delete mode 100644 output_tests/_not_done/structs/everything/types.go diff --git a/output_tests/_json_test.go b/output_tests/_json_test.go deleted file mode 100644 index 8c7d795..0000000 --- a/output_tests/_json_test.go +++ /dev/null @@ -1,152 +0,0 @@ -package test - -import ( - "bytes" - "encoding/json" - "fmt" - "strings" - "testing" - - "github.com/davecgh/go-spew/spew" - fuzz "github.com/google/gofuzz" - jsoniter "github.com/json-iterator/go" -) - -func Test_Roundtrip(t *testing.T) { - fz := fuzz.New().MaxDepth(10).NilChance(0.3) - for i := 0; i < 1000; i++ { - var before T - fz.Fuzz(&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)) - } - - var afterStd T - err = json.Unmarshal(jbIter, &afterStd) - if err != nil { - t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - var afterIter T - err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) - if err != nil { - t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - 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 { - c := spew.ConfigState{ - SortKeys: true, - SpewKeys: true, - } - return c.Sprintf("%v", obj) -} - -func dump(obj interface{}) string { - cfg := spew.ConfigState{ - Indent: indentStr, - } - return cfg.Sdump(obj) -} - -func indent(src []byte, prefix string) string { - var buf bytes.Buffer - err := json.Indent(&buf, src, prefix, indentStr) - if err != nil { - return fmt.Sprintf("!!! %v", err) - } - return buf.String() -} - -func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := fn(obj) - if err != nil { - t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) - } - _ = jb - } -} - -func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = fn(jb, &after) - if err != nil { - t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) - } - } -} - -func BenchmarkStandardMarshal(t *testing.B) { - benchmarkMarshal(t, "stdlib", json.Marshal) -} - -func BenchmarkStandardUnmarshal(t *testing.B) { - benchmarkUnmarshal(t, "stdlib", json.Unmarshal) -} - -func BenchmarkJSONIterMarshalFastest(t *testing.B) { - benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) -} - -func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { - benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) -} - -func BenchmarkJSONIterMarshalDefault(t *testing.B) { - benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) -} - -func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { - benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) -} - -func BenchmarkJSONIterMarshalCompatible(t *testing.B) { - benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) -} - -func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { - benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) -} diff --git a/output_tests/_not_done/TODO b/output_tests/_not_done/TODO deleted file mode 100644 index a1291e0..0000000 --- a/output_tests/_not_done/TODO +++ /dev/null @@ -1,3 +0,0 @@ -arrays -interfaces -complex diff --git a/output_tests/_not_done/structs/everything/json_test.go b/output_tests/_not_done/structs/everything/json_test.go deleted file mode 100644 index 8c7d795..0000000 --- a/output_tests/_not_done/structs/everything/json_test.go +++ /dev/null @@ -1,152 +0,0 @@ -package test - -import ( - "bytes" - "encoding/json" - "fmt" - "strings" - "testing" - - "github.com/davecgh/go-spew/spew" - fuzz "github.com/google/gofuzz" - jsoniter "github.com/json-iterator/go" -) - -func Test_Roundtrip(t *testing.T) { - fz := fuzz.New().MaxDepth(10).NilChance(0.3) - for i := 0; i < 1000; i++ { - var before T - fz.Fuzz(&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)) - } - - var afterStd T - err = json.Unmarshal(jbIter, &afterStd) - if err != nil { - t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - var afterIter T - err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) - if err != nil { - t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", - err, indent(jbIter, " ")) - } - 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 { - c := spew.ConfigState{ - SortKeys: true, - SpewKeys: true, - } - return c.Sprintf("%v", obj) -} - -func dump(obj interface{}) string { - cfg := spew.ConfigState{ - Indent: indentStr, - } - return cfg.Sdump(obj) -} - -func indent(src []byte, prefix string) string { - var buf bytes.Buffer - err := json.Indent(&buf, src, prefix, indentStr) - if err != nil { - return fmt.Sprintf("!!! %v", err) - } - return buf.String() -} - -func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) { - t.ReportAllocs() - t.ResetTimer() - - var obj T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&obj) - for i := 0; i < t.N; i++ { - jb, err := fn(obj) - if err != nil { - t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err) - } - _ = jb - } -} - -func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) { - t.ReportAllocs() - t.ResetTimer() - - var before T - fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3) - fz.Fuzz(&before) - jb, err := json.Marshal(before) - if err != nil { - t.Fatalf("failed to marshal: %v", err) - } - - for i := 0; i < t.N; i++ { - var after T - err = fn(jb, &after) - if err != nil { - t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err) - } - } -} - -func BenchmarkStandardMarshal(t *testing.B) { - benchmarkMarshal(t, "stdlib", json.Marshal) -} - -func BenchmarkStandardUnmarshal(t *testing.B) { - benchmarkUnmarshal(t, "stdlib", json.Unmarshal) -} - -func BenchmarkJSONIterMarshalFastest(t *testing.B) { - benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal) -} - -func BenchmarkJSONIterUnmarshalFastest(t *testing.B) { - benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal) -} - -func BenchmarkJSONIterMarshalDefault(t *testing.B) { - benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal) -} - -func BenchmarkJSONIterUnmarshalDefault(t *testing.B) { - benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal) -} - -func BenchmarkJSONIterMarshalCompatible(t *testing.B) { - benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal) -} - -func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) { - benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal) -} diff --git a/output_tests/_not_done/structs/everything/types.go b/output_tests/_not_done/structs/everything/types.go deleted file mode 100644 index 9e3f621..0000000 --- a/output_tests/_not_done/structs/everything/types.go +++ /dev/null @@ -1,204 +0,0 @@ -package test - -type ByteAlias byte - -type BoolAlias bool - -type Int32Alias int32 - -type Float32Alias float32 - -type StringAlias string - -type SliceStringAlias []string - -type SlicePtrStringAlias []*string - -type MapStringStringAlias map[string]string - -type Inner struct { - Byte byte - BytePtr *byte - ByteAlias ByteAlias - ByteAliasPtr *ByteAlias - Bool bool - BoolPtr *bool - BoolAlias BoolAlias - BoolAliasPtr *BoolAlias - Int8 int8 - Int8Ptr *int8 - Int16 int16 - Int16Ptr *int16 - Int32 int32 - Int32Ptr *int32 - Int32Alias Int32Alias - Int32AliasPtr *Int32Alias - Uint8 uint8 - Uint8Ptr *uint8 - Uint16 uint16 - Uint16Ptr *uint16 - Uint32 uint32 - Uint32Ptr *uint32 - Float32 float32 - Float32Ptr *float32 - Float32Alias Float32Alias - Float32AliasPtr *Float32Alias - Float64 float64 - Float64Ptr *float64 - String string - StringPtr *string - StringAlias StringAlias - StringAliasPtr *StringAlias - Struct struct { - Byte byte - BytePtr *byte - ByteAlias ByteAlias - ByteAliasPtr *ByteAlias - Bool bool - BoolPtr *bool - BoolAlias BoolAlias - BoolAliasPtr *BoolAlias - Int8 int8 - Int8Ptr *int8 - Int16 int16 - Int16Ptr *int16 - Int32 int32 - Int32Ptr *int32 - Int32Alias Int32Alias - Int32AliasPtr *Int32Alias - Uint8 uint8 - Uint8Ptr *uint8 - Uint16 uint16 - Uint16Ptr *uint16 - Uint32 uint32 - Uint32Ptr *uint32 - Float32 float32 - Float32Ptr *float32 - Float32Alias Float32Alias - Float32AliasPtr *Float32Alias - Float64 float64 - Float64Ptr *float64 - String string - StringPtr *string - StringAlias StringAlias - StringAliasPtr *StringAlias - Struct struct{} - StructPtr *Inner - SliceString []string - SliceStringAlias SliceStringAlias - SlicePtrString []*string - SliceStringPtrAlias SlicePtrStringAlias - SliceStringPtr *[]string - SliceByte []byte - } - StructPtr *struct { - Byte byte - BytePtr *byte - ByteAlias ByteAlias - ByteAliasPtr *ByteAlias - Bool bool - BoolPtr *bool - BoolAlias BoolAlias - BoolAliasPtr *BoolAlias - Int8 int8 - Int8Ptr *int8 - Int16 int16 - Int16Ptr *int16 - Int32 int32 - Int32Ptr *int32 - Int32Alias Int32Alias - Int32AliasPtr *Int32Alias - Uint8 uint8 - Uint8Ptr *uint8 - Uint16 uint16 - Uint16Ptr *uint16 - Uint32 uint32 - Uint32Ptr *uint32 - Float32 float32 - Float32Ptr *float32 - Float32Alias Float32Alias - Float32AliasPtr *Float32Alias - Float64 float64 - Float64Ptr *float64 - String string - StringPtr *string - StringAlias StringAlias - StringAliasPtr *StringAlias - Struct struct{} - StructPtr *Inner - SliceString []string - SliceStringAlias SliceStringAlias - SlicePtrString []*string - SliceStringPtrAlias SlicePtrStringAlias - SliceStringPtr *[]string - SliceByte []byte - MapStringString map[string]string - MapStringStringPtr *map[string]string - MapStringStringAlias MapStringStringAlias - MapStringStringAliasPtr *MapStringStringAlias - } - SliceString []string - SliceStringAlias SliceStringAlias - SlicePtrString []*string - SliceStringPtrAlias SlicePtrStringAlias - SliceStringPtr *[]string - SliceByte []byte - MapStringString map[string]string - MapStringStringPtr *map[string]string - MapStringStringAlias MapStringStringAlias - MapStringStringAliasPtr *MapStringStringAlias -} - -type T struct { - Byte byte - BytePtr *byte - ByteAlias ByteAlias - ByteAliasPtr *ByteAlias - Bool bool - BoolPtr *bool - BoolAlias BoolAlias - BoolAliasPtr *BoolAlias - Int8 int8 - Int8Ptr *int8 - Int16 int16 - Int16Ptr *int16 - Int32 int32 - Int32Ptr *int32 - Int32Alias Int32Alias - Int32AliasPtr *Int32Alias - Uint8 uint8 - Uint8Ptr *uint8 - Uint16 uint16 - Uint16Ptr *uint16 - Uint32 uint32 - Uint32Ptr *uint32 - Float32 float32 - Float32Ptr *float32 - Float32Alias Float32Alias - Float32AliasPtr *Float32Alias - Float64 float64 - Float64Ptr *float64 - String string - StringPtr *string - StringAlias StringAlias - StringAliasPtr *StringAlias - StructPtr *Inner - Struct struct { - Struct struct { - Struct struct { - Struct struct { - String string - } - } - } - } - SliceString []string - SliceStringAlias SliceStringAlias - SlicePtrString []*string - SliceStringPtrAlias SlicePtrStringAlias - SliceStringPtr *[]string - MapStringString map[string]string - MapStringStringPtr *map[string]string - MapStringStringAlias MapStringStringAlias - MapStringStringAliasPtr *MapStringStringAlias -} From 79c404050509100a233091e97125778fed09acce Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Thu, 29 Jun 2017 21:14:18 -0700 Subject: [PATCH 2/5] sync up some straggler json_tests --- .../omitempty/ptr_bool/json_test.go | 27 ++++++++++++++----- .../omitempty/ptr_float32/json_test.go | 27 ++++++++++++++----- .../omitempty/ptr_int32/json_test.go | 27 ++++++++++++++----- .../ptr_map_string_string/json_test.go | 27 ++++++++++++++----- .../omitempty/ptr_slice_string/json_test.go | 27 ++++++++++++++----- .../omitempty/ptr_string/string/json_test.go | 27 ++++++++++++++----- .../omitempty/ptr_uint32/json_test.go | 27 ++++++++++++++----- 7 files changed, 140 insertions(+), 49 deletions(-) diff --git a/output_tests/struct_tags/omitempty/ptr_bool/json_test.go b/output_tests/struct_tags/omitempty/ptr_bool/json_test.go index a7b1d0d..8c7d795 100644 --- a/output_tests/struct_tags/omitempty/ptr_bool/json_test.go +++ b/output_tests/struct_tags/omitempty/ptr_bool/json_test.go @@ -3,6 +3,8 @@ package test import ( "bytes" "encoding/json" + "fmt" + "strings" "testing" "github.com/davecgh/go-spew/spew" @@ -18,29 +20,37 @@ func Test_Roundtrip(t *testing.T) { jbStd, err := json.Marshal(before) if err != nil { - t.Errorf("failed to marshal with stdlib: %v", err) + 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.Errorf("failed to marshal with jsoniter: %v", err) + 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.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", + t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", indent(jbStd, " "), indent(jbIter, " "), dump(before)) } var afterStd T err = json.Unmarshal(jbIter, &afterStd) if err != nil { - t.Errorf("failed to unmarshal with stdlib: %v", err) + t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", + err, indent(jbIter, " ")) } var afterIter T err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { - t.Errorf("failed to unmarshal with jsoniter: %v", err) + t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", + err, indent(jbIter, " ")) } if fingerprint(afterStd) != fingerprint(afterIter) { - t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", + t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", dump(afterStd), dump(afterIter), indent(jbIter, " ")) } } @@ -65,7 +75,10 @@ func dump(obj interface{}) string { func indent(src []byte, prefix string) string { var buf bytes.Buffer - json.Indent(&buf, src, prefix, indentStr) + err := json.Indent(&buf, src, prefix, indentStr) + if err != nil { + return fmt.Sprintf("!!! %v", err) + } return buf.String() } diff --git a/output_tests/struct_tags/omitempty/ptr_float32/json_test.go b/output_tests/struct_tags/omitempty/ptr_float32/json_test.go index a7b1d0d..8c7d795 100644 --- a/output_tests/struct_tags/omitempty/ptr_float32/json_test.go +++ b/output_tests/struct_tags/omitempty/ptr_float32/json_test.go @@ -3,6 +3,8 @@ package test import ( "bytes" "encoding/json" + "fmt" + "strings" "testing" "github.com/davecgh/go-spew/spew" @@ -18,29 +20,37 @@ func Test_Roundtrip(t *testing.T) { jbStd, err := json.Marshal(before) if err != nil { - t.Errorf("failed to marshal with stdlib: %v", err) + 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.Errorf("failed to marshal with jsoniter: %v", err) + 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.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", + t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", indent(jbStd, " "), indent(jbIter, " "), dump(before)) } var afterStd T err = json.Unmarshal(jbIter, &afterStd) if err != nil { - t.Errorf("failed to unmarshal with stdlib: %v", err) + t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", + err, indent(jbIter, " ")) } var afterIter T err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { - t.Errorf("failed to unmarshal with jsoniter: %v", err) + t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", + err, indent(jbIter, " ")) } if fingerprint(afterStd) != fingerprint(afterIter) { - t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", + t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", dump(afterStd), dump(afterIter), indent(jbIter, " ")) } } @@ -65,7 +75,10 @@ func dump(obj interface{}) string { func indent(src []byte, prefix string) string { var buf bytes.Buffer - json.Indent(&buf, src, prefix, indentStr) + err := json.Indent(&buf, src, prefix, indentStr) + if err != nil { + return fmt.Sprintf("!!! %v", err) + } return buf.String() } diff --git a/output_tests/struct_tags/omitempty/ptr_int32/json_test.go b/output_tests/struct_tags/omitempty/ptr_int32/json_test.go index a7b1d0d..8c7d795 100644 --- a/output_tests/struct_tags/omitempty/ptr_int32/json_test.go +++ b/output_tests/struct_tags/omitempty/ptr_int32/json_test.go @@ -3,6 +3,8 @@ package test import ( "bytes" "encoding/json" + "fmt" + "strings" "testing" "github.com/davecgh/go-spew/spew" @@ -18,29 +20,37 @@ func Test_Roundtrip(t *testing.T) { jbStd, err := json.Marshal(before) if err != nil { - t.Errorf("failed to marshal with stdlib: %v", err) + 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.Errorf("failed to marshal with jsoniter: %v", err) + 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.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", + t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", indent(jbStd, " "), indent(jbIter, " "), dump(before)) } var afterStd T err = json.Unmarshal(jbIter, &afterStd) if err != nil { - t.Errorf("failed to unmarshal with stdlib: %v", err) + t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", + err, indent(jbIter, " ")) } var afterIter T err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { - t.Errorf("failed to unmarshal with jsoniter: %v", err) + t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", + err, indent(jbIter, " ")) } if fingerprint(afterStd) != fingerprint(afterIter) { - t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", + t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", dump(afterStd), dump(afterIter), indent(jbIter, " ")) } } @@ -65,7 +75,10 @@ func dump(obj interface{}) string { func indent(src []byte, prefix string) string { var buf bytes.Buffer - json.Indent(&buf, src, prefix, indentStr) + err := json.Indent(&buf, src, prefix, indentStr) + if err != nil { + return fmt.Sprintf("!!! %v", err) + } return buf.String() } diff --git a/output_tests/struct_tags/omitempty/ptr_map_string_string/json_test.go b/output_tests/struct_tags/omitempty/ptr_map_string_string/json_test.go index a7b1d0d..8c7d795 100644 --- a/output_tests/struct_tags/omitempty/ptr_map_string_string/json_test.go +++ b/output_tests/struct_tags/omitempty/ptr_map_string_string/json_test.go @@ -3,6 +3,8 @@ package test import ( "bytes" "encoding/json" + "fmt" + "strings" "testing" "github.com/davecgh/go-spew/spew" @@ -18,29 +20,37 @@ func Test_Roundtrip(t *testing.T) { jbStd, err := json.Marshal(before) if err != nil { - t.Errorf("failed to marshal with stdlib: %v", err) + 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.Errorf("failed to marshal with jsoniter: %v", err) + 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.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", + t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", indent(jbStd, " "), indent(jbIter, " "), dump(before)) } var afterStd T err = json.Unmarshal(jbIter, &afterStd) if err != nil { - t.Errorf("failed to unmarshal with stdlib: %v", err) + t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", + err, indent(jbIter, " ")) } var afterIter T err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { - t.Errorf("failed to unmarshal with jsoniter: %v", err) + t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", + err, indent(jbIter, " ")) } if fingerprint(afterStd) != fingerprint(afterIter) { - t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", + t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", dump(afterStd), dump(afterIter), indent(jbIter, " ")) } } @@ -65,7 +75,10 @@ func dump(obj interface{}) string { func indent(src []byte, prefix string) string { var buf bytes.Buffer - json.Indent(&buf, src, prefix, indentStr) + err := json.Indent(&buf, src, prefix, indentStr) + if err != nil { + return fmt.Sprintf("!!! %v", err) + } return buf.String() } diff --git a/output_tests/struct_tags/omitempty/ptr_slice_string/json_test.go b/output_tests/struct_tags/omitempty/ptr_slice_string/json_test.go index a7b1d0d..8c7d795 100644 --- a/output_tests/struct_tags/omitempty/ptr_slice_string/json_test.go +++ b/output_tests/struct_tags/omitempty/ptr_slice_string/json_test.go @@ -3,6 +3,8 @@ package test import ( "bytes" "encoding/json" + "fmt" + "strings" "testing" "github.com/davecgh/go-spew/spew" @@ -18,29 +20,37 @@ func Test_Roundtrip(t *testing.T) { jbStd, err := json.Marshal(before) if err != nil { - t.Errorf("failed to marshal with stdlib: %v", err) + 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.Errorf("failed to marshal with jsoniter: %v", err) + 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.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", + t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", indent(jbStd, " "), indent(jbIter, " "), dump(before)) } var afterStd T err = json.Unmarshal(jbIter, &afterStd) if err != nil { - t.Errorf("failed to unmarshal with stdlib: %v", err) + t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", + err, indent(jbIter, " ")) } var afterIter T err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { - t.Errorf("failed to unmarshal with jsoniter: %v", err) + t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", + err, indent(jbIter, " ")) } if fingerprint(afterStd) != fingerprint(afterIter) { - t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", + t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", dump(afterStd), dump(afterIter), indent(jbIter, " ")) } } @@ -65,7 +75,10 @@ func dump(obj interface{}) string { func indent(src []byte, prefix string) string { var buf bytes.Buffer - json.Indent(&buf, src, prefix, indentStr) + err := json.Indent(&buf, src, prefix, indentStr) + if err != nil { + return fmt.Sprintf("!!! %v", err) + } return buf.String() } diff --git a/output_tests/struct_tags/omitempty/ptr_string/string/json_test.go b/output_tests/struct_tags/omitempty/ptr_string/string/json_test.go index a7b1d0d..8c7d795 100644 --- a/output_tests/struct_tags/omitempty/ptr_string/string/json_test.go +++ b/output_tests/struct_tags/omitempty/ptr_string/string/json_test.go @@ -3,6 +3,8 @@ package test import ( "bytes" "encoding/json" + "fmt" + "strings" "testing" "github.com/davecgh/go-spew/spew" @@ -18,29 +20,37 @@ func Test_Roundtrip(t *testing.T) { jbStd, err := json.Marshal(before) if err != nil { - t.Errorf("failed to marshal with stdlib: %v", err) + 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.Errorf("failed to marshal with jsoniter: %v", err) + 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.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", + t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", indent(jbStd, " "), indent(jbIter, " "), dump(before)) } var afterStd T err = json.Unmarshal(jbIter, &afterStd) if err != nil { - t.Errorf("failed to unmarshal with stdlib: %v", err) + t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", + err, indent(jbIter, " ")) } var afterIter T err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { - t.Errorf("failed to unmarshal with jsoniter: %v", err) + t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", + err, indent(jbIter, " ")) } if fingerprint(afterStd) != fingerprint(afterIter) { - t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", + t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", dump(afterStd), dump(afterIter), indent(jbIter, " ")) } } @@ -65,7 +75,10 @@ func dump(obj interface{}) string { func indent(src []byte, prefix string) string { var buf bytes.Buffer - json.Indent(&buf, src, prefix, indentStr) + err := json.Indent(&buf, src, prefix, indentStr) + if err != nil { + return fmt.Sprintf("!!! %v", err) + } return buf.String() } diff --git a/output_tests/struct_tags/omitempty/ptr_uint32/json_test.go b/output_tests/struct_tags/omitempty/ptr_uint32/json_test.go index a7b1d0d..8c7d795 100644 --- a/output_tests/struct_tags/omitempty/ptr_uint32/json_test.go +++ b/output_tests/struct_tags/omitempty/ptr_uint32/json_test.go @@ -3,6 +3,8 @@ package test import ( "bytes" "encoding/json" + "fmt" + "strings" "testing" "github.com/davecgh/go-spew/spew" @@ -18,29 +20,37 @@ func Test_Roundtrip(t *testing.T) { jbStd, err := json.Marshal(before) if err != nil { - t.Errorf("failed to marshal with stdlib: %v", err) + 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.Errorf("failed to marshal with jsoniter: %v", err) + 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.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", + t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s", indent(jbStd, " "), indent(jbIter, " "), dump(before)) } var afterStd T err = json.Unmarshal(jbIter, &afterStd) if err != nil { - t.Errorf("failed to unmarshal with stdlib: %v", err) + t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", + err, indent(jbIter, " ")) } var afterIter T err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) if err != nil { - t.Errorf("failed to unmarshal with jsoniter: %v", err) + t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", + err, indent(jbIter, " ")) } if fingerprint(afterStd) != fingerprint(afterIter) { - t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", + t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s", dump(afterStd), dump(afterIter), indent(jbIter, " ")) } } @@ -65,7 +75,10 @@ func dump(obj interface{}) string { func indent(src []byte, prefix string) string { var buf bytes.Buffer - json.Indent(&buf, src, prefix, indentStr) + err := json.Indent(&buf, src, prefix, indentStr) + if err != nil { + return fmt.Sprintf("!!! %v", err) + } return buf.String() } From b07d1abc4f681f0a4a632ec7e59a85f122cb2e5c Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Thu, 29 Jun 2017 20:46:38 -0700 Subject: [PATCH 3/5] Add output_test for partial unmarshal --- .../partial_unmarshal/partial_test.go | 84 +++++++++++++++++++ output_tests/partial_unmarshal/types.go | 11 +++ 2 files changed, 95 insertions(+) create mode 100644 output_tests/partial_unmarshal/partial_test.go create mode 100644 output_tests/partial_unmarshal/types.go diff --git a/output_tests/partial_unmarshal/partial_test.go b/output_tests/partial_unmarshal/partial_test.go new file mode 100644 index 0000000..bcff3d0 --- /dev/null +++ b/output_tests/partial_unmarshal/partial_test.go @@ -0,0 +1,84 @@ +// NOTE: This test is different than most of the other JSON roundtrip tests. +package test + +import ( + "bytes" + "encoding/json" + "fmt" + "strings" + "testing" + + "github.com/davecgh/go-spew/spew" + fuzz "github.com/google/gofuzz" + jsoniter "github.com/json-iterator/go" +) + +func Test_PartialUnmarshal(t *testing.T) { + fz := fuzz.New().MaxDepth(10).NilChance(0.3) + for i := 0; i < 1000; i++ { + var before T1 + fz.Fuzz(&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)) + } + + var afterStd T2 + err = json.Unmarshal(jbIter, &afterStd) + if err != nil { + t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s", + err, indent(jbIter, " ")) + } + var afterIter T2 + err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter) + if err != nil { + t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s", + err, indent(jbIter, " ")) + } + 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 { + c := spew.ConfigState{ + SortKeys: true, + SpewKeys: true, + } + return c.Sprintf("%v", obj) +} + +func dump(obj interface{}) string { + cfg := spew.ConfigState{ + Indent: indentStr, + } + return cfg.Sdump(obj) +} + +func indent(src []byte, prefix string) string { + var buf bytes.Buffer + err := json.Indent(&buf, src, prefix, indentStr) + if err != nil { + return fmt.Sprintf("!!! %v", err) + } + return buf.String() +} diff --git a/output_tests/partial_unmarshal/types.go b/output_tests/partial_unmarshal/types.go new file mode 100644 index 0000000..7d2496e --- /dev/null +++ b/output_tests/partial_unmarshal/types.go @@ -0,0 +1,11 @@ +package test + +type T1 struct { + F1 string + F2 string + F3 string +} + +type T2 struct { + F1 string +} From 9ec64591b62f94568713fb905167c484340da38f Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Thu, 29 Jun 2017 21:45:38 -0700 Subject: [PATCH 4/5] Enhance test for overlap and embedded --- .../anonymous/overlap/different_levels/types.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/output_tests/struct/anonymous/overlap/different_levels/types.go b/output_tests/struct/anonymous/overlap/different_levels/types.go index 400441d..1d55d0a 100644 --- a/output_tests/struct/anonymous/overlap/different_levels/types.go +++ b/output_tests/struct/anonymous/overlap/different_levels/types.go @@ -1,10 +1,15 @@ package test -type Embedded struct { +type E1 struct { F1 int32 } -type T struct { - F1 string - Embedded +type E2 struct { + F2 string +} + +type T struct { + E1 + E2 + F1 string } From c1411e0ad5199695c808c647e066bfc1c7650a7f Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Thu, 29 Jun 2017 21:28:24 -0700 Subject: [PATCH 5/5] Add test for json tag on embedded field --- output_tests/struct_tags/fieldname/types.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/output_tests/struct_tags/fieldname/types.go b/output_tests/struct_tags/fieldname/types.go index 3140950..2d09aee 100644 --- a/output_tests/struct_tags/fieldname/types.go +++ b/output_tests/struct_tags/fieldname/types.go @@ -1,5 +1,9 @@ package test +type E struct { + E1 string +} + type T struct { F1 string `json:"F1"` F2 string `json:"f2"` @@ -7,4 +11,5 @@ type T struct { F4 string `json:"-,"` F5 string `json:","` F6 string `json:""` + E `json:"e"` }