jsoniter_any_array_test.go 3.5 KB

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