jsoniter_any_array_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package jsoniter
  2. import (
  3. "testing"
  4. "github.com/json-iterator/go/require"
  5. "io"
  6. )
  7. func Test_read_empty_array_as_any(t *testing.T) {
  8. should := require.New(t)
  9. any, err := UnmarshalAnyFromString("[]")
  10. should.Nil(err)
  11. should.Equal(0, any.Size())
  12. }
  13. func Test_read_one_element_array_as_any(t *testing.T) {
  14. should := require.New(t)
  15. any, err := UnmarshalAnyFromString("[1]")
  16. should.Nil(err)
  17. should.Equal(1, any.Size())
  18. }
  19. func Test_read_two_element_array_as_any(t *testing.T) {
  20. should := require.New(t)
  21. any, err := UnmarshalAnyFromString("[1,2]")
  22. should.Nil(err)
  23. should.Equal(1, any.Get(0).ToInt())
  24. should.Equal(2, any.Size())
  25. should.True(any.ToBool())
  26. should.Equal(1, any.ToInt())
  27. }
  28. func Test_read_array_with_any_iterator(t *testing.T) {
  29. should := require.New(t)
  30. any, err := UnmarshalAnyFromString("[1,2]")
  31. should.Nil(err)
  32. var element Any
  33. var elements []int
  34. for next, hasNext := any.IterateArray(); hasNext; {
  35. element, hasNext = next()
  36. elements = append(elements, element.ToInt())
  37. }
  38. should.Equal([]int{1, 2}, elements)
  39. }
  40. func Test_wrap_array(t *testing.T) {
  41. should := require.New(t)
  42. any := Wrap([]int{1, 2, 3})
  43. should.Equal("[1,2,3]", any.ToString())
  44. var element Any
  45. var elements []int
  46. for next, hasNext := any.IterateArray(); hasNext; {
  47. element, hasNext = next()
  48. elements = append(elements, element.ToInt())
  49. }
  50. should.Equal([]int{1, 2, 3}, elements)
  51. any = Wrap([]int{1, 2, 3})
  52. should.Equal(3, any.Size())
  53. any = Wrap([]int{1, 2, 3})
  54. should.Equal(2, any.Get(1).ToInt())
  55. }
  56. func Test_array_lazy_any_get(t *testing.T) {
  57. should := require.New(t)
  58. any, err := UnmarshalAnyFromString("[1,[2,3],4]")
  59. should.Nil(err)
  60. should.Equal(3, any.Get(1, 1).ToInt())
  61. should.Equal("[1,[2,3],4]", any.ToString())
  62. }
  63. func Test_array_lazy_any_get_all(t *testing.T) {
  64. should := require.New(t)
  65. any, err := UnmarshalAnyFromString("[[1],[2],[3,4]]")
  66. should.Nil(err)
  67. should.Equal("[1,2,3]", any.Get('*', 0).ToString())
  68. }
  69. func Test_array_wrapper_any_get_all(t *testing.T) {
  70. should := require.New(t)
  71. any := wrapArray([][]int{
  72. {1, 2},
  73. {3, 4},
  74. {5, 6},
  75. })
  76. should.Equal("[1,3,5]", any.Get('*', 0).ToString())
  77. }
  78. func Test_array_lazy_any_get_invalid(t *testing.T) {
  79. should := require.New(t)
  80. any, err := UnmarshalAnyFromString("[]")
  81. should.Nil(err)
  82. should.Equal(Invalid, any.Get(1, 1).ValueType())
  83. should.NotNil(any.Get(1, 1).LastError())
  84. should.Equal(Invalid, any.Get("1").ValueType())
  85. should.NotNil(any.Get("1").LastError())
  86. }
  87. func Test_array_lazy_any_set(t *testing.T) {
  88. should := require.New(t)
  89. any, err := UnmarshalAnyFromString("[1,[2,3],4]")
  90. should.Nil(err)
  91. any.GetArray()[0] = WrapInt64(2)
  92. str, err := MarshalToString(any)
  93. should.Nil(err)
  94. should.Equal("[2,[2,3],4]", str)
  95. }
  96. func Test_invalid_array(t *testing.T) {
  97. _, err := UnmarshalAnyFromString("[")
  98. if err == nil || err == io.EOF {
  99. t.FailNow()
  100. }
  101. }