From 86f5f60c4e8857efcb0fc4451e68218d4f862ec8 Mon Sep 17 00:00:00 2001 From: Chuntao Lu Date: Thu, 19 Oct 2017 19:07:55 -0700 Subject: [PATCH] Add tests for integer keyed maps --- tests/basic_test.go | 9 +++++ tests/data.go | 65 ++++++++++++++++++++++++++++++ tests/errors.go | 3 ++ tests/errors_test.go | 42 +++++++++++++++++++ tests/non_string_keyed_map_test.go | 44 -------------------- 5 files changed, 119 insertions(+), 44 deletions(-) delete mode 100644 tests/non_string_keyed_map_test.go diff --git a/tests/basic_test.go b/tests/basic_test.go index b727c9e..0186784 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -38,6 +38,15 @@ var testCases = []struct { {&IntsValue, IntsString}, {&mapStringStringValue, mapStringStringString}, {&namedTypeValue, namedTypeValueString}, + {&mapMyIntStringValue, mapMyIntStringValueString}, + {&mapIntStringValue, mapIntStringValueString}, + {&mapInt32StringValue, mapInt32StringValueString}, + {&mapInt64StringValue, mapInt64StringValueString}, + {&mapUintStringValue, mapUintStringValueString}, + {&mapUint32StringValue, mapUint32StringValueString}, + {&mapUint64StringValue, mapUint64StringValueString}, + {&mapUintptrStringValue, mapUintptrStringValueString}, + {&intKeyedMapStructValue, intKeyedMapStructValueString}, } func TestMarshal(t *testing.T) { diff --git a/tests/data.go b/tests/data.go index ca8676e..234e578 100644 --- a/tests/data.go +++ b/tests/data.go @@ -692,3 +692,68 @@ type EmbeddedStruct struct { var structWithInterfaceString = `{"f1":1,"f2":{"f1":11,"f2":"22"},"f3":"3"}` var structWithInterfaceValueFilled = StructWithInterface{1, &EmbeddedStruct{11, "22"}, "3"} + +//easyjson:json +type MapIntString map[int]string + +var mapIntStringValue = MapIntString{3: "hi"} +var mapIntStringValueString = `{"3":"hi"}` + +//easyjson:json +type MapInt32String map[int32]string + +var mapInt32StringValue = MapInt32String{-354634382: "life"} +var mapInt32StringValueString = `{"-354634382":"life"}` + +//easyjson:json +type MapInt64String map[int64]string + +var mapInt64StringValue = MapInt64String{-3546343826724305832: "life"} +var mapInt64StringValueString = `{"-3546343826724305832":"life"}` + +//easyjson:json +type MapUintString map[uint]string + +var mapUintStringValue = MapUintString{42: "life"} +var mapUintStringValueString = `{"42":"life"}` + +//easyjson:json +type MapUint32String map[uint32]string + +var mapUint32StringValue = MapUint32String{354634382: "life"} +var mapUint32StringValueString = `{"354634382":"life"}` + +//easyjson:json +type MapUint64String map[uint64]string + +var mapUint64StringValue = MapUint64String{3546343826724305832: "life"} +var mapUint64StringValueString = `{"3546343826724305832":"life"}` + +//easyjson:json +type MapUintptrString map[uintptr]string + +var mapUintptrStringValue = MapUintptrString{272679208: "obj"} +var mapUintptrStringValueString = `{"272679208":"obj"}` + +type MyInt int + +//easyjson:json +type MapMyIntString map[MyInt]string + +var mapMyIntStringValue = MapMyIntString{MyInt(42): "life"} +var mapMyIntStringValueString = `{"42":"life"}` + +//easyjson:json +type IntKeyedMapStruct struct { + Foo MapMyIntString `json:"foo"` + Bar map[int16]MapUint32String `json:"bar"` +} + +var intKeyedMapStructValue = IntKeyedMapStruct{ + Foo: mapMyIntStringValue, + Bar: map[int16]MapUint32String{32: mapUint32StringValue}, +} +var intKeyedMapStructValueString = `{` + + `"foo":{"42":"life"},` + + `"bar":{"32":{"354634382":"life"}}` + + `}` diff --git a/tests/errors.go b/tests/errors.go index 2ec3299..14360fc 100644 --- a/tests/errors.go +++ b/tests/errors.go @@ -21,3 +21,6 @@ type ErrorNestedStruct struct { ErrorStruct ErrorStruct `json:"error_struct"` Int int `json:"int"` } + +//easyjson:json +type ErrorIntMap map[uint32]string diff --git a/tests/errors_test.go b/tests/errors_test.go index 756f7db..40fa335 100644 --- a/tests/errors_test.go +++ b/tests/errors_test.go @@ -241,3 +241,45 @@ func TestMultipleErrorsNestedStruct(t *testing.T) { } } } + +func TestMultipleErrorsIntMap(t *testing.T) { + for i, test := range []struct { + Data []byte + Offsets []int + }{ + { + Data: []byte(`{"a":"NumErr"}`), + Offsets: []int{1}, + }, + { + Data: []byte(`{"":"ErrSyntax"}`), + Offsets: []int{1}, + }, + { + Data: []byte(`{"a":"NumErr","33147483647":"ErrRange","-1":"ErrRange"}`), + Offsets: []int{1, 14, 39}, + }, + } { + l := jlexer.Lexer{ + Data: test.Data, + UseMultipleErrors: true, + } + + var v ErrorIntMap + + v.UnmarshalEasyJSON(&l) + + errors := l.GetNonFatalErrors() + + if len(errors) != len(test.Offsets) { + t.Errorf("[%d] TestMultipleErrorsInt(): errornum: want: %d, got %d", i, len(test.Offsets), len(errors)) + return + } + + for ii, e := range errors { + if e.Offset != test.Offsets[ii] { + t.Errorf("[%d] TestMultipleErrorsInt(): offset[%d]: want %d, got %d", i, ii, test.Offsets[ii], e.Offset) + } + } + } +} diff --git a/tests/non_string_keyed_map_test.go b/tests/non_string_keyed_map_test.go deleted file mode 100644 index 392b8f3..0000000 --- a/tests/non_string_keyed_map_test.go +++ /dev/null @@ -1,44 +0,0 @@ -package tests - -import ( - "os" - "testing" - - "github.com/mailru/easyjson/gen" -) - -type IntMap map[int]string -type IntMapSlice []IntMap -type IntMapArray [2]IntMap -type IntMapPtr *IntMap -type IntMapMap map[string]IntMap - -func TestNonStringKeyedtMapEncoder(t *testing.T) { - f := "non_string_keyed_map_easyjson.go" - for _, test := range []struct { - Data interface{} - }{ - { - Data: IntMap{}, - }, - { - Data: IntMapSlice{}, - }, - { - Data: IntMapArray{}, - }, - { - Data: IntMapPtr(nil), - }, - { - Data: IntMapMap{}, - }, - } { - g := gen.NewGenerator(f) - g.Add(test.Data) - e := g.Run(os.Stdout) - if e == nil { - t.Errorf("generation for %#v should have errored", test.Data) - } - } -}