1
0
mirror of https://github.com/json-iterator/go.git synced 2025-07-03 23:30:41 +02:00
Files
any_tests
jsoniter_any_array_test.go
jsoniter_any_bool_test.go
jsoniter_any_float_test.go
jsoniter_any_int_test.go
jsoniter_any_map_test.go
jsoniter_any_null_test.go
jsoniter_any_object_test.go
jsoniter_any_string_test.go
jsoniter_must_be_valid_test.go
jsoniter_wrap_test.go
api_tests
benchmarks
extension_tests
extra
misc_tests
skip_tests
type_tests
value_tests
.codecov.yml
.gitignore
.travis.yml
Gopkg.lock
Gopkg.toml
LICENSE
README.md
adapter.go
any.go
any_array.go
any_bool.go
any_float.go
any_int32.go
any_int64.go
any_invalid.go
any_nil.go
any_number.go
any_object.go
any_str.go
any_uint32.go
any_uint64.go
build.sh
config.go
config_with_sync_map.go
config_without_sync_map.go
example_test.go
fuzzy_mode_convert_table.md
iter.go
iter_array.go
iter_float.go
iter_int.go
iter_object.go
iter_skip.go
iter_skip_sloppy.go
iter_skip_sloppy_test.go
iter_skip_strict.go
iter_str.go
jsoniter.go
pool.go
reflect.go
reflect_array.go
reflect_dynamic.go
reflect_extension.go
reflect_json_number.go
reflect_json_raw_message.go
reflect_map.go
reflect_marshaler.go
reflect_native.go
reflect_optional.go
reflect_slice.go
reflect_struct_decoder.go
reflect_struct_encoder.go
stream.go
stream_float.go
stream_int.go
stream_str.go
stream_test.go
test.sh
json-iterator/any_tests/jsoniter_wrap_test.go

120 lines
3.4 KiB
Go
Raw Normal View History

2018-02-14 10:13:34 +08:00
package any_tests
2017-07-05 13:55:10 +08:00
import (
"testing"
"github.com/stretchr/testify/require"
2018-02-14 10:13:34 +08:00
"github.com/json-iterator/go"
2017-07-05 13:55:10 +08:00
)
func Test_wrap_and_valuetype_everything(t *testing.T) {
should := require.New(t)
var i interface{}
2018-02-14 10:13:34 +08:00
any := jsoniter.Get([]byte("123"))
2017-07-05 13:55:10 +08:00
// default of number type is float64
i = float64(123)
should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(int8(10))
should.Equal(any.ValueType(), jsoniter.NumberValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
// get interface is not int8 interface
// i = int8(10)
// should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(int16(10))
should.Equal(any.ValueType(), jsoniter.NumberValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
//i = int16(10)
//should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(int32(10))
should.Equal(any.ValueType(), jsoniter.NumberValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
i = int32(10)
should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(int64(10))
should.Equal(any.ValueType(), jsoniter.NumberValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
i = int64(10)
should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(uint(10))
should.Equal(any.ValueType(), jsoniter.NumberValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
// not equal
//i = uint(10)
//should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(uint8(10))
should.Equal(any.ValueType(), jsoniter.NumberValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
// not equal
// i = uint8(10)
// should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(uint16(10))
should.Equal(any.ValueType(), jsoniter.NumberValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(uint32(10))
should.Equal(any.ValueType(), jsoniter.NumberValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
i = uint32(10)
should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(uint64(10))
should.Equal(any.ValueType(), jsoniter.NumberValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
i = uint64(10)
should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(float32(10))
should.Equal(any.ValueType(), jsoniter.NumberValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
// not equal
//i = float32(10)
//should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(float64(10))
should.Equal(any.ValueType(), jsoniter.NumberValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
i = float64(10)
should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(true)
should.Equal(any.ValueType(), jsoniter.BoolValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
i = true
should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(false)
should.Equal(any.ValueType(), jsoniter.BoolValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
i = false
should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(nil)
should.Equal(any.ValueType(), jsoniter.NilValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
i = nil
should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
2017-07-05 13:55:10 +08:00
any.WriteTo(stream)
should.Equal("null", string(stream.Buffer()))
should.Equal(any.LastError(), nil)
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(struct{ age int }{age: 1})
should.Equal(any.ValueType(), jsoniter.ObjectValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
i = struct{ age int }{age: 1}
should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap(map[string]interface{}{"abc": 1})
should.Equal(any.ValueType(), jsoniter.ObjectValue)
2017-07-05 13:55:10 +08:00
should.Equal(any.LastError(), nil)
i = map[string]interface{}{"abc": 1}
should.Equal(i, any.GetInterface())
2018-02-14 10:13:34 +08:00
any = jsoniter.Wrap("abc")
2017-07-05 13:55:10 +08:00
i = "abc"
should.Equal(i, any.GetInterface())
should.Equal(nil, any.LastError())
}