jsoniter_any_int_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "io"
  5. "testing"
  6. "github.com/json-iterator/go/require"
  7. )
  8. var intConvertMap = map[string]int{
  9. "null": 0,
  10. "321.1": 321,
  11. "-321.1": -321,
  12. `"1.1"`: 1,
  13. `"-321.1"`: -321,
  14. "0.0": 0,
  15. "0": 0,
  16. `"0"`: 0,
  17. `"0.0"`: 0,
  18. "-1.1": -1,
  19. "true": 1,
  20. "false": 0,
  21. `"true"`: 0,
  22. `"false"`: 0,
  23. `"true123"`: 0,
  24. `"123true"`: 123,
  25. `"-123true"`: -123,
  26. `"1.2332e6"`: 1,
  27. `""`: 0,
  28. "+": 0,
  29. "-": 0,
  30. "[]": 0,
  31. "[1,2]": 1,
  32. `["1","2"]`: 1,
  33. // object in php cannot convert to int
  34. "{}": 0,
  35. }
  36. func Test_read_any_to_int(t *testing.T) {
  37. should := require.New(t)
  38. // int
  39. for k, v := range intConvertMap {
  40. any := Get([]byte(k))
  41. should.Equal(v, any.ToInt(), fmt.Sprintf("origin val %v", k))
  42. }
  43. // int32
  44. for k, v := range intConvertMap {
  45. any := Get([]byte(k))
  46. should.Equal(int32(v), any.ToInt32(), fmt.Sprintf("original val is %v", k))
  47. }
  48. // int64
  49. for k, v := range intConvertMap {
  50. any := Get([]byte(k))
  51. should.Equal(int64(v), any.ToInt64(), fmt.Sprintf("original val is %v", k))
  52. }
  53. }
  54. var uintConvertMap = map[string]int{
  55. "null": 0,
  56. "321.1": 321,
  57. `"1.1"`: 1,
  58. `"-123.1"`: 0,
  59. "0.0": 0,
  60. "0": 0,
  61. `"0"`: 0,
  62. `"0.0"`: 0,
  63. `"00.0"`: 0,
  64. "true": 1,
  65. "false": 0,
  66. `"true"`: 0,
  67. `"false"`: 0,
  68. `"true123"`: 0,
  69. `"+1"`: 1,
  70. `"123true"`: 123,
  71. `"-123true"`: 0,
  72. `"1.2332e6"`: 1,
  73. `""`: 0,
  74. "+": 0,
  75. "-": 0,
  76. ".": 0,
  77. "[]": 0,
  78. "[1,2]": 1,
  79. "{}": 0,
  80. "{1,2}": 0,
  81. "-1.1": 0,
  82. "-321.1": 0,
  83. }
  84. func Test_read_any_to_uint(t *testing.T) {
  85. should := require.New(t)
  86. for k, v := range uintConvertMap {
  87. any := Get([]byte(k))
  88. should.Equal(uint64(v), any.ToUint64(), fmt.Sprintf("origin val %v", k))
  89. }
  90. for k, v := range uintConvertMap {
  91. any := Get([]byte(k))
  92. should.Equal(uint32(v), any.ToUint32(), fmt.Sprintf("origin val %v", k))
  93. }
  94. for k, v := range uintConvertMap {
  95. any := Get([]byte(k))
  96. should.Equal(uint(v), any.ToUint(), fmt.Sprintf("origin val %v", k))
  97. }
  98. }
  99. func Test_read_int64_as_any(t *testing.T) {
  100. should := require.New(t)
  101. any := Get([]byte("1234"))
  102. should.Equal(1234, any.ToInt())
  103. should.Equal(io.EOF, any.LastError())
  104. should.Equal("1234", any.ToString())
  105. should.True(any.ToBool())
  106. }
  107. func Test_int_lazy_any_get(t *testing.T) {
  108. should := require.New(t)
  109. any := Get([]byte("1234"))
  110. should.Equal(Invalid, any.Get(1, "2").ValueType())
  111. }