jsoniter_invalid_test.go 1.7 KB

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