-
Notifications
You must be signed in to change notification settings - Fork 78
Replace testify assertions with custom testing helpers
#159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,3 @@ | ||
| module github.com/stretchr/objx | ||
|
|
||
| go 1.20 | ||
|
|
||
| require github.com/stretchr/testify v1.11.1 | ||
|
|
||
| require ( | ||
| github.com/davecgh/go-spew v1.1.1 // indirect | ||
| github.com/pmezard/go-difflib v1.0.0 // indirect | ||
| gopkg.in/yaml.v3 v3.0.1 // indirect | ||
| ) | ||
|
|
||
| exclude github.com/stretchr/testify v1.8.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +0,0 @@ | ||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= | ||
| github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
| gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| // This file provides minimal assert/require helpers built on Go's standard library to | ||
| // remove the dependency on github.com/stretchr/testify from objx tests. | ||
| package objx_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| type nonfatal struct{} | ||
| type fatal struct{} | ||
|
|
||
| var assert nonfatal | ||
| var require fatal | ||
|
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This idea is awesome ! I was like: WTF why removing testify imports everywhere. It won't work! You got me, and I love the solution you found. |
||
|
|
||
| func (nonfatal) fail(t *testing.T, msg string, msgAndArgs ...interface{}) bool { | ||
| t.Helper() | ||
| if len(msgAndArgs) > 0 { | ||
| msg = fmt.Sprintf(msg, msgAndArgs...) | ||
| } | ||
| t.Error(msg) | ||
| return false | ||
| } | ||
|
|
||
| func (fatal) fail(t *testing.T, msg string, msgAndArgs ...interface{}) { | ||
| t.Helper() | ||
| if len(msgAndArgs) > 0 { | ||
| msg = fmt.Sprintf(msg, msgAndArgs...) | ||
| } | ||
| t.Fatal(msg) | ||
| } | ||
|
|
||
| func (a nonfatal) Equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) bool { | ||
| t.Helper() | ||
| if !reflect.DeepEqual(expected, actual) { | ||
| return a.fail(t, "not equal:\nexpected: %#v\nactual: %#v", expected, actual) | ||
| } | ||
| return true | ||
| } | ||
| func (a fatal) Equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) { | ||
| t.Helper() | ||
| if !reflect.DeepEqual(expected, actual) { | ||
| a.fail(t, "not equal:\nexpected: %#v\nactual: %#v", expected, actual) | ||
| } | ||
| } | ||
|
|
||
| func (a nonfatal) NotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) bool { | ||
| t.Helper() | ||
| if reflect.DeepEqual(expected, actual) { | ||
| return a.fail(t, "should not be equal: %#v", actual) | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func (a fatal) NotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) { | ||
| t.Helper() | ||
| if reflect.DeepEqual(expected, actual) { | ||
| a.fail(t, "should not be equal: %#v", actual) | ||
| } | ||
| } | ||
|
|
||
| func isNil(i interface{}) bool { | ||
| if i == nil { | ||
| return true | ||
| } | ||
| v := reflect.ValueOf(i) | ||
| switch v.Kind() { | ||
| case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: | ||
| return v.IsNil() | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func (a nonfatal) Nil(t *testing.T, object interface{}, msgAndArgs ...interface{}) bool { | ||
| t.Helper() | ||
| if !isNil(object) { | ||
| return a.fail(t, "expected nil, got: %#v", object) | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func (a fatal) Nil(t *testing.T, object interface{}, msgAndArgs ...interface{}) { | ||
| t.Helper() | ||
| if !isNil(object) { | ||
| a.fail(t, "expected nil, got: %#v", object) | ||
| } | ||
| } | ||
|
|
||
| func (a nonfatal) NotNil(t *testing.T, object interface{}, msgAndArgs ...interface{}) bool { | ||
| t.Helper() | ||
| if isNil(object) { | ||
| return a.fail(t, "expected not nil") | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func (a fatal) NotNil(t *testing.T, object interface{}, msgAndArgs ...interface{}) { | ||
| t.Helper() | ||
| if isNil(object) { | ||
| a.fail(t, "expected not nil") | ||
| } | ||
| } | ||
|
|
||
| func (a nonfatal) True(t *testing.T, value bool, msgAndArgs ...interface{}) bool { | ||
| t.Helper() | ||
| if !value { | ||
| return a.fail(t, "expected true, got false") | ||
| } | ||
| return true | ||
| } | ||
| func (a fatal) True(t *testing.T, value bool, msgAndArgs ...interface{}) { | ||
| t.Helper() | ||
| if !value { | ||
| a.fail(t, "expected true, got false") | ||
| } | ||
| } | ||
|
|
||
| func (a nonfatal) False(t *testing.T, value bool, msgAndArgs ...interface{}) bool { | ||
| t.Helper() | ||
| if value { | ||
| return a.fail(t, "expected false, got true") | ||
| } | ||
| return true | ||
| } | ||
| func (a fatal) False(t *testing.T, value bool, msgAndArgs ...interface{}) { | ||
| t.Helper() | ||
| if value { | ||
| a.fail(t, "expected false, got true") | ||
| } | ||
| } | ||
|
|
||
| func (a nonfatal) NoError(t *testing.T, err error, msgAndArgs ...interface{}) bool { | ||
| t.Helper() | ||
| if err != nil { | ||
| return a.fail(t, "expected no error, got: %v", err) | ||
| } | ||
| return true | ||
| } | ||
| func (a fatal) NoError(t *testing.T, err error, msgAndArgs ...interface{}) { | ||
| t.Helper() | ||
| if err != nil { | ||
| a.fail(t, "expected no error, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func (a nonfatal) Error(t *testing.T, err error, msgAndArgs ...interface{}) bool { | ||
| t.Helper() | ||
| if err == nil { | ||
| return a.fail(t, "expected error, got nil") | ||
| } | ||
| return true | ||
| } | ||
| func (a fatal) Error(t *testing.T, err error, msgAndArgs ...interface{}) { | ||
| t.Helper() | ||
| if err == nil { | ||
| a.fail(t, "expected error, got nil") | ||
| } | ||
| } | ||
|
|
||
| func (a nonfatal) Panics(t *testing.T, f func(), msgAndArgs ...interface{}) bool { | ||
| t.Helper() | ||
| defer func() { | ||
| if r := recover(); r == nil { | ||
| _ = a.fail(t, "expected panic, but function did not panic") | ||
| } | ||
| }() | ||
| f() | ||
| return true | ||
| } | ||
|
|
||
| func (a fatal) Panics(t *testing.T, f func(), msgAndArgs ...interface{}) { | ||
| t.Helper() | ||
| defer func() { | ||
| if r := recover(); r == nil { | ||
| a.fail(t, "expected panic, but function did not panic") | ||
| } | ||
| }() | ||
| f() | ||
| } | ||
|
|
||
| func (a nonfatal) Empty(t *testing.T, object interface{}, msgAndArgs ...interface{}) bool { | ||
| t.Helper() | ||
| if !isEmpty(object) { | ||
| return a.fail(t, "expected empty, got: %#v", object) | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func (a fatal) Empty(t *testing.T, object interface{}, msgAndArgs ...interface{}) { | ||
| t.Helper() | ||
| if !isEmpty(object) { | ||
| a.fail(t, "expected empty, got: %#v", object) | ||
| } | ||
| } | ||
|
|
||
| func isEmpty(object interface{}) bool { | ||
| if object == nil { | ||
| return true | ||
| } | ||
| v := reflect.ValueOf(object) | ||
| switch v.Kind() { | ||
| case reflect.Array, reflect.Slice, reflect.Map, reflect.Chan, reflect.String: | ||
| return v.Len() == 0 | ||
| case reflect.Ptr, reflect.Interface: | ||
| if v.IsNil() { | ||
| return true | ||
| } | ||
| return isEmpty(v.Elem().Interface()) | ||
| } | ||
| // numbers and structs are never considered empty here | ||
| return false | ||
| } | ||
|
|
||
| func (a nonfatal) Len(t *testing.T, object interface{}, length int, msgAndArgs ...interface{}) bool { | ||
| t.Helper() | ||
| v := reflect.ValueOf(object) | ||
| switch v.Kind() { | ||
| case reflect.Array, reflect.Slice, reflect.Map, reflect.Chan, reflect.String: | ||
| if v.Len() != length { | ||
| return a.fail(t, "unexpected length, expected %d got %d", length, v.Len()) | ||
| } | ||
| return true | ||
| default: | ||
| return a.fail(t, "Len not supported for kind %s", v.Kind()) | ||
| } | ||
| } | ||
|
|
||
| func (a fatal) Len(t *testing.T, object interface{}, length int, msgAndArgs ...interface{}) { | ||
| t.Helper() | ||
| v := reflect.ValueOf(object) | ||
| switch v.Kind() { | ||
| case reflect.Array, reflect.Slice, reflect.Map, reflect.Chan, reflect.String: | ||
| if v.Len() != length { | ||
| a.fail(t, "unexpected length, expected %d got %d", length, v.Len()) | ||
| } | ||
| default: | ||
| a.fail(t, "Len not supported for kind %s", v.Kind()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,6 @@ import ( | |
| "testing" | ||
|
|
||
| "github.com/stretchr/objx" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| /* | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,6 @@ import ( | |
| "testing" | ||
|
|
||
| "github.com/stretchr/objx" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| /* | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment that mentions why this file exists: this is a reimplementation of a subset of Testify's API to break a cycle dependency with Testify.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i added some words about that