jsoniter_must_be_valid_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package any_tests
  2. import (
  3. "testing"
  4. "github.com/json-iterator/go"
  5. "github.com/stretchr/testify/require"
  6. )
  7. // if must be valid is useless, just drop this test
  8. func Test_must_be_valid(t *testing.T) {
  9. should := require.New(t)
  10. any := jsoniter.Get([]byte("123"))
  11. should.Equal(any.MustBeValid().ToInt(), 123)
  12. any = jsoniter.Wrap(int8(10))
  13. should.Equal(any.MustBeValid().ToInt(), 10)
  14. any = jsoniter.Wrap(int16(10))
  15. should.Equal(any.MustBeValid().ToInt(), 10)
  16. any = jsoniter.Wrap(int32(10))
  17. should.Equal(any.MustBeValid().ToInt(), 10)
  18. any = jsoniter.Wrap(int64(10))
  19. should.Equal(any.MustBeValid().ToInt(), 10)
  20. any = jsoniter.Wrap(uint(10))
  21. should.Equal(any.MustBeValid().ToInt(), 10)
  22. any = jsoniter.Wrap(uint8(10))
  23. should.Equal(any.MustBeValid().ToInt(), 10)
  24. any = jsoniter.Wrap(uint16(10))
  25. should.Equal(any.MustBeValid().ToInt(), 10)
  26. any = jsoniter.Wrap(uint32(10))
  27. should.Equal(any.MustBeValid().ToInt(), 10)
  28. any = jsoniter.Wrap(uint64(10))
  29. should.Equal(any.MustBeValid().ToInt(), 10)
  30. any = jsoniter.Wrap(float32(10))
  31. should.Equal(any.MustBeValid().ToFloat64(), float64(10))
  32. any = jsoniter.Wrap(float64(10))
  33. should.Equal(any.MustBeValid().ToFloat64(), float64(10))
  34. any = jsoniter.Wrap(true)
  35. should.Equal(any.MustBeValid().ToFloat64(), float64(1))
  36. any = jsoniter.Wrap(false)
  37. should.Equal(any.MustBeValid().ToFloat64(), float64(0))
  38. any = jsoniter.Wrap(nil)
  39. should.Equal(any.MustBeValid().ToFloat64(), float64(0))
  40. any = jsoniter.Wrap(struct{ age int }{age: 1})
  41. should.Equal(any.MustBeValid().ToFloat64(), float64(0))
  42. any = jsoniter.Wrap(map[string]interface{}{"abc": 1})
  43. should.Equal(any.MustBeValid().ToFloat64(), float64(0))
  44. any = jsoniter.Wrap("abc")
  45. should.Equal(any.MustBeValid().ToFloat64(), float64(0))
  46. any = jsoniter.Wrap([]int{})
  47. should.Equal(any.MustBeValid().ToFloat64(), float64(0))
  48. any = jsoniter.Wrap([]int{1, 2})
  49. should.Equal(any.MustBeValid().ToFloat64(), float64(1))
  50. }