Explorar o código

test []interface{}

Tao Wen %!s(int64=8) %!d(string=hai) anos
pai
achega
ac3b3cd160
Modificáronse 2 ficheiros con 37 adicións e 0 borrados
  1. 22 0
      jsoniter_customize_test.go
  2. 15 0
      jsoniter_invalid_test.go

+ 22 - 0
jsoniter_customize_test.go

@@ -7,6 +7,8 @@ import (
 	"testing"
 	"time"
 	"unsafe"
+	"fmt"
+	"reflect"
 )
 
 func Test_customize_type_decoder(t *testing.T) {
@@ -285,3 +287,23 @@ func Test_marshal_json_with_time(t *testing.T) {
 	should.Nil(Unmarshal([]byte(`{"TF1":{"F1":"fake","F2":"fake"}}`), &obj))
 	should.NotNil(obj.TF1.F2)
 }
+
+func Test_unmarshal_empty_interface_as_int64(t *testing.T) {
+	var obj interface{}
+	RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *Iterator) {
+		switch iter.WhatIsNext() {
+		case NumberValue:
+			*(*interface{})(ptr) = iter.ReadInt64()
+		default:
+			*(*interface{})(ptr) = iter.Read()
+		}
+	})
+	should := require.New(t)
+	Unmarshal([]byte("100"), &obj)
+	should.Equal(int64(100), obj)
+	Unmarshal([]byte(`"hello"`), &obj)
+	should.Equal("hello", obj)
+	var arr []interface{}
+	Unmarshal([]byte("[100]"), &arr)
+	should.Equal(int64(100), arr[0])
+}

+ 15 - 0
jsoniter_invalid_test.go

@@ -98,3 +98,18 @@ func Test_invalid_float(t *testing.T) {
 		})
 	}
 }
+
+func Test_chan(t *testing.T) {
+	t.Skip("do not support chan")
+
+	type TestObject struct {
+		MyChan chan bool
+		MyField int
+	}
+
+	should := require.New(t)
+	obj := TestObject{}
+	str, err := json.Marshal(obj)
+	should.Nil(err)
+	should.Equal(``, str)
+}