requirements.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package require
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. type TestingT interface {
  8. Errorf(format string, args ...interface{})
  9. FailNow()
  10. }
  11. // Fail reports a failure through
  12. func FailNow(t TestingT, failureMessage string, msgAndArgs ...interface{}) {
  13. assert.Fail(t, failureMessage, msgAndArgs...)
  14. t.FailNow()
  15. }
  16. // Implements asserts that an object is implemented by the specified interface.
  17. //
  18. // require.Implements(t, (*MyInterface)(nil), new(MyObject), "MyObject")
  19. func Implements(t TestingT, interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) {
  20. if !assert.Implements(t, interfaceObject, object, msgAndArgs...) {
  21. t.FailNow()
  22. }
  23. }
  24. // IsType asserts that the specified objects are of the same type.
  25. func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs ...interface{}) {
  26. if !assert.IsType(t, expectedType, object, msgAndArgs...) {
  27. t.FailNow()
  28. }
  29. }
  30. // Equal asserts that two objects are equal.
  31. //
  32. // require.Equal(t, 123, 123, "123 and 123 should be equal")
  33. func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) {
  34. if !assert.Equal(t, expected, actual, msgAndArgs...) {
  35. t.FailNow()
  36. }
  37. }
  38. // EqualValues asserts that two objects are equal or convertable to each other.
  39. //
  40. // require.EqualValues(t, uint32(123), int32(123), "123 and 123 should be equal")
  41. func EqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) {
  42. if !assert.EqualValues(t, expected, actual, msgAndArgs...) {
  43. t.FailNow()
  44. }
  45. }
  46. // Exactly asserts that two objects are equal is value and type.
  47. //
  48. // require.Exactly(t, int32(123), int64(123), "123 and 123 should NOT be equal")
  49. func Exactly(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) {
  50. if !assert.Exactly(t, expected, actual, msgAndArgs...) {
  51. t.FailNow()
  52. }
  53. }
  54. // NotNil asserts that the specified object is not nil.
  55. //
  56. // require.NotNil(t, err, "err should be something")
  57. func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) {
  58. if !assert.NotNil(t, object, msgAndArgs...) {
  59. t.FailNow()
  60. }
  61. }
  62. // Nil asserts that the specified object is nil.
  63. //
  64. // require.Nil(t, err, "err should be nothing")
  65. func Nil(t TestingT, object interface{}, msgAndArgs ...interface{}) {
  66. if !assert.Nil(t, object, msgAndArgs...) {
  67. t.FailNow()
  68. }
  69. }
  70. // Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either
  71. // a slice or a channel with len == 0.
  72. //
  73. // require.Empty(t, obj)
  74. func Empty(t TestingT, object interface{}, msgAndArgs ...interface{}) {
  75. if !assert.Empty(t, object, msgAndArgs...) {
  76. t.FailNow()
  77. }
  78. }
  79. // NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either
  80. // a slice or a channel with len == 0.
  81. //
  82. // require.NotEmpty(t, obj)
  83. // require.Equal(t, "one", obj[0])
  84. func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) {
  85. if !assert.NotEmpty(t, object, msgAndArgs...) {
  86. t.FailNow()
  87. }
  88. }
  89. // Len asserts that the specified object has specific length.
  90. // Len also fails if the object has a type that len() not accept.
  91. //
  92. // require.Len(t, mySlice, 3, "The size of slice is not 3")
  93. func Len(t TestingT, object interface{}, length int, msgAndArgs ...interface{}) {
  94. if !assert.Len(t, object, length, msgAndArgs...) {
  95. t.FailNow()
  96. }
  97. }
  98. // True asserts that the specified value is true.
  99. //
  100. // require.True(t, myBool, "myBool should be true")
  101. func True(t TestingT, value bool, msgAndArgs ...interface{}) {
  102. if !assert.True(t, value, msgAndArgs...) {
  103. t.FailNow()
  104. }
  105. }
  106. // False asserts that the specified value is false.
  107. //
  108. // require.False(t, myBool, "myBool should be false")
  109. func False(t TestingT, value bool, msgAndArgs ...interface{}) {
  110. if !assert.False(t, value, msgAndArgs...) {
  111. t.FailNow()
  112. }
  113. }
  114. // NotEqual asserts that the specified values are NOT equal.
  115. //
  116. // require.NotEqual(t, obj1, obj2, "two objects shouldn't be equal")
  117. func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) {
  118. if !assert.NotEqual(t, expected, actual, msgAndArgs...) {
  119. t.FailNow()
  120. }
  121. }
  122. // Contains asserts that the specified string, list(array, slice...) or map contains the
  123. // specified substring or element.
  124. //
  125. // require.Contains(t, "Hello World", "World", "But 'Hello World' does contain 'World'")
  126. // require.Contains(t, ["Hello", "World"], "World", "But ["Hello", "World"] does contain 'World'")
  127. // require.Contains(t, {"Hello": "World"}, "Hello", "But {'Hello': 'World'} does contain 'Hello'")
  128. func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) {
  129. if !assert.Contains(t, s, contains, msgAndArgs...) {
  130. t.FailNow()
  131. }
  132. }
  133. // NotContains asserts that the specified string does NOT contain the specified substring.
  134. //
  135. // require.NotContains(t, "Hello World", "Earth", "But 'Hello World' does NOT contain 'Earth'")
  136. func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) {
  137. if !assert.NotContains(t, s, contains, msgAndArgs...) {
  138. t.FailNow()
  139. }
  140. }
  141. // Condition uses a Comparison to assert a complex condition.
  142. func Condition(t TestingT, comp assert.Comparison, msgAndArgs ...interface{}) {
  143. if !assert.Condition(t, comp, msgAndArgs...) {
  144. t.FailNow()
  145. }
  146. }
  147. // Panics asserts that the code inside the specified PanicTestFunc panics.
  148. //
  149. // require.Panics(t, func(){
  150. // GoCrazy()
  151. // }, "Calling GoCrazy() should panic")
  152. func Panics(t TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) {
  153. if !assert.Panics(t, f, msgAndArgs...) {
  154. t.FailNow()
  155. }
  156. }
  157. // NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic.
  158. //
  159. // require.NotPanics(t, func(){
  160. // RemainCalm()
  161. // }, "Calling RemainCalm() should NOT panic")
  162. func NotPanics(t TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) {
  163. if !assert.NotPanics(t, f, msgAndArgs...) {
  164. t.FailNow()
  165. }
  166. }
  167. // WithinDuration asserts that the two times are within duration delta of each other.
  168. //
  169. // require.WithinDuration(t, time.Now(), time.Now(), 10*time.Second, "The difference should not be more than 10s")
  170. func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) {
  171. if !assert.WithinDuration(t, expected, actual, delta, msgAndArgs...) {
  172. t.FailNow()
  173. }
  174. }
  175. // InDelta asserts that the two numerals are within delta of each other.
  176. //
  177. // require.InDelta(t, math.Pi, (22 / 7.0), 0.01)
  178. func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) {
  179. if !assert.InDelta(t, expected, actual, delta, msgAndArgs...) {
  180. t.FailNow()
  181. }
  182. }
  183. // InEpsilon asserts that expected and actual have a relative error less than epsilon
  184. func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) {
  185. if !assert.InEpsilon(t, expected, actual, epsilon, msgAndArgs...) {
  186. t.FailNow()
  187. }
  188. }
  189. // Regexp asserts that a specified regexp matches a string.
  190. //
  191. // require.Regexp(t, regexp.MustCompile("start"), "it's starting")
  192. // require.Regexp(t, "start...$", "it's not starting")
  193. func Regexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) {
  194. if !assert.Regexp(t, rx, str, msgAndArgs...) {
  195. t.FailNow()
  196. }
  197. }
  198. // NotRegexp asserts that a specified regexp does not match a string.
  199. //
  200. // require.NotRegexp(t, regexp.MustCompile("starts"), "it's starting")
  201. // require.NotRegexp(t, "^start", "it's not starting")
  202. func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) {
  203. if !assert.NotRegexp(t, rx, str, msgAndArgs...) {
  204. t.FailNow()
  205. }
  206. }
  207. // JSONEq asserts that two JSON strings are equivalent.
  208. //
  209. // assert.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
  210. //
  211. // Returns whether the assertion was successful (true) or not (false).
  212. func JSONEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) {
  213. var expectedJSONAsInterface, actualJSONAsInterface interface{}
  214. if err := json.Unmarshal([]byte(expected), &expectedJSONAsInterface); err != nil {
  215. t.FailNow()
  216. }
  217. if err := json.Unmarshal([]byte(actual), &actualJSONAsInterface); err != nil {
  218. t.FailNow()
  219. }
  220. Equal(t, expectedJSONAsInterface, actualJSONAsInterface, msgAndArgs...)
  221. }
  222. /*
  223. Errors
  224. */
  225. // NoError asserts that a function returned no error (i.e. `nil`).
  226. //
  227. // actualObj, err := SomeFunction()
  228. // require.NoError(t, err)
  229. // require.Equal(t, actualObj, expectedObj)
  230. //
  231. // Returns whether the assertion was successful (true) or not (false).
  232. func NoError(t TestingT, err error, msgAndArgs ...interface{}) {
  233. if !assert.NoError(t, err, msgAndArgs...) {
  234. t.FailNow()
  235. }
  236. }
  237. // Error asserts that a function returned an error (i.e. not `nil`).
  238. //
  239. // actualObj, err := SomeFunction()
  240. // require.Error(t, err, "An error was expected")
  241. // require.Equal(t, err, expectedError)
  242. // }
  243. func Error(t TestingT, err error, msgAndArgs ...interface{}) {
  244. if !assert.Error(t, err, msgAndArgs...) {
  245. t.FailNow()
  246. }
  247. }
  248. // EqualError asserts that a function returned an error (i.e. not `nil`)
  249. // and that it is equal to the provided error.
  250. //
  251. // actualObj, err := SomeFunction()
  252. // require.Error(t, err, "An error was expected")
  253. // require.Equal(t, err, expectedError)
  254. // }
  255. func EqualError(t TestingT, theError error, errString string, msgAndArgs ...interface{}) {
  256. if !assert.EqualError(t, theError, errString, msgAndArgs...) {
  257. t.FailNow()
  258. }
  259. }
  260. // Zero asserts that i is the zero value for its type and returns the truth.
  261. func Zero(t TestingT, i interface{}, msgAndArgs ...interface{}) {
  262. if !assert.Zero(t, i, msgAndArgs...) {
  263. t.FailNow()
  264. }
  265. }
  266. // NotZero asserts that i is not the zero value for its type and returns the truth.
  267. func NotZero(t TestingT, i interface{}, msgAndArgs ...interface{}) {
  268. if !assert.NotZero(t, i, msgAndArgs...) {
  269. t.FailNow()
  270. }
  271. }