jsoniter_invalid_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package jsoniter
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "testing"
  5. "encoding/json"
  6. "io"
  7. )
  8. func Test_missing_object_end(t *testing.T) {
  9. should := require.New(t)
  10. type TestObject struct {
  11. Metric string `json:"metric"`
  12. Tags map[string]interface{} `json:"tags"`
  13. }
  14. obj := TestObject{}
  15. should.NotNil(UnmarshalFromString(`{"metric": "sys.777","tags": {"a":"123"}`, &obj))
  16. }
  17. func Test_missing_array_end(t *testing.T) {
  18. should := require.New(t)
  19. should.NotNil(UnmarshalFromString(`[1,2,3`, &[]int{}))
  20. }
  21. func Test_invalid_any(t *testing.T) {
  22. should := require.New(t)
  23. any := Get([]byte("[]"))
  24. should.Equal(Invalid, any.Get(0.3).ValueType())
  25. // is nil correct ?
  26. should.Equal(nil, any.Get(0.3).GetInterface())
  27. any = any.Get(0.3)
  28. should.Equal(false, any.ToBool())
  29. should.Equal(int(0), any.ToInt())
  30. should.Equal(int32(0), any.ToInt32())
  31. should.Equal(int64(0), any.ToInt64())
  32. should.Equal(uint(0), any.ToUint())
  33. should.Equal(uint32(0), any.ToUint32())
  34. should.Equal(uint64(0), any.ToUint64())
  35. should.Equal(float32(0), any.ToFloat32())
  36. should.Equal(float64(0), any.ToFloat64())
  37. should.Equal("", any.ToString())
  38. should.Equal(Invalid, any.Get(0.1).Get(1).ValueType())
  39. }
  40. func Test_invalid_struct_input(t *testing.T) {
  41. should := require.New(t)
  42. type TestObject struct{}
  43. input := []byte{54, 141, 30}
  44. obj := TestObject{}
  45. should.NotNil(Unmarshal(input, &obj))
  46. }
  47. func Test_invalid_slice_input(t *testing.T) {
  48. should := require.New(t)
  49. type TestObject struct{}
  50. input := []byte{93}
  51. obj := []string{}
  52. should.NotNil(Unmarshal(input, &obj))
  53. }
  54. func Test_invalid_array_input(t *testing.T) {
  55. should := require.New(t)
  56. type TestObject struct{}
  57. input := []byte{93}
  58. obj := [0]string{}
  59. should.NotNil(Unmarshal(input, &obj))
  60. }
  61. func Test_double_negative(t *testing.T) {
  62. should := require.New(t)
  63. var v interface{}
  64. should.NotNil(json.Unmarshal([]byte(`--2`), &v))
  65. var vFloat64 float64
  66. should.NotNil(UnmarshalFromString(`--2`, &vFloat64))
  67. var vFloat32 float32
  68. should.NotNil(UnmarshalFromString(`--2`, &vFloat32))
  69. var vInt int
  70. should.NotNil(UnmarshalFromString(`--2`, &vInt))
  71. iter := ParseString(ConfigDefault, `--2`)
  72. iter.Skip()
  73. should.NotEqual(io.EOF, iter.Error)
  74. should.NotNil(iter.Error)
  75. }
  76. func Test_leading_zero(t *testing.T) {
  77. should := require.New(t)
  78. var v interface{}
  79. should.NotNil(json.Unmarshal([]byte(`01`), &v))
  80. var vFloat64 float64
  81. should.NotNil(UnmarshalFromString(`01`, &vFloat64))
  82. var vFloat32 float32
  83. should.NotNil(UnmarshalFromString(`01`, &vFloat32))
  84. var vInt int
  85. should.NotNil(UnmarshalFromString(`01`, &vInt))
  86. iter := ParseString(ConfigDefault, `01,`)
  87. iter.Skip()
  88. should.NotEqual(io.EOF, iter.Error)
  89. should.NotNil(iter.Error)
  90. }
  91. func Test_empty_as_number(t *testing.T) {
  92. should := require.New(t)
  93. iter := ParseString(ConfigDefault, `,`)
  94. iter.ReadFloat64()
  95. should.NotEqual(io.EOF, iter.Error)
  96. should.NotNil(iter.Error)
  97. iter = ParseString(ConfigDefault, `,`)
  98. iter.ReadFloat32()
  99. should.NotEqual(io.EOF, iter.Error)
  100. should.NotNil(iter.Error)
  101. }