Add tests for integer keyed maps

This commit is contained in:
Chuntao Lu
2017-10-19 20:28:37 -07:00
parent 06d7c00bd4
commit 86f5f60c4e
5 changed files with 119 additions and 44 deletions
+9
View File
@@ -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) {
+65
View File
@@ -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"}}` +
`}`
+3
View File
@@ -21,3 +21,6 @@ type ErrorNestedStruct struct {
ErrorStruct ErrorStruct `json:"error_struct"`
Int int `json:"int"`
}
//easyjson:json
type ErrorIntMap map[uint32]string
+42
View File
@@ -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)
}
}
}
}
-44
View File
@@ -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)
}
}
}