本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
6.6KB

  1. package assert
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
  7. switch kind {
  8. case reflect.Int:
  9. {
  10. intobj1 := obj1.(int)
  11. intobj2 := obj2.(int)
  12. if intobj1 > intobj2 {
  13. return -1, true
  14. }
  15. if intobj1 == intobj2 {
  16. return 0, true
  17. }
  18. if intobj1 < intobj2 {
  19. return 1, true
  20. }
  21. }
  22. case reflect.Int8:
  23. {
  24. int8obj1 := obj1.(int8)
  25. int8obj2 := obj2.(int8)
  26. if int8obj1 > int8obj2 {
  27. return -1, true
  28. }
  29. if int8obj1 == int8obj2 {
  30. return 0, true
  31. }
  32. if int8obj1 < int8obj2 {
  33. return 1, true
  34. }
  35. }
  36. case reflect.Int16:
  37. {
  38. int16obj1 := obj1.(int16)
  39. int16obj2 := obj2.(int16)
  40. if int16obj1 > int16obj2 {
  41. return -1, true
  42. }
  43. if int16obj1 == int16obj2 {
  44. return 0, true
  45. }
  46. if int16obj1 < int16obj2 {
  47. return 1, true
  48. }
  49. }
  50. case reflect.Int32:
  51. {
  52. int32obj1 := obj1.(int32)
  53. int32obj2 := obj2.(int32)
  54. if int32obj1 > int32obj2 {
  55. return -1, true
  56. }
  57. if int32obj1 == int32obj2 {
  58. return 0, true
  59. }
  60. if int32obj1 < int32obj2 {
  61. return 1, true
  62. }
  63. }
  64. case reflect.Int64:
  65. {
  66. int64obj1 := obj1.(int64)
  67. int64obj2 := obj2.(int64)
  68. if int64obj1 > int64obj2 {
  69. return -1, true
  70. }
  71. if int64obj1 == int64obj2 {
  72. return 0, true
  73. }
  74. if int64obj1 < int64obj2 {
  75. return 1, true
  76. }
  77. }
  78. case reflect.Uint:
  79. {
  80. uintobj1 := obj1.(uint)
  81. uintobj2 := obj2.(uint)
  82. if uintobj1 > uintobj2 {
  83. return -1, true
  84. }
  85. if uintobj1 == uintobj2 {
  86. return 0, true
  87. }
  88. if uintobj1 < uintobj2 {
  89. return 1, true
  90. }
  91. }
  92. case reflect.Uint8:
  93. {
  94. uint8obj1 := obj1.(uint8)
  95. uint8obj2 := obj2.(uint8)
  96. if uint8obj1 > uint8obj2 {
  97. return -1, true
  98. }
  99. if uint8obj1 == uint8obj2 {
  100. return 0, true
  101. }
  102. if uint8obj1 < uint8obj2 {
  103. return 1, true
  104. }
  105. }
  106. case reflect.Uint16:
  107. {
  108. uint16obj1 := obj1.(uint16)
  109. uint16obj2 := obj2.(uint16)
  110. if uint16obj1 > uint16obj2 {
  111. return -1, true
  112. }
  113. if uint16obj1 == uint16obj2 {
  114. return 0, true
  115. }
  116. if uint16obj1 < uint16obj2 {
  117. return 1, true
  118. }
  119. }
  120. case reflect.Uint32:
  121. {
  122. uint32obj1 := obj1.(uint32)
  123. uint32obj2 := obj2.(uint32)
  124. if uint32obj1 > uint32obj2 {
  125. return -1, true
  126. }
  127. if uint32obj1 == uint32obj2 {
  128. return 0, true
  129. }
  130. if uint32obj1 < uint32obj2 {
  131. return 1, true
  132. }
  133. }
  134. case reflect.Uint64:
  135. {
  136. uint64obj1 := obj1.(uint64)
  137. uint64obj2 := obj2.(uint64)
  138. if uint64obj1 > uint64obj2 {
  139. return -1, true
  140. }
  141. if uint64obj1 == uint64obj2 {
  142. return 0, true
  143. }
  144. if uint64obj1 < uint64obj2 {
  145. return 1, true
  146. }
  147. }
  148. case reflect.Float32:
  149. {
  150. float32obj1 := obj1.(float32)
  151. float32obj2 := obj2.(float32)
  152. if float32obj1 > float32obj2 {
  153. return -1, true
  154. }
  155. if float32obj1 == float32obj2 {
  156. return 0, true
  157. }
  158. if float32obj1 < float32obj2 {
  159. return 1, true
  160. }
  161. }
  162. case reflect.Float64:
  163. {
  164. float64obj1 := obj1.(float64)
  165. float64obj2 := obj2.(float64)
  166. if float64obj1 > float64obj2 {
  167. return -1, true
  168. }
  169. if float64obj1 == float64obj2 {
  170. return 0, true
  171. }
  172. if float64obj1 < float64obj2 {
  173. return 1, true
  174. }
  175. }
  176. case reflect.String:
  177. {
  178. stringobj1 := obj1.(string)
  179. stringobj2 := obj2.(string)
  180. if stringobj1 > stringobj2 {
  181. return -1, true
  182. }
  183. if stringobj1 == stringobj2 {
  184. return 0, true
  185. }
  186. if stringobj1 < stringobj2 {
  187. return 1, true
  188. }
  189. }
  190. }
  191. return 0, false
  192. }
  193. // Greater asserts that the first element is greater than the second
  194. //
  195. // assert.Greater(t, 2, 1)
  196. // assert.Greater(t, float64(2), float64(1))
  197. // assert.Greater(t, "b", "a")
  198. func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
  199. if h, ok := t.(tHelper); ok {
  200. h.Helper()
  201. }
  202. e1Kind := reflect.ValueOf(e1).Kind()
  203. e2Kind := reflect.ValueOf(e2).Kind()
  204. if e1Kind != e2Kind {
  205. return Fail(t, "Elements should be the same type", msgAndArgs...)
  206. }
  207. res, isComparable := compare(e1, e2, e1Kind)
  208. if !isComparable {
  209. return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...)
  210. }
  211. if res != -1 {
  212. return Fail(t, fmt.Sprintf("\"%v\" is not greater than \"%v\"", e1, e2), msgAndArgs...)
  213. }
  214. return true
  215. }
  216. // GreaterOrEqual asserts that the first element is greater than or equal to the second
  217. //
  218. // assert.GreaterOrEqual(t, 2, 1)
  219. // assert.GreaterOrEqual(t, 2, 2)
  220. // assert.GreaterOrEqual(t, "b", "a")
  221. // assert.GreaterOrEqual(t, "b", "b")
  222. func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
  223. if h, ok := t.(tHelper); ok {
  224. h.Helper()
  225. }
  226. e1Kind := reflect.ValueOf(e1).Kind()
  227. e2Kind := reflect.ValueOf(e2).Kind()
  228. if e1Kind != e2Kind {
  229. return Fail(t, "Elements should be the same type", msgAndArgs...)
  230. }
  231. res, isComparable := compare(e1, e2, e1Kind)
  232. if !isComparable {
  233. return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...)
  234. }
  235. if res != -1 && res != 0 {
  236. return Fail(t, fmt.Sprintf("\"%v\" is not greater than or equal to \"%v\"", e1, e2), msgAndArgs...)
  237. }
  238. return true
  239. }
  240. // Less asserts that the first element is less than the second
  241. //
  242. // assert.Less(t, 1, 2)
  243. // assert.Less(t, float64(1), float64(2))
  244. // assert.Less(t, "a", "b")
  245. func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
  246. if h, ok := t.(tHelper); ok {
  247. h.Helper()
  248. }
  249. e1Kind := reflect.ValueOf(e1).Kind()
  250. e2Kind := reflect.ValueOf(e2).Kind()
  251. if e1Kind != e2Kind {
  252. return Fail(t, "Elements should be the same type", msgAndArgs...)
  253. }
  254. res, isComparable := compare(e1, e2, e1Kind)
  255. if !isComparable {
  256. return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...)
  257. }
  258. if res != 1 {
  259. return Fail(t, fmt.Sprintf("\"%v\" is not less than \"%v\"", e1, e2), msgAndArgs...)
  260. }
  261. return true
  262. }
  263. // LessOrEqual asserts that the first element is less than or equal to the second
  264. //
  265. // assert.LessOrEqual(t, 1, 2)
  266. // assert.LessOrEqual(t, 2, 2)
  267. // assert.LessOrEqual(t, "a", "b")
  268. // assert.LessOrEqual(t, "b", "b")
  269. func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
  270. if h, ok := t.(tHelper); ok {
  271. h.Helper()
  272. }
  273. e1Kind := reflect.ValueOf(e1).Kind()
  274. e2Kind := reflect.ValueOf(e2).Kind()
  275. if e1Kind != e2Kind {
  276. return Fail(t, "Elements should be the same type", msgAndArgs...)
  277. }
  278. res, isComparable := compare(e1, e2, e1Kind)
  279. if !isComparable {
  280. return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...)
  281. }
  282. if res != 1 && res != 0 {
  283. return Fail(t, fmt.Sprintf("\"%v\" is not less than or equal to \"%v\"", e1, e2), msgAndArgs...)
  284. }
  285. return true
  286. }
上海开阖软件有限公司 沪ICP备12045867号-1