jsoniter_any_array_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. }
  57. func Test_array_wrapper_any_get_all(t *testing.T) {
  58. should := require.New(t)
  59. any := wrapArray([][]int{
  60. {1, 2},
  61. {3, 4},
  62. {5, 6},
  63. })
  64. should.Equal("[1,3,5]", any.Get('*', 0).ToString())
  65. }
  66. func Test_array_lazy_any_get_invalid(t *testing.T) {
  67. should := require.New(t)
  68. any := Get([]byte("[]"))
  69. should.Equal(Invalid, any.Get(1, 1).ValueType())
  70. should.NotNil(any.Get(1, 1).LastError())
  71. should.Equal(Invalid, any.Get("1").ValueType())
  72. should.NotNil(any.Get("1").LastError())
  73. }
  74. func Test_invalid_array(t *testing.T) {
  75. should := require.New(t)
  76. any := Get([]byte("["), 0)
  77. should.Equal(Invalid, any.ValueType())
  78. }