diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0ffd24cb3..08796ae29 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,9 +10,9 @@ jobs: - "1.19" - "1.20" steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup Go - uses: actions/setup-go@v3.2.0 + uses: actions/setup-go@v4.1.0 with: go-version: ${{ matrix.go_version }} - run: ./.ci.gogenerate.sh @@ -29,9 +29,9 @@ jobs: - "1.19" - "1.20" steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup Go - uses: actions/setup-go@v3.2.0 + uses: actions/setup-go@v4.1.0 with: go-version: ${{ matrix.go_version }} - run: go test -v -race ./... diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 67fc95b66..cd4331ca0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -13,7 +13,7 @@ jobs: contents: write steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Create GitHub release from tag uses: softprops/action-gh-release@v1 \ No newline at end of file diff --git a/assert/assertion_compare.go b/assert/assertion_compare.go index 75813066f..34b9c1384 100644 --- a/assert/assertion_compare.go +++ b/assert/assertion_compare.go @@ -308,7 +308,7 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) { case reflect.Struct: { // All structs enter here. We're not interested in most types. - if !canConvert(obj1Value, timeType) { + if !obj1Value.CanConvert(timeType) { break } @@ -328,7 +328,7 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) { case reflect.Slice: { // We only care about the []byte type. - if !canConvert(obj1Value, bytesType) { + if !obj1Value.CanConvert(bytesType) { break } diff --git a/assert/assertion_compare_can_convert.go b/assert/assertion_compare_can_convert.go deleted file mode 100644 index da867903e..000000000 --- a/assert/assertion_compare_can_convert.go +++ /dev/null @@ -1,16 +0,0 @@ -//go:build go1.17 -// +build go1.17 - -// TODO: once support for Go 1.16 is dropped, this file can be -// merged/removed with assertion_compare_go1.17_test.go and -// assertion_compare_legacy.go - -package assert - -import "reflect" - -// Wrapper around reflect.Value.CanConvert, for compatibility -// reasons. -func canConvert(value reflect.Value, to reflect.Type) bool { - return value.CanConvert(to) -} diff --git a/assert/assertion_compare_go1.17_test.go b/assert/assertion_compare_go1.17_test.go deleted file mode 100644 index 53e01ed46..000000000 --- a/assert/assertion_compare_go1.17_test.go +++ /dev/null @@ -1,182 +0,0 @@ -//go:build go1.17 -// +build go1.17 - -// TODO: once support for Go 1.16 is dropped, this file can be -// merged/removed with assertion_compare_can_convert.go and -// assertion_compare_legacy.go - -package assert - -import ( - "bytes" - "reflect" - "testing" - "time" -) - -func TestCompare17(t *testing.T) { - type customTime time.Time - type customBytes []byte - for _, currCase := range []struct { - less interface{} - greater interface{} - cType string - }{ - {less: time.Now(), greater: time.Now().Add(time.Hour), cType: "time.Time"}, - {less: customTime(time.Now()), greater: customTime(time.Now().Add(time.Hour)), cType: "time.Time"}, - {less: []byte{1, 1}, greater: []byte{1, 2}, cType: "[]byte"}, - {less: customBytes([]byte{1, 1}), greater: customBytes([]byte{1, 2}), cType: "[]byte"}, - } { - resLess, isComparable := compare(currCase.less, currCase.greater, reflect.ValueOf(currCase.less).Kind()) - if !isComparable { - t.Error("object should be comparable for type " + currCase.cType) - } - - if resLess != compareLess { - t.Errorf("object less (%v) should be less than greater (%v) for type "+currCase.cType, - currCase.less, currCase.greater) - } - - resGreater, isComparable := compare(currCase.greater, currCase.less, reflect.ValueOf(currCase.less).Kind()) - if !isComparable { - t.Error("object are comparable for type " + currCase.cType) - } - - if resGreater != compareGreater { - t.Errorf("object greater should be greater than less for type " + currCase.cType) - } - - resEqual, isComparable := compare(currCase.less, currCase.less, reflect.ValueOf(currCase.less).Kind()) - if !isComparable { - t.Error("object are comparable for type " + currCase.cType) - } - - if resEqual != 0 { - t.Errorf("objects should be equal for type " + currCase.cType) - } - } -} - -func TestGreater17(t *testing.T) { - mockT := new(testing.T) - - if !Greater(mockT, 2, 1) { - t.Error("Greater should return true") - } - - if Greater(mockT, 1, 1) { - t.Error("Greater should return false") - } - - if Greater(mockT, 1, 2) { - t.Error("Greater should return false") - } - - // Check error report - for _, currCase := range []struct { - less interface{} - greater interface{} - msg string - }{ - {less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than "[1 2]"`}, - {less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 00:00:00 +0000 UTC" is not greater than "0001-01-01 01:00:00 +0000 UTC"`}, - } { - out := &outputT{buf: bytes.NewBuffer(nil)} - False(t, Greater(out, currCase.less, currCase.greater)) - Contains(t, out.buf.String(), currCase.msg) - Contains(t, out.helpers, "github.com/stretchr/testify/assert.Greater") - } -} - -func TestGreaterOrEqual17(t *testing.T) { - mockT := new(testing.T) - - if !GreaterOrEqual(mockT, 2, 1) { - t.Error("GreaterOrEqual should return true") - } - - if !GreaterOrEqual(mockT, 1, 1) { - t.Error("GreaterOrEqual should return true") - } - - if GreaterOrEqual(mockT, 1, 2) { - t.Error("GreaterOrEqual should return false") - } - - // Check error report - for _, currCase := range []struct { - less interface{} - greater interface{} - msg string - }{ - {less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than or equal to "[1 2]"`}, - {less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 00:00:00 +0000 UTC" is not greater than or equal to "0001-01-01 01:00:00 +0000 UTC"`}, - } { - out := &outputT{buf: bytes.NewBuffer(nil)} - False(t, GreaterOrEqual(out, currCase.less, currCase.greater)) - Contains(t, out.buf.String(), currCase.msg) - Contains(t, out.helpers, "github.com/stretchr/testify/assert.GreaterOrEqual") - } -} - -func TestLess17(t *testing.T) { - mockT := new(testing.T) - - if !Less(mockT, 1, 2) { - t.Error("Less should return true") - } - - if Less(mockT, 1, 1) { - t.Error("Less should return false") - } - - if Less(mockT, 2, 1) { - t.Error("Less should return false") - } - - // Check error report - for _, currCase := range []struct { - less interface{} - greater interface{} - msg string - }{ - {less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than "[1 1]"`}, - {less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 01:00:00 +0000 UTC" is not less than "0001-01-01 00:00:00 +0000 UTC"`}, - } { - out := &outputT{buf: bytes.NewBuffer(nil)} - False(t, Less(out, currCase.greater, currCase.less)) - Contains(t, out.buf.String(), currCase.msg) - Contains(t, out.helpers, "github.com/stretchr/testify/assert.Less") - } -} - -func TestLessOrEqual17(t *testing.T) { - mockT := new(testing.T) - - if !LessOrEqual(mockT, 1, 2) { - t.Error("LessOrEqual should return true") - } - - if !LessOrEqual(mockT, 1, 1) { - t.Error("LessOrEqual should return true") - } - - if LessOrEqual(mockT, 2, 1) { - t.Error("LessOrEqual should return false") - } - - // Check error report - for _, currCase := range []struct { - less interface{} - greater interface{} - msg string - }{ - {less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than or equal to "[1 1]"`}, - {less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 01:00:00 +0000 UTC" is not less than or equal to "0001-01-01 00:00:00 +0000 UTC"`}, - } { - out := &outputT{buf: bytes.NewBuffer(nil)} - False(t, LessOrEqual(out, currCase.greater, currCase.less)) - Contains(t, out.buf.String(), currCase.msg) - Contains(t, out.helpers, "github.com/stretchr/testify/assert.LessOrEqual") - } -} diff --git a/assert/assertion_compare_legacy.go b/assert/assertion_compare_legacy.go deleted file mode 100644 index 1701af2a3..000000000 --- a/assert/assertion_compare_legacy.go +++ /dev/null @@ -1,16 +0,0 @@ -//go:build !go1.17 -// +build !go1.17 - -// TODO: once support for Go 1.16 is dropped, this file can be -// merged/removed with assertion_compare_go1.17_test.go and -// assertion_compare_can_convert.go - -package assert - -import "reflect" - -// Older versions of Go does not have the reflect.Value.CanConvert -// method. -func canConvert(value reflect.Value, to reflect.Type) bool { - return false -} diff --git a/assert/assertion_compare_test.go b/assert/assertion_compare_test.go index a38d88060..e1553137a 100644 --- a/assert/assertion_compare_test.go +++ b/assert/assertion_compare_test.go @@ -6,6 +6,7 @@ import ( "reflect" "runtime" "testing" + "time" ) func TestCompare(t *testing.T) { @@ -22,6 +23,8 @@ func TestCompare(t *testing.T) { type customFloat32 float32 type customFloat64 float64 type customString string + type customTime time.Time + type customBytes []byte for _, currCase := range []struct { less interface{} greater interface{} @@ -52,6 +55,10 @@ func TestCompare(t *testing.T) { {less: customFloat32(1.23), greater: customFloat32(2.23), cType: "float32"}, {less: float64(1.23), greater: float64(2.34), cType: "float64"}, {less: customFloat64(1.23), greater: customFloat64(2.34), cType: "float64"}, + {less: time.Now(), greater: time.Now().Add(time.Hour), cType: "time.Time"}, + {less: customTime(time.Now()), greater: customTime(time.Now().Add(time.Hour)), cType: "time.Time"}, + {less: []byte{1, 1}, greater: []byte{1, 2}, cType: "[]byte"}, + {less: customBytes([]byte{1, 1}), greater: customBytes([]byte{1, 2}), cType: "[]byte"}, } { resLess, isComparable := compare(currCase.less, currCase.greater, reflect.ValueOf(currCase.less).Kind()) if !isComparable { @@ -148,6 +155,8 @@ func TestGreater(t *testing.T) { {less: uint64(1), greater: uint64(2), msg: `"1" is not greater than "2"`}, {less: float32(1.23), greater: float32(2.34), msg: `"1.23" is not greater than "2.34"`}, {less: float64(1.23), greater: float64(2.34), msg: `"1.23" is not greater than "2.34"`}, + {less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than "[1 2]"`}, + {less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 00:00:00 +0000 UTC" is not greater than "0001-01-01 01:00:00 +0000 UTC"`}, } { out := &outputT{buf: bytes.NewBuffer(nil)} False(t, Greater(out, currCase.less, currCase.greater)) @@ -189,6 +198,8 @@ func TestGreaterOrEqual(t *testing.T) { {less: uint64(1), greater: uint64(2), msg: `"1" is not greater than or equal to "2"`}, {less: float32(1.23), greater: float32(2.34), msg: `"1.23" is not greater than or equal to "2.34"`}, {less: float64(1.23), greater: float64(2.34), msg: `"1.23" is not greater than or equal to "2.34"`}, + {less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than or equal to "[1 2]"`}, + {less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 00:00:00 +0000 UTC" is not greater than or equal to "0001-01-01 01:00:00 +0000 UTC"`}, } { out := &outputT{buf: bytes.NewBuffer(nil)} False(t, GreaterOrEqual(out, currCase.less, currCase.greater)) @@ -230,6 +241,8 @@ func TestLess(t *testing.T) { {less: uint64(1), greater: uint64(2), msg: `"2" is not less than "1"`}, {less: float32(1.23), greater: float32(2.34), msg: `"2.34" is not less than "1.23"`}, {less: float64(1.23), greater: float64(2.34), msg: `"2.34" is not less than "1.23"`}, + {less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than "[1 1]"`}, + {less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 01:00:00 +0000 UTC" is not less than "0001-01-01 00:00:00 +0000 UTC"`}, } { out := &outputT{buf: bytes.NewBuffer(nil)} False(t, Less(out, currCase.greater, currCase.less)) @@ -271,6 +284,8 @@ func TestLessOrEqual(t *testing.T) { {less: uint64(1), greater: uint64(2), msg: `"2" is not less than or equal to "1"`}, {less: float32(1.23), greater: float32(2.34), msg: `"2.34" is not less than or equal to "1.23"`}, {less: float64(1.23), greater: float64(2.34), msg: `"2.34" is not less than or equal to "1.23"`}, + {less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than or equal to "[1 1]"`}, + {less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 01:00:00 +0000 UTC" is not less than or equal to "0001-01-01 00:00:00 +0000 UTC"`}, } { out := &outputT{buf: bytes.NewBuffer(nil)} False(t, LessOrEqual(out, currCase.greater, currCase.less)) diff --git a/assert/assertions.go b/assert/assertions.go index 719164e7e..782c8e5ce 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -59,20 +59,25 @@ func ObjectsAreEqual(expected, actual interface{}) bool { if expected == nil || actual == nil { return expected == actual } - - exp, ok := expected.([]byte) - if !ok { + switch exp := expected.(type) { + case []byte: + act, ok := actual.([]byte) + if !ok { + return false + } + if exp == nil || act == nil { + return exp == nil && act == nil + } + return bytes.Equal(exp, act) + case time.Time: + act, ok := actual.(time.Time) + if !ok { + return false + } + return exp.Equal(act) + default: return reflect.DeepEqual(expected, actual) } - - act, ok := actual.([]byte) - if !ok { - return false - } - if exp == nil || act == nil { - return exp == nil && act == nil - } - return bytes.Equal(exp, act) } // copyExportedFields iterates downward through nested data structures and creates a copy diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 40e372015..0595f212f 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -148,6 +148,12 @@ func TestObjectsAreEqual(t *testing.T) { t.Fail() } + tm := time.Now() + tz := tm.In(time.Local) + if !ObjectsAreEqualValues(tm, tz) { + t.Error("ObjectsAreEqualValues should return true for time.Time objects with different time zones") + } + } type Nested struct {