jsoniter_invalid_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "github.com/stretchr/testify/require"
  5. "io"
  6. "testing"
  7. "bytes"
  8. )
  9. func Test_missing_object_end(t *testing.T) {
  10. should := require.New(t)
  11. type TestObject struct {
  12. Metric string `json:"metric"`
  13. Tags map[string]interface{} `json:"tags"`
  14. }
  15. obj := TestObject{}
  16. should.NotNil(UnmarshalFromString(`{"metric": "sys.777","tags": {"a":"123"}`, &obj))
  17. }
  18. func Test_missing_array_end(t *testing.T) {
  19. should := require.New(t)
  20. should.NotNil(UnmarshalFromString(`[1,2,3`, &[]int{}))
  21. }
  22. func Test_invalid_any(t *testing.T) {
  23. should := require.New(t)
  24. any := Get([]byte("[]"))
  25. should.Equal(InvalidValue, any.Get(0.3).ValueType())
  26. // is nil correct ?
  27. should.Equal(nil, any.Get(0.3).GetInterface())
  28. any = any.Get(0.3)
  29. should.Equal(false, any.ToBool())
  30. should.Equal(int(0), any.ToInt())
  31. should.Equal(int32(0), any.ToInt32())
  32. should.Equal(int64(0), any.ToInt64())
  33. should.Equal(uint(0), any.ToUint())
  34. should.Equal(uint32(0), any.ToUint32())
  35. should.Equal(uint64(0), any.ToUint64())
  36. should.Equal(float32(0), any.ToFloat32())
  37. should.Equal(float64(0), any.ToFloat64())
  38. should.Equal("", any.ToString())
  39. should.Equal(InvalidValue, any.Get(0.1).Get(1).ValueType())
  40. }
  41. func Test_invalid_struct_input(t *testing.T) {
  42. should := require.New(t)
  43. type TestObject struct{}
  44. input := []byte{54, 141, 30}
  45. obj := TestObject{}
  46. should.NotNil(Unmarshal(input, &obj))
  47. }
  48. func Test_invalid_slice_input(t *testing.T) {
  49. should := require.New(t)
  50. type TestObject struct{}
  51. input := []byte{93}
  52. obj := []string{}
  53. should.NotNil(Unmarshal(input, &obj))
  54. }
  55. func Test_invalid_array_input(t *testing.T) {
  56. should := require.New(t)
  57. type TestObject struct{}
  58. input := []byte{93}
  59. obj := [0]string{}
  60. should.NotNil(Unmarshal(input, &obj))
  61. }
  62. func Test_invalid_float(t *testing.T) {
  63. inputs := []string{
  64. `1.e1`, // dot without following digit
  65. `1.`, // dot can not be the last char
  66. ``, // empty number
  67. `01`, // extra leading zero
  68. `-`, // negative without digit
  69. `--`, // double negative
  70. `--2`, // double negative
  71. }
  72. for _, input := range inputs {
  73. t.Run(input, func(t *testing.T) {
  74. should := require.New(t)
  75. iter := ParseString(ConfigDefault, input+",")
  76. iter.Skip()
  77. should.NotEqual(io.EOF, iter.Error)
  78. should.NotNil(iter.Error)
  79. v := float64(0)
  80. should.NotNil(json.Unmarshal([]byte(input), &v))
  81. iter = ParseString(ConfigDefault, input+",")
  82. iter.ReadFloat64()
  83. should.NotEqual(io.EOF, iter.Error)
  84. should.NotNil(iter.Error)
  85. iter = ParseString(ConfigDefault, input+",")
  86. iter.ReadFloat32()
  87. should.NotEqual(io.EOF, iter.Error)
  88. should.NotNil(iter.Error)
  89. })
  90. }
  91. }
  92. func Test_chan(t *testing.T) {
  93. t.Skip("do not support chan")
  94. type TestObject struct {
  95. MyChan chan bool
  96. MyField int
  97. }
  98. should := require.New(t)
  99. obj := TestObject{}
  100. str, err := json.Marshal(obj)
  101. should.Nil(err)
  102. should.Equal(``, str)
  103. }
  104. func Test_invalid_number(t *testing.T) {
  105. type Message struct {
  106. Number int `json:"number"`
  107. }
  108. obj := Message{}
  109. decoder := ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewBufferString(`{"number":"5"}`))
  110. err := decoder.Decode(&obj)
  111. result, err := ConfigCompatibleWithStandardLibrary.Marshal(err.Error())
  112. should := require.New(t)
  113. should.Nil(err)
  114. should.Contains(string(result), "\xff")
  115. }