utils_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package mapping
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. const testTagName = "key"
  8. type Foo struct {
  9. Str string
  10. StrWithTag string `key:"stringwithtag"`
  11. StrWithTagAndOption string `key:"stringwithtag,string"`
  12. }
  13. func TestDeferInt(t *testing.T) {
  14. i := 1
  15. s := "hello"
  16. number := struct {
  17. f float64
  18. }{
  19. f: 6.4,
  20. }
  21. cases := []struct {
  22. t reflect.Type
  23. expect reflect.Kind
  24. }{
  25. {
  26. t: reflect.TypeOf(i),
  27. expect: reflect.Int,
  28. },
  29. {
  30. t: reflect.TypeOf(&i),
  31. expect: reflect.Int,
  32. },
  33. {
  34. t: reflect.TypeOf(s),
  35. expect: reflect.String,
  36. },
  37. {
  38. t: reflect.TypeOf(&s),
  39. expect: reflect.String,
  40. },
  41. {
  42. t: reflect.TypeOf(number.f),
  43. expect: reflect.Float64,
  44. },
  45. {
  46. t: reflect.TypeOf(&number.f),
  47. expect: reflect.Float64,
  48. },
  49. }
  50. for _, each := range cases {
  51. t.Run(each.t.String(), func(t *testing.T) {
  52. assert.Equal(t, each.expect, Deref(each.t).Kind())
  53. })
  54. }
  55. }
  56. func TestParseKeyAndOptionWithoutTag(t *testing.T) {
  57. var foo Foo
  58. rte := reflect.TypeOf(&foo).Elem()
  59. field, _ := rte.FieldByName("Str")
  60. key, options, err := parseKeyAndOptions(testTagName, field)
  61. assert.Nil(t, err)
  62. assert.Equal(t, "Str", key)
  63. assert.Nil(t, options)
  64. }
  65. func TestParseKeyAndOptionWithTagWithoutOption(t *testing.T) {
  66. var foo Foo
  67. rte := reflect.TypeOf(&foo).Elem()
  68. field, _ := rte.FieldByName("StrWithTag")
  69. key, options, err := parseKeyAndOptions(testTagName, field)
  70. assert.Nil(t, err)
  71. assert.Equal(t, "stringwithtag", key)
  72. assert.Nil(t, options)
  73. }
  74. func TestParseKeyAndOptionWithTagAndOption(t *testing.T) {
  75. var foo Foo
  76. rte := reflect.TypeOf(&foo).Elem()
  77. field, _ := rte.FieldByName("StrWithTagAndOption")
  78. key, options, err := parseKeyAndOptions(testTagName, field)
  79. assert.Nil(t, err)
  80. assert.Equal(t, "stringwithtag", key)
  81. assert.True(t, options.FromString)
  82. }
  83. func TestValidatePtrWithNonPtr(t *testing.T) {
  84. var foo string
  85. rve := reflect.ValueOf(foo)
  86. assert.NotNil(t, ValidatePtr(&rve))
  87. }
  88. func TestValidatePtrWithPtr(t *testing.T) {
  89. var foo string
  90. rve := reflect.ValueOf(&foo)
  91. assert.Nil(t, ValidatePtr(&rve))
  92. }
  93. func TestValidatePtrWithNilPtr(t *testing.T) {
  94. var foo *string
  95. rve := reflect.ValueOf(foo)
  96. assert.NotNil(t, ValidatePtr(&rve))
  97. }
  98. func TestValidatePtrWithZeroValue(t *testing.T) {
  99. var s string
  100. e := reflect.Zero(reflect.TypeOf(s))
  101. assert.NotNil(t, ValidatePtr(&e))
  102. }
  103. func TestSetValueNotSettable(t *testing.T) {
  104. var i int
  105. assert.NotNil(t, setValue(reflect.Int, reflect.ValueOf(i), "1"))
  106. }
  107. func TestParseKeyAndOptionsErrors(t *testing.T) {
  108. type Bar struct {
  109. OptionsValue string `key:",options=a=b"`
  110. DefaultValue string `key:",default=a=b"`
  111. }
  112. var bar Bar
  113. _, _, err := parseKeyAndOptions("key", reflect.TypeOf(&bar).Elem().Field(0))
  114. assert.NotNil(t, err)
  115. _, _, err = parseKeyAndOptions("key", reflect.TypeOf(&bar).Elem().Field(1))
  116. assert.NotNil(t, err)
  117. }
  118. func TestSetValueFormatErrors(t *testing.T) {
  119. type Bar struct {
  120. IntValue int
  121. UintValue uint
  122. FloatValue float32
  123. MapValue map[string]interface{}
  124. }
  125. var bar Bar
  126. tests := []struct {
  127. kind reflect.Kind
  128. target reflect.Value
  129. value string
  130. }{
  131. {
  132. kind: reflect.Int,
  133. target: reflect.ValueOf(&bar.IntValue).Elem(),
  134. value: "a",
  135. },
  136. {
  137. kind: reflect.Uint,
  138. target: reflect.ValueOf(&bar.UintValue).Elem(),
  139. value: "a",
  140. },
  141. {
  142. kind: reflect.Float32,
  143. target: reflect.ValueOf(&bar.FloatValue).Elem(),
  144. value: "a",
  145. },
  146. {
  147. kind: reflect.Map,
  148. target: reflect.ValueOf(&bar.MapValue).Elem(),
  149. },
  150. }
  151. for _, test := range tests {
  152. t.Run(test.kind.String(), func(t *testing.T) {
  153. err := setValue(test.kind, test.target, test.value)
  154. assert.NotEqual(t, errValueNotSettable, err)
  155. assert.NotNil(t, err)
  156. })
  157. }
  158. }
  159. func TestRepr(t *testing.T) {
  160. var (
  161. f32 float32 = 1.1
  162. f64 = 2.2
  163. i8 int8 = 1
  164. i16 int16 = 2
  165. i32 int32 = 3
  166. i64 int64 = 4
  167. u8 uint8 = 5
  168. u16 uint16 = 6
  169. u32 uint32 = 7
  170. u64 uint64 = 8
  171. )
  172. tests := []struct {
  173. v interface{}
  174. expect string
  175. }{
  176. {
  177. nil,
  178. "",
  179. },
  180. {
  181. mockStringable{},
  182. "mocked",
  183. },
  184. {
  185. new(mockStringable),
  186. "mocked",
  187. },
  188. {
  189. newMockPtr(),
  190. "mockptr",
  191. },
  192. {
  193. true,
  194. "true",
  195. },
  196. {
  197. false,
  198. "false",
  199. },
  200. {
  201. f32,
  202. "1.1",
  203. },
  204. {
  205. f64,
  206. "2.2",
  207. },
  208. {
  209. i8,
  210. "1",
  211. },
  212. {
  213. i16,
  214. "2",
  215. },
  216. {
  217. i32,
  218. "3",
  219. },
  220. {
  221. i64,
  222. "4",
  223. },
  224. {
  225. u8,
  226. "5",
  227. },
  228. {
  229. u16,
  230. "6",
  231. },
  232. {
  233. u32,
  234. "7",
  235. },
  236. {
  237. u64,
  238. "8",
  239. },
  240. {
  241. []byte(`abcd`),
  242. "abcd",
  243. },
  244. {
  245. mockOpacity{val: 1},
  246. "{1}",
  247. },
  248. }
  249. for _, test := range tests {
  250. t.Run(test.expect, func(t *testing.T) {
  251. assert.Equal(t, test.expect, Repr(test.v))
  252. })
  253. }
  254. }
  255. type mockStringable struct{}
  256. func (m mockStringable) String() string {
  257. return "mocked"
  258. }
  259. type mockPtr struct{}
  260. func newMockPtr() *mockPtr {
  261. return new(mockPtr)
  262. }
  263. func (m *mockPtr) String() string {
  264. return "mockptr"
  265. }
  266. type mockOpacity struct {
  267. val int
  268. }