jsoniter_invalid_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "github.com/stretchr/testify/require"
  5. "io"
  6. "testing"
  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(InvalidValue, 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(InvalidValue, 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_invalid_float(t *testing.T) {
  62. inputs := []string{
  63. `1.e1`, // dot without following digit
  64. `1.`, // dot can not be the last char
  65. ``, // empty number
  66. `01`, // extra leading zero
  67. `-`, // negative without digit
  68. `--`, // double negative
  69. `--2`, // double negative
  70. }
  71. for _, input := range inputs {
  72. t.Run(input, func(t *testing.T) {
  73. should := require.New(t)
  74. iter := ParseString(ConfigDefault, input+",")
  75. iter.Skip()
  76. should.NotEqual(io.EOF, iter.Error)
  77. should.NotNil(iter.Error)
  78. v := float64(0)
  79. should.NotNil(json.Unmarshal([]byte(input), &v))
  80. iter = ParseString(ConfigDefault, input+",")
  81. iter.ReadFloat64()
  82. should.NotEqual(io.EOF, iter.Error)
  83. should.NotNil(iter.Error)
  84. iter = ParseString(ConfigDefault, input+",")
  85. iter.ReadFloat32()
  86. should.NotEqual(io.EOF, iter.Error)
  87. should.NotNil(iter.Error)
  88. })
  89. }
  90. }
  91. func Test_chan(t *testing.T) {
  92. t.Skip("do not support chan")
  93. type TestObject struct {
  94. MyChan chan bool
  95. MyField int
  96. }
  97. should := require.New(t)
  98. obj := TestObject{}
  99. str, err := json.Marshal(obj)
  100. should.Nil(err)
  101. should.Equal(``, str)
  102. }