jsoniter_any_array_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package jsoniter
  2. import (
  3. "testing"
  4. "github.com/json-iterator/go/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(Array, any.Get().ValueType())
  10. should.Equal(Invalid, any.Get(0.3).ValueType())
  11. should.Equal(0, any.Size())
  12. should.Equal(Array, 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(1, any.GetArray()[0].ToInt())
  36. should.Equal([]interface{}{float64(1), float64(2)}, any.GetInterface())
  37. stream := NewStream(ConfigDefault, nil, 32)
  38. any.WriteTo(stream)
  39. should.Equal("[1,2]", string(stream.Buffer()))
  40. }
  41. func Test_wrap_array(t *testing.T) {
  42. should := require.New(t)
  43. any := Wrap([]int{1, 2, 3})
  44. should.Equal("[1,2,3]", any.ToString())
  45. }
  46. func Test_array_lazy_any_get(t *testing.T) {
  47. should := require.New(t)
  48. any := Get([]byte("[1,[2,3],4]"))
  49. should.Equal(3, any.Get(1, 1).ToInt())
  50. should.Equal("[1,[2,3],4]", any.ToString())
  51. }
  52. func Test_array_lazy_any_get_all(t *testing.T) {
  53. should := require.New(t)
  54. any := Get([]byte("[[1],[2],[3,4]]"))
  55. should.Equal("[1,2,3]", any.Get('*', 0).ToString())
  56. any = Get([]byte("[[[1],[2],[3,4]]]"), 0, '*', 0)
  57. should.Equal("[1,2,3]", any.ToString())
  58. }
  59. func Test_array_wrapper_any_get_all(t *testing.T) {
  60. should := require.New(t)
  61. any := wrapArray([][]int{
  62. {1, 2},
  63. {3, 4},
  64. {5, 6},
  65. })
  66. should.Equal("[1,3,5]", any.Get('*', 0).ToString())
  67. should.Equal(Array, any.ValueType())
  68. should.True(any.ToBool())
  69. should.Equal(1, any.Get(0, 0).ToInt())
  70. }
  71. func Test_array_lazy_any_get_invalid(t *testing.T) {
  72. should := require.New(t)
  73. any := Get([]byte("[]"))
  74. should.Equal(Invalid, any.Get(1, 1).ValueType())
  75. should.NotNil(any.Get(1, 1).LastError())
  76. should.Equal(Invalid, any.Get("1").ValueType())
  77. should.NotNil(any.Get("1").LastError())
  78. }
  79. func Test_invalid_array(t *testing.T) {
  80. should := require.New(t)
  81. any := Get([]byte("["), 0)
  82. should.Equal(Invalid, any.ValueType())
  83. }