jsoniter_invalid_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. }