value_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package paladin
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. type testUnmarshler struct {
  9. Text string
  10. Int int
  11. }
  12. func TestValueUnmarshal(t *testing.T) {
  13. s := `
  14. int = 100
  15. text = "hello"
  16. `
  17. v := Value{val: s, raw: s}
  18. obj := new(testUnmarshler)
  19. assert.Nil(t, v.UnmarshalTOML(obj))
  20. // error
  21. v = Value{val: nil, raw: ""}
  22. assert.NotNil(t, v.UnmarshalTOML(obj))
  23. }
  24. func TestValue(t *testing.T) {
  25. var tests = []struct {
  26. in interface{}
  27. out interface{}
  28. }{
  29. {
  30. "text",
  31. "text",
  32. },
  33. {
  34. time.Duration(time.Second * 10),
  35. "10s",
  36. },
  37. {
  38. int64(100),
  39. int64(100),
  40. },
  41. {
  42. float64(100.1),
  43. float64(100.1),
  44. },
  45. {
  46. true,
  47. true,
  48. },
  49. {
  50. nil,
  51. nil,
  52. },
  53. }
  54. for _, test := range tests {
  55. t.Run(fmt.Sprint(test.in), func(t *testing.T) {
  56. v := Value{val: test.in, raw: fmt.Sprint(test.in)}
  57. switch test.in.(type) {
  58. case nil:
  59. s, err := v.String()
  60. assert.NotNil(t, err)
  61. assert.Equal(t, s, "", test.in)
  62. i, err := v.Int64()
  63. assert.NotNil(t, err)
  64. assert.Equal(t, i, int64(0), test.in)
  65. f, err := v.Float64()
  66. assert.NotNil(t, err)
  67. assert.Equal(t, f, float64(0.0), test.in)
  68. b, err := v.Bool()
  69. assert.NotNil(t, err)
  70. assert.Equal(t, b, false, test.in)
  71. case string:
  72. val, err := v.String()
  73. assert.Nil(t, err)
  74. assert.Equal(t, val, test.out.(string), test.in)
  75. case int64:
  76. val, err := v.Int()
  77. assert.Nil(t, err)
  78. assert.Equal(t, val, int(test.out.(int64)), test.in)
  79. val32, err := v.Int32()
  80. assert.Nil(t, err)
  81. assert.Equal(t, val32, int32(test.out.(int64)), test.in)
  82. val64, err := v.Int64()
  83. assert.Nil(t, err)
  84. assert.Equal(t, val64, test.out.(int64), test.in)
  85. case float64:
  86. val32, err := v.Float32()
  87. assert.Nil(t, err)
  88. assert.Equal(t, val32, float32(test.out.(float64)), test.in)
  89. val64, err := v.Float64()
  90. assert.Nil(t, err)
  91. assert.Equal(t, val64, test.out.(float64), test.in)
  92. case bool:
  93. val, err := v.Bool()
  94. assert.Nil(t, err)
  95. assert.Equal(t, val, test.out.(bool), test.in)
  96. case time.Duration:
  97. v.val = test.out
  98. val, err := v.Duration()
  99. assert.Nil(t, err)
  100. assert.Equal(t, val, test.in.(time.Duration), test.out)
  101. }
  102. })
  103. }
  104. }
  105. func TestValueSlice(t *testing.T) {
  106. var tests = []struct {
  107. in interface{}
  108. out interface{}
  109. }{
  110. {
  111. nil,
  112. nil,
  113. },
  114. {
  115. []interface{}{"a", "b", "c"},
  116. []string{"a", "b", "c"},
  117. },
  118. {
  119. []interface{}{1, 2, 3},
  120. []int64{1, 2, 3},
  121. },
  122. {
  123. []interface{}{1.1, 1.2, 1.3},
  124. []float64{1.1, 1.2, 1.3},
  125. },
  126. {
  127. []interface{}{true, false, true},
  128. []bool{true, false, true},
  129. },
  130. }
  131. for _, test := range tests {
  132. t.Run(fmt.Sprint(test.in), func(t *testing.T) {
  133. v := Value{val: test.in, raw: fmt.Sprint(test.in)}
  134. switch test.in.(type) {
  135. case nil:
  136. var s []string
  137. assert.NotNil(t, v.Slice(&s))
  138. case []string:
  139. var s []string
  140. assert.Nil(t, v.Slice(&s))
  141. assert.Equal(t, s, test.out)
  142. case []int64:
  143. var s []int64
  144. assert.Nil(t, v.Slice(&s))
  145. assert.Equal(t, s, test.out)
  146. case []float64:
  147. var s []float64
  148. assert.Nil(t, v.Slice(&s))
  149. assert.Equal(t, s, test.out)
  150. case []bool:
  151. var s []bool
  152. assert.Nil(t, v.Slice(&s))
  153. assert.Equal(t, s, test.out)
  154. }
  155. })
  156. }
  157. }
  158. func BenchmarkValueInt(b *testing.B) {
  159. v := &Value{val: int64(100), raw: "100"}
  160. b.RunParallel(func(pb *testing.PB) {
  161. for pb.Next() {
  162. v.Int64()
  163. }
  164. })
  165. }
  166. func BenchmarkValueFloat(b *testing.B) {
  167. v := &Value{val: float64(100.1), raw: "100.1"}
  168. b.RunParallel(func(pb *testing.PB) {
  169. for pb.Next() {
  170. v.Float64()
  171. }
  172. })
  173. }
  174. func BenchmarkValueBool(b *testing.B) {
  175. v := &Value{val: true, raw: "true"}
  176. b.RunParallel(func(pb *testing.PB) {
  177. for pb.Next() {
  178. v.Bool()
  179. }
  180. })
  181. }
  182. func BenchmarkValueString(b *testing.B) {
  183. v := &Value{val: "text", raw: "text"}
  184. b.RunParallel(func(pb *testing.PB) {
  185. for pb.Next() {
  186. v.String()
  187. }
  188. })
  189. }
  190. func BenchmarkValueSlice(b *testing.B) {
  191. v := &Value{val: []interface{}{1, 2, 3}, raw: "100"}
  192. b.RunParallel(func(pb *testing.PB) {
  193. var slice []int64
  194. for pb.Next() {
  195. v.Slice(&slice)
  196. }
  197. })
  198. }