├── require_forward.go.tmpl ├── require.go.tmpl ├── go.mod ├── requirements.go ├── Readme.md ├── forward_requirements.go ├── doc.go ├── LICENSE ├── go.sum ├── requirements_test.go ├── forward_requirements_test.go ├── require_forward.go └── require.go /require_forward.go.tmpl: -------------------------------------------------------------------------------- 1 | {{.CommentWithoutT "a"}} 2 | func (a *Assertions) {{.DocInfo.Name}}({{.Params}}) { 3 | {{.DocInfo.Name}}(a.t, {{.ForwardedParams}}) 4 | } 5 | -------------------------------------------------------------------------------- /require.go.tmpl: -------------------------------------------------------------------------------- 1 | {{.Comment}} 2 | func {{.DocInfo.Name}}(t TestingT, {{.Params}}) { 3 | if !assert.{{.DocInfo.Name}}(t, {{.ForwardedParams}}) { 4 | t.FailNow() 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tj/assert 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/davecgh/go-spew v1.1.1 // indirect 7 | github.com/stretchr/testify v1.6.1 8 | gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c // indirect 9 | ) 10 | -------------------------------------------------------------------------------- /requirements.go: -------------------------------------------------------------------------------- 1 | package assert 2 | 3 | // TestingT is an interface wrapper around *testing.T 4 | type TestingT interface { 5 | Errorf(format string, args ...interface{}) 6 | FailNow() 7 | } 8 | 9 | //go:generate go run ../_codegen/main.go -output-package=require -template=require.go.tmpl 10 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # assert [![GoDoc](https://godoc.org/github.com/tj/assert?status.svg)](https://godoc.org/github.com/tj/assert) 2 | 3 | Assertion pkg for Go, copied from [github.com/stretchr/testify's](//github.com/stretchr/testify) require package. 4 | 5 | I find early errors more useful than the `t.Errorf()` calls, which often fall through to nil pointers causing panics etc. 6 | -------------------------------------------------------------------------------- /forward_requirements.go: -------------------------------------------------------------------------------- 1 | package assert 2 | 3 | // Assertions provides assertion methods around the 4 | // TestingT interface. 5 | type Assertions struct { 6 | t TestingT 7 | } 8 | 9 | // New makes a new Assertions object for the specified TestingT. 10 | func New(t TestingT) *Assertions { 11 | return &Assertions{ 12 | t: t, 13 | } 14 | } 15 | 16 | //go:generate go run ../_codegen/main.go -output-package=require -template=require_forward.go.tmpl 17 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | // Package assert implements the same assertions as the `assert` package but 2 | // stops test execution when a test fails. 3 | // 4 | // Example Usage 5 | // 6 | // The following is a complete example using require in a standard test function: 7 | // import ( 8 | // "testing" 9 | // "github.com/stretchr/testify/require" 10 | // ) 11 | // 12 | // func TestSomething(t *testing.T) { 13 | // 14 | // var a string = "Hello" 15 | // var b string = "Hello" 16 | // 17 | // require.Equal(t, a, b, "The two words should be the same.") 18 | // 19 | // } 20 | // 21 | // Assertions 22 | // 23 | // The `require` package have same global functions as in the `assert` package, 24 | // but instead of returning a boolean result they call `t.FailNow()`. 25 | // 26 | // Every assertion function also takes an optional string message as the final argument, 27 | // allowing custom error messages to be appended to the message the assertion method outputs. 28 | package assert 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 TJ Holowaychuk 4 | Copyright (c) 2012-2018 Mat Ryer and Tyler Bunnell 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 2 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 4 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 5 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 6 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 7 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 8 | github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= 9 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 10 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 11 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 12 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= 13 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 14 | gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo= 15 | gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 16 | -------------------------------------------------------------------------------- /requirements_test.go: -------------------------------------------------------------------------------- 1 | package assert 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | "time" 7 | ) 8 | 9 | // AssertionTesterInterface defines an interface to be used for testing assertion methods 10 | type AssertionTesterInterface interface { 11 | TestMethod() 12 | } 13 | 14 | // AssertionTesterConformingObject is an object that conforms to the AssertionTesterInterface interface 15 | type AssertionTesterConformingObject struct { 16 | } 17 | 18 | func (a *AssertionTesterConformingObject) TestMethod() { 19 | } 20 | 21 | // AssertionTesterNonConformingObject is an object that does not conform to the AssertionTesterInterface interface 22 | type AssertionTesterNonConformingObject struct { 23 | } 24 | 25 | type MockT struct { 26 | Failed bool 27 | } 28 | 29 | func (t *MockT) FailNow() { 30 | t.Failed = true 31 | } 32 | 33 | func (t *MockT) Errorf(format string, args ...interface{}) { 34 | _, _ = format, args 35 | } 36 | 37 | func TestImplements(t *testing.T) { 38 | 39 | Implements(t, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) 40 | 41 | mockT := new(MockT) 42 | Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) 43 | if !mockT.Failed { 44 | t.Error("Check should fail") 45 | } 46 | } 47 | 48 | func TestIsType(t *testing.T) { 49 | 50 | IsType(t, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) 51 | 52 | mockT := new(MockT) 53 | IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) 54 | if !mockT.Failed { 55 | t.Error("Check should fail") 56 | } 57 | } 58 | 59 | func TestEqual(t *testing.T) { 60 | 61 | Equal(t, 1, 1) 62 | 63 | mockT := new(MockT) 64 | Equal(mockT, 1, 2) 65 | if !mockT.Failed { 66 | t.Error("Check should fail") 67 | } 68 | 69 | } 70 | 71 | func TestNotEqual(t *testing.T) { 72 | 73 | NotEqual(t, 1, 2) 74 | mockT := new(MockT) 75 | NotEqual(mockT, 2, 2) 76 | if !mockT.Failed { 77 | t.Error("Check should fail") 78 | } 79 | } 80 | 81 | func TestExactly(t *testing.T) { 82 | 83 | a := float32(1) 84 | b := float32(1) 85 | c := float64(1) 86 | 87 | Exactly(t, a, b) 88 | 89 | mockT := new(MockT) 90 | Exactly(mockT, a, c) 91 | if !mockT.Failed { 92 | t.Error("Check should fail") 93 | } 94 | } 95 | 96 | func TestNotNil(t *testing.T) { 97 | 98 | NotNil(t, new(AssertionTesterConformingObject)) 99 | 100 | mockT := new(MockT) 101 | NotNil(mockT, nil) 102 | if !mockT.Failed { 103 | t.Error("Check should fail") 104 | } 105 | } 106 | 107 | func TestNil(t *testing.T) { 108 | 109 | Nil(t, nil) 110 | 111 | mockT := new(MockT) 112 | Nil(mockT, new(AssertionTesterConformingObject)) 113 | if !mockT.Failed { 114 | t.Error("Check should fail") 115 | } 116 | } 117 | 118 | func TestTrue(t *testing.T) { 119 | 120 | True(t, true) 121 | 122 | mockT := new(MockT) 123 | True(mockT, false) 124 | if !mockT.Failed { 125 | t.Error("Check should fail") 126 | } 127 | } 128 | 129 | func TestFalse(t *testing.T) { 130 | 131 | False(t, false) 132 | 133 | mockT := new(MockT) 134 | False(mockT, true) 135 | if !mockT.Failed { 136 | t.Error("Check should fail") 137 | } 138 | } 139 | 140 | func TestContains(t *testing.T) { 141 | 142 | Contains(t, "Hello World", "Hello") 143 | 144 | mockT := new(MockT) 145 | Contains(mockT, "Hello World", "Salut") 146 | if !mockT.Failed { 147 | t.Error("Check should fail") 148 | } 149 | } 150 | 151 | func TestNotContains(t *testing.T) { 152 | 153 | NotContains(t, "Hello World", "Hello!") 154 | 155 | mockT := new(MockT) 156 | NotContains(mockT, "Hello World", "Hello") 157 | if !mockT.Failed { 158 | t.Error("Check should fail") 159 | } 160 | } 161 | 162 | func TestPanics(t *testing.T) { 163 | 164 | Panics(t, func() { 165 | panic("Panic!") 166 | }) 167 | 168 | mockT := new(MockT) 169 | Panics(mockT, func() {}) 170 | if !mockT.Failed { 171 | t.Error("Check should fail") 172 | } 173 | } 174 | 175 | func TestNotPanics(t *testing.T) { 176 | 177 | NotPanics(t, func() {}) 178 | 179 | mockT := new(MockT) 180 | NotPanics(mockT, func() { 181 | panic("Panic!") 182 | }) 183 | if !mockT.Failed { 184 | t.Error("Check should fail") 185 | } 186 | } 187 | 188 | func TestNoError(t *testing.T) { 189 | 190 | NoError(t, nil) 191 | 192 | mockT := new(MockT) 193 | NoError(mockT, errors.New("some error")) 194 | if !mockT.Failed { 195 | t.Error("Check should fail") 196 | } 197 | } 198 | 199 | func TestError(t *testing.T) { 200 | 201 | Error(t, errors.New("some error")) 202 | 203 | mockT := new(MockT) 204 | Error(mockT, nil) 205 | if !mockT.Failed { 206 | t.Error("Check should fail") 207 | } 208 | } 209 | 210 | func TestEqualError(t *testing.T) { 211 | 212 | EqualError(t, errors.New("some error"), "some error") 213 | 214 | mockT := new(MockT) 215 | EqualError(mockT, errors.New("some error"), "Not some error") 216 | if !mockT.Failed { 217 | t.Error("Check should fail") 218 | } 219 | } 220 | 221 | func TestEmpty(t *testing.T) { 222 | 223 | Empty(t, "") 224 | 225 | mockT := new(MockT) 226 | Empty(mockT, "x") 227 | if !mockT.Failed { 228 | t.Error("Check should fail") 229 | } 230 | } 231 | 232 | func TestNotEmpty(t *testing.T) { 233 | 234 | NotEmpty(t, "x") 235 | 236 | mockT := new(MockT) 237 | NotEmpty(mockT, "") 238 | if !mockT.Failed { 239 | t.Error("Check should fail") 240 | } 241 | } 242 | 243 | func TestWithinDuration(t *testing.T) { 244 | 245 | a := time.Now() 246 | b := a.Add(10 * time.Second) 247 | 248 | WithinDuration(t, a, b, 15*time.Second) 249 | 250 | mockT := new(MockT) 251 | WithinDuration(mockT, a, b, 5*time.Second) 252 | if !mockT.Failed { 253 | t.Error("Check should fail") 254 | } 255 | } 256 | 257 | func TestInDelta(t *testing.T) { 258 | 259 | InDelta(t, 1.001, 1, 0.01) 260 | 261 | mockT := new(MockT) 262 | InDelta(mockT, 1, 2, 0.5) 263 | if !mockT.Failed { 264 | t.Error("Check should fail") 265 | } 266 | } 267 | 268 | func TestZero(t *testing.T) { 269 | 270 | Zero(t, "") 271 | 272 | mockT := new(MockT) 273 | Zero(mockT, "x") 274 | if !mockT.Failed { 275 | t.Error("Check should fail") 276 | } 277 | } 278 | 279 | func TestNotZero(t *testing.T) { 280 | 281 | NotZero(t, "x") 282 | 283 | mockT := new(MockT) 284 | NotZero(mockT, "") 285 | if !mockT.Failed { 286 | t.Error("Check should fail") 287 | } 288 | } 289 | 290 | func TestJSONEq_EqualSONString(t *testing.T) { 291 | mockT := new(MockT) 292 | JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`) 293 | if mockT.Failed { 294 | t.Error("Check should pass") 295 | } 296 | } 297 | 298 | func TestJSONEq_EquivalentButNotEqual(t *testing.T) { 299 | mockT := new(MockT) 300 | JSONEq(mockT, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) 301 | if mockT.Failed { 302 | t.Error("Check should pass") 303 | } 304 | } 305 | 306 | func TestJSONEq_HashOfArraysAndHashes(t *testing.T) { 307 | mockT := new(MockT) 308 | JSONEq(mockT, "{\r\n\t\"numeric\": 1.5,\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]],\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\"\r\n}", 309 | "{\r\n\t\"numeric\": 1.5,\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\",\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]]\r\n}") 310 | if mockT.Failed { 311 | t.Error("Check should pass") 312 | } 313 | } 314 | 315 | func TestJSONEq_Array(t *testing.T) { 316 | mockT := new(MockT) 317 | JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`) 318 | if mockT.Failed { 319 | t.Error("Check should pass") 320 | } 321 | } 322 | 323 | func TestJSONEq_HashAndArrayNotEquivalent(t *testing.T) { 324 | mockT := new(MockT) 325 | JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`) 326 | if !mockT.Failed { 327 | t.Error("Check should fail") 328 | } 329 | } 330 | 331 | func TestJSONEq_HashesNotEquivalent(t *testing.T) { 332 | mockT := new(MockT) 333 | JSONEq(mockT, `{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) 334 | if !mockT.Failed { 335 | t.Error("Check should fail") 336 | } 337 | } 338 | 339 | func TestJSONEq_ActualIsNotJSON(t *testing.T) { 340 | mockT := new(MockT) 341 | JSONEq(mockT, `{"foo": "bar"}`, "Not JSON") 342 | if !mockT.Failed { 343 | t.Error("Check should fail") 344 | } 345 | } 346 | 347 | func TestJSONEq_ExpectedIsNotJSON(t *testing.T) { 348 | mockT := new(MockT) 349 | JSONEq(mockT, "Not JSON", `{"foo": "bar", "hello": "world"}`) 350 | if !mockT.Failed { 351 | t.Error("Check should fail") 352 | } 353 | } 354 | 355 | func TestJSONEq_ExpectedAndActualNotJSON(t *testing.T) { 356 | mockT := new(MockT) 357 | JSONEq(mockT, "Not JSON", "Not JSON") 358 | if !mockT.Failed { 359 | t.Error("Check should fail") 360 | } 361 | } 362 | 363 | func TestJSONEq_ArraysOfDifferentOrder(t *testing.T) { 364 | mockT := new(MockT) 365 | JSONEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`) 366 | if !mockT.Failed { 367 | t.Error("Check should fail") 368 | } 369 | } 370 | -------------------------------------------------------------------------------- /forward_requirements_test.go: -------------------------------------------------------------------------------- 1 | package assert 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | "time" 7 | ) 8 | 9 | func TestImplementsWrapper(t *testing.T) { 10 | require := New(t) 11 | 12 | require.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) 13 | 14 | mockT := new(MockT) 15 | mockRequire := New(mockT) 16 | mockRequire.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) 17 | if !mockT.Failed { 18 | t.Error("Check should fail") 19 | } 20 | } 21 | 22 | func TestIsTypeWrapper(t *testing.T) { 23 | require := New(t) 24 | require.IsType(new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) 25 | 26 | mockT := new(MockT) 27 | mockRequire := New(mockT) 28 | mockRequire.IsType(new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) 29 | if !mockT.Failed { 30 | t.Error("Check should fail") 31 | } 32 | } 33 | 34 | func TestEqualWrapper(t *testing.T) { 35 | require := New(t) 36 | require.Equal(1, 1) 37 | 38 | mockT := new(MockT) 39 | mockRequire := New(mockT) 40 | mockRequire.Equal(1, 2) 41 | if !mockT.Failed { 42 | t.Error("Check should fail") 43 | } 44 | } 45 | 46 | func TestNotEqualWrapper(t *testing.T) { 47 | require := New(t) 48 | require.NotEqual(1, 2) 49 | 50 | mockT := new(MockT) 51 | mockRequire := New(mockT) 52 | mockRequire.NotEqual(2, 2) 53 | if !mockT.Failed { 54 | t.Error("Check should fail") 55 | } 56 | } 57 | 58 | func TestExactlyWrapper(t *testing.T) { 59 | require := New(t) 60 | 61 | a := float32(1) 62 | b := float32(1) 63 | c := float64(1) 64 | 65 | require.Exactly(a, b) 66 | 67 | mockT := new(MockT) 68 | mockRequire := New(mockT) 69 | mockRequire.Exactly(a, c) 70 | if !mockT.Failed { 71 | t.Error("Check should fail") 72 | } 73 | } 74 | 75 | func TestNotNilWrapper(t *testing.T) { 76 | require := New(t) 77 | require.NotNil(t, new(AssertionTesterConformingObject)) 78 | 79 | mockT := new(MockT) 80 | mockRequire := New(mockT) 81 | mockRequire.NotNil(nil) 82 | if !mockT.Failed { 83 | t.Error("Check should fail") 84 | } 85 | } 86 | 87 | func TestNilWrapper(t *testing.T) { 88 | require := New(t) 89 | require.Nil(nil) 90 | 91 | mockT := new(MockT) 92 | mockRequire := New(mockT) 93 | mockRequire.Nil(new(AssertionTesterConformingObject)) 94 | if !mockT.Failed { 95 | t.Error("Check should fail") 96 | } 97 | } 98 | 99 | func TestTrueWrapper(t *testing.T) { 100 | require := New(t) 101 | require.True(true) 102 | 103 | mockT := new(MockT) 104 | mockRequire := New(mockT) 105 | mockRequire.True(false) 106 | if !mockT.Failed { 107 | t.Error("Check should fail") 108 | } 109 | } 110 | 111 | func TestFalseWrapper(t *testing.T) { 112 | require := New(t) 113 | require.False(false) 114 | 115 | mockT := new(MockT) 116 | mockRequire := New(mockT) 117 | mockRequire.False(true) 118 | if !mockT.Failed { 119 | t.Error("Check should fail") 120 | } 121 | } 122 | 123 | func TestContainsWrapper(t *testing.T) { 124 | require := New(t) 125 | require.Contains("Hello World", "Hello") 126 | 127 | mockT := new(MockT) 128 | mockRequire := New(mockT) 129 | mockRequire.Contains("Hello World", "Salut") 130 | if !mockT.Failed { 131 | t.Error("Check should fail") 132 | } 133 | } 134 | 135 | func TestNotContainsWrapper(t *testing.T) { 136 | require := New(t) 137 | require.NotContains("Hello World", "Hello!") 138 | 139 | mockT := new(MockT) 140 | mockRequire := New(mockT) 141 | mockRequire.NotContains("Hello World", "Hello") 142 | if !mockT.Failed { 143 | t.Error("Check should fail") 144 | } 145 | } 146 | 147 | func TestPanicsWrapper(t *testing.T) { 148 | require := New(t) 149 | require.Panics(func() { 150 | panic("Panic!") 151 | }) 152 | 153 | mockT := new(MockT) 154 | mockRequire := New(mockT) 155 | mockRequire.Panics(func() {}) 156 | if !mockT.Failed { 157 | t.Error("Check should fail") 158 | } 159 | } 160 | 161 | func TestNotPanicsWrapper(t *testing.T) { 162 | require := New(t) 163 | require.NotPanics(func() {}) 164 | 165 | mockT := new(MockT) 166 | mockRequire := New(mockT) 167 | mockRequire.NotPanics(func() { 168 | panic("Panic!") 169 | }) 170 | if !mockT.Failed { 171 | t.Error("Check should fail") 172 | } 173 | } 174 | 175 | func TestNoErrorWrapper(t *testing.T) { 176 | require := New(t) 177 | require.NoError(nil) 178 | 179 | mockT := new(MockT) 180 | mockRequire := New(mockT) 181 | mockRequire.NoError(errors.New("some error")) 182 | if !mockT.Failed { 183 | t.Error("Check should fail") 184 | } 185 | } 186 | 187 | func TestErrorWrapper(t *testing.T) { 188 | require := New(t) 189 | require.Error(errors.New("some error")) 190 | 191 | mockT := new(MockT) 192 | mockRequire := New(mockT) 193 | mockRequire.Error(nil) 194 | if !mockT.Failed { 195 | t.Error("Check should fail") 196 | } 197 | } 198 | 199 | func TestEqualErrorWrapper(t *testing.T) { 200 | require := New(t) 201 | require.EqualError(errors.New("some error"), "some error") 202 | 203 | mockT := new(MockT) 204 | mockRequire := New(mockT) 205 | mockRequire.EqualError(errors.New("some error"), "Not some error") 206 | if !mockT.Failed { 207 | t.Error("Check should fail") 208 | } 209 | } 210 | 211 | func TestEmptyWrapper(t *testing.T) { 212 | require := New(t) 213 | require.Empty("") 214 | 215 | mockT := new(MockT) 216 | mockRequire := New(mockT) 217 | mockRequire.Empty("x") 218 | if !mockT.Failed { 219 | t.Error("Check should fail") 220 | } 221 | } 222 | 223 | func TestNotEmptyWrapper(t *testing.T) { 224 | require := New(t) 225 | require.NotEmpty("x") 226 | 227 | mockT := new(MockT) 228 | mockRequire := New(mockT) 229 | mockRequire.NotEmpty("") 230 | if !mockT.Failed { 231 | t.Error("Check should fail") 232 | } 233 | } 234 | 235 | func TestWithinDurationWrapper(t *testing.T) { 236 | require := New(t) 237 | a := time.Now() 238 | b := a.Add(10 * time.Second) 239 | 240 | require.WithinDuration(a, b, 15*time.Second) 241 | 242 | mockT := new(MockT) 243 | mockRequire := New(mockT) 244 | mockRequire.WithinDuration(a, b, 5*time.Second) 245 | if !mockT.Failed { 246 | t.Error("Check should fail") 247 | } 248 | } 249 | 250 | func TestInDeltaWrapper(t *testing.T) { 251 | require := New(t) 252 | require.InDelta(1.001, 1, 0.01) 253 | 254 | mockT := new(MockT) 255 | mockRequire := New(mockT) 256 | mockRequire.InDelta(1, 2, 0.5) 257 | if !mockT.Failed { 258 | t.Error("Check should fail") 259 | } 260 | } 261 | 262 | func TestZeroWrapper(t *testing.T) { 263 | require := New(t) 264 | require.Zero(0) 265 | 266 | mockT := new(MockT) 267 | mockRequire := New(mockT) 268 | mockRequire.Zero(1) 269 | if !mockT.Failed { 270 | t.Error("Check should fail") 271 | } 272 | } 273 | 274 | func TestNotZeroWrapper(t *testing.T) { 275 | require := New(t) 276 | require.NotZero(1) 277 | 278 | mockT := new(MockT) 279 | mockRequire := New(mockT) 280 | mockRequire.NotZero(0) 281 | if !mockT.Failed { 282 | t.Error("Check should fail") 283 | } 284 | } 285 | 286 | func TestJSONEqWrapper_EqualSONString(t *testing.T) { 287 | mockT := new(MockT) 288 | mockRequire := New(mockT) 289 | 290 | mockRequire.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`) 291 | if mockT.Failed { 292 | t.Error("Check should pass") 293 | } 294 | } 295 | 296 | func TestJSONEqWrapper_EquivalentButNotEqual(t *testing.T) { 297 | mockT := new(MockT) 298 | mockRequire := New(mockT) 299 | 300 | mockRequire.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) 301 | if mockT.Failed { 302 | t.Error("Check should pass") 303 | } 304 | } 305 | 306 | func TestJSONEqWrapper_HashOfArraysAndHashes(t *testing.T) { 307 | mockT := new(MockT) 308 | mockRequire := New(mockT) 309 | 310 | mockRequire.JSONEq("{\r\n\t\"numeric\": 1.5,\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]],\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\"\r\n}", 311 | "{\r\n\t\"numeric\": 1.5,\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\",\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]]\r\n}") 312 | if mockT.Failed { 313 | t.Error("Check should pass") 314 | } 315 | } 316 | 317 | func TestJSONEqWrapper_Array(t *testing.T) { 318 | mockT := new(MockT) 319 | mockRequire := New(mockT) 320 | 321 | mockRequire.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`) 322 | if mockT.Failed { 323 | t.Error("Check should pass") 324 | } 325 | } 326 | 327 | func TestJSONEqWrapper_HashAndArrayNotEquivalent(t *testing.T) { 328 | mockT := new(MockT) 329 | mockRequire := New(mockT) 330 | 331 | mockRequire.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`) 332 | if !mockT.Failed { 333 | t.Error("Check should fail") 334 | } 335 | } 336 | 337 | func TestJSONEqWrapper_HashesNotEquivalent(t *testing.T) { 338 | mockT := new(MockT) 339 | mockRequire := New(mockT) 340 | 341 | mockRequire.JSONEq(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) 342 | if !mockT.Failed { 343 | t.Error("Check should fail") 344 | } 345 | } 346 | 347 | func TestJSONEqWrapper_ActualIsNotJSON(t *testing.T) { 348 | mockT := new(MockT) 349 | mockRequire := New(mockT) 350 | 351 | mockRequire.JSONEq(`{"foo": "bar"}`, "Not JSON") 352 | if !mockT.Failed { 353 | t.Error("Check should fail") 354 | } 355 | } 356 | 357 | func TestJSONEqWrapper_ExpectedIsNotJSON(t *testing.T) { 358 | mockT := new(MockT) 359 | mockRequire := New(mockT) 360 | 361 | mockRequire.JSONEq("Not JSON", `{"foo": "bar", "hello": "world"}`) 362 | if !mockT.Failed { 363 | t.Error("Check should fail") 364 | } 365 | } 366 | 367 | func TestJSONEqWrapper_ExpectedAndActualNotJSON(t *testing.T) { 368 | mockT := new(MockT) 369 | mockRequire := New(mockT) 370 | 371 | mockRequire.JSONEq("Not JSON", "Not JSON") 372 | if !mockT.Failed { 373 | t.Error("Check should fail") 374 | } 375 | } 376 | 377 | func TestJSONEqWrapper_ArraysOfDifferentOrder(t *testing.T) { 378 | mockT := new(MockT) 379 | mockRequire := New(mockT) 380 | 381 | mockRequire.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`) 382 | if !mockT.Failed { 383 | t.Error("Check should fail") 384 | } 385 | } 386 | -------------------------------------------------------------------------------- /require_forward.go: -------------------------------------------------------------------------------- 1 | /* 2 | * CODE GENERATED AUTOMATICALLY WITH github.com/stretchr/testify/_codegen 3 | * THIS FILE MUST NOT BE EDITED BY HAND 4 | */ 5 | 6 | package assert 7 | 8 | import ( 9 | http "net/http" 10 | url "net/url" 11 | time "time" 12 | 13 | assert "github.com/stretchr/testify/assert" 14 | ) 15 | 16 | // Condition uses a Comparison to assert a complex condition. 17 | func (a *Assertions) Condition(comp assert.Comparison, msgAndArgs ...interface{}) { 18 | Condition(a.t, comp, msgAndArgs...) 19 | } 20 | 21 | // Contains asserts that the specified string, list(array, slice...) or map contains the 22 | // specified substring or element. 23 | // 24 | // a.Contains("Hello World", "World") 25 | // a.Contains(["Hello", "World"], "World") 26 | // a.Contains({"Hello": "World"}, "Hello") 27 | func (a *Assertions) Contains(s interface{}, contains interface{}, msgAndArgs ...interface{}) { 28 | Contains(a.t, s, contains, msgAndArgs...) 29 | } 30 | 31 | // DirExists checks whether a directory exists in the given path. It also fails 32 | // if the path is a file rather a directory or there is an error checking whether it exists. 33 | func (a *Assertions) DirExists(path string, msgAndArgs ...interface{}) { 34 | DirExists(a.t, path, msgAndArgs...) 35 | } 36 | 37 | // ElementsMatch asserts that the specified listA(array, slice...) is equal to specified 38 | // listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, 39 | // the number of appearances of each of them in both lists should match. 40 | // 41 | // a.ElementsMatch([1, 3, 2, 3], [1, 3, 3, 2]) 42 | func (a *Assertions) ElementsMatch(listA interface{}, listB interface{}, msgAndArgs ...interface{}) { 43 | ElementsMatch(a.t, listA, listB, msgAndArgs...) 44 | } 45 | 46 | // Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either 47 | // a slice or a channel with len == 0. 48 | // 49 | // a.Empty(obj) 50 | func (a *Assertions) Empty(object interface{}, msgAndArgs ...interface{}) { 51 | Empty(a.t, object, msgAndArgs...) 52 | } 53 | 54 | // Equal asserts that two objects are equal. 55 | // 56 | // a.Equal(123, 123) 57 | // 58 | // Pointer variable equality is determined based on the equality of the 59 | // referenced values (as opposed to the memory addresses). Function equality 60 | // cannot be determined and will always fail. 61 | func (a *Assertions) Equal(expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 62 | Equal(a.t, expected, actual, msgAndArgs...) 63 | } 64 | 65 | // EqualError asserts that a function returned an error (i.e. not `nil`) 66 | // and that it is equal to the provided error. 67 | // 68 | // actualObj, err := SomeFunction() 69 | // a.EqualError(err, expectedErrorString) 70 | func (a *Assertions) EqualError(theError error, errString string, msgAndArgs ...interface{}) { 71 | EqualError(a.t, theError, errString, msgAndArgs...) 72 | } 73 | 74 | // EqualValues asserts that two objects are equal or convertable to the same types 75 | // and equal. 76 | // 77 | // a.EqualValues(uint32(123), int32(123)) 78 | func (a *Assertions) EqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 79 | EqualValues(a.t, expected, actual, msgAndArgs...) 80 | } 81 | 82 | // Error asserts that a function returned an error (i.e. not `nil`). 83 | // 84 | // actualObj, err := SomeFunction() 85 | // if a.Error(err) { 86 | // assert.Equal(t, expectedError, err) 87 | // } 88 | func (a *Assertions) Error(err error, msgAndArgs ...interface{}) { 89 | Error(a.t, err, msgAndArgs...) 90 | } 91 | 92 | // Eventually asserts that given condition will be met in waitFor time, 93 | // periodically checking target function each tick. 94 | // 95 | // a.Eventually(func() bool { return true; }, time.Second, 10*time.Millisecond) 96 | func (a *Assertions) Eventually(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) { 97 | Eventually(a.t, condition, waitFor, tick, msgAndArgs...) 98 | } 99 | 100 | // Exactly asserts that two objects are equal in value and type. 101 | // 102 | // a.Exactly(int32(123), int64(123)) 103 | func (a *Assertions) Exactly(expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 104 | Exactly(a.t, expected, actual, msgAndArgs...) 105 | } 106 | 107 | // Fail reports a failure through 108 | func (a *Assertions) Fail(failureMessage string, msgAndArgs ...interface{}) { 109 | Fail(a.t, failureMessage, msgAndArgs...) 110 | } 111 | 112 | // FailNow fails test 113 | func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...interface{}) { 114 | FailNow(a.t, failureMessage, msgAndArgs...) 115 | } 116 | 117 | // False asserts that the specified value is false. 118 | // 119 | // a.False(myBool) 120 | func (a *Assertions) False(value bool, msgAndArgs ...interface{}) { 121 | False(a.t, value, msgAndArgs...) 122 | } 123 | 124 | // FileExists checks whether a file exists in the given path. It also fails if 125 | // the path points to a directory or there is an error when trying to check the file. 126 | func (a *Assertions) FileExists(path string, msgAndArgs ...interface{}) { 127 | FileExists(a.t, path, msgAndArgs...) 128 | } 129 | 130 | // Greater asserts that the first element is greater than the second 131 | // 132 | // a.Greater(2, 1) 133 | // a.Greater(float64(2), float64(1)) 134 | // a.Greater("b", "a") 135 | func (a *Assertions) Greater(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) { 136 | Greater(a.t, e1, e2, msgAndArgs...) 137 | } 138 | 139 | // GreaterOrEqual asserts that the first element is greater than or equal to the second 140 | // 141 | // a.GreaterOrEqual(2, 1) 142 | // a.GreaterOrEqual(2, 2) 143 | // a.GreaterOrEqual("b", "a") 144 | // a.GreaterOrEqual("b", "b") 145 | func (a *Assertions) GreaterOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) { 146 | GreaterOrEqual(a.t, e1, e2, msgAndArgs...) 147 | } 148 | 149 | // HTTPBodyContains asserts that a specified handler returns a 150 | // body that contains a string. 151 | // 152 | // a.HTTPBodyContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") 153 | // 154 | // Returns whether the assertion was successful (true) or not (false). 155 | func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) { 156 | HTTPBodyContains(a.t, handler, method, url, values, str, msgAndArgs...) 157 | } 158 | 159 | // HTTPBodyNotContains asserts that a specified handler returns a 160 | // body that does not contain a string. 161 | // 162 | // a.HTTPBodyNotContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") 163 | // 164 | // Returns whether the assertion was successful (true) or not (false). 165 | func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) { 166 | HTTPBodyNotContains(a.t, handler, method, url, values, str, msgAndArgs...) 167 | } 168 | 169 | // HTTPError asserts that a specified handler returns an error status code. 170 | // 171 | // a.HTTPError(myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} 172 | // 173 | // Returns whether the assertion was successful (true) or not (false). 174 | func (a *Assertions) HTTPError(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) { 175 | HTTPError(a.t, handler, method, url, values, msgAndArgs...) 176 | } 177 | 178 | // HTTPRedirect asserts that a specified handler returns a redirect status code. 179 | // 180 | // a.HTTPRedirect(myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} 181 | // 182 | // Returns whether the assertion was successful (true) or not (false). 183 | func (a *Assertions) HTTPRedirect(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) { 184 | HTTPRedirect(a.t, handler, method, url, values, msgAndArgs...) 185 | } 186 | 187 | // HTTPStatusCode asserts that a specified handler returns a specified status code. 188 | // 189 | // a.HTTPStatusCode(myHandler, "GET", "/notImplemented", nil, 501) 190 | // 191 | // Returns whether the assertion was successful (true) or not (false). 192 | func (a *Assertions) HTTPStatusCode(handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msgAndArgs ...interface{}) { 193 | HTTPStatusCode(a.t, handler, method, url, values, statuscode, msgAndArgs...) 194 | } 195 | 196 | // HTTPSuccess asserts that a specified handler returns a success status code. 197 | // 198 | // a.HTTPSuccess(myHandler, "POST", "http://www.google.com", nil) 199 | // 200 | // Returns whether the assertion was successful (true) or not (false). 201 | func (a *Assertions) HTTPSuccess(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) { 202 | HTTPSuccess(a.t, handler, method, url, values, msgAndArgs...) 203 | } 204 | 205 | // Implements asserts that an object is implemented by the specified interface. 206 | // 207 | // a.Implements((*MyInterface)(nil), new(MyObject)) 208 | func (a *Assertions) Implements(interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) { 209 | Implements(a.t, interfaceObject, object, msgAndArgs...) 210 | } 211 | 212 | // InDelta asserts that the two numerals are within delta of each other. 213 | // 214 | // a.InDelta(math.Pi, 22/7.0, 0.01) 215 | func (a *Assertions) InDelta(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) { 216 | InDelta(a.t, expected, actual, delta, msgAndArgs...) 217 | } 218 | 219 | // InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. 220 | func (a *Assertions) InDeltaMapValues(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) { 221 | InDeltaMapValues(a.t, expected, actual, delta, msgAndArgs...) 222 | } 223 | 224 | // InDeltaSlice is the same as InDelta, except it compares two slices. 225 | func (a *Assertions) InDeltaSlice(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) { 226 | InDeltaSlice(a.t, expected, actual, delta, msgAndArgs...) 227 | } 228 | 229 | // InEpsilon asserts that expected and actual have a relative error less than epsilon 230 | func (a *Assertions) InEpsilon(expected interface{}, actual interface{}, epsilon float64, msgAndArgs ...interface{}) { 231 | InEpsilon(a.t, expected, actual, epsilon, msgAndArgs...) 232 | } 233 | 234 | // InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices. 235 | func (a *Assertions) InEpsilonSlice(expected interface{}, actual interface{}, epsilon float64, msgAndArgs ...interface{}) { 236 | InEpsilonSlice(a.t, expected, actual, epsilon, msgAndArgs...) 237 | } 238 | 239 | // IsType asserts that the specified objects are of the same type. 240 | func (a *Assertions) IsType(expectedType interface{}, object interface{}, msgAndArgs ...interface{}) { 241 | IsType(a.t, expectedType, object, msgAndArgs...) 242 | } 243 | 244 | // JSONEq asserts that two JSON strings are equivalent. 245 | // 246 | // a.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) 247 | func (a *Assertions) JSONEq(expected string, actual string, msgAndArgs ...interface{}) { 248 | JSONEq(a.t, expected, actual, msgAndArgs...) 249 | } 250 | 251 | // Len asserts that the specified object has specific length. 252 | // Len also fails if the object has a type that len() not accept. 253 | // 254 | // a.Len(mySlice, 3) 255 | func (a *Assertions) Len(object interface{}, length int, msgAndArgs ...interface{}) { 256 | Len(a.t, object, length, msgAndArgs...) 257 | } 258 | 259 | // Less asserts that the first element is less than the second 260 | // 261 | // a.Less(1, 2) 262 | // a.Less(float64(1), float64(2)) 263 | // a.Less("a", "b") 264 | func (a *Assertions) Less(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) { 265 | Less(a.t, e1, e2, msgAndArgs...) 266 | } 267 | 268 | // LessOrEqual asserts that the first element is less than or equal to the second 269 | // 270 | // a.LessOrEqual(1, 2) 271 | // a.LessOrEqual(2, 2) 272 | // a.LessOrEqual("a", "b") 273 | // a.LessOrEqual("b", "b") 274 | func (a *Assertions) LessOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) { 275 | LessOrEqual(a.t, e1, e2, msgAndArgs...) 276 | } 277 | 278 | // Never asserts that the given condition doesn't satisfy in waitFor time, 279 | // periodically checking the target function each tick. 280 | // 281 | // a.Never(func() bool { return false; }, time.Second, 10*time.Millisecond) 282 | func (a *Assertions) Never(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) { 283 | Never(a.t, condition, waitFor, tick, msgAndArgs...) 284 | } 285 | 286 | // Nil asserts that the specified object is nil. 287 | // 288 | // a.Nil(err) 289 | func (a *Assertions) Nil(object interface{}, msgAndArgs ...interface{}) { 290 | Nil(a.t, object, msgAndArgs...) 291 | } 292 | 293 | // NoDirExists checks whether a directory does not exist in the given path. 294 | // It fails if the path points to an existing _directory_ only. 295 | func (a *Assertions) NoDirExists(path string, msgAndArgs ...interface{}) { 296 | NoDirExists(a.t, path, msgAndArgs...) 297 | } 298 | 299 | // NoError asserts that a function returned no error (i.e. `nil`). 300 | // 301 | // actualObj, err := SomeFunction() 302 | // if a.NoError(err) { 303 | // assert.Equal(t, expectedObj, actualObj) 304 | // } 305 | func (a *Assertions) NoError(err error, msgAndArgs ...interface{}) { 306 | NoError(a.t, err, msgAndArgs...) 307 | } 308 | 309 | // NoFileExists checks whether a file does not exist in a given path. It fails 310 | // if the path points to an existing _file_ only. 311 | func (a *Assertions) NoFileExists(path string, msgAndArgs ...interface{}) { 312 | NoFileExists(a.t, path, msgAndArgs...) 313 | } 314 | 315 | // NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the 316 | // specified substring or element. 317 | // 318 | // a.NotContains("Hello World", "Earth") 319 | // a.NotContains(["Hello", "World"], "Earth") 320 | // a.NotContains({"Hello": "World"}, "Earth") 321 | func (a *Assertions) NotContains(s interface{}, contains interface{}, msgAndArgs ...interface{}) { 322 | NotContains(a.t, s, contains, msgAndArgs...) 323 | } 324 | 325 | // NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either 326 | // a slice or a channel with len == 0. 327 | // 328 | // if a.NotEmpty(obj) { 329 | // assert.Equal(t, "two", obj[1]) 330 | // } 331 | func (a *Assertions) NotEmpty(object interface{}, msgAndArgs ...interface{}) { 332 | NotEmpty(a.t, object, msgAndArgs...) 333 | } 334 | 335 | // NotEqual asserts that the specified values are NOT equal. 336 | // 337 | // a.NotEqual(obj1, obj2) 338 | // 339 | // Pointer variable equality is determined based on the equality of the 340 | // referenced values (as opposed to the memory addresses). 341 | func (a *Assertions) NotEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 342 | NotEqual(a.t, expected, actual, msgAndArgs...) 343 | } 344 | 345 | // NotEqualValues asserts that two objects are not equal even when converted to the same type 346 | // 347 | // a.NotEqualValues(obj1, obj2) 348 | func (a *Assertions) NotEqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 349 | NotEqualValues(a.t, expected, actual, msgAndArgs...) 350 | } 351 | 352 | // NotNil asserts that the specified object is not nil. 353 | // 354 | // a.NotNil(err) 355 | func (a *Assertions) NotNil(object interface{}, msgAndArgs ...interface{}) { 356 | NotNil(a.t, object, msgAndArgs...) 357 | } 358 | 359 | // NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic. 360 | // 361 | // a.NotPanics(func(){ RemainCalm() }) 362 | func (a *Assertions) NotPanics(f assert.PanicTestFunc, msgAndArgs ...interface{}) { 363 | NotPanics(a.t, f, msgAndArgs...) 364 | } 365 | 366 | // NotRegexp asserts that a specified regexp does not match a string. 367 | // 368 | // a.NotRegexp(regexp.MustCompile("starts"), "it's starting") 369 | // a.NotRegexp("^start", "it's not starting") 370 | func (a *Assertions) NotRegexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) { 371 | NotRegexp(a.t, rx, str, msgAndArgs...) 372 | } 373 | 374 | // NotSame asserts that two pointers do not reference the same object. 375 | // 376 | // a.NotSame(ptr1, ptr2) 377 | // 378 | // Both arguments must be pointer variables. Pointer variable sameness is 379 | // determined based on the equality of both type and value. 380 | func (a *Assertions) NotSame(expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 381 | NotSame(a.t, expected, actual, msgAndArgs...) 382 | } 383 | 384 | // NotSubset asserts that the specified list(array, slice...) contains not all 385 | // elements given in the specified subset(array, slice...). 386 | // 387 | // a.NotSubset([1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]") 388 | func (a *Assertions) NotSubset(list interface{}, subset interface{}, msgAndArgs ...interface{}) { 389 | NotSubset(a.t, list, subset, msgAndArgs...) 390 | } 391 | 392 | // NotZero asserts that i is not the zero value for its type. 393 | func (a *Assertions) NotZero(i interface{}, msgAndArgs ...interface{}) { 394 | NotZero(a.t, i, msgAndArgs...) 395 | } 396 | 397 | // Panics asserts that the code inside the specified PanicTestFunc panics. 398 | // 399 | // a.Panics(func(){ GoCrazy() }) 400 | func (a *Assertions) Panics(f assert.PanicTestFunc, msgAndArgs ...interface{}) { 401 | Panics(a.t, f, msgAndArgs...) 402 | } 403 | 404 | // PanicsWithError asserts that the code inside the specified PanicTestFunc 405 | // panics, and that the recovered panic value is an error that satisfies the 406 | // EqualError comparison. 407 | // 408 | // a.PanicsWithError("crazy error", func(){ GoCrazy() }) 409 | func (a *Assertions) PanicsWithError(errString string, f assert.PanicTestFunc, msgAndArgs ...interface{}) { 410 | PanicsWithError(a.t, errString, f, msgAndArgs...) 411 | } 412 | 413 | // PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that 414 | // the recovered panic value equals the expected panic value. 415 | // 416 | // a.PanicsWithValue("crazy error", func(){ GoCrazy() }) 417 | func (a *Assertions) PanicsWithValue(expected interface{}, f assert.PanicTestFunc, msgAndArgs ...interface{}) { 418 | PanicsWithValue(a.t, expected, f, msgAndArgs...) 419 | } 420 | 421 | // Regexp asserts that a specified regexp matches a string. 422 | // 423 | // a.Regexp(regexp.MustCompile("start"), "it's starting") 424 | // a.Regexp("start...$", "it's not starting") 425 | func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) { 426 | Regexp(a.t, rx, str, msgAndArgs...) 427 | } 428 | 429 | // Same asserts that two pointers reference the same object. 430 | // 431 | // a.Same(ptr1, ptr2) 432 | // 433 | // Both arguments must be pointer variables. Pointer variable sameness is 434 | // determined based on the equality of both type and value. 435 | func (a *Assertions) Same(expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 436 | Same(a.t, expected, actual, msgAndArgs...) 437 | } 438 | 439 | // Subset asserts that the specified list(array, slice...) contains all 440 | // elements given in the specified subset(array, slice...). 441 | // 442 | // a.Subset([1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]") 443 | func (a *Assertions) Subset(list interface{}, subset interface{}, msgAndArgs ...interface{}) { 444 | Subset(a.t, list, subset, msgAndArgs...) 445 | } 446 | 447 | // True asserts that the specified value is true. 448 | // 449 | // a.True(myBool) 450 | func (a *Assertions) True(value bool, msgAndArgs ...interface{}) { 451 | True(a.t, value, msgAndArgs...) 452 | } 453 | 454 | // WithinDuration asserts that the two times are within duration delta of each other. 455 | // 456 | // a.WithinDuration(time.Now(), time.Now(), 10*time.Second) 457 | func (a *Assertions) WithinDuration(expected time.Time, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) { 458 | WithinDuration(a.t, expected, actual, delta, msgAndArgs...) 459 | } 460 | 461 | // YAMLEq asserts that two YAML strings are equivalent. 462 | func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) { 463 | YAMLEq(a.t, expected, actual, msgAndArgs...) 464 | } 465 | 466 | // Zero asserts that i is the zero value for its type. 467 | func (a *Assertions) Zero(i interface{}, msgAndArgs ...interface{}) { 468 | Zero(a.t, i, msgAndArgs...) 469 | } 470 | -------------------------------------------------------------------------------- /require.go: -------------------------------------------------------------------------------- 1 | /* 2 | * CODE GENERATED AUTOMATICALLY WITH github.com/stretchr/testify/_codegen 3 | * THIS FILE MUST NOT BE EDITED BY HAND 4 | */ 5 | 6 | package assert 7 | 8 | import ( 9 | http "net/http" 10 | url "net/url" 11 | time "time" 12 | 13 | assert "github.com/stretchr/testify/assert" 14 | ) 15 | 16 | // Condition uses a Comparison to assert a complex condition. 17 | func Condition(t TestingT, comp assert.Comparison, msgAndArgs ...interface{}) { 18 | if !assert.Condition(t, comp, msgAndArgs...) { 19 | t.FailNow() 20 | } 21 | } 22 | 23 | // Contains asserts that the specified string, list(array, slice...) or map contains the 24 | // specified substring or element. 25 | // 26 | // assert.Contains(t, "Hello World", "World") 27 | // assert.Contains(t, ["Hello", "World"], "World") 28 | // assert.Contains(t, {"Hello": "World"}, "Hello") 29 | func Contains(t TestingT, s interface{}, contains interface{}, msgAndArgs ...interface{}) { 30 | if !assert.Contains(t, s, contains, msgAndArgs...) { 31 | t.FailNow() 32 | } 33 | } 34 | 35 | // DirExists checks whether a directory exists in the given path. It also fails 36 | // if the path is a file rather a directory or there is an error checking whether it exists. 37 | func DirExists(t TestingT, path string, msgAndArgs ...interface{}) { 38 | if !assert.DirExists(t, path, msgAndArgs...) { 39 | t.FailNow() 40 | } 41 | } 42 | 43 | // ElementsMatch asserts that the specified listA(array, slice...) is equal to specified 44 | // listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, 45 | // the number of appearances of each of them in both lists should match. 46 | // 47 | // assert.ElementsMatch(t, [1, 3, 2, 3], [1, 3, 3, 2]) 48 | func ElementsMatch(t TestingT, listA interface{}, listB interface{}, msgAndArgs ...interface{}) { 49 | if !assert.ElementsMatch(t, listA, listB, msgAndArgs...) { 50 | t.FailNow() 51 | } 52 | } 53 | 54 | // Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either 55 | // a slice or a channel with len == 0. 56 | // 57 | // assert.Empty(t, obj) 58 | func Empty(t TestingT, object interface{}, msgAndArgs ...interface{}) { 59 | if !assert.Empty(t, object, msgAndArgs...) { 60 | t.FailNow() 61 | } 62 | } 63 | 64 | // Equal asserts that two objects are equal. 65 | // 66 | // assert.Equal(t, 123, 123) 67 | // 68 | // Pointer variable equality is determined based on the equality of the 69 | // referenced values (as opposed to the memory addresses). Function equality 70 | // cannot be determined and will always fail. 71 | func Equal(t TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 72 | if !assert.Equal(t, expected, actual, msgAndArgs...) { 73 | t.FailNow() 74 | } 75 | } 76 | 77 | // EqualError asserts that a function returned an error (i.e. not `nil`) 78 | // and that it is equal to the provided error. 79 | // 80 | // actualObj, err := SomeFunction() 81 | // assert.EqualError(t, err, expectedErrorString) 82 | func EqualError(t TestingT, theError error, errString string, msgAndArgs ...interface{}) { 83 | if !assert.EqualError(t, theError, errString, msgAndArgs...) { 84 | t.FailNow() 85 | } 86 | } 87 | 88 | // EqualValues asserts that two objects are equal or convertable to the same types 89 | // and equal. 90 | // 91 | // assert.EqualValues(t, uint32(123), int32(123)) 92 | func EqualValues(t TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 93 | if !assert.EqualValues(t, expected, actual, msgAndArgs...) { 94 | t.FailNow() 95 | } 96 | } 97 | 98 | // Error asserts that a function returned an error (i.e. not `nil`). 99 | // 100 | // actualObj, err := SomeFunction() 101 | // if assert.Error(t, err) { 102 | // assert.Equal(t, expectedError, err) 103 | // } 104 | func Error(t TestingT, err error, msgAndArgs ...interface{}) { 105 | if !assert.Error(t, err, msgAndArgs...) { 106 | t.FailNow() 107 | } 108 | } 109 | 110 | // Eventually asserts that given condition will be met in waitFor time, 111 | // periodically checking target function each tick. 112 | // 113 | // assert.Eventually(t, func() bool { return true; }, time.Second, 10*time.Millisecond) 114 | func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) { 115 | if !assert.Eventually(t, condition, waitFor, tick, msgAndArgs...) { 116 | t.FailNow() 117 | } 118 | } 119 | 120 | // Exactly asserts that two objects are equal in value and type. 121 | // 122 | // assert.Exactly(t, int32(123), int64(123)) 123 | func Exactly(t TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 124 | if !assert.Exactly(t, expected, actual, msgAndArgs...) { 125 | t.FailNow() 126 | } 127 | } 128 | 129 | // Fail reports a failure through 130 | func Fail(t TestingT, failureMessage string, msgAndArgs ...interface{}) { 131 | if !assert.Fail(t, failureMessage, msgAndArgs...) { 132 | t.FailNow() 133 | } 134 | } 135 | 136 | // FailNow fails test 137 | func FailNow(t TestingT, failureMessage string, msgAndArgs ...interface{}) { 138 | if !assert.FailNow(t, failureMessage, msgAndArgs...) { 139 | t.FailNow() 140 | } 141 | } 142 | 143 | // False asserts that the specified value is false. 144 | // 145 | // assert.False(t, myBool) 146 | func False(t TestingT, value bool, msgAndArgs ...interface{}) { 147 | if !assert.False(t, value, msgAndArgs...) { 148 | t.FailNow() 149 | } 150 | } 151 | 152 | // FileExists checks whether a file exists in the given path. It also fails if 153 | // the path points to a directory or there is an error when trying to check the file. 154 | func FileExists(t TestingT, path string, msgAndArgs ...interface{}) { 155 | if !assert.FileExists(t, path, msgAndArgs...) { 156 | t.FailNow() 157 | } 158 | } 159 | 160 | // Greater asserts that the first element is greater than the second 161 | // 162 | // assert.Greater(t, 2, 1) 163 | // assert.Greater(t, float64(2), float64(1)) 164 | // assert.Greater(t, "b", "a") 165 | func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) { 166 | if !assert.Greater(t, e1, e2, msgAndArgs...) { 167 | t.FailNow() 168 | } 169 | } 170 | 171 | // GreaterOrEqual asserts that the first element is greater than or equal to the second 172 | // 173 | // assert.GreaterOrEqual(t, 2, 1) 174 | // assert.GreaterOrEqual(t, 2, 2) 175 | // assert.GreaterOrEqual(t, "b", "a") 176 | // assert.GreaterOrEqual(t, "b", "b") 177 | func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) { 178 | if !assert.GreaterOrEqual(t, e1, e2, msgAndArgs...) { 179 | t.FailNow() 180 | } 181 | } 182 | 183 | // HTTPBodyContains asserts that a specified handler returns a 184 | // body that contains a string. 185 | // 186 | // assert.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") 187 | // 188 | // Returns whether the assertion was successful (true) or not (false). 189 | func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) { 190 | if !assert.HTTPBodyContains(t, handler, method, url, values, str, msgAndArgs...) { 191 | t.FailNow() 192 | } 193 | } 194 | 195 | // HTTPBodyNotContains asserts that a specified handler returns a 196 | // body that does not contain a string. 197 | // 198 | // assert.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") 199 | // 200 | // Returns whether the assertion was successful (true) or not (false). 201 | func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) { 202 | if !assert.HTTPBodyNotContains(t, handler, method, url, values, str, msgAndArgs...) { 203 | t.FailNow() 204 | } 205 | } 206 | 207 | // HTTPError asserts that a specified handler returns an error status code. 208 | // 209 | // assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} 210 | // 211 | // Returns whether the assertion was successful (true) or not (false). 212 | func HTTPError(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) { 213 | if !assert.HTTPError(t, handler, method, url, values, msgAndArgs...) { 214 | t.FailNow() 215 | } 216 | } 217 | 218 | // HTTPRedirect asserts that a specified handler returns a redirect status code. 219 | // 220 | // assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} 221 | // 222 | // Returns whether the assertion was successful (true) or not (false). 223 | func HTTPRedirect(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) { 224 | if !assert.HTTPRedirect(t, handler, method, url, values, msgAndArgs...) { 225 | t.FailNow() 226 | } 227 | } 228 | 229 | // HTTPStatusCode asserts that a specified handler returns a specified status code. 230 | // 231 | // assert.HTTPStatusCode(t, myHandler, "GET", "/notImplemented", nil, 501) 232 | // 233 | // Returns whether the assertion was successful (true) or not (false). 234 | func HTTPStatusCode(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msgAndArgs ...interface{}) { 235 | if !assert.HTTPStatusCode(t, handler, method, url, values, statuscode, msgAndArgs...) { 236 | t.FailNow() 237 | } 238 | } 239 | 240 | // HTTPSuccess asserts that a specified handler returns a success status code. 241 | // 242 | // assert.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil) 243 | // 244 | // Returns whether the assertion was successful (true) or not (false). 245 | func HTTPSuccess(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) { 246 | if !assert.HTTPSuccess(t, handler, method, url, values, msgAndArgs...) { 247 | t.FailNow() 248 | } 249 | } 250 | 251 | // Implements asserts that an object is implemented by the specified interface. 252 | // 253 | // assert.Implements(t, (*MyInterface)(nil), new(MyObject)) 254 | func Implements(t TestingT, interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) { 255 | if !assert.Implements(t, interfaceObject, object, msgAndArgs...) { 256 | t.FailNow() 257 | } 258 | } 259 | 260 | // InDelta asserts that the two numerals are within delta of each other. 261 | // 262 | // assert.InDelta(t, math.Pi, 22/7.0, 0.01) 263 | func InDelta(t TestingT, expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) { 264 | if !assert.InDelta(t, expected, actual, delta, msgAndArgs...) { 265 | t.FailNow() 266 | } 267 | } 268 | 269 | // InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. 270 | func InDeltaMapValues(t TestingT, expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) { 271 | if !assert.InDeltaMapValues(t, expected, actual, delta, msgAndArgs...) { 272 | t.FailNow() 273 | } 274 | } 275 | 276 | // InDeltaSlice is the same as InDelta, except it compares two slices. 277 | func InDeltaSlice(t TestingT, expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) { 278 | if !assert.InDeltaSlice(t, expected, actual, delta, msgAndArgs...) { 279 | t.FailNow() 280 | } 281 | } 282 | 283 | // InEpsilon asserts that expected and actual have a relative error less than epsilon 284 | func InEpsilon(t TestingT, expected interface{}, actual interface{}, epsilon float64, msgAndArgs ...interface{}) { 285 | if !assert.InEpsilon(t, expected, actual, epsilon, msgAndArgs...) { 286 | t.FailNow() 287 | } 288 | } 289 | 290 | // InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices. 291 | func InEpsilonSlice(t TestingT, expected interface{}, actual interface{}, epsilon float64, msgAndArgs ...interface{}) { 292 | if !assert.InEpsilonSlice(t, expected, actual, epsilon, msgAndArgs...) { 293 | t.FailNow() 294 | } 295 | } 296 | 297 | // IsType asserts that the specified objects are of the same type. 298 | func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs ...interface{}) { 299 | if !assert.IsType(t, expectedType, object, msgAndArgs...) { 300 | t.FailNow() 301 | } 302 | } 303 | 304 | // JSONEq asserts that two JSON strings are equivalent. 305 | // 306 | // assert.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) 307 | func JSONEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) { 308 | if !assert.JSONEq(t, expected, actual, msgAndArgs...) { 309 | t.FailNow() 310 | } 311 | } 312 | 313 | // Len asserts that the specified object has specific length. 314 | // Len also fails if the object has a type that len() not accept. 315 | // 316 | // assert.Len(t, mySlice, 3) 317 | func Len(t TestingT, object interface{}, length int, msgAndArgs ...interface{}) { 318 | if !assert.Len(t, object, length, msgAndArgs...) { 319 | t.FailNow() 320 | } 321 | } 322 | 323 | // Less asserts that the first element is less than the second 324 | // 325 | // assert.Less(t, 1, 2) 326 | // assert.Less(t, float64(1), float64(2)) 327 | // assert.Less(t, "a", "b") 328 | func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) { 329 | if !assert.Less(t, e1, e2, msgAndArgs...) { 330 | t.FailNow() 331 | } 332 | } 333 | 334 | // LessOrEqual asserts that the first element is less than or equal to the second 335 | // 336 | // assert.LessOrEqual(t, 1, 2) 337 | // assert.LessOrEqual(t, 2, 2) 338 | // assert.LessOrEqual(t, "a", "b") 339 | // assert.LessOrEqual(t, "b", "b") 340 | func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) { 341 | if !assert.LessOrEqual(t, e1, e2, msgAndArgs...) { 342 | t.FailNow() 343 | } 344 | } 345 | 346 | // Never asserts that the given condition doesn't satisfy in waitFor time, 347 | // periodically checking the target function each tick. 348 | // 349 | // assert.Never(t, func() bool { return false; }, time.Second, 10*time.Millisecond) 350 | func Never(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) { 351 | if !assert.Never(t, condition, waitFor, tick, msgAndArgs...) { 352 | t.FailNow() 353 | } 354 | } 355 | 356 | // Nil asserts that the specified object is nil. 357 | // 358 | // assert.Nil(t, err) 359 | func Nil(t TestingT, object interface{}, msgAndArgs ...interface{}) { 360 | if !assert.Nil(t, object, msgAndArgs...) { 361 | t.FailNow() 362 | } 363 | } 364 | 365 | // NoDirExists checks whether a directory does not exist in the given path. 366 | // It fails if the path points to an existing _directory_ only. 367 | func NoDirExists(t TestingT, path string, msgAndArgs ...interface{}) { 368 | if !assert.NoDirExists(t, path, msgAndArgs...) { 369 | t.FailNow() 370 | } 371 | } 372 | 373 | // NoError asserts that a function returned no error (i.e. `nil`). 374 | // 375 | // actualObj, err := SomeFunction() 376 | // if assert.NoError(t, err) { 377 | // assert.Equal(t, expectedObj, actualObj) 378 | // } 379 | func NoError(t TestingT, err error, msgAndArgs ...interface{}) { 380 | if !assert.NoError(t, err, msgAndArgs...) { 381 | t.FailNow() 382 | } 383 | } 384 | 385 | // NoFileExists checks whether a file does not exist in a given path. It fails 386 | // if the path points to an existing _file_ only. 387 | func NoFileExists(t TestingT, path string, msgAndArgs ...interface{}) { 388 | if !assert.NoFileExists(t, path, msgAndArgs...) { 389 | t.FailNow() 390 | } 391 | } 392 | 393 | // NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the 394 | // specified substring or element. 395 | // 396 | // assert.NotContains(t, "Hello World", "Earth") 397 | // assert.NotContains(t, ["Hello", "World"], "Earth") 398 | // assert.NotContains(t, {"Hello": "World"}, "Earth") 399 | func NotContains(t TestingT, s interface{}, contains interface{}, msgAndArgs ...interface{}) { 400 | if !assert.NotContains(t, s, contains, msgAndArgs...) { 401 | t.FailNow() 402 | } 403 | } 404 | 405 | // NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either 406 | // a slice or a channel with len == 0. 407 | // 408 | // if assert.NotEmpty(t, obj) { 409 | // assert.Equal(t, "two", obj[1]) 410 | // } 411 | func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) { 412 | if !assert.NotEmpty(t, object, msgAndArgs...) { 413 | t.FailNow() 414 | } 415 | } 416 | 417 | // NotEqual asserts that the specified values are NOT equal. 418 | // 419 | // assert.NotEqual(t, obj1, obj2) 420 | // 421 | // Pointer variable equality is determined based on the equality of the 422 | // referenced values (as opposed to the memory addresses). 423 | func NotEqual(t TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 424 | if !assert.NotEqual(t, expected, actual, msgAndArgs...) { 425 | t.FailNow() 426 | } 427 | } 428 | 429 | // NotEqualValues asserts that two objects are not equal even when converted to the same type 430 | // 431 | // assert.NotEqualValues(t, obj1, obj2) 432 | func NotEqualValues(t TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 433 | if !assert.NotEqualValues(t, expected, actual, msgAndArgs...) { 434 | t.FailNow() 435 | } 436 | } 437 | 438 | // NotNil asserts that the specified object is not nil. 439 | // 440 | // assert.NotNil(t, err) 441 | func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) { 442 | if !assert.NotNil(t, object, msgAndArgs...) { 443 | t.FailNow() 444 | } 445 | } 446 | 447 | // NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic. 448 | // 449 | // assert.NotPanics(t, func(){ RemainCalm() }) 450 | func NotPanics(t TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) { 451 | if !assert.NotPanics(t, f, msgAndArgs...) { 452 | t.FailNow() 453 | } 454 | } 455 | 456 | // NotRegexp asserts that a specified regexp does not match a string. 457 | // 458 | // assert.NotRegexp(t, regexp.MustCompile("starts"), "it's starting") 459 | // assert.NotRegexp(t, "^start", "it's not starting") 460 | func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) { 461 | if !assert.NotRegexp(t, rx, str, msgAndArgs...) { 462 | t.FailNow() 463 | } 464 | } 465 | 466 | // NotSame asserts that two pointers do not reference the same object. 467 | // 468 | // assert.NotSame(t, ptr1, ptr2) 469 | // 470 | // Both arguments must be pointer variables. Pointer variable sameness is 471 | // determined based on the equality of both type and value. 472 | func NotSame(t TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 473 | if !assert.NotSame(t, expected, actual, msgAndArgs...) { 474 | t.FailNow() 475 | } 476 | } 477 | 478 | // NotSubset asserts that the specified list(array, slice...) contains not all 479 | // elements given in the specified subset(array, slice...). 480 | // 481 | // assert.NotSubset(t, [1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]") 482 | func NotSubset(t TestingT, list interface{}, subset interface{}, msgAndArgs ...interface{}) { 483 | if !assert.NotSubset(t, list, subset, msgAndArgs...) { 484 | t.FailNow() 485 | } 486 | } 487 | 488 | // NotZero asserts that i is not the zero value for its type. 489 | func NotZero(t TestingT, i interface{}, msgAndArgs ...interface{}) { 490 | if !assert.NotZero(t, i, msgAndArgs...) { 491 | t.FailNow() 492 | } 493 | } 494 | 495 | // Panics asserts that the code inside the specified PanicTestFunc panics. 496 | // 497 | // assert.Panics(t, func(){ GoCrazy() }) 498 | func Panics(t TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) { 499 | if !assert.Panics(t, f, msgAndArgs...) { 500 | t.FailNow() 501 | } 502 | } 503 | 504 | // PanicsWithError asserts that the code inside the specified PanicTestFunc 505 | // panics, and that the recovered panic value is an error that satisfies the 506 | // EqualError comparison. 507 | // 508 | // assert.PanicsWithError(t, "crazy error", func(){ GoCrazy() }) 509 | func PanicsWithError(t TestingT, errString string, f assert.PanicTestFunc, msgAndArgs ...interface{}) { 510 | if !assert.PanicsWithError(t, errString, f, msgAndArgs...) { 511 | t.FailNow() 512 | } 513 | } 514 | 515 | // PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that 516 | // the recovered panic value equals the expected panic value. 517 | // 518 | // assert.PanicsWithValue(t, "crazy error", func(){ GoCrazy() }) 519 | func PanicsWithValue(t TestingT, expected interface{}, f assert.PanicTestFunc, msgAndArgs ...interface{}) { 520 | if !assert.PanicsWithValue(t, expected, f, msgAndArgs...) { 521 | t.FailNow() 522 | } 523 | } 524 | 525 | // Regexp asserts that a specified regexp matches a string. 526 | // 527 | // assert.Regexp(t, regexp.MustCompile("start"), "it's starting") 528 | // assert.Regexp(t, "start...$", "it's not starting") 529 | func Regexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) { 530 | if !assert.Regexp(t, rx, str, msgAndArgs...) { 531 | t.FailNow() 532 | } 533 | } 534 | 535 | // Same asserts that two pointers reference the same object. 536 | // 537 | // assert.Same(t, ptr1, ptr2) 538 | // 539 | // Both arguments must be pointer variables. Pointer variable sameness is 540 | // determined based on the equality of both type and value. 541 | func Same(t TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) { 542 | if !assert.Same(t, expected, actual, msgAndArgs...) { 543 | t.FailNow() 544 | } 545 | } 546 | 547 | // Subset asserts that the specified list(array, slice...) contains all 548 | // elements given in the specified subset(array, slice...). 549 | // 550 | // assert.Subset(t, [1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]") 551 | func Subset(t TestingT, list interface{}, subset interface{}, msgAndArgs ...interface{}) { 552 | if !assert.Subset(t, list, subset, msgAndArgs...) { 553 | t.FailNow() 554 | } 555 | } 556 | 557 | // True asserts that the specified value is true. 558 | // 559 | // assert.True(t, myBool) 560 | func True(t TestingT, value bool, msgAndArgs ...interface{}) { 561 | if !assert.True(t, value, msgAndArgs...) { 562 | t.FailNow() 563 | } 564 | } 565 | 566 | // WithinDuration asserts that the two times are within duration delta of each other. 567 | // 568 | // assert.WithinDuration(t, time.Now(), time.Now(), 10*time.Second) 569 | func WithinDuration(t TestingT, expected time.Time, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) { 570 | if !assert.WithinDuration(t, expected, actual, delta, msgAndArgs...) { 571 | t.FailNow() 572 | } 573 | } 574 | 575 | // YAMLEq asserts that two YAML strings are equivalent. 576 | func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) { 577 | if !assert.YAMLEq(t, expected, actual, msgAndArgs...) { 578 | t.FailNow() 579 | } 580 | } 581 | 582 | // Zero asserts that i is the zero value for its type. 583 | func Zero(t TestingT, i interface{}, msgAndArgs ...interface{}) { 584 | if !assert.Zero(t, i, msgAndArgs...) { 585 | t.FailNow() 586 | } 587 | } 588 | --------------------------------------------------------------------------------