From 550531a0463343b5996a2554a76f5fc9d68d6bc3 Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 11:40:20 +0800 Subject: [PATCH 1/4] increase coverage --- feature_stream_float.go | 2 +- jsoniter_1dot8_only_test.go | 9 ++++---- jsoniter_any_array_test.go | 3 ++- jsoniter_any_bool_test.go | 17 +++++++++++++++ jsoniter_any_int_test.go | 14 +++++++++++++ jsoniter_object_test.go | 5 +++-- wrap_test.go | 41 +++++++++++++++++++++++++++++++++++++ 7 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 wrap_test.go diff --git a/feature_stream_float.go b/feature_stream_float.go index 0b8e8a0..281370b 100644 --- a/feature_stream_float.go +++ b/feature_stream_float.go @@ -1,8 +1,8 @@ package jsoniter import ( - "strconv" "math" + "strconv" ) var _POW10 []uint64 diff --git a/jsoniter_1dot8_only_test.go b/jsoniter_1dot8_only_test.go index 50f7a6a..e2154c4 100644 --- a/jsoniter_1dot8_only_test.go +++ b/jsoniter_1dot8_only_test.go @@ -3,11 +3,12 @@ package jsoniter import ( - "testing" - "encoding/json" - "github.com/json-iterator/go/require" "bytes" + "encoding/json" + "testing" "unicode/utf8" + + "github.com/json-iterator/go/require" ) func Test_new_encoder(t *testing.T) { @@ -41,4 +42,4 @@ func Test_string_encode_with_std_without_html_escape(t *testing.T) { jsoniterOutput := string(jsoniterOutputBytes) should.Equal(stdOutput, jsoniterOutput) } -} \ No newline at end of file +} diff --git a/jsoniter_any_array_test.go b/jsoniter_any_array_test.go index 577745e..e793b14 100644 --- a/jsoniter_any_array_test.go +++ b/jsoniter_any_array_test.go @@ -1,8 +1,9 @@ package jsoniter import ( - "github.com/json-iterator/go/require" "testing" + + "github.com/json-iterator/go/require" ) func Test_read_empty_array_as_any(t *testing.T) { diff --git a/jsoniter_any_bool_test.go b/jsoniter_any_bool_test.go index f227cb8..f6b09ee 100644 --- a/jsoniter_any_bool_test.go +++ b/jsoniter_any_bool_test.go @@ -44,4 +44,21 @@ func Test_read_bool_as_any(t *testing.T) { should.False(any.ToBool(), fmt.Sprintf("origin val is %v", k)) } } + +} + +func Test_write_bool_to_stream(t *testing.T) { + should := require.New(t) + any := Get([]byte("true")) + stream := NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("true", string(stream.Buffer())) + should.Equal(any.ValueType(), Bool) + + any = Get([]byte("false")) + stream = NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("false", string(stream.Buffer())) + + should.Equal(any.ValueType(), Bool) } diff --git a/jsoniter_any_int_test.go b/jsoniter_any_int_test.go index 04c8722..0982497 100644 --- a/jsoniter_any_int_test.go +++ b/jsoniter_any_int_test.go @@ -123,6 +123,10 @@ 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(), Number) + stream := NewStream(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) @@ -137,6 +141,10 @@ 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(), Number) + stream := NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("12345", string(stream.Buffer())) } func Test_read_uint32_to_any(t *testing.T) { @@ -153,6 +161,9 @@ func Test_read_uint32_to_any(t *testing.T) { should.Equal("12345", any.ToString()) should.Equal(true, any.ToBool()) should.Equal(any.ValueType(), Number) + stream := NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("12345", string(stream.Buffer())) } func Test_read_uint64_to_any(t *testing.T) { @@ -169,6 +180,9 @@ func Test_read_uint64_to_any(t *testing.T) { should.Equal("12345", any.ToString()) should.Equal(true, any.ToBool()) should.Equal(any.ValueType(), Number) + stream := NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("12345", string(stream.Buffer())) } func Test_int_lazy_any_get(t *testing.T) { diff --git a/jsoniter_object_test.go b/jsoniter_object_test.go index 9c5e532..c2f9083 100644 --- a/jsoniter_object_test.go +++ b/jsoniter_object_test.go @@ -3,8 +3,9 @@ package jsoniter import ( "bytes" "fmt" - "github.com/json-iterator/go/require" "testing" + + "github.com/json-iterator/go/require" ) func Test_empty_object(t *testing.T) { @@ -381,7 +382,7 @@ func Test_shadow_struct_field(t *testing.T) { should.Contains(output, `"max_age":20`) } -func Test_embeded_order(t *testing.T) { +func Test_embedded_order(t *testing.T) { type A struct { Field2 string } diff --git a/wrap_test.go b/wrap_test.go new file mode 100644 index 0000000..7cc59b5 --- /dev/null +++ b/wrap_test.go @@ -0,0 +1,41 @@ +package jsoniter + +import ( + "testing" + + "github.com/json-iterator/go/require" +) + +func Test_wrap_and_valuetype_everything(t *testing.T) { + should := require.New(t) + any := Wrap(int8(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(int16(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(int32(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(int64(10)) + should.Equal(any.ValueType(), Number) + + any = Wrap(uint(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(uint8(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(uint16(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(uint32(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(uint64(10)) + should.Equal(any.ValueType(), Number) + + any = Wrap(float32(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(float64(10)) + should.Equal(any.ValueType(), Number) + + any = Wrap(true) + should.Equal(any.ValueType(), Bool) + any = Wrap(false) + should.Equal(any.ValueType(), Bool) + +} From 1de44419ea2d43ca299b2296e6b0708b836ec863 Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 13:55:10 +0800 Subject: [PATCH 2/4] increase coverage --- feature_any_string.go | 2 +- jsoniter_any_float_test.go | 1 + jsoniter_any_int_test.go | 3 + jsoniter_demo_test.go | 6 +- jsoniter_invalid_test.go | 28 +++++++++ jsoniter_wrap_test.go | 118 +++++++++++++++++++++++++++++++++++++ wrap_test.go | 41 ------------- 7 files changed, 154 insertions(+), 45 deletions(-) create mode 100644 jsoniter_invalid_test.go create mode 100644 jsoniter_wrap_test.go delete mode 100644 wrap_test.go diff --git a/feature_any_string.go b/feature_any_string.go index 7b8c39a..5a0e8da 100644 --- a/feature_any_string.go +++ b/feature_any_string.go @@ -136,7 +136,7 @@ func (any *stringAny) ToFloat64() float64 { // eg 123true => 123, -12.12xxa => -12.12 endPos := 1 for i := 1; i < len(any.val); i++ { - if any.val[i] == '.' || any.val[i] == 'e' { + if any.val[i] == '.' || any.val[i] == 'e' || any.val[i] == 'E' { endPos = i + 1 continue } diff --git a/jsoniter_any_float_test.go b/jsoniter_any_float_test.go index 3845cd2..9e179b6 100644 --- a/jsoniter_any_float_test.go +++ b/jsoniter_any_float_test.go @@ -70,4 +70,5 @@ func Test_read_float_to_any(t *testing.T) { should.Equal(uint64(0), any2.ToUint64()) should.Equal(any.ValueType(), Number) + should.Equal("1.23E+01", any.ToString()) } diff --git a/jsoniter_any_int_test.go b/jsoniter_any_int_test.go index 0982497..e5b2baf 100644 --- a/jsoniter_any_int_test.go +++ b/jsoniter_any_int_test.go @@ -183,6 +183,9 @@ func Test_read_uint64_to_any(t *testing.T) { stream := NewStream(ConfigDefault, nil, 32) any.WriteTo(stream) should.Equal("12345", string(stream.Buffer())) + stream = NewStream(ConfigDefault, nil, 32) + stream.WriteUint(uint(123)) + should.Equal("123", string(stream.Buffer())) } func Test_int_lazy_any_get(t *testing.T) { diff --git a/jsoniter_demo_test.go b/jsoniter_demo_test.go index 92fa02c..5c49268 100644 --- a/jsoniter_demo_test.go +++ b/jsoniter_demo_test.go @@ -2,9 +2,9 @@ package jsoniter import ( "encoding/json" - "fmt" - "github.com/json-iterator/go/require" "testing" + + "github.com/json-iterator/go/require" ) func Test_bind_api_demo(t *testing.T) { @@ -21,7 +21,7 @@ func Test_iterator_api_demo(t *testing.T) { for iter.ReadArray() { total += iter.ReadInt() } - fmt.Println(total) + //fmt.Println(total) } type People struct { diff --git a/jsoniter_invalid_test.go b/jsoniter_invalid_test.go new file mode 100644 index 0000000..d7876ee --- /dev/null +++ b/jsoniter_invalid_test.go @@ -0,0 +1,28 @@ +package jsoniter + +import ( + "testing" + + "github.com/json-iterator/go/require" +) + +func Test_invalid(t *testing.T) { + should := require.New(t) + any := Get([]byte("[]")) + should.Equal(Invalid, any.Get(0.3).ValueType()) + // is nil correct ? + should.Equal(nil, any.Get(0.3).GetInterface()) + + any = any.Get(0.3) + should.Equal(false, any.ToBool()) + should.Equal(int(0), any.ToInt()) + should.Equal(int32(0), any.ToInt32()) + should.Equal(int64(0), any.ToInt64()) + should.Equal(uint(0), any.ToUint()) + should.Equal(uint32(0), any.ToUint32()) + should.Equal(uint64(0), any.ToUint64()) + should.Equal(float32(0), any.ToFloat32()) + should.Equal(float64(0), any.ToFloat64()) + should.Equal("", any.ToString()) + +} diff --git a/jsoniter_wrap_test.go b/jsoniter_wrap_test.go new file mode 100644 index 0000000..117a0e2 --- /dev/null +++ b/jsoniter_wrap_test.go @@ -0,0 +1,118 @@ +package jsoniter + +import ( + "testing" + + "github.com/json-iterator/go/require" +) + +func Test_wrap_and_valuetype_everything(t *testing.T) { + should := require.New(t) + var i interface{} + any := Get([]byte("123")) + // default of number type is float64 + i = float64(123) + should.Equal(i, any.GetInterface()) + + any = Wrap(int8(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + // get interface is not int8 interface + // i = int8(10) + // should.Equal(i, any.GetInterface()) + + any = Wrap(int16(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + //i = int16(10) + //should.Equal(i, any.GetInterface()) + + any = Wrap(int32(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + i = int32(10) + should.Equal(i, any.GetInterface()) + any = Wrap(int64(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + i = int64(10) + should.Equal(i, any.GetInterface()) + + any = Wrap(uint(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + // not equal + //i = uint(10) + //should.Equal(i, any.GetInterface()) + any = Wrap(uint8(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + // not equal + // i = uint8(10) + // should.Equal(i, any.GetInterface()) + any = Wrap(uint16(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + any = Wrap(uint32(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + i = uint32(10) + should.Equal(i, any.GetInterface()) + any = Wrap(uint64(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + i = uint64(10) + should.Equal(i, any.GetInterface()) + + any = Wrap(float32(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + // not equal + //i = float32(10) + //should.Equal(i, any.GetInterface()) + any = Wrap(float64(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + i = float64(10) + should.Equal(i, any.GetInterface()) + + any = Wrap(true) + should.Equal(any.ValueType(), Bool) + should.Equal(any.LastError(), nil) + i = true + should.Equal(i, any.GetInterface()) + any = Wrap(false) + should.Equal(any.ValueType(), Bool) + should.Equal(any.LastError(), nil) + i = false + should.Equal(i, any.GetInterface()) + + any = Wrap(nil) + should.Equal(any.ValueType(), Nil) + should.Equal(any.LastError(), nil) + i = nil + should.Equal(i, any.GetInterface()) + + stream := NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("null", string(stream.Buffer())) + should.Equal(any.LastError(), nil) + + any = Wrap(struct{ age int }{age: 1}) + should.Equal(any.ValueType(), Object) + should.Equal(any.LastError(), nil) + i = struct{ age int }{age: 1} + should.Equal(i, any.GetInterface()) + + any = Wrap(map[string]interface{}{"abc": 1}) + should.Equal(any.ValueType(), Object) + should.Equal(any.LastError(), nil) + i = map[string]interface{}{"abc": 1} + should.Equal(i, any.GetInterface()) + + any = Wrap("abc") + i = "abc" + should.Equal(i, any.GetInterface()) + should.Equal(nil, any.LastError()) + +} diff --git a/wrap_test.go b/wrap_test.go deleted file mode 100644 index 7cc59b5..0000000 --- a/wrap_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package jsoniter - -import ( - "testing" - - "github.com/json-iterator/go/require" -) - -func Test_wrap_and_valuetype_everything(t *testing.T) { - should := require.New(t) - any := Wrap(int8(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(int16(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(int32(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(int64(10)) - should.Equal(any.ValueType(), Number) - - any = Wrap(uint(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(uint8(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(uint16(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(uint32(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(uint64(10)) - should.Equal(any.ValueType(), Number) - - any = Wrap(float32(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(float64(10)) - should.Equal(any.ValueType(), Number) - - any = Wrap(true) - should.Equal(any.ValueType(), Bool) - any = Wrap(false) - should.Equal(any.ValueType(), Bool) - -} From ca6a524d4f19c3aaa17859a17cf726b6cd1affa8 Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 14:11:27 +0800 Subject: [PATCH 3/4] add codecov.yml ignore output tests --- .codecov.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .codecov.yml diff --git a/.codecov.yml b/.codecov.yml new file mode 100644 index 0000000..a5727d1 --- /dev/null +++ b/.codecov.yml @@ -0,0 +1,3 @@ +codecov: + ignore: + - "output_tests" From 27725b7139aa74c56974aa4973bd6405ebb8d32c Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 15:17:39 +0800 Subject: [PATCH 4/4] update codecov.yml --- .codecov.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.codecov.yml b/.codecov.yml index a5727d1..f948b1a 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -1,3 +1,5 @@ codecov: ignore: - "output_tests" + - "assert" +