jsoniter_any_int_test.go 2.4 KB

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