jsoniter_invalid_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package jsoniter
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "testing"
  5. "encoding/json"
  6. )
  7. func Test_missing_object_end(t *testing.T) {
  8. should := require.New(t)
  9. type TestObject struct {
  10. Metric string `json:"metric"`
  11. Tags map[string]interface{} `json:"tags"`
  12. }
  13. obj := TestObject{}
  14. should.NotNil(UnmarshalFromString(`{"metric": "sys.777","tags": {"a":"123"}`, &obj))
  15. }
  16. func Test_missing_array_end(t *testing.T) {
  17. should := require.New(t)
  18. should.NotNil(UnmarshalFromString(`[1,2,3`, &[]int{}))
  19. }
  20. func Test_invalid_any(t *testing.T) {
  21. should := require.New(t)
  22. any := Get([]byte("[]"))
  23. should.Equal(Invalid, any.Get(0.3).ValueType())
  24. // is nil correct ?
  25. should.Equal(nil, any.Get(0.3).GetInterface())
  26. any = any.Get(0.3)
  27. should.Equal(false, any.ToBool())
  28. should.Equal(int(0), any.ToInt())
  29. should.Equal(int32(0), any.ToInt32())
  30. should.Equal(int64(0), any.ToInt64())
  31. should.Equal(uint(0), any.ToUint())
  32. should.Equal(uint32(0), any.ToUint32())
  33. should.Equal(uint64(0), any.ToUint64())
  34. should.Equal(float32(0), any.ToFloat32())
  35. should.Equal(float64(0), any.ToFloat64())
  36. should.Equal("", any.ToString())
  37. should.Equal(Invalid, any.Get(0.1).Get(1).ValueType())
  38. }
  39. func Test_invalid_struct_input(t *testing.T) {
  40. should := require.New(t)
  41. type TestObject struct{}
  42. input := []byte{54, 141, 30}
  43. obj := TestObject{}
  44. should.NotNil(Unmarshal(input, &obj))
  45. }
  46. func Test_invalid_slice_input(t *testing.T) {
  47. should := require.New(t)
  48. type TestObject struct{}
  49. input := []byte{93}
  50. obj := []string{}
  51. should.NotNil(Unmarshal(input, &obj))
  52. }
  53. func Test_invalid_array_input(t *testing.T) {
  54. should := require.New(t)
  55. type TestObject struct{}
  56. input := []byte{93}
  57. obj := [0]string{}
  58. should.NotNil(Unmarshal(input, &obj))
  59. }
  60. func Test_double_negative(t *testing.T) {
  61. should := require.New(t)
  62. var v interface{}
  63. should.NotNil(json.Unmarshal([]byte(`--2`), &v))
  64. var vFloat64 float64
  65. should.NotNil(UnmarshalFromString(`--2`, &vFloat64))
  66. var vFloat32 float32
  67. should.NotNil(UnmarshalFromString(`--2`, &vFloat32))
  68. var vInt int
  69. should.NotNil(UnmarshalFromString(`--2`, &vInt))
  70. }