jsoniter_any_string_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package jsoniter
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. var stringConvertMap = map[string]string{
  7. "null": "",
  8. "321.1": "321.1",
  9. `"1.1"`: "1.1",
  10. `"-123.1"`: "-123.1",
  11. "0.0": "0.0",
  12. "0": "0",
  13. `"0"`: "0",
  14. `"0.0"`: "0.0",
  15. `"00.0"`: "00.0",
  16. "true": "true",
  17. "false": "false",
  18. `"true"`: "true",
  19. `"false"`: "false",
  20. `"true123"`: "true123",
  21. `"+1"`: "+1",
  22. "[]": "[]",
  23. "[1,2]": "[1,2]",
  24. "{}": "{}",
  25. "{1,2}": "{1,2}",
  26. `{"a":1, "b":true}`: `{"a":1, "b":true}`,
  27. }
  28. func Test_read_any_to_string(t *testing.T) {
  29. should := require.New(t)
  30. for k, v := range stringConvertMap {
  31. any := Get([]byte(k))
  32. should.Equal(v, any.ToString(), "original val "+k)
  33. }
  34. }
  35. func Test_read_string_as_any(t *testing.T) {
  36. should := require.New(t)
  37. any := Get([]byte(`"hello"`))
  38. should.Equal("hello", any.ToString())
  39. should.True(any.ToBool())
  40. any = Get([]byte(`" "`))
  41. should.False(any.ToBool())
  42. any = Get([]byte(`"false"`))
  43. should.True(any.ToBool())
  44. any = Get([]byte(`"123"`))
  45. should.Equal(123, any.ToInt())
  46. }
  47. func Test_wrap_string(t *testing.T) {
  48. should := require.New(t)
  49. any := WrapString("123")
  50. should.Equal(123, any.ToInt())
  51. }