From 761ce8cce25aa226d309bf36b58a412d6319794c Mon Sep 17 00:00:00 2001 From: Tao Wen Date: Tue, 13 Feb 2018 20:58:29 +0800 Subject: [PATCH] consolidate more tests --- .../jsoniter_any_array_test.go | 41 ++++----- .../jsoniter_any_bool_test.go | 19 +++-- .../jsoniter_any_float_test.go | 13 +-- .../jsoniter_any_int_test.go | 45 +++++----- .../jsoniter_any_map_test.go | 7 +- .../jsoniter_any_null_test.go | 5 +- .../jsoniter_any_object_test.go | 31 +++---- .../jsoniter_any_string_test.go | 15 ++-- api_tests/config_test.go | 16 ++++ api_tests/decoder_test.go | 42 ++++++++++ .../encoder_18_test.go | 9 +- api_tests/encoder_test.go | 20 +++++ api_tests/marshal_indent_test.go | 36 ++++++++ compatible_test.go | 40 --------- jsoniter_adapter_test.go | 84 ------------------- jsoniter_alias_test.go | 62 -------------- type_tests/builtin_test.go | 22 ++++- type_tests/struct_tags_test.go | 6 +- 18 files changed, 237 insertions(+), 276 deletions(-) rename jsoniter_any_array_test.go => any_tests/jsoniter_any_array_test.go (74%) rename jsoniter_any_bool_test.go => any_tests/jsoniter_any_bool_test.go (70%) rename jsoniter_any_float_test.go => any_tests/jsoniter_any_float_test.go (88%) rename jsoniter_any_int_test.go => any_tests/jsoniter_any_int_test.go (81%) rename jsoniter_any_map_test.go => any_tests/jsoniter_any_map_test.go (55%) rename jsoniter_any_null_test.go => any_tests/jsoniter_any_null_test.go (75%) rename jsoniter_any_object_test.go => any_tests/jsoniter_any_object_test.go (75%) rename jsoniter_any_string_test.go => any_tests/jsoniter_any_string_test.go (79%) create mode 100644 api_tests/config_test.go rename jsoniter_1dot8_only_test.go => api_tests/encoder_18_test.go (86%) create mode 100644 api_tests/encoder_test.go create mode 100644 api_tests/marshal_indent_test.go delete mode 100644 compatible_test.go delete mode 100644 jsoniter_adapter_test.go delete mode 100644 jsoniter_alias_test.go diff --git a/jsoniter_any_array_test.go b/any_tests/jsoniter_any_array_test.go similarity index 74% rename from jsoniter_any_array_test.go rename to any_tests/jsoniter_any_array_test.go index dbffd82..9159f1b 100644 --- a/jsoniter_any_array_test.go +++ b/any_tests/jsoniter_any_array_test.go @@ -1,18 +1,19 @@ -package jsoniter +package any_tests import ( "testing" "github.com/stretchr/testify/require" + "github.com/json-iterator/go" ) func Test_read_empty_array_as_any(t *testing.T) { should := require.New(t) - any := Get([]byte("[]")) - should.Equal(ArrayValue, any.Get().ValueType()) - should.Equal(InvalidValue, any.Get(0.3).ValueType()) + any := jsoniter.Get([]byte("[]")) + should.Equal(jsoniter.ArrayValue, any.Get().ValueType()) + should.Equal(jsoniter.InvalidValue, any.Get(0.3).ValueType()) should.Equal(0, any.Size()) - should.Equal(ArrayValue, any.ValueType()) + should.Equal(jsoniter.ArrayValue, any.ValueType()) should.Nil(any.LastError()) should.Equal(0, any.ToInt()) should.Equal(int32(0), any.ToInt32()) @@ -26,19 +27,19 @@ func Test_read_empty_array_as_any(t *testing.T) { func Test_read_one_element_array_as_any(t *testing.T) { should := require.New(t) - any := Get([]byte("[1]")) + any := jsoniter.Get([]byte("[1]")) should.Equal(1, any.Size()) } func Test_read_two_element_array_as_any(t *testing.T) { should := require.New(t) - any := Get([]byte("[1,2]")) + any := jsoniter.Get([]byte("[1,2]")) should.Equal(1, any.Get(0).ToInt()) should.Equal(2, any.Size()) should.True(any.ToBool()) should.Equal(1, any.ToInt()) should.Equal([]interface{}{float64(1), float64(2)}, any.GetInterface()) - stream := NewStream(ConfigDefault, nil, 32) + stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32) any.WriteTo(stream) should.Equal("[1,2]", string(stream.Buffer())) arr := []int{} @@ -48,8 +49,8 @@ func Test_read_two_element_array_as_any(t *testing.T) { func Test_wrap_array_and_convert_to_any(t *testing.T) { should := require.New(t) - any := Wrap([]int{1, 2, 3}) - any2 := Wrap([]int{}) + any := jsoniter.Wrap([]int{1, 2, 3}) + any2 := jsoniter.Wrap([]int{}) should.Equal("[1,2,3]", any.ToString()) should.True(any.ToBool()) @@ -80,43 +81,43 @@ func Test_wrap_array_and_convert_to_any(t *testing.T) { func Test_array_lazy_any_get(t *testing.T) { should := require.New(t) - any := Get([]byte("[1,[2,3],4]")) + any := jsoniter.Get([]byte("[1,[2,3],4]")) should.Equal(3, any.Get(1, 1).ToInt()) should.Equal("[1,[2,3],4]", any.ToString()) } func Test_array_lazy_any_get_all(t *testing.T) { should := require.New(t) - any := Get([]byte("[[1],[2],[3,4]]")) + any := jsoniter.Get([]byte("[[1],[2],[3,4]]")) should.Equal("[1,2,3]", any.Get('*', 0).ToString()) - any = Get([]byte("[[[1],[2],[3,4]]]"), 0, '*', 0) + any = jsoniter.Get([]byte("[[[1],[2],[3,4]]]"), 0, '*', 0) should.Equal("[1,2,3]", any.ToString()) } func Test_array_wrapper_any_get_all(t *testing.T) { should := require.New(t) - any := wrapArray([][]int{ + any := jsoniter.Wrap([][]int{ {1, 2}, {3, 4}, {5, 6}, }) should.Equal("[1,3,5]", any.Get('*', 0).ToString()) - should.Equal(ArrayValue, any.ValueType()) + should.Equal(jsoniter.ArrayValue, any.ValueType()) should.True(any.ToBool()) should.Equal(1, any.Get(0, 0).ToInt()) } func Test_array_lazy_any_get_invalid(t *testing.T) { should := require.New(t) - any := Get([]byte("[]")) - should.Equal(InvalidValue, any.Get(1, 1).ValueType()) + any := jsoniter.Get([]byte("[]")) + should.Equal(jsoniter.InvalidValue, any.Get(1, 1).ValueType()) should.NotNil(any.Get(1, 1).LastError()) - should.Equal(InvalidValue, any.Get("1").ValueType()) + should.Equal(jsoniter.InvalidValue, any.Get("1").ValueType()) should.NotNil(any.Get("1").LastError()) } func Test_invalid_array(t *testing.T) { should := require.New(t) - any := Get([]byte("["), 0) - should.Equal(InvalidValue, any.ValueType()) + any := jsoniter.Get([]byte("["), 0) + should.Equal(jsoniter.InvalidValue, any.ValueType()) } diff --git a/jsoniter_any_bool_test.go b/any_tests/jsoniter_any_bool_test.go similarity index 70% rename from jsoniter_any_bool_test.go rename to any_tests/jsoniter_any_bool_test.go index a71743a..65cd26c 100644 --- a/jsoniter_any_bool_test.go +++ b/any_tests/jsoniter_any_bool_test.go @@ -1,10 +1,11 @@ -package jsoniter +package any_tests import ( "fmt" "testing" "github.com/stretchr/testify/require" + "github.com/json-iterator/go" ) var boolConvertMap = map[string]bool{ @@ -35,9 +36,9 @@ var boolConvertMap = map[string]bool{ func Test_read_bool_as_any(t *testing.T) { should := require.New(t) - var any Any + var any jsoniter.Any for k, v := range boolConvertMap { - any = Get([]byte(k)) + any = jsoniter.Get([]byte(k)) if v { should.True(any.ToBool(), fmt.Sprintf("origin val is %v", k)) } else { @@ -49,16 +50,16 @@ func Test_read_bool_as_any(t *testing.T) { func Test_write_bool_to_stream(t *testing.T) { should := require.New(t) - any := Get([]byte("true")) - stream := NewStream(ConfigDefault, nil, 32) + any := jsoniter.Get([]byte("true")) + stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32) any.WriteTo(stream) should.Equal("true", string(stream.Buffer())) - should.Equal(any.ValueType(), BoolValue) + should.Equal(any.ValueType(), jsoniter.BoolValue) - any = Get([]byte("false")) - stream = NewStream(ConfigDefault, nil, 32) + any = jsoniter.Get([]byte("false")) + stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32) any.WriteTo(stream) should.Equal("false", string(stream.Buffer())) - should.Equal(any.ValueType(), BoolValue) + should.Equal(any.ValueType(), jsoniter.BoolValue) } diff --git a/jsoniter_any_float_test.go b/any_tests/jsoniter_any_float_test.go similarity index 88% rename from jsoniter_any_float_test.go rename to any_tests/jsoniter_any_float_test.go index 391ac32..4247b44 100644 --- a/jsoniter_any_float_test.go +++ b/any_tests/jsoniter_any_float_test.go @@ -1,9 +1,10 @@ -package jsoniter +package any_tests import ( "testing" "github.com/stretchr/testify/require" + "github.com/json-iterator/go" ) var floatConvertMap = map[string]float64{ @@ -67,22 +68,22 @@ var floatConvertMap = map[string]float64{ func Test_read_any_to_float(t *testing.T) { should := require.New(t) for k, v := range floatConvertMap { - any := Get([]byte(k)) + any := jsoniter.Get([]byte(k)) should.Equal(float64(v), any.ToFloat64(), "the original val is "+k) } for k, v := range floatConvertMap { - any := Get([]byte(k)) + any := jsoniter.Get([]byte(k)) should.Equal(float32(v), any.ToFloat32(), "the original val is "+k) } } func Test_read_float_to_any(t *testing.T) { should := require.New(t) - any := WrapFloat64(12.3) + any := jsoniter.WrapFloat64(12.3) anyFloat64 := float64(12.3) //negaAnyFloat64 := float64(-1.1) - any2 := WrapFloat64(-1.1) + any2 := jsoniter.WrapFloat64(-1.1) should.Equal(float64(12.3), any.ToFloat64()) //should.Equal("12.3", any.ToString()) should.True(any.ToBool()) @@ -96,7 +97,7 @@ func Test_read_float_to_any(t *testing.T) { should.Equal(uint(0), any2.ToUint()) should.Equal(uint32(0), any2.ToUint32()) should.Equal(uint64(0), any2.ToUint64()) - should.Equal(any.ValueType(), NumberValue) + should.Equal(any.ValueType(), jsoniter.NumberValue) should.Equal("1.23E+01", any.ToString()) } diff --git a/jsoniter_any_int_test.go b/any_tests/jsoniter_any_int_test.go similarity index 81% rename from jsoniter_any_int_test.go rename to any_tests/jsoniter_any_int_test.go index 2328085..f83a8ad 100644 --- a/jsoniter_any_int_test.go +++ b/any_tests/jsoniter_any_int_test.go @@ -1,10 +1,11 @@ -package jsoniter +package any_tests import ( "fmt" "testing" "github.com/stretchr/testify/require" + "github.com/json-iterator/go" ) var intConvertMap = map[string]int{ @@ -41,19 +42,19 @@ func Test_read_any_to_int(t *testing.T) { // int for k, v := range intConvertMap { - any := Get([]byte(k)) + any := jsoniter.Get([]byte(k)) should.Equal(v, any.ToInt(), fmt.Sprintf("origin val %v", k)) } // int32 for k, v := range intConvertMap { - any := Get([]byte(k)) + any := jsoniter.Get([]byte(k)) should.Equal(int32(v), any.ToInt32(), fmt.Sprintf("original val is %v", k)) } // int64 for k, v := range intConvertMap { - any := Get([]byte(k)) + any := jsoniter.Get([]byte(k)) should.Equal(int64(v), any.ToInt64(), fmt.Sprintf("original val is %v", k)) } @@ -94,17 +95,17 @@ func Test_read_any_to_uint(t *testing.T) { should := require.New(t) for k, v := range uintConvertMap { - any := Get([]byte(k)) + any := jsoniter.Get([]byte(k)) should.Equal(uint64(v), any.ToUint64(), fmt.Sprintf("origin val %v", k)) } for k, v := range uintConvertMap { - any := Get([]byte(k)) + any := jsoniter.Get([]byte(k)) should.Equal(uint32(v), any.ToUint32(), fmt.Sprintf("origin val %v", k)) } for k, v := range uintConvertMap { - any := Get([]byte(k)) + any := jsoniter.Get([]byte(k)) should.Equal(uint(v), any.ToUint(), fmt.Sprintf("origin val %v", k)) } @@ -112,7 +113,7 @@ func Test_read_any_to_uint(t *testing.T) { func Test_read_int64_to_any(t *testing.T) { should := require.New(t) - any := WrapInt64(12345) + any := jsoniter.WrapInt64(12345) should.Equal(12345, any.ToInt()) should.Equal(int32(12345), any.ToInt32()) should.Equal(int64(12345), any.ToInt64()) @@ -123,14 +124,14 @@ func Test_read_int64_to_any(t *testing.T) { should.Equal(float64(12345), any.ToFloat64()) should.Equal("12345", any.ToString()) should.Equal(true, any.ToBool()) - should.Equal(any.ValueType(), NumberValue) - stream := NewStream(ConfigDefault, nil, 32) + should.Equal(any.ValueType(), jsoniter.NumberValue) + stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32) any.WriteTo(stream) should.Equal("12345", string(stream.Buffer())) } func Test_read_int32_to_any(t *testing.T) { should := require.New(t) - any := WrapInt32(12345) + any := jsoniter.WrapInt32(12345) should.Equal(12345, any.ToInt()) should.Equal(int32(12345), any.ToInt32()) should.Equal(int64(12345), any.ToInt64()) @@ -141,15 +142,15 @@ func Test_read_int32_to_any(t *testing.T) { should.Equal(float64(12345), any.ToFloat64()) should.Equal("12345", any.ToString()) should.Equal(true, any.ToBool()) - should.Equal(any.ValueType(), NumberValue) - stream := NewStream(ConfigDefault, nil, 32) + should.Equal(any.ValueType(), jsoniter.NumberValue) + stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32) any.WriteTo(stream) should.Equal("12345", string(stream.Buffer())) } func Test_read_uint32_to_any(t *testing.T) { should := require.New(t) - any := WrapUint32(12345) + any := jsoniter.WrapUint32(12345) should.Equal(12345, any.ToInt()) should.Equal(int32(12345), any.ToInt32()) should.Equal(int64(12345), any.ToInt64()) @@ -160,15 +161,15 @@ func Test_read_uint32_to_any(t *testing.T) { should.Equal(float64(12345), any.ToFloat64()) should.Equal("12345", any.ToString()) should.Equal(true, any.ToBool()) - should.Equal(any.ValueType(), NumberValue) - stream := NewStream(ConfigDefault, nil, 32) + should.Equal(any.ValueType(), jsoniter.NumberValue) + stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32) any.WriteTo(stream) should.Equal("12345", string(stream.Buffer())) } func Test_read_uint64_to_any(t *testing.T) { should := require.New(t) - any := WrapUint64(12345) + any := jsoniter.WrapUint64(12345) should.Equal(12345, any.ToInt()) should.Equal(int32(12345), any.ToInt32()) should.Equal(int64(12345), any.ToInt64()) @@ -179,19 +180,19 @@ func Test_read_uint64_to_any(t *testing.T) { should.Equal(float64(12345), any.ToFloat64()) should.Equal("12345", any.ToString()) should.Equal(true, any.ToBool()) - should.Equal(any.ValueType(), NumberValue) - stream := NewStream(ConfigDefault, nil, 32) + should.Equal(any.ValueType(), jsoniter.NumberValue) + stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32) any.WriteTo(stream) should.Equal("12345", string(stream.Buffer())) - stream = NewStream(ConfigDefault, nil, 32) + stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32) stream.WriteUint(uint(123)) should.Equal("123", string(stream.Buffer())) } func Test_int_lazy_any_get(t *testing.T) { should := require.New(t) - any := Get([]byte("1234")) + any := jsoniter.Get([]byte("1234")) // panic!! //should.Equal(any.LastError(), io.EOF) - should.Equal(InvalidValue, any.Get(1, "2").ValueType()) + should.Equal(jsoniter.InvalidValue, any.Get(1, "2").ValueType()) } diff --git a/jsoniter_any_map_test.go b/any_tests/jsoniter_any_map_test.go similarity index 55% rename from jsoniter_any_map_test.go rename to any_tests/jsoniter_any_map_test.go index f93ea4e..85931b9 100644 --- a/jsoniter_any_map_test.go +++ b/any_tests/jsoniter_any_map_test.go @@ -1,14 +1,15 @@ -package jsoniter +package any_tests import ( "github.com/stretchr/testify/require" "testing" + "github.com/json-iterator/go" ) func Test_wrap_map(t *testing.T) { should := require.New(t) - any := Wrap(map[string]string{"Field1": "hello"}) + any := jsoniter.Wrap(map[string]string{"Field1": "hello"}) should.Equal("hello", any.Get("Field1").ToString()) - any = Wrap(map[string]string{"Field1": "hello"}) + any = jsoniter.Wrap(map[string]string{"Field1": "hello"}) should.Equal(1, any.Size()) } diff --git a/jsoniter_any_null_test.go b/any_tests/jsoniter_any_null_test.go similarity index 75% rename from jsoniter_any_null_test.go rename to any_tests/jsoniter_any_null_test.go index eb09497..5f44b72 100644 --- a/jsoniter_any_null_test.go +++ b/any_tests/jsoniter_any_null_test.go @@ -1,13 +1,14 @@ -package jsoniter +package any_tests import ( "github.com/stretchr/testify/require" "testing" + "github.com/json-iterator/go" ) func Test_read_null_as_any(t *testing.T) { should := require.New(t) - any := Get([]byte(`null`)) + any := jsoniter.Get([]byte(`null`)) should.Equal(0, any.ToInt()) should.Equal(float64(0), any.ToFloat64()) should.Equal("", any.ToString()) diff --git a/jsoniter_any_object_test.go b/any_tests/jsoniter_any_object_test.go similarity index 75% rename from jsoniter_any_object_test.go rename to any_tests/jsoniter_any_object_test.go index 9b215f9..89abaa4 100644 --- a/jsoniter_any_object_test.go +++ b/any_tests/jsoniter_any_object_test.go @@ -1,26 +1,27 @@ -package jsoniter +package any_tests import ( "testing" "github.com/stretchr/testify/require" + "github.com/json-iterator/go" ) func Test_read_object_as_any(t *testing.T) { should := require.New(t) - any := Get([]byte(`{"a":"stream","c":"d"}`)) + any := jsoniter.Get([]byte(`{"a":"stream","c":"d"}`)) should.Equal(`{"a":"stream","c":"d"}`, any.ToString()) // partial parse should.Equal("stream", any.Get("a").ToString()) should.Equal("d", any.Get("c").ToString()) should.Equal(2, len(any.Keys())) - any = Get([]byte(`{"a":"stream","c":"d"}`)) + any = jsoniter.Get([]byte(`{"a":"stream","c":"d"}`)) // full parse should.Equal(2, len(any.Keys())) should.Equal(2, any.Size()) should.True(any.ToBool()) should.Equal(0, any.ToInt()) - should.Equal(ObjectValue, any.ValueType()) + should.Equal(jsoniter.ObjectValue, any.ValueType()) should.Nil(any.LastError()) obj := struct { A string @@ -31,26 +32,26 @@ func Test_read_object_as_any(t *testing.T) { func Test_object_lazy_any_get(t *testing.T) { should := require.New(t) - any := Get([]byte(`{"a":{"stream":{"c":"d"}}}`)) + any := jsoniter.Get([]byte(`{"a":{"stream":{"c":"d"}}}`)) should.Equal("d", any.Get("a", "stream", "c").ToString()) } func Test_object_lazy_any_get_all(t *testing.T) { should := require.New(t) - any := Get([]byte(`{"a":[0],"stream":[1]}`)) + any := jsoniter.Get([]byte(`{"a":[0],"stream":[1]}`)) should.Contains(any.Get('*', 0).ToString(), `"a":0`) } func Test_object_lazy_any_get_invalid(t *testing.T) { should := require.New(t) - any := Get([]byte(`{}`)) - should.Equal(InvalidValue, any.Get("a", "stream", "c").ValueType()) - should.Equal(InvalidValue, any.Get(1).ValueType()) + any := jsoniter.Get([]byte(`{}`)) + should.Equal(jsoniter.InvalidValue, any.Get("a", "stream", "c").ValueType()) + should.Equal(jsoniter.InvalidValue, any.Get(1).ValueType()) } func Test_wrap_map_and_convert_to_any(t *testing.T) { should := require.New(t) - any := Wrap(map[string]interface{}{"a": 1}) + any := jsoniter.Wrap(map[string]interface{}{"a": 1}) should.True(any.ToBool()) should.Equal(0, any.ToInt()) should.Equal(int32(0), any.ToInt32()) @@ -68,9 +69,9 @@ func Test_wrap_object_and_convert_to_any(t *testing.T) { Field1 string field2 string } - any := Wrap(TestObject{"hello", "world"}) + any := jsoniter.Wrap(TestObject{"hello", "world"}) should.Equal("hello", any.Get("Field1").ToString()) - any = Wrap(TestObject{"hello", "world"}) + any = jsoniter.Wrap(TestObject{"hello", "world"}) should.Equal(2, any.Size()) should.Equal(`{"Field1":"hello"}`, any.Get('*').ToString()) @@ -96,11 +97,11 @@ func Test_wrap_object_and_convert_to_any(t *testing.T) { func Test_any_within_struct(t *testing.T) { should := require.New(t) type TestObject struct { - Field1 Any - Field2 Any + Field1 jsoniter.Any + Field2 jsoniter.Any } obj := TestObject{} - err := UnmarshalFromString(`{"Field1": "hello", "Field2": [1,2,3]}`, &obj) + err := jsoniter.UnmarshalFromString(`{"Field1": "hello", "Field2": [1,2,3]}`, &obj) should.Nil(err) should.Equal("hello", obj.Field1.ToString()) should.Equal("[1,2,3]", obj.Field2.ToString()) diff --git a/jsoniter_any_string_test.go b/any_tests/jsoniter_any_string_test.go similarity index 79% rename from jsoniter_any_string_test.go rename to any_tests/jsoniter_any_string_test.go index 7c66b8b..44058a3 100644 --- a/jsoniter_any_string_test.go +++ b/any_tests/jsoniter_any_string_test.go @@ -1,9 +1,10 @@ -package jsoniter +package any_tests import ( "testing" "github.com/stretchr/testify/require" + "github.com/json-iterator/go" ) var stringConvertMap = map[string]string{ @@ -31,27 +32,27 @@ var stringConvertMap = map[string]string{ func Test_read_any_to_string(t *testing.T) { should := require.New(t) for k, v := range stringConvertMap { - any := Get([]byte(k)) + any := jsoniter.Get([]byte(k)) should.Equal(v, any.ToString(), "original val "+k) } } func Test_read_string_as_any(t *testing.T) { should := require.New(t) - any := Get([]byte(`"hello"`)) + any := jsoniter.Get([]byte(`"hello"`)) should.Equal("hello", any.ToString()) should.True(any.ToBool()) - any = Get([]byte(`" "`)) + any = jsoniter.Get([]byte(`" "`)) should.False(any.ToBool()) - any = Get([]byte(`"false"`)) + any = jsoniter.Get([]byte(`"false"`)) should.True(any.ToBool()) - any = Get([]byte(`"123"`)) + any = jsoniter.Get([]byte(`"123"`)) should.Equal(123, any.ToInt()) } func Test_wrap_string(t *testing.T) { should := require.New(t) - any := Get([]byte("-32000")).MustBeValid() + any := jsoniter.Get([]byte("-32000")).MustBeValid() should.Equal(-32000, any.ToInt()) should.NoError(any.LastError()) } diff --git a/api_tests/config_test.go b/api_tests/config_test.go new file mode 100644 index 0000000..f12ff38 --- /dev/null +++ b/api_tests/config_test.go @@ -0,0 +1,16 @@ +package test + +import ( + "testing" + "github.com/stretchr/testify/require" + "github.com/json-iterator/go" + "encoding/json" +) + +func Test_use_number_for_unmarshal(t *testing.T) { + should := require.New(t) + api := jsoniter.Config{UseNumber: true}.Froze() + var obj interface{} + should.Nil(api.UnmarshalFromString("123", &obj)) + should.Equal(json.Number("123"), obj) +} diff --git a/api_tests/decoder_test.go b/api_tests/decoder_test.go index f85968f..e4d3a3a 100644 --- a/api_tests/decoder_test.go +++ b/api_tests/decoder_test.go @@ -5,6 +5,8 @@ import ( "github.com/stretchr/testify/require" "testing" "github.com/json-iterator/go" + "io/ioutil" + "encoding/json" ) func Test_disallowUnknownFields(t *testing.T) { @@ -15,3 +17,43 @@ func Test_disallowUnknownFields(t *testing.T) { decoder.DisallowUnknownFields() should.Error(decoder.Decode(&obj)) } + + +func Test_new_decoder(t *testing.T) { + should := require.New(t) + decoder1 := json.NewDecoder(bytes.NewBufferString(`[1][2]`)) + decoder2 := jsoniter.NewDecoder(bytes.NewBufferString(`[1][2]`)) + arr1 := []int{} + should.Nil(decoder1.Decode(&arr1)) + should.Equal([]int{1}, arr1) + arr2 := []int{} + should.True(decoder1.More()) + buffered, _ := ioutil.ReadAll(decoder1.Buffered()) + should.Equal("[2]", string(buffered)) + should.Nil(decoder2.Decode(&arr2)) + should.Equal([]int{1}, arr2) + should.True(decoder2.More()) + buffered, _ = ioutil.ReadAll(decoder2.Buffered()) + should.Equal("[2]", string(buffered)) + + should.Nil(decoder1.Decode(&arr1)) + should.Equal([]int{2}, arr1) + should.False(decoder1.More()) + should.Nil(decoder2.Decode(&arr2)) + should.Equal([]int{2}, arr2) + should.False(decoder2.More()) +} + +func Test_use_number(t *testing.T) { + should := require.New(t) + decoder1 := json.NewDecoder(bytes.NewBufferString(`123`)) + decoder1.UseNumber() + decoder2 := jsoniter.NewDecoder(bytes.NewBufferString(`123`)) + decoder2.UseNumber() + var obj1 interface{} + should.Nil(decoder1.Decode(&obj1)) + should.Equal(json.Number("123"), obj1) + var obj2 interface{} + should.Nil(decoder2.Decode(&obj2)) + should.Equal(json.Number("123"), obj2) +} diff --git a/jsoniter_1dot8_only_test.go b/api_tests/encoder_18_test.go similarity index 86% rename from jsoniter_1dot8_only_test.go rename to api_tests/encoder_18_test.go index 1ac6186..35d306a 100644 --- a/jsoniter_1dot8_only_test.go +++ b/api_tests/encoder_18_test.go @@ -1,6 +1,6 @@ -// +build go1.8 +//+build go1.8 -package jsoniter +package test import ( "bytes" @@ -9,6 +9,7 @@ import ( "unicode/utf8" "github.com/stretchr/testify/require" + "github.com/json-iterator/go" ) func Test_new_encoder(t *testing.T) { @@ -19,14 +20,14 @@ func Test_new_encoder(t *testing.T) { encoder1.Encode([]int{1}) should.Equal("[1]\n", buf1.String()) buf2 := &bytes.Buffer{} - encoder2 := NewEncoder(buf2) + encoder2 := jsoniter.NewEncoder(buf2) encoder2.SetEscapeHTML(false) encoder2.Encode([]int{1}) should.Equal("[1]\n", buf2.String()) } func Test_string_encode_with_std_without_html_escape(t *testing.T) { - api := Config{EscapeHTML: false}.Froze() + api := jsoniter.Config{EscapeHTML: false}.Froze() should := require.New(t) for i := 0; i < utf8.RuneSelf; i++ { input := string([]byte{byte(i)}) diff --git a/api_tests/encoder_test.go b/api_tests/encoder_test.go new file mode 100644 index 0000000..1245088 --- /dev/null +++ b/api_tests/encoder_test.go @@ -0,0 +1,20 @@ +package test + +import ( + "testing" + "github.com/stretchr/testify/require" + "bytes" + "github.com/json-iterator/go" + "encoding/json" +) + +// Standard Encoder has trailing newline. +func TestEncoderHasTrailingNewline(t *testing.T) { + should := require.New(t) + var buf, stdbuf bytes.Buffer + enc := jsoniter.ConfigCompatibleWithStandardLibrary.NewEncoder(&buf) + enc.Encode(1) + stdenc := json.NewEncoder(&stdbuf) + stdenc.Encode(1) + should.Equal(stdbuf.Bytes(), buf.Bytes()) +} \ No newline at end of file diff --git a/api_tests/marshal_indent_test.go b/api_tests/marshal_indent_test.go new file mode 100644 index 0000000..e227d78 --- /dev/null +++ b/api_tests/marshal_indent_test.go @@ -0,0 +1,36 @@ +package test + +import ( + "encoding/json" + "github.com/stretchr/testify/require" + "testing" + "github.com/json-iterator/go" +) + +func Test_marshal_indent(t *testing.T) { + should := require.New(t) + obj := struct { + F1 int + F2 []int + }{1, []int{2, 3, 4}} + output, err := json.MarshalIndent(obj, "", " ") + should.Nil(err) + should.Equal("{\n \"F1\": 1,\n \"F2\": [\n 2,\n 3,\n 4\n ]\n}", string(output)) + output, err = jsoniter.MarshalIndent(obj, "", " ") + should.Nil(err) + should.Equal("{\n \"F1\": 1,\n \"F2\": [\n 2,\n 3,\n 4\n ]\n}", string(output)) +} + +func Test_marshal_indent_map(t *testing.T) { + should := require.New(t) + obj := map[int]int{1: 2} + output, err := json.MarshalIndent(obj, "", " ") + should.Nil(err) + should.Equal("{\n \"1\": 2\n}", string(output)) + output, err = jsoniter.MarshalIndent(obj, "", " ") + should.Nil(err) + should.Equal("{\n \"1\": 2\n}", string(output)) + output, err = jsoniter.ConfigCompatibleWithStandardLibrary.MarshalIndent(obj, "", " ") + should.Nil(err) + should.Equal("{\n \"1\": 2\n}", string(output)) +} diff --git a/compatible_test.go b/compatible_test.go deleted file mode 100644 index 4b725c5..0000000 --- a/compatible_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package jsoniter - -import ( - "bytes" - "encoding/json" - "testing" - - "github.com/stretchr/testify/require" -) - -// Standard Encoder has trailing newline. -func TestEncoderHasTrailingNewline(t *testing.T) { - should := require.New(t) - var buf, stdbuf bytes.Buffer - enc := ConfigCompatibleWithStandardLibrary.NewEncoder(&buf) - enc.Encode(1) - stdenc := json.NewEncoder(&stdbuf) - stdenc.Encode(1) - should.Equal(stdbuf.Bytes(), buf.Bytes()) -} - -// Non-nil but empty map should be ignored. -func TestOmitempty(t *testing.T) { - o := struct { - A string `json:"a,omitempty"` - B string `json:"b,omitempty"` - Annotations map[string]string `json:"annotations,omitempty"` - }{ - A: "a", - B: "b", - Annotations: map[string]string{}, - } - should := require.New(t) - var buf, stdbuf bytes.Buffer - enc := ConfigCompatibleWithStandardLibrary.NewEncoder(&buf) - enc.Encode(o) - stdenc := json.NewEncoder(&stdbuf) - stdenc.Encode(o) - should.Equal(string(stdbuf.Bytes()), string(buf.Bytes())) -} diff --git a/jsoniter_adapter_test.go b/jsoniter_adapter_test.go deleted file mode 100644 index 30f03de..0000000 --- a/jsoniter_adapter_test.go +++ /dev/null @@ -1,84 +0,0 @@ -package jsoniter - -import ( - "bytes" - "encoding/json" - "github.com/stretchr/testify/require" - "io/ioutil" - "testing" -) - -func Test_new_decoder(t *testing.T) { - should := require.New(t) - decoder1 := json.NewDecoder(bytes.NewBufferString(`[1][2]`)) - decoder2 := NewDecoder(bytes.NewBufferString(`[1][2]`)) - arr1 := []int{} - should.Nil(decoder1.Decode(&arr1)) - should.Equal([]int{1}, arr1) - arr2 := []int{} - should.True(decoder1.More()) - buffered, _ := ioutil.ReadAll(decoder1.Buffered()) - should.Equal("[2]", string(buffered)) - should.Nil(decoder2.Decode(&arr2)) - should.Equal([]int{1}, arr2) - should.True(decoder2.More()) - buffered, _ = ioutil.ReadAll(decoder2.Buffered()) - should.Equal("[2]", string(buffered)) - - should.Nil(decoder1.Decode(&arr1)) - should.Equal([]int{2}, arr1) - should.False(decoder1.More()) - should.Nil(decoder2.Decode(&arr2)) - should.Equal([]int{2}, arr2) - should.False(decoder2.More()) -} - -func Test_use_number(t *testing.T) { - should := require.New(t) - decoder1 := json.NewDecoder(bytes.NewBufferString(`123`)) - decoder1.UseNumber() - decoder2 := NewDecoder(bytes.NewBufferString(`123`)) - decoder2.UseNumber() - var obj1 interface{} - should.Nil(decoder1.Decode(&obj1)) - should.Equal(json.Number("123"), obj1) - var obj2 interface{} - should.Nil(decoder2.Decode(&obj2)) - should.Equal(json.Number("123"), obj2) -} - -func Test_use_number_for_unmarshal(t *testing.T) { - should := require.New(t) - api := Config{UseNumber: true}.Froze() - var obj interface{} - should.Nil(api.UnmarshalFromString("123", &obj)) - should.Equal(json.Number("123"), obj) -} - -func Test_marshal_indent(t *testing.T) { - should := require.New(t) - obj := struct { - F1 int - F2 []int - }{1, []int{2, 3, 4}} - output, err := json.MarshalIndent(obj, "", " ") - should.Nil(err) - should.Equal("{\n \"F1\": 1,\n \"F2\": [\n 2,\n 3,\n 4\n ]\n}", string(output)) - output, err = MarshalIndent(obj, "", " ") - should.Nil(err) - should.Equal("{\n \"F1\": 1,\n \"F2\": [\n 2,\n 3,\n 4\n ]\n}", string(output)) -} - -func Test_marshal_indent_map(t *testing.T) { - should := require.New(t) - obj := map[int]int{1: 2} - output, err := json.MarshalIndent(obj, "", " ") - should.Nil(err) - should.Equal("{\n \"1\": 2\n}", string(output)) - output, err = MarshalIndent(obj, "", " ") - should.Nil(err) - should.Equal("{\n \"1\": 2\n}", string(output)) - output, err = ConfigCompatibleWithStandardLibrary.MarshalIndent(obj, "", " ") - should.Nil(err) - should.Equal("{\n \"1\": 2\n}", string(output)) -} diff --git a/jsoniter_alias_test.go b/jsoniter_alias_test.go deleted file mode 100644 index 246651f..0000000 --- a/jsoniter_alias_test.go +++ /dev/null @@ -1,62 +0,0 @@ -package jsoniter - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func Test_alias(t *testing.T) { - should := require.New(t) - type myint int - type myint8 int8 - type myint16 int16 - type myint32 int32 - type myint64 int64 - type myuint uint - type myuint8 uint8 - type myuint16 uint16 - type myuint32 uint32 - type myuint64 uint64 - type myfloat32 float32 - type myfloat64 float64 - type mystring string - type mybool bool - type myuintptr uintptr - var a struct { - A myint8 `json:"a"` - B myint16 `json:"stream"` - C myint32 `json:"c"` - D myint64 `json:"d"` - E myuint8 `json:"e"` - F myuint16 `json:"f"` - G myuint32 `json:"g"` - H myuint64 `json:"h"` - I myfloat32 `json:"i"` - J myfloat64 `json:"j"` - K mystring `json:"k"` - L myint `json:"l"` - M myuint `json:"m"` - N mybool `json:"n"` - O myuintptr `json:"o"` - } - - should.Nil(UnmarshalFromString(`{"a" : 1, "stream" : 1, "c": 1, "d" : 1, "e" : 1, "f" : 1, "g" : 1, "h": 1, "i" : 1, "j" : 1, "k" :"xxxx", "l" : 1, "m":1, "n": true, "o" : 1}`, &a)) - should.Equal(myfloat32(1), a.I) - should.Equal(myfloat64(1), a.J) - should.Equal(myint8(1), a.A) - should.Equal(myint16(1), a.B) - should.Equal(myint32(1), a.C) - should.Equal(myint64(1), a.D) - should.Equal(myuint8(1), a.E) - should.Equal(myuint16(1), a.F) - should.Equal(myuint32(1), a.G) - should.Equal(myuint64(1), a.H) - should.Equal(mystring("xxxx"), a.K) - should.Equal(mybool(true), a.N) - should.Equal(myuintptr(1), a.O) - b, err := Marshal(a) - should.Nil(err) - should.Equal(`{"a":1,"stream":1,"c":1,"d":1,"e":1,"f":1,"g":1,"h":1,"i":1,"j":1,"k":"xxxx","l":1,"m":1,"n":true,"o":1}`, string(b)) - -} diff --git a/type_tests/builtin_test.go b/type_tests/builtin_test.go index 4f809c5..03dd345 100644 --- a/type_tests/builtin_test.go +++ b/type_tests/builtin_test.go @@ -28,6 +28,23 @@ func init() { (*uint32Alias)(nil), (*uintptr)(nil), (*uintptrAlias)(nil), + (*struct { + A int8Alias `json:"a"` + B int16Alias `json:"stream"` + C int32Alias `json:"c"` + D int64Alias `json:"d"` + E uintAlias `json:"e"` + F uint16Alias `json:"f"` + G uint32Alias `json:"g"` + H uint64Alias `json:"h"` + I float32Alias `json:"i"` + J float64Alias `json:"j"` + K stringAlias `json:"k"` + L intAlias `json:"l"` + M uintAlias `json:"m"` + N boolAlias `json:"n"` + O uintptrAlias `json:"o"` + })(nil), ) } @@ -46,4 +63,7 @@ type ptrStringAlias *string type uint8Alias uint8 type uint16Alias uint16 type uint32Alias uint32 -type uintptrAlias uintptr \ No newline at end of file +type uintptrAlias uintptr +type uintAlias uint +type uint64Alias uint64 +type intAlias int diff --git a/type_tests/struct_tags_test.go b/type_tests/struct_tags_test.go index d3fee27..1a7a1b5 100644 --- a/type_tests/struct_tags_test.go +++ b/type_tests/struct_tags_test.go @@ -137,6 +137,11 @@ func init() { F1 uint32 `json:"F1"` F2 uint32 `json:"F2,string"` })(nil), + (*struct { + A string `json:"a,omitempty"` + B string `json:"b,omitempty"` + Annotations map[string]string `json:"annotations,omitempty"` + })(nil), ) } @@ -193,7 +198,6 @@ type StringFieldName struct { StringFieldNameE `json:"e"` } - type StructFieldNameS1 struct { S1F string }