forward_assertions.go 10 KB

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