jsoniter_wrap_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package any_tests
  2. import (
  3. "testing"
  4. "github.com/json-iterator/go"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func Test_wrap_and_valuetype_everything(t *testing.T) {
  8. should := require.New(t)
  9. var i interface{}
  10. any := jsoniter.Get([]byte("123"))
  11. // default of number type is float64
  12. i = float64(123)
  13. should.Equal(i, any.GetInterface())
  14. any = jsoniter.Wrap(int8(10))
  15. should.Equal(any.ValueType(), jsoniter.NumberValue)
  16. should.Equal(any.LastError(), nil)
  17. // get interface is not int8 interface
  18. // i = int8(10)
  19. // should.Equal(i, any.GetInterface())
  20. any = jsoniter.Wrap(int16(10))
  21. should.Equal(any.ValueType(), jsoniter.NumberValue)
  22. should.Equal(any.LastError(), nil)
  23. //i = int16(10)
  24. //should.Equal(i, any.GetInterface())
  25. any = jsoniter.Wrap(int32(10))
  26. should.Equal(any.ValueType(), jsoniter.NumberValue)
  27. should.Equal(any.LastError(), nil)
  28. i = int32(10)
  29. should.Equal(i, any.GetInterface())
  30. any = jsoniter.Wrap(int64(10))
  31. should.Equal(any.ValueType(), jsoniter.NumberValue)
  32. should.Equal(any.LastError(), nil)
  33. i = int64(10)
  34. should.Equal(i, any.GetInterface())
  35. any = jsoniter.Wrap(uint(10))
  36. should.Equal(any.ValueType(), jsoniter.NumberValue)
  37. should.Equal(any.LastError(), nil)
  38. // not equal
  39. //i = uint(10)
  40. //should.Equal(i, any.GetInterface())
  41. any = jsoniter.Wrap(uint8(10))
  42. should.Equal(any.ValueType(), jsoniter.NumberValue)
  43. should.Equal(any.LastError(), nil)
  44. // not equal
  45. // i = uint8(10)
  46. // should.Equal(i, any.GetInterface())
  47. any = jsoniter.Wrap(uint16(10))
  48. should.Equal(any.ValueType(), jsoniter.NumberValue)
  49. should.Equal(any.LastError(), nil)
  50. any = jsoniter.Wrap(uint32(10))
  51. should.Equal(any.ValueType(), jsoniter.NumberValue)
  52. should.Equal(any.LastError(), nil)
  53. i = uint32(10)
  54. should.Equal(i, any.GetInterface())
  55. any = jsoniter.Wrap(uint64(10))
  56. should.Equal(any.ValueType(), jsoniter.NumberValue)
  57. should.Equal(any.LastError(), nil)
  58. i = uint64(10)
  59. should.Equal(i, any.GetInterface())
  60. any = jsoniter.Wrap(float32(10))
  61. should.Equal(any.ValueType(), jsoniter.NumberValue)
  62. should.Equal(any.LastError(), nil)
  63. // not equal
  64. //i = float32(10)
  65. //should.Equal(i, any.GetInterface())
  66. any = jsoniter.Wrap(float64(10))
  67. should.Equal(any.ValueType(), jsoniter.NumberValue)
  68. should.Equal(any.LastError(), nil)
  69. i = float64(10)
  70. should.Equal(i, any.GetInterface())
  71. any = jsoniter.Wrap(true)
  72. should.Equal(any.ValueType(), jsoniter.BoolValue)
  73. should.Equal(any.LastError(), nil)
  74. i = true
  75. should.Equal(i, any.GetInterface())
  76. any = jsoniter.Wrap(false)
  77. should.Equal(any.ValueType(), jsoniter.BoolValue)
  78. should.Equal(any.LastError(), nil)
  79. i = false
  80. should.Equal(i, any.GetInterface())
  81. any = jsoniter.Wrap(nil)
  82. should.Equal(any.ValueType(), jsoniter.NilValue)
  83. should.Equal(any.LastError(), nil)
  84. i = nil
  85. should.Equal(i, any.GetInterface())
  86. stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
  87. any.WriteTo(stream)
  88. should.Equal("null", string(stream.Buffer()))
  89. should.Equal(any.LastError(), nil)
  90. any = jsoniter.Wrap(struct{ age int }{age: 1})
  91. should.Equal(any.ValueType(), jsoniter.ObjectValue)
  92. should.Equal(any.LastError(), nil)
  93. i = struct{ age int }{age: 1}
  94. should.Equal(i, any.GetInterface())
  95. any = jsoniter.Wrap(map[string]interface{}{"abc": 1})
  96. should.Equal(any.ValueType(), jsoniter.ObjectValue)
  97. should.Equal(any.LastError(), nil)
  98. i = map[string]interface{}{"abc": 1}
  99. should.Equal(i, any.GetInterface())
  100. any = jsoniter.Wrap("abc")
  101. i = "abc"
  102. should.Equal(i, any.GetInterface())
  103. should.Equal(nil, any.LastError())
  104. }