1 package any_tests
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/json-iterator/go"
8 "github.com/stretchr/testify/require"
9 )
10
11 var intConvertMap = map[string]int{
12 "null": 0,
13 "321.1": 321,
14 "-321.1": -321,
15 `"1.1"`: 1,
16 `"-321.1"`: -321,
17 "0.0": 0,
18 "0": 0,
19 `"0"`: 0,
20 `"0.0"`: 0,
21 "-1.1": -1,
22 "true": 1,
23 "false": 0,
24 `"true"`: 0,
25 `"false"`: 0,
26 `"true123"`: 0,
27 `"123true"`: 123,
28 `"-123true"`: -123,
29 `"1.2332e6"`: 1,
30 `""`: 0,
31 "+": 0,
32 "-": 0,
33 "[]": 0,
34 "[1,2]": 1,
35 `["1","2"]`: 1,
36
37 "{}": 0,
38 }
39
40 func Test_read_any_to_int(t *testing.T) {
41 should := require.New(t)
42
43
44 for k, v := range intConvertMap {
45 any := jsoniter.Get([]byte(k))
46 should.Equal(v, any.ToInt(), fmt.Sprintf("origin val %v", k))
47 }
48
49
50 for k, v := range intConvertMap {
51 any := jsoniter.Get([]byte(k))
52 should.Equal(int32(v), any.ToInt32(), fmt.Sprintf("original val is %v", k))
53 }
54
55
56 for k, v := range intConvertMap {
57 any := jsoniter.Get([]byte(k))
58 should.Equal(int64(v), any.ToInt64(), fmt.Sprintf("original val is %v", k))
59 }
60
61 }
62
63 var uintConvertMap = map[string]int{
64 "null": 0,
65 "321.1": 321,
66 `"1.1"`: 1,
67 `"-123.1"`: 0,
68 "0.0": 0,
69 "0": 0,
70 `"0"`: 0,
71 `"0.0"`: 0,
72 `"00.0"`: 0,
73 "true": 1,
74 "false": 0,
75 `"true"`: 0,
76 `"false"`: 0,
77 `"true123"`: 0,
78 `"+1"`: 1,
79 `"123true"`: 123,
80 `"-123true"`: 0,
81 `"1.2332e6"`: 1,
82 `""`: 0,
83 "+": 0,
84 "-": 0,
85 ".": 0,
86 "[]": 0,
87 "[1,2]": 1,
88 "{}": 0,
89 "{1,2}": 0,
90 "-1.1": 0,
91 "-321.1": 0,
92 }
93
94 func Test_read_any_to_uint(t *testing.T) {
95 should := require.New(t)
96
97 for k, v := range uintConvertMap {
98 any := jsoniter.Get([]byte(k))
99 should.Equal(uint64(v), any.ToUint64(), fmt.Sprintf("origin val %v", k))
100 }
101
102 for k, v := range uintConvertMap {
103 any := jsoniter.Get([]byte(k))
104 should.Equal(uint32(v), any.ToUint32(), fmt.Sprintf("origin val %v", k))
105 }
106
107 for k, v := range uintConvertMap {
108 any := jsoniter.Get([]byte(k))
109 should.Equal(uint(v), any.ToUint(), fmt.Sprintf("origin val %v", k))
110 }
111
112 }
113
114 func Test_read_int64_to_any(t *testing.T) {
115 should := require.New(t)
116 any := jsoniter.WrapInt64(12345)
117 should.Equal(12345, any.ToInt())
118 should.Equal(int32(12345), any.ToInt32())
119 should.Equal(int64(12345), any.ToInt64())
120 should.Equal(uint(12345), any.ToUint())
121 should.Equal(uint32(12345), any.ToUint32())
122 should.Equal(uint64(12345), any.ToUint64())
123 should.Equal(float32(12345), any.ToFloat32())
124 should.Equal(float64(12345), any.ToFloat64())
125 should.Equal("12345", any.ToString())
126 should.Equal(true, any.ToBool())
127 should.Equal(any.ValueType(), jsoniter.NumberValue)
128 stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
129 any.WriteTo(stream)
130 should.Equal("12345", string(stream.Buffer()))
131 }
132 func Test_read_int32_to_any(t *testing.T) {
133 should := require.New(t)
134 any := jsoniter.WrapInt32(12345)
135 should.Equal(12345, any.ToInt())
136 should.Equal(int32(12345), any.ToInt32())
137 should.Equal(int64(12345), any.ToInt64())
138 should.Equal(uint(12345), any.ToUint())
139 should.Equal(uint32(12345), any.ToUint32())
140 should.Equal(uint64(12345), any.ToUint64())
141 should.Equal(float32(12345), any.ToFloat32())
142 should.Equal(float64(12345), any.ToFloat64())
143 should.Equal("12345", any.ToString())
144 should.Equal(true, any.ToBool())
145 should.Equal(any.ValueType(), jsoniter.NumberValue)
146 stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
147 any.WriteTo(stream)
148 should.Equal("12345", string(stream.Buffer()))
149 }
150
151 func Test_read_uint32_to_any(t *testing.T) {
152 should := require.New(t)
153 any := jsoniter.WrapUint32(12345)
154 should.Equal(12345, any.ToInt())
155 should.Equal(int32(12345), any.ToInt32())
156 should.Equal(int64(12345), any.ToInt64())
157 should.Equal(uint(12345), any.ToUint())
158 should.Equal(uint32(12345), any.ToUint32())
159 should.Equal(uint64(12345), any.ToUint64())
160 should.Equal(float32(12345), any.ToFloat32())
161 should.Equal(float64(12345), any.ToFloat64())
162 should.Equal("12345", any.ToString())
163 should.Equal(true, any.ToBool())
164 should.Equal(any.ValueType(), jsoniter.NumberValue)
165 stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
166 any.WriteTo(stream)
167 should.Equal("12345", string(stream.Buffer()))
168 }
169
170 func Test_read_uint64_to_any(t *testing.T) {
171 should := require.New(t)
172 any := jsoniter.WrapUint64(12345)
173 should.Equal(12345, any.ToInt())
174 should.Equal(int32(12345), any.ToInt32())
175 should.Equal(int64(12345), any.ToInt64())
176 should.Equal(uint(12345), any.ToUint())
177 should.Equal(uint32(12345), any.ToUint32())
178 should.Equal(uint64(12345), any.ToUint64())
179 should.Equal(float32(12345), any.ToFloat32())
180 should.Equal(float64(12345), any.ToFloat64())
181 should.Equal("12345", any.ToString())
182 should.Equal(true, any.ToBool())
183 should.Equal(any.ValueType(), jsoniter.NumberValue)
184 stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
185 any.WriteTo(stream)
186 should.Equal("12345", string(stream.Buffer()))
187 stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
188 stream.WriteUint(uint(123))
189 should.Equal("123", string(stream.Buffer()))
190 }
191
192 func Test_int_lazy_any_get(t *testing.T) {
193 should := require.New(t)
194 any := jsoniter.Get([]byte("1234"))
195
196
197 should.Equal(jsoniter.InvalidValue, any.Get(1, "2").ValueType())
198 }
199
View as plain text