forward_requirements.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package require
  2. import (
  3. "time"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. type Assertions struct {
  7. t TestingT
  8. }
  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{}) {
  16. FailNow(a.t, failureMessage, msgAndArgs...)
  17. }
  18. // Implements asserts that an object is implemented by the specified interface.
  19. func (a *Assertions) Implements(interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) {
  20. Implements(a.t, interfaceObject, object, msgAndArgs...)
  21. }
  22. // IsType asserts that the specified objects are of the same type.
  23. func (a *Assertions) IsType(expectedType interface{}, object interface{}, msgAndArgs ...interface{}) {
  24. IsType(a.t, expectedType, object, msgAndArgs...)
  25. }
  26. // Equal asserts that two objects are equal.
  27. //
  28. // require.Equal(123, 123, "123 and 123 should be equal")
  29. func (a *Assertions) Equal(expected, actual interface{}, msgAndArgs ...interface{}) {
  30. Equal(a.t, expected, actual, msgAndArgs...)
  31. }
  32. // Exactly asserts that two objects are equal is value and type.
  33. //
  34. // require.Exactly(int32(123), int64(123), "123 and 123 should NOT be equal")
  35. func (a *Assertions) Exactly(expected, actual interface{}, msgAndArgs ...interface{}) {
  36. Exactly(a.t, expected, actual, msgAndArgs...)
  37. }
  38. // NotNil asserts that the specified object is not nil.
  39. //
  40. // require.NotNil(err, "err should be something")
  41. func (a *Assertions) NotNil(object interface{}, msgAndArgs ...interface{}) {
  42. NotNil(a.t, object, msgAndArgs...)
  43. }
  44. // Nil asserts that the specified object is nil.
  45. //
  46. // require.Nil(err, "err should be nothing")
  47. func (a *Assertions) Nil(object interface{}, msgAndArgs ...interface{}) {
  48. Nil(a.t, object, msgAndArgs...)
  49. }
  50. // Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or a
  51. // slice with len == 0.
  52. //
  53. // require.Empty(obj)
  54. func (a *Assertions) Empty(object interface{}, msgAndArgs ...interface{}) {
  55. Empty(a.t, object, msgAndArgs...)
  56. }
  57. // Empty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or a
  58. // slice with len == 0.
  59. //
  60. // if require.NotEmpty(obj) {
  61. // require.Equal("two", obj[1])
  62. // }
  63. func (a *Assertions) NotEmpty(object interface{}, msgAndArgs ...interface{}) {
  64. NotEmpty(a.t, object, msgAndArgs...)
  65. }
  66. // Len asserts that the specified object has specific length.
  67. // Len also fails if the object has a type that len() not accept.
  68. //
  69. // require.Len(mySlice, 3, "The size of slice is not 3")
  70. func (a *Assertions) Len(object interface{}, length int, msgAndArgs ...interface{}) {
  71. Len(a.t, object, length, msgAndArgs...)
  72. }
  73. // True asserts that the specified value is true.
  74. //
  75. // require.True(myBool, "myBool should be true")
  76. func (a *Assertions) True(value bool, msgAndArgs ...interface{}) {
  77. True(a.t, value, msgAndArgs...)
  78. }
  79. // False asserts that the specified value is false.
  80. //
  81. // require.False(myBool, "myBool should be false")
  82. func (a *Assertions) False(value bool, msgAndArgs ...interface{}) {
  83. False(a.t, value, msgAndArgs...)
  84. }
  85. // NotEqual asserts that the specified values are NOT equal.
  86. //
  87. // require.NotEqual(obj1, obj2, "two objects shouldn't be equal")
  88. func (a *Assertions) NotEqual(expected, actual interface{}, msgAndArgs ...interface{}) {
  89. NotEqual(a.t, expected, actual, msgAndArgs...)
  90. }
  91. // Contains asserts that the specified string contains the specified substring.
  92. //
  93. // require.Contains("Hello World", "World", "But 'Hello World' does contain 'World'")
  94. func (a *Assertions) Contains(s, contains interface{}, msgAndArgs ...interface{}) {
  95. Contains(a.t, s, contains, msgAndArgs...)
  96. }
  97. // NotContains asserts that the specified string does NOT contain the specified substring.
  98. //
  99. // require.NotContains("Hello World", "Earth", "But 'Hello World' does NOT contain 'Earth'")
  100. func (a *Assertions) NotContains(s, contains interface{}, msgAndArgs ...interface{}) {
  101. NotContains(a.t, s, contains, msgAndArgs...)
  102. }
  103. // Uses a Comparison to assert a complex condition.
  104. func (a *Assertions) Condition(comp assert.Comparison, msgAndArgs ...interface{}) {
  105. Condition(a.t, comp, msgAndArgs...)
  106. }
  107. // Panics asserts that the code inside the specified PanicTestFunc panics.
  108. //
  109. // require.Panics(func(){
  110. // GoCrazy()
  111. // }, "Calling GoCrazy() should panic")
  112. func (a *Assertions) Panics(f assert.PanicTestFunc, msgAndArgs ...interface{}) {
  113. Panics(a.t, f, msgAndArgs...)
  114. }
  115. // NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic.
  116. //
  117. // require.NotPanics(func(){
  118. // RemainCalm()
  119. // }, "Calling RemainCalm() should NOT panic")
  120. func (a *Assertions) NotPanics(f assert.PanicTestFunc, msgAndArgs ...interface{}) {
  121. NotPanics(a.t, f, msgAndArgs...)
  122. }
  123. // WithinDuration asserts that the two times are within duration delta of each other.
  124. //
  125. // require.WithinDuration(time.Now(), time.Now(), 10*time.Second, "The difference should not be more than 10s")
  126. func (a *Assertions) WithinDuration(expected, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) {
  127. WithinDuration(a.t, expected, actual, delta, msgAndArgs...)
  128. }
  129. // InDelta asserts that the two numerals are within delta of each other.
  130. //
  131. // require.InDelta(t, math.Pi, (22 / 7.0), 0.01)
  132. func (a *Assertions) InDelta(expected, actual interface{}, delta float64, msgAndArgs ...interface{}) {
  133. InDelta(a.t, expected, actual, delta, msgAndArgs...)
  134. }
  135. // InEpsilon asserts that expected and actual have a relative error less than epsilon
  136. func (a *Assertions) InEpsilon(expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) {
  137. InEpsilon(a.t, expected, actual, epsilon, msgAndArgs...)
  138. }
  139. // NoError asserts that a function returned no error (i.e. `nil`).
  140. //
  141. // actualObj, err := SomeFunction()
  142. // if require.NoError(err) {
  143. // require.Equal(actualObj, expectedObj)
  144. // }
  145. func (a *Assertions) NoError(theError error, msgAndArgs ...interface{}) {
  146. NoError(a.t, theError, msgAndArgs...)
  147. }
  148. // Error asserts that a function returned an error (i.e. not `nil`).
  149. //
  150. // actualObj, err := SomeFunction()
  151. // if require.Error(err, "An error was expected") {
  152. // require.Equal(err, expectedError)
  153. // }
  154. func (a *Assertions) Error(theError error, msgAndArgs ...interface{}) {
  155. Error(a.t, theError, msgAndArgs...)
  156. }
  157. // EqualError asserts that a function returned an error (i.e. not `nil`)
  158. // and that it is equal to the provided error.
  159. //
  160. // actualObj, err := SomeFunction()
  161. // if require.Error(err, "An error was expected") {
  162. // require.Equal(err, expectedError)
  163. // }
  164. func (a *Assertions) EqualError(theError error, errString string, msgAndArgs ...interface{}) {
  165. EqualError(a.t, theError, errString, msgAndArgs...)
  166. }
  167. // Regexp asserts that a specified regexp matches a string.
  168. //
  169. // require.Regexp(t, regexp.MustCompile("start"), "it's starting")
  170. // require.Regexp(t, "start...$", "it's not starting")
  171. func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) {
  172. Regexp(a.t, rx, str, msgAndArgs...)
  173. }
  174. // NotRegexp asserts that a specified regexp does not match a string.
  175. //
  176. // require.NotRegexp(t, regexp.MustCompile("starts"), "it's starting")
  177. // require.NotRegexp(t, "^start", "it's not starting")
  178. func (a *Assertions) NotRegexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) {
  179. NotRegexp(a.t, rx, str, msgAndArgs...)
  180. }
  181. // Zero asserts that i is the zero value for its type and returns the truth.
  182. func (a *Assertions) Zero(i interface{}, msgAndArgs ...interface{}) {
  183. Zero(a.t, i, msgAndArgs...)
  184. }
  185. // NotZero asserts that i is not the zero value for its type and returns the truth.
  186. func (a *Assertions) NotZero(i interface{}, msgAndArgs ...interface{}) {
  187. NotZero(a.t, i, msgAndArgs...)
  188. }
  189. // JSONEq asserts that two JSON strings are equivalent.
  190. //
  191. // assert.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
  192. //
  193. // Returns whether the assertion was successful (true) or not (false).
  194. func (a *Assertions) JSONEq(expected string, actual string, msgAndArgs ...interface{}) {
  195. JSONEq(a.t, expected, actual, msgAndArgs...)
  196. }