github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/jsoni/any_tests/jsoniter_any_array_test.go (about)

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