util_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package valid
  2. import (
  3. "database/sql"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestEnsureString(t *testing.T) {
  9. str := "abc"
  10. bytes := []byte("abc")
  11. tests := []struct {
  12. tag string
  13. value interface{}
  14. expected string
  15. hasError bool
  16. }{
  17. {"t1", "abc", "abc", false},
  18. {"t2", &str, "", true},
  19. {"t3", bytes, "abc", false},
  20. {"t4", &bytes, "", true},
  21. {"t5", 100, "", true},
  22. }
  23. for _, test := range tests {
  24. s, err := EnsureString(test.value)
  25. if test.hasError {
  26. assert.NotNil(t, err, test.tag)
  27. } else {
  28. assert.Nil(t, err, test.tag)
  29. assert.Equal(t, test.expected, s, test.tag)
  30. }
  31. }
  32. }
  33. type MyString string
  34. func TestStringOrBytes(t *testing.T) {
  35. str := "abc"
  36. bytes := []byte("abc")
  37. var str2 string
  38. var bytes2 []byte
  39. var str3 MyString = "abc"
  40. var str4 *string
  41. tests := []struct {
  42. tag string
  43. value interface{}
  44. str string
  45. bs []byte
  46. isString bool
  47. isBytes bool
  48. }{
  49. {"t1", str, "abc", nil, true, false},
  50. {"t2", &str, "", nil, false, false},
  51. {"t3", bytes, "", []byte("abc"), false, true},
  52. {"t4", &bytes, "", nil, false, false},
  53. {"t5", 100, "", nil, false, false},
  54. {"t6", str2, "", nil, true, false},
  55. {"t7", &str2, "", nil, false, false},
  56. {"t8", bytes2, "", nil, false, true},
  57. {"t9", &bytes2, "", nil, false, false},
  58. {"t10", str3, "abc", nil, true, false},
  59. {"t11", &str3, "", nil, false, false},
  60. {"t12", str4, "", nil, false, false},
  61. }
  62. for _, test := range tests {
  63. isString, str, isBytes, bs := StringOrBytes(test.value)
  64. assert.Equal(t, test.str, str, test.tag)
  65. assert.Equal(t, test.bs, bs, test.tag)
  66. assert.Equal(t, test.isString, isString, test.tag)
  67. assert.Equal(t, test.isBytes, isBytes, test.tag)
  68. }
  69. }
  70. func TestLengthOfValue(t *testing.T) {
  71. var a [3]int
  72. tests := []struct {
  73. tag string
  74. value interface{}
  75. length int
  76. err string
  77. }{
  78. {"t1", "abc", 3, ""},
  79. {"t2", []int{1, 2}, 2, ""},
  80. {"t3", map[string]int{"A": 1, "B": 2}, 2, ""},
  81. {"t4", a, 3, ""},
  82. {"t5", &a, 0, "cannot get the length of ptr"},
  83. {"t6", 123, 0, "cannot get the length of int"},
  84. }
  85. for _, test := range tests {
  86. l, err := LengthOfValue(test.value)
  87. assert.Equal(t, test.length, l, test.tag)
  88. assertError(t, test.err, err, test.tag)
  89. }
  90. }
  91. func TestToInt(t *testing.T) {
  92. var a int
  93. tests := []struct {
  94. tag string
  95. value interface{}
  96. result int64
  97. err string
  98. }{
  99. {"t1", 1, 1, ""},
  100. {"t2", int8(1), 1, ""},
  101. {"t3", int16(1), 1, ""},
  102. {"t4", int32(1), 1, ""},
  103. {"t5", int64(1), 1, ""},
  104. {"t6", &a, 0, "cannot convert ptr to int64"},
  105. {"t7", uint(1), 0, "cannot convert uint to int64"},
  106. {"t8", float64(1), 0, "cannot convert float64 to int64"},
  107. {"t9", "abc", 0, "cannot convert string to int64"},
  108. {"t10", []int{1, 2}, 0, "cannot convert slice to int64"},
  109. {"t11", map[string]int{"A": 1}, 0, "cannot convert map to int64"},
  110. }
  111. for _, test := range tests {
  112. l, err := ToInt(test.value)
  113. assert.Equal(t, test.result, l, test.tag)
  114. assertError(t, test.err, err, test.tag)
  115. }
  116. }
  117. func TestToUint(t *testing.T) {
  118. var a int
  119. var b uint
  120. tests := []struct {
  121. tag string
  122. value interface{}
  123. result uint64
  124. err string
  125. }{
  126. {"t1", uint(1), 1, ""},
  127. {"t2", uint8(1), 1, ""},
  128. {"t3", uint16(1), 1, ""},
  129. {"t4", uint32(1), 1, ""},
  130. {"t5", uint64(1), 1, ""},
  131. {"t6", 1, 0, "cannot convert int to uint64"},
  132. {"t7", &a, 0, "cannot convert ptr to uint64"},
  133. {"t8", &b, 0, "cannot convert ptr to uint64"},
  134. {"t9", float64(1), 0, "cannot convert float64 to uint64"},
  135. {"t10", "abc", 0, "cannot convert string to uint64"},
  136. {"t11", []int{1, 2}, 0, "cannot convert slice to uint64"},
  137. {"t12", map[string]int{"A": 1}, 0, "cannot convert map to uint64"},
  138. }
  139. for _, test := range tests {
  140. l, err := ToUint(test.value)
  141. assert.Equal(t, test.result, l, test.tag)
  142. assertError(t, test.err, err, test.tag)
  143. }
  144. }
  145. func TestToFloat(t *testing.T) {
  146. var a int
  147. var b uint
  148. tests := []struct {
  149. tag string
  150. value interface{}
  151. result float64
  152. err string
  153. }{
  154. {"t1", float32(1), 1, ""},
  155. {"t2", float64(1), 1, ""},
  156. {"t3", 1, 0, "cannot convert int to float64"},
  157. {"t4", uint(1), 0, "cannot convert uint to float64"},
  158. {"t5", &a, 0, "cannot convert ptr to float64"},
  159. {"t6", &b, 0, "cannot convert ptr to float64"},
  160. {"t7", "abc", 0, "cannot convert string to float64"},
  161. {"t8", []int{1, 2}, 0, "cannot convert slice to float64"},
  162. {"t9", map[string]int{"A": 1}, 0, "cannot convert map to float64"},
  163. }
  164. for _, test := range tests {
  165. l, err := ToFloat(test.value)
  166. assert.Equal(t, test.result, l, test.tag)
  167. assertError(t, test.err, err, test.tag)
  168. }
  169. }
  170. func TestIsEmpty(t *testing.T) {
  171. var s1 string
  172. var s2 = "a"
  173. var s3 *string
  174. s4 := struct{}{}
  175. time1 := time.Now()
  176. var time2 time.Time
  177. tests := []struct {
  178. tag string
  179. value interface{}
  180. empty bool
  181. }{
  182. // nil
  183. {"t0", nil, true},
  184. // string
  185. {"t1.1", "", true},
  186. {"t1.2", "1", false},
  187. {"t1.3", MyString(""), true},
  188. {"t1.4", MyString("1"), false},
  189. // slice
  190. {"t2.1", []byte(""), true},
  191. {"t2.2", []byte("1"), false},
  192. // map
  193. {"t3.1", map[string]int{}, true},
  194. {"t3.2", map[string]int{"a": 1}, false},
  195. // bool
  196. {"t4.1", false, true},
  197. {"t4.2", true, false},
  198. // int
  199. {"t5.1", 0, true},
  200. {"t5.2", int8(0), true},
  201. {"t5.3", int16(0), true},
  202. {"t5.4", int32(0), true},
  203. {"t5.5", int64(0), true},
  204. {"t5.6", 1, false},
  205. {"t5.7", int8(1), false},
  206. {"t5.8", int16(1), false},
  207. {"t5.9", int32(1), false},
  208. {"t5.10", int64(1), false},
  209. // uint
  210. {"t6.1", uint(0), true},
  211. {"t6.2", uint8(0), true},
  212. {"t6.3", uint16(0), true},
  213. {"t6.4", uint32(0), true},
  214. {"t6.5", uint64(0), true},
  215. {"t6.6", uint(1), false},
  216. {"t6.7", uint8(1), false},
  217. {"t6.8", uint16(1), false},
  218. {"t6.9", uint32(1), false},
  219. {"t6.10", uint64(1), false},
  220. // float
  221. {"t7.1", float32(0), true},
  222. {"t7.2", float64(0), true},
  223. {"t7.3", float32(1), false},
  224. {"t7.4", float64(1), false},
  225. // interface, ptr
  226. {"t8.1", &s1, true},
  227. {"t8.2", &s2, false},
  228. {"t8.3", s3, true},
  229. // struct
  230. {"t9.1", s4, false},
  231. {"t9.2", &s4, false},
  232. // time.Time
  233. {"t10.1", time1, false},
  234. {"t10.2", &time1, false},
  235. {"t10.3", time2, true},
  236. {"t10.4", &time2, true},
  237. }
  238. for _, test := range tests {
  239. empty := IsEmpty(test.value)
  240. assert.Equal(t, test.empty, empty, test.tag)
  241. }
  242. }
  243. func TestIndirect(t *testing.T) {
  244. var a = 100
  245. var b *int
  246. var c *sql.NullInt64
  247. tests := []struct {
  248. tag string
  249. value interface{}
  250. result interface{}
  251. isNil bool
  252. }{
  253. {"t1", 100, 100, false},
  254. {"t2", &a, 100, false},
  255. {"t3", b, nil, true},
  256. {"t4", nil, nil, true},
  257. {"t5", sql.NullInt64{Int64: 0, Valid: false}, nil, true},
  258. {"t6", sql.NullInt64{Int64: 1, Valid: false}, nil, true},
  259. {"t7", &sql.NullInt64{Int64: 0, Valid: false}, nil, true},
  260. {"t8", &sql.NullInt64{Int64: 1, Valid: false}, nil, true},
  261. {"t9", sql.NullInt64{Int64: 0, Valid: true}, int64(0), false},
  262. {"t10", sql.NullInt64{Int64: 1, Valid: true}, int64(1), false},
  263. {"t11", &sql.NullInt64{Int64: 0, Valid: true}, int64(0), false},
  264. {"t12", &sql.NullInt64{Int64: 1, Valid: true}, int64(1), false},
  265. {"t13", c, nil, true},
  266. }
  267. for _, test := range tests {
  268. result, isNil := Indirect(test.value)
  269. assert.Equal(t, test.result, result, test.tag)
  270. assert.Equal(t, test.isNil, isNil, test.tag)
  271. }
  272. }