jsoniter_any_array_test.go 3.7 KB

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