Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion accessors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
)

func TestAccessorsAccessGetSingleField(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions conversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"testing"

"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConversionJSON(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
)

var fixtures = []struct {
Expand Down
10 changes: 0 additions & 10 deletions go.mod
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
10 changes: 0 additions & 10 deletions go.sum
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=
2 changes: 0 additions & 2 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"testing"

"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var TestMap = objx.Map{
Expand Down
2 changes: 0 additions & 2 deletions mutations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"testing"

"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestExclude(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
)

func TestHashWithKey(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions simple_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"testing"

"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSimpleExample(t *testing.T) {
Expand Down
240 changes: 240 additions & 0 deletions std_assert_test.go
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
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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


import (
"fmt"
"reflect"
"testing"
)

type nonfatal struct{}
type fatal struct{}

var assert nonfatal
var require fatal
Comment on lines +14 to +15
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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())
}
}
1 change: 0 additions & 1 deletion tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
)

func TestHas(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion type_specific_codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
)

/*
Expand Down
1 change: 0 additions & 1 deletion type_specific_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
)

/*
Expand Down
1 change: 0 additions & 1 deletion value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
)

func TestStringTypeString(t *testing.T) {
Expand Down