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