Explorar o código

increase coverage

Xargin %!s(int64=8) %!d(string=hai) anos
pai
achega
8675af13bf
Modificáronse 3 ficheiros con 77 adicións e 67 borrados
  1. 17 65
      feature_any_object.go
  2. 27 1
      jsoniter_any_array_test.go
  3. 33 1
      jsoniter_any_object_test.go

+ 17 - 65
feature_any_object.go

@@ -182,59 +182,35 @@ func (any *objectAny) ToBool() bool {
 }
 
 func (any *objectAny) ToInt() int {
-	if any.val.NumField() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *objectAny) ToInt32() int32 {
-	if any.val.NumField() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *objectAny) ToInt64() int64 {
-	if any.val.NumField() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *objectAny) ToUint() uint {
-	if any.val.NumField() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *objectAny) ToUint32() uint32 {
-	if any.val.NumField() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *objectAny) ToUint64() uint64 {
-	if any.val.NumField() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *objectAny) ToFloat32() float32 {
-	if any.val.NumField() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *objectAny) ToFloat64() float64 {
-	if any.val.NumField() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *objectAny) ToString() string {
@@ -333,63 +309,39 @@ func (any *mapAny) LastError() error {
 }
 
 func (any *mapAny) ToBool() bool {
-	return any.val.Len() != 0
+	return true
 }
 
 func (any *mapAny) ToInt() int {
-	if any.val.Len() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *mapAny) ToInt32() int32 {
-	if any.val.Len() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *mapAny) ToInt64() int64 {
-	if any.val.Len() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *mapAny) ToUint() uint {
-	if any.val.Len() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *mapAny) ToUint32() uint32 {
-	if any.val.Len() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *mapAny) ToUint64() uint64 {
-	if any.val.Len() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *mapAny) ToFloat32() float32 {
-	if any.val.Len() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *mapAny) ToFloat64() float64 {
-	if any.val.Len() == 0 {
-		return 0
-	}
-	return 1
+	return 0
 }
 
 func (any *mapAny) ToString() string {

+ 27 - 1
jsoniter_any_array_test.go

@@ -47,10 +47,36 @@ func Test_read_two_element_array_as_any(t *testing.T) {
 	should.Equal([]int{1, 2}, arr)
 }
 
-func Test_wrap_array(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{})
+
 	should.Equal("[1,2,3]", any.ToString())
+	should.True(any.ToBool())
+	should.False(any2.ToBool())
+
+	should.Equal(1, any.ToInt())
+	should.Equal(0, any2.ToInt())
+	should.Equal(int32(1), any.ToInt32())
+	should.Equal(int32(0), any2.ToInt32())
+	should.Equal(int64(1), any.ToInt64())
+	should.Equal(int64(0), any2.ToInt64())
+	should.Equal(uint(1), any.ToUint())
+	should.Equal(uint(0), any2.ToUint())
+	should.Equal(uint32(1), any.ToUint32())
+	should.Equal(uint32(0), any2.ToUint32())
+	should.Equal(uint64(1), any.ToUint64())
+	should.Equal(uint64(0), any2.ToUint64())
+	should.Equal(float32(1), any.ToFloat32())
+	should.Equal(float32(0), any2.ToFloat32())
+	should.Equal(float64(1), any.ToFloat64())
+	should.Equal(float64(0), any2.ToFloat64())
+	should.Equal(3, any.Size())
+	should.Equal(0, any2.Size())
+
+	var i interface{} = []int{1, 2, 3}
+	should.Equal(i, any.GetInterface())
 }
 
 func Test_array_lazy_any_get(t *testing.T) {

+ 33 - 1
jsoniter_any_object_test.go

@@ -49,7 +49,21 @@ func Test_object_lazy_any_get_invalid(t *testing.T) {
 	should.Equal(Invalid, any.Get(1).ValueType())
 }
 
-func Test_wrap_object(t *testing.T) {
+func Test_wrap_map_and_convert_to_any(t *testing.T) {
+	should := require.New(t)
+	any := Wrap(map[string]interface{}{"a": 1})
+	should.True(any.ToBool())
+	should.Equal(0, any.ToInt())
+	should.Equal(int32(0), any.ToInt32())
+	should.Equal(int64(0), any.ToInt64())
+	should.Equal(float32(0), any.ToFloat32())
+	should.Equal(float64(0), any.ToFloat64())
+	should.Equal(uint(0), any.ToUint())
+	should.Equal(uint32(0), any.ToUint32())
+	should.Equal(uint64(0), any.ToUint64())
+}
+
+func Test_wrap_object_and_convert_to_any(t *testing.T) {
 	should := require.New(t)
 	type TestObject struct {
 		Field1 string
@@ -60,6 +74,24 @@ func Test_wrap_object(t *testing.T) {
 	any = Wrap(TestObject{"hello", "world"})
 	should.Equal(2, any.Size())
 	should.Equal(`{"Field1":"hello"}`, any.Get('*').ToString())
+
+	should.Equal(0, any.ToInt())
+	should.Equal(int32(0), any.ToInt32())
+	should.Equal(int64(0), any.ToInt64())
+	should.Equal(float32(0), any.ToFloat32())
+	should.Equal(float64(0), any.ToFloat64())
+	should.Equal(uint(0), any.ToUint())
+	should.Equal(uint32(0), any.ToUint32())
+	should.Equal(uint64(0), any.ToUint64())
+	should.True(any.ToBool())
+	should.Equal(`{"Field1":"hello"}`, any.ToString())
+
+	// cannot pass!
+	//stream := NewStream(ConfigDefault, nil, 32)
+	//any.WriteTo(stream)
+	//should.Equal(`{"Field1":"hello"}`, string(stream.Buffer()))
+	// cannot pass!
+
 }
 
 func Test_any_within_struct(t *testing.T) {