File: jsoniter_any_array_test.go

package info (click to toggle)
golang-github-json-iterator-go 1.1.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie, trixie-proposed-updates
  • size: 916 kB
  • sloc: sh: 19; makefile: 3
file content (123 lines) | stat: -rw-r--r-- 3,745 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package any_tests

import (
	"testing"

	"github.com/json-iterator/go"
	"github.com/stretchr/testify/require"
)

func Test_read_empty_array_as_any(t *testing.T) {
	should := require.New(t)
	any := jsoniter.Get([]byte("[]"))
	should.Equal(jsoniter.ArrayValue, any.Get().ValueType())
	should.Equal(jsoniter.InvalidValue, any.Get(0.3).ValueType())
	should.Equal(0, any.Size())
	should.Equal(jsoniter.ArrayValue, any.ValueType())
	should.Nil(any.LastError())
	should.Equal(0, any.ToInt())
	should.Equal(int32(0), any.ToInt32())
	should.Equal(int64(0), any.ToInt64())
	should.Equal(uint(0), any.ToUint())
	should.Equal(uint32(0), any.ToUint32())
	should.Equal(uint64(0), any.ToUint64())
	should.Equal(float32(0), any.ToFloat32())
	should.Equal(float64(0), any.ToFloat64())
}

func Test_read_one_element_array_as_any(t *testing.T) {
	should := require.New(t)
	any := jsoniter.Get([]byte("[1]"))
	should.Equal(1, any.Size())
}

func Test_read_two_element_array_as_any(t *testing.T) {
	should := require.New(t)
	any := jsoniter.Get([]byte("[1,2]"))
	should.Equal(1, any.Get(0).ToInt())
	should.Equal(2, any.Size())
	should.True(any.ToBool())
	should.Equal(1, any.ToInt())
	should.Equal([]interface{}{float64(1), float64(2)}, any.GetInterface())
	stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
	any.WriteTo(stream)
	should.Equal("[1,2]", string(stream.Buffer()))
	arr := []int{}
	any.ToVal(&arr)
	should.Equal([]int{1, 2}, arr)
}

func Test_wrap_array_and_convert_to_any(t *testing.T) {
	should := require.New(t)
	any := jsoniter.Wrap([]int{1, 2, 3})
	any2 := jsoniter.Wrap([]int{})

	should.Equal("[1,2,3]", any.ToString())
	should.True(any.ToBool())
	should.False(any2.ToBool())

	should.Equal(1, any.ToInt())
	should.Equal(0, any2.ToInt())
	should.Equal(int32(1), any.ToInt32())
	should.Equal(int32(0), any2.ToInt32())
	should.Equal(int64(1), any.ToInt64())
	should.Equal(int64(0), any2.ToInt64())
	should.Equal(uint(1), any.ToUint())
	should.Equal(uint(0), any2.ToUint())
	should.Equal(uint32(1), any.ToUint32())
	should.Equal(uint32(0), any2.ToUint32())
	should.Equal(uint64(1), any.ToUint64())
	should.Equal(uint64(0), any2.ToUint64())
	should.Equal(float32(1), any.ToFloat32())
	should.Equal(float32(0), any2.ToFloat32())
	should.Equal(float64(1), any.ToFloat64())
	should.Equal(float64(0), any2.ToFloat64())
	should.Equal(3, any.Size())
	should.Equal(0, any2.Size())

	var i interface{} = []int{1, 2, 3}
	should.Equal(i, any.GetInterface())
}

func Test_array_lazy_any_get(t *testing.T) {
	should := require.New(t)
	any := jsoniter.Get([]byte("[1,[2,3],4]"))
	should.Equal(3, any.Get(1, 1).ToInt())
	should.Equal("[1,[2,3],4]", any.ToString())
}

func Test_array_lazy_any_get_all(t *testing.T) {
	should := require.New(t)
	any := jsoniter.Get([]byte("[[1],[2],[3,4]]"))
	should.Equal("[1,2,3]", any.Get('*', 0).ToString())
	any = jsoniter.Get([]byte("[[[1],[2],[3,4]]]"), 0, '*', 0)
	should.Equal("[1,2,3]", any.ToString())
}

func Test_array_wrapper_any_get_all(t *testing.T) {
	should := require.New(t)
	any := jsoniter.Wrap([][]int{
		{1, 2},
		{3, 4},
		{5, 6},
	})
	should.Equal("[1,3,5]", any.Get('*', 0).ToString())
	should.Equal(jsoniter.ArrayValue, any.ValueType())
	should.True(any.ToBool())
	should.Equal(1, any.Get(0, 0).ToInt())
}

func Test_array_lazy_any_get_invalid(t *testing.T) {
	should := require.New(t)
	any := jsoniter.Get([]byte("[]"))
	should.Equal(jsoniter.InvalidValue, any.Get(1, 1).ValueType())
	should.NotNil(any.Get(1, 1).LastError())
	should.Equal(jsoniter.InvalidValue, any.Get("1").ValueType())
	should.NotNil(any.Get("1").LastError())
}

func Test_invalid_array(t *testing.T) {
	should := require.New(t)
	any := jsoniter.Get([]byte("["), 0)
	should.Equal(jsoniter.InvalidValue, any.ValueType())
}