소스 검색

increase coverage

Xargin 8 년 전
부모
커밋
1de44419ea
7개의 변경된 파일154개의 추가작업 그리고 45개의 파일을 삭제
  1. 1 1
      feature_any_string.go
  2. 1 0
      jsoniter_any_float_test.go
  3. 3 0
      jsoniter_any_int_test.go
  4. 3 3
      jsoniter_demo_test.go
  5. 28 0
      jsoniter_invalid_test.go
  6. 118 0
      jsoniter_wrap_test.go
  7. 0 41
      wrap_test.go

+ 1 - 1
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
 		}

+ 1 - 0
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())
 }

+ 3 - 0
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) {

+ 3 - 3
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 {

+ 28 - 0
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())
+
+}

+ 118 - 0
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())
+
+}

+ 0 - 41
wrap_test.go

@@ -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)
-
-}