| 1 |
package any_tests |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/json-iterator/go" |
| 7 |
"github.com/stretchr/testify/require" |
| 8 |
) |
| 9 |
|
| 10 |
func Test_read_empty_array_as_any(t *testing.T) { |
| 11 |
should := require.New(t) |
| 12 |
any := jsoniter.Get([]byte("[]")) |
| 13 |
should.Equal(jsoniter.ArrayValue, any.Get().ValueType()) |
| 14 |
should.Equal(jsoniter.InvalidValue, any.Get(0.3).ValueType()) |
| 15 |
should.Equal(0, any.Size()) |
| 16 |
should.Equal(jsoniter.ArrayValue, any.ValueType()) |
| 17 |
should.Nil(any.LastError()) |
| 18 |
should.Equal(0, any.ToInt()) |
| 19 |
should.Equal(int32(0), any.ToInt32()) |
| 20 |
should.Equal(int64(0), any.ToInt64()) |
| 21 |
should.Equal(uint(0), any.ToUint()) |
| 22 |
should.Equal(uint32(0), any.ToUint32()) |
| 23 |
should.Equal(uint64(0), any.ToUint64()) |
| 24 |
should.Equal(float32(0), any.ToFloat32()) |
| 25 |
should.Equal(float64(0), any.ToFloat64()) |
| 26 |
} |
| 27 |
|
| 28 |
func Test_read_one_element_array_as_any(t *testing.T) { |
| 29 |
should := require.New(t) |
| 30 |
any := jsoniter.Get([]byte("[1]")) |
| 31 |
should.Equal(1, any.Size()) |
| 32 |
} |
| 33 |
|
| 34 |
func Test_read_two_element_array_as_any(t *testing.T) { |
| 35 |
should := require.New(t) |
| 36 |
any := jsoniter.Get([]byte("[1,2]")) |
| 37 |
should.Equal(1, any.Get(0).ToInt()) |
| 38 |
should.Equal(2, any.Size()) |
| 39 |
should.True(any.ToBool()) |
| 40 |
should.Equal(1, any.ToInt()) |
| 41 |
should.Equal([]interface{}{float64(1), float64(2)}, any.GetInterface()) |
| 42 |
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32) |
| 43 |
any.WriteTo(stream) |
| 44 |
should.Equal("[1,2]", string(stream.Buffer())) |
| 45 |
arr := []int{} |
| 46 |
any.ToVal(&arr) |
| 47 |
should.Equal([]int{1, 2}, arr) |
| 48 |
} |
| 49 |
|
| 50 |
func Test_wrap_array_and_convert_to_any(t *testing.T) { |
| 51 |
should := require.New(t) |
| 52 |
any := jsoniter.Wrap([]int{1, 2, 3}) |
| 53 |
any2 := jsoniter.Wrap([]int{}) |
| 54 |
|
| 55 |
should.Equal("[1,2,3]", any.ToString()) |
| 56 |
should.True(any.ToBool()) |
| 57 |
should.False(any2.ToBool()) |
| 58 |
|
| 59 |
should.Equal(1, any.ToInt()) |
| 60 |
should.Equal(0, any2.ToInt()) |
| 61 |
should.Equal(int32(1), any.ToInt32()) |
| 62 |
should.Equal(int32(0), any2.ToInt32()) |
| 63 |
should.Equal(int64(1), any.ToInt64()) |
| 64 |
should.Equal(int64(0), any2.ToInt64()) |
| 65 |
should.Equal(uint(1), any.ToUint()) |
| 66 |
should.Equal(uint(0), any2.ToUint()) |
| 67 |
should.Equal(uint32(1), any.ToUint32()) |
| 68 |
should.Equal(uint32(0), any2.ToUint32()) |
| 69 |
should.Equal(uint64(1), any.ToUint64()) |
| 70 |
should.Equal(uint64(0), any2.ToUint64()) |
| 71 |
should.Equal(float32(1), any.ToFloat32()) |
| 72 |
should.Equal(float32(0), any2.ToFloat32()) |
| 73 |
should.Equal(float64(1), any.ToFloat64()) |
| 74 |
should.Equal(float64(0), any2.ToFloat64()) |
| 75 |
should.Equal(3, any.Size()) |
| 76 |
should.Equal(0, any2.Size()) |
| 77 |
|
| 78 |
var i interface{} = []int{1, 2, 3} |
| 79 |
should.Equal(i, any.GetInterface()) |
| 80 |
} |
| 81 |
|
| 82 |
func Test_array_lazy_any_get(t *testing.T) { |
| 83 |
should := require.New(t) |
| 84 |
any := jsoniter.Get([]byte("[1,[2,3],4]")) |
| 85 |
should.Equal(3, any.Get(1, 1).ToInt()) |
| 86 |
should.Equal("[1,[2,3],4]", any.ToString()) |
| 87 |
} |
| 88 |
|
| 89 |
func Test_array_lazy_any_get_all(t *testing.T) { |
| 90 |
should := require.New(t) |
| 91 |
any := jsoniter.Get([]byte("[[1],[2],[3,4]]")) |
| 92 |
should.Equal("[1,2,3]", any.Get('*', 0).ToString()) |
| 93 |
any = jsoniter.Get([]byte("[[[1],[2],[3,4]]]"), 0, '*', 0) |
| 94 |
should.Equal("[1,2,3]", any.ToString()) |
| 95 |
} |
| 96 |
|
| 97 |
func Test_array_wrapper_any_get_all(t *testing.T) { |
| 98 |
should := require.New(t) |
| 99 |
any := jsoniter.Wrap([][]int{ |
| 100 |
{1, 2}, |
| 101 |
{3, 4}, |
| 102 |
{5, 6}, |
| 103 |
}) |
| 104 |
should.Equal("[1,3,5]", any.Get('*', 0).ToString()) |
| 105 |
should.Equal(jsoniter.ArrayValue, any.ValueType()) |
| 106 |
should.True(any.ToBool()) |
| 107 |
should.Equal(1, any.Get(0, 0).ToInt()) |
| 108 |
} |
| 109 |
|
| 110 |
func Test_array_lazy_any_get_invalid(t *testing.T) { |
| 111 |
should := require.New(t) |
| 112 |
any := jsoniter.Get([]byte("[]")) |
| 113 |
should.Equal(jsoniter.InvalidValue, any.Get(1, 1).ValueType()) |
| 114 |
should.NotNil(any.Get(1, 1).LastError()) |
| 115 |
should.Equal(jsoniter.InvalidValue, any.Get("1").ValueType()) |
| 116 |
should.NotNil(any.Get("1").LastError()) |
| 117 |
} |
| 118 |
|
| 119 |
func Test_invalid_array(t *testing.T) { |
| 120 |
should := require.New(t) |
| 121 |
any := jsoniter.Get([]byte("["), 0) |
| 122 |
should.Equal(jsoniter.InvalidValue, any.ValueType()) |
| 123 |
} |