value_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package objx_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/objx"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestStringTypeString(t *testing.T) {
  8. m := objx.Map{
  9. "string": "foo",
  10. }
  11. assert.Equal(t, "foo", m.Get("string").String())
  12. }
  13. func TestStringTypeBool(t *testing.T) {
  14. m := objx.Map{
  15. "bool": true,
  16. }
  17. assert.Equal(t, "true", m.Get("bool").String())
  18. }
  19. func TestStringTypeInt(t *testing.T) {
  20. m := objx.Map{
  21. "int": int(1),
  22. "int8": int8(8),
  23. "int16": int16(16),
  24. "int32": int32(32),
  25. "int64": int64(64),
  26. }
  27. assert.Equal(t, "1", m.Get("int").String())
  28. assert.Equal(t, "8", m.Get("int8").String())
  29. assert.Equal(t, "16", m.Get("int16").String())
  30. assert.Equal(t, "32", m.Get("int32").String())
  31. assert.Equal(t, "64", m.Get("int64").String())
  32. }
  33. func TestStringTypeUint(t *testing.T) {
  34. m := objx.Map{
  35. "uint": uint(1),
  36. "uint8": uint8(8),
  37. "uint16": uint16(16),
  38. "uint32": uint32(32),
  39. "uint64": uint64(64),
  40. }
  41. assert.Equal(t, "1", m.Get("uint").String())
  42. assert.Equal(t, "8", m.Get("uint8").String())
  43. assert.Equal(t, "16", m.Get("uint16").String())
  44. assert.Equal(t, "32", m.Get("uint32").String())
  45. assert.Equal(t, "64", m.Get("uint64").String())
  46. }
  47. func TestStringTypeFloat(t *testing.T) {
  48. m := objx.Map{
  49. "float32": float32(32.32),
  50. "float64": float64(64.64),
  51. }
  52. assert.Equal(t, "32.32", m.Get("float32").String())
  53. assert.Equal(t, "64.64", m.Get("float64").String())
  54. }
  55. func TestStringTypeOther(t *testing.T) {
  56. m := objx.Map{
  57. "other": []string{"foo", "bar"},
  58. "nilValue": nil,
  59. }
  60. assert.Equal(t, "[]string{\"foo\", \"bar\"}", m.Get("other").String())
  61. assert.Equal(t, "", m.Get("nilValue").String())
  62. }
  63. func TestStringSliceTypeString(t *testing.T) {
  64. m := objx.Map{
  65. "string": []string{"foo", "bar"},
  66. }
  67. assert.Equal(t, []string{"foo", "bar"}, m.Get("string").StringSlice())
  68. }
  69. func TestStringSliceTypeBool(t *testing.T) {
  70. m := objx.Map{
  71. "bool": []bool{true, false},
  72. }
  73. assert.Equal(t, []string{"true", "false"}, m.Get("bool").StringSlice())
  74. }
  75. func TestStringSliceTypeInt(t *testing.T) {
  76. m := objx.Map{
  77. "int": []int{1, 2},
  78. "int8": []int8{8, 9},
  79. "int16": []int16{16, 17},
  80. "int32": []int32{32, 33},
  81. "int64": []int64{64, 65},
  82. }
  83. assert.Equal(t, []string{"1", "2"}, m.Get("int").StringSlice())
  84. assert.Equal(t, []string{"8", "9"}, m.Get("int8").StringSlice())
  85. assert.Equal(t, []string{"16", "17"}, m.Get("int16").StringSlice())
  86. assert.Equal(t, []string{"32", "33"}, m.Get("int32").StringSlice())
  87. assert.Equal(t, []string{"64", "65"}, m.Get("int64").StringSlice())
  88. }
  89. func TestStringSliceTypeUint(t *testing.T) {
  90. m := objx.Map{
  91. "uint": []uint{1, 2},
  92. "uint8": []uint8{8, 9},
  93. "uint16": []uint16{16, 17},
  94. "uint32": []uint32{32, 33},
  95. "uint64": []uint64{64, 65},
  96. }
  97. assert.Equal(t, []string{"1", "2"}, m.Get("uint").StringSlice())
  98. assert.Equal(t, []string{"8", "9"}, m.Get("uint8").StringSlice())
  99. assert.Equal(t, []string{"16", "17"}, m.Get("uint16").StringSlice())
  100. assert.Equal(t, []string{"32", "33"}, m.Get("uint32").StringSlice())
  101. assert.Equal(t, []string{"64", "65"}, m.Get("uint64").StringSlice())
  102. }
  103. func TestStringSliceTypeFloat(t *testing.T) {
  104. m := objx.Map{
  105. "float32": []float32{32.32, 33.33},
  106. "float64": []float64{64.64, 65.65},
  107. }
  108. assert.Equal(t, []string{"32.32", "33.33"}, m.Get("float32").StringSlice())
  109. assert.Equal(t, []string{"64.64", "65.65"}, m.Get("float64").StringSlice())
  110. }
  111. func TestStringSliceTypeOther(t *testing.T) {
  112. m := objx.Map{
  113. "other": "foo",
  114. }
  115. assert.Equal(t, []string{}, m.Get("other").StringSlice())
  116. assert.Equal(t, []string{"bar"}, m.Get("other").StringSlice([]string{"bar"}))
  117. }