2017-06-17 21:32:48 +08:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
2017-06-19 23:43:53 +08:00
|
|
|
"testing"
|
2017-07-03 19:40:12 +08:00
|
|
|
|
2017-07-07 09:13:25 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-06-17 21:32:48 +08:00
|
|
|
)
|
|
|
|
|
2017-07-05 00:39:20 +08:00
|
|
|
var stringConvertMap = map[string]string{
|
2017-07-18 09:45:25 +08:00
|
|
|
"null": "",
|
|
|
|
"321.1": "321.1",
|
|
|
|
`"1.1"`: "1.1",
|
|
|
|
`"-123.1"`: "-123.1",
|
|
|
|
"0.0": "0.0",
|
|
|
|
"0": "0",
|
|
|
|
`"0"`: "0",
|
|
|
|
`"0.0"`: "0.0",
|
|
|
|
`"00.0"`: "00.0",
|
|
|
|
"true": "true",
|
|
|
|
"false": "false",
|
|
|
|
`"true"`: "true",
|
|
|
|
`"false"`: "false",
|
|
|
|
`"true123"`: "true123",
|
|
|
|
`"+1"`: "+1",
|
|
|
|
"[]": "[]",
|
|
|
|
"[1,2]": "[1,2]",
|
|
|
|
"{}": "{}",
|
2017-07-09 14:48:34 +08:00
|
|
|
`{"a":1, "stream":true}`: `{"a":1, "stream":true}`,
|
2017-07-05 00:39:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_read_any_to_string(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
for k, v := range stringConvertMap {
|
|
|
|
any := Get([]byte(k))
|
|
|
|
should.Equal(v, any.ToString(), "original val "+k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 21:32:48 +08:00
|
|
|
func Test_read_string_as_any(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2017-06-18 22:21:54 +08:00
|
|
|
any := Get([]byte(`"hello"`))
|
2017-06-17 21:32:48 +08:00
|
|
|
should.Equal("hello", any.ToString())
|
|
|
|
should.True(any.ToBool())
|
2017-06-18 22:21:54 +08:00
|
|
|
any = Get([]byte(`" "`))
|
2017-06-17 21:32:48 +08:00
|
|
|
should.False(any.ToBool())
|
2017-06-18 22:21:54 +08:00
|
|
|
any = Get([]byte(`"false"`))
|
2017-07-03 19:40:12 +08:00
|
|
|
should.True(any.ToBool())
|
2017-06-18 22:21:54 +08:00
|
|
|
any = Get([]byte(`"123"`))
|
2017-06-17 21:32:48 +08:00
|
|
|
should.Equal(123, any.ToInt())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_wrap_string(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2018-01-27 16:25:48 +08:00
|
|
|
any := Get([]byte("-32000")).MustBeValid()
|
|
|
|
should.Equal(-32000, any.ToInt())
|
|
|
|
should.NoError(any.LastError())
|
2017-06-19 23:43:53 +08:00
|
|
|
}
|