| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package jsoniter
- import (
- "testing"
- "github.com/json-iterator/go/require"
- "io"
- )
- func Test_read_empty_array_as_any(t *testing.T) {
- should := require.New(t)
- any, err := UnmarshalAnyFromString("[]")
- should.Nil(err)
- should.Equal(0, any.Size())
- }
- func Test_read_one_element_array_as_any(t *testing.T) {
- should := require.New(t)
- any, err := UnmarshalAnyFromString("[1]")
- should.Nil(err)
- should.Equal(1, any.Size())
- }
- func Test_read_two_element_array_as_any(t *testing.T) {
- should := require.New(t)
- any, err := UnmarshalAnyFromString("[1,2]")
- should.Nil(err)
- should.Equal(1, any.Get(0).ToInt())
- should.Equal(2, any.Size())
- should.True(any.ToBool())
- should.Equal(1, any.ToInt())
- }
- func Test_read_array_with_any_iterator(t *testing.T) {
- should := require.New(t)
- any, err := UnmarshalAnyFromString("[1,2]")
- should.Nil(err)
- var element Any
- var elements []int
- for next, hasNext := any.IterateArray(); hasNext; {
- element, hasNext = next()
- elements = append(elements, element.ToInt())
- }
- should.Equal([]int{1, 2}, elements)
- }
- func Test_wrap_array(t *testing.T) {
- should := require.New(t)
- any := Wrap([]int{1, 2, 3})
- should.Equal("[1,2,3]", any.ToString())
- var element Any
- var elements []int
- for next, hasNext := any.IterateArray(); hasNext; {
- element, hasNext = next()
- elements = append(elements, element.ToInt())
- }
- should.Equal([]int{1, 2, 3}, elements)
- any = Wrap([]int{1, 2, 3})
- should.Equal(3, any.Size())
- any = Wrap([]int{1, 2, 3})
- should.Equal(2, any.Get(1).ToInt())
- }
- func Test_array_lazy_any_get(t *testing.T) {
- should := require.New(t)
- any, err := UnmarshalAnyFromString("[1,[2,3],4]")
- should.Nil(err)
- should.Equal(3, any.Get(1, 1).ToInt())
- should.Equal("[1,[2,3],4]", any.ToString())
- }
- func Test_array_lazy_any_get_all(t *testing.T) {
- should := require.New(t)
- any, err := UnmarshalAnyFromString("[[1],[2],[3,4]]")
- should.Nil(err)
- should.Equal("[1,2,3]", any.Get('*', 0).ToString())
- }
- func Test_array_wrapper_any_get_all(t *testing.T) {
- should := require.New(t)
- any := wrapArray([][]int{
- {1, 2},
- {3, 4},
- {5, 6},
- })
- should.Equal("[1,3,5]", any.Get('*', 0).ToString())
- }
- func Test_array_lazy_any_get_invalid(t *testing.T) {
- should := require.New(t)
- any, err := UnmarshalAnyFromString("[]")
- should.Nil(err)
- should.Equal(Invalid, any.Get(1, 1).ValueType())
- should.NotNil(any.Get(1, 1).LastError())
- should.Equal(Invalid, any.Get("1").ValueType())
- should.NotNil(any.Get("1").LastError())
- }
- func Test_array_lazy_any_set(t *testing.T) {
- should := require.New(t)
- any, err := UnmarshalAnyFromString("[1,[2,3],4]")
- should.Nil(err)
- any.GetArray()[0] = WrapInt64(2)
- str, err := MarshalToString(any)
- should.Nil(err)
- should.Equal("[2,[2,3],4]", str)
- }
- func Test_invalid_array(t *testing.T) {
- _, err := UnmarshalAnyFromString("[")
- if err == nil || err == io.EOF {
- t.FailNow()
- }
- }
|