From f199b16b76686c57217e0798c9e3a987c77ceb79 Mon Sep 17 00:00:00 2001 From: Aaron Kaswen-Wilk Date: Mon, 30 Dec 2024 14:51:11 +0100 Subject: [PATCH] formatting fixes --- assert/assertion_format.go | 2 +- assert/assertion_forward.go | 2 +- assert/assertions.go | 20 ++++++++++++-- assert/assertions_test.go | 55 ++++++++++++++++++++++++++++++------- require/require.go | 2 +- require/require_forward.go | 2 +- 6 files changed, 67 insertions(+), 16 deletions(-) diff --git a/assert/assertion_format.go b/assert/assertion_format.go index 5afc612d3..9c731a2c9 100644 --- a/assert/assertion_format.go +++ b/assert/assertion_format.go @@ -3,8 +3,8 @@ package assert import ( - jsonmatch "github.com/stretchr/testify/assert/jsonmatch" http "net/http" + jsonmatch "github.com/stretchr/testify/assert/jsonmatch" url "net/url" time "time" ) diff --git a/assert/assertion_forward.go b/assert/assertion_forward.go index 72ce059ff..b9cca2384 100644 --- a/assert/assertion_forward.go +++ b/assert/assertion_forward.go @@ -3,8 +3,8 @@ package assert import ( - jsonmatch "github.com/stretchr/testify/assert/jsonmatch" http "net/http" + jsonmatch "github.com/stretchr/testify/assert/jsonmatch" url "net/url" time "time" ) diff --git a/assert/assertions.go b/assert/assertions.go index b2bdcc1f7..14aa0c4f2 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -211,6 +211,7 @@ the problem actually occurred in calling code.*/ // of each stack frame leading from the current test to the assert call that // failed. func CallerInfo() []string { + var pc uintptr var ok bool var file string @@ -475,6 +476,7 @@ func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) } return true + } // validateEqualArgs checks whether provided arguments can be safely used in the @@ -529,7 +531,7 @@ func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{} same, ok := samePointers(expected, actual) if !ok { - // fails when the arguments are not pointers + //fails when the arguments are not pointers return !(Fail(t, "Both arguments must be pointers", msgAndArgs...)) } @@ -548,7 +550,7 @@ func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{} func samePointers(first, second interface{}) (same bool, ok bool) { firstPtr, secondPtr := reflect.ValueOf(first), reflect.ValueOf(second) if firstPtr.Kind() != reflect.Ptr || secondPtr.Kind() != reflect.Ptr { - return false, false // not both are pointers + return false, false //not both are pointers } firstType, secondType := reflect.TypeOf(first), reflect.TypeOf(second) @@ -609,6 +611,7 @@ func EqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interfa } return true + } // EqualExportedValues asserts that the types of two objects are equal and their public @@ -663,6 +666,7 @@ func Exactly(t TestingT, expected, actual interface{}, msgAndArgs ...interface{} } return Equal(t, expected, actual, msgAndArgs...) + } // NotNil asserts that the specified object is not nil. @@ -712,6 +716,7 @@ func Nil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { // isEmpty gets whether the specified object is considered empty or not. func isEmpty(object interface{}) bool { + // get nil case out of the way if object == nil { return true @@ -752,6 +757,7 @@ func Empty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { } return pass + } // NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either @@ -770,6 +776,7 @@ func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { } return pass + } // getLen tries to get the length of an object. @@ -813,6 +820,7 @@ func True(t TestingT, value bool, msgAndArgs ...interface{}) bool { } return true + } // False asserts that the specified value is false. @@ -827,6 +835,7 @@ func False(t TestingT, value bool, msgAndArgs ...interface{}) bool { } return true + } // NotEqual asserts that the specified values are NOT equal. @@ -849,6 +858,7 @@ func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{ } return true + } // NotEqualValues asserts that two objects are not equal even when converted to the same type @@ -871,6 +881,7 @@ func NotEqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...inte // return (true, false) if element was not found. // return (true, true) if element was found. func containsElement(list interface{}, element interface{}) (ok, found bool) { + listValue := reflect.ValueOf(list) listType := reflect.TypeOf(list) if listType == nil { @@ -905,6 +916,7 @@ func containsElement(list interface{}, element interface{}) (ok, found bool) { } } return true, false + } // Contains asserts that the specified string, list(array, slice...) or map contains the @@ -927,6 +939,7 @@ func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bo } return true + } // NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the @@ -949,6 +962,7 @@ func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) } return true + } // Subset asserts that the specified list(array, slice...) or map contains all @@ -1654,6 +1668,7 @@ func matchRegexp(rx interface{}, str interface{}) bool { default: return r.MatchString(fmt.Sprint(v)) } + } // Regexp asserts that a specified regexp matches a string. @@ -1689,6 +1704,7 @@ func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interf } return !match + } // Zero asserts that i is the zero value for its type. diff --git a/assert/assertions_test.go b/assert/assertions_test.go index e55f88366..1b52d0c4e 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -323,15 +323,11 @@ func TestCopyExportedFields(t *testing.T) { }}, }, { - input: S4{ - []*Nested{ - {1, 2}, - }, + input: S4{[]*Nested{ + {1, 2}}, }, - expected: S4{ - []*Nested{ - {1, nil}, - }, + expected: S4{[]*Nested{ + {1, nil}}, }, }, { @@ -518,6 +514,7 @@ func TestEqualExportedValues(t *testing.T) { } func TestImplements(t *testing.T) { + mockT := new(testing.T) if !Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) { @@ -529,9 +526,11 @@ func TestImplements(t *testing.T) { if Implements(mockT, (*AssertionTesterInterface)(nil), nil) { t.Error("Implements method should return false: nil does not implement AssertionTesterInterface") } + } func TestNotImplements(t *testing.T) { + mockT := new(testing.T) if !NotImplements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) { @@ -543,9 +542,11 @@ func TestNotImplements(t *testing.T) { if NotImplements(mockT, (*AssertionTesterInterface)(nil), nil) { t.Error("NotImplements method should return false: nil can't be checked to be implementing AssertionTesterInterface or not") } + } func TestIsType(t *testing.T) { + mockT := new(testing.T) if !IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) { @@ -554,6 +555,7 @@ func TestIsType(t *testing.T) { if IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) { t.Error("IsType should return false: AssertionTesterConformingObject is not the same type as AssertionTesterNonConformingObject") } + } func TestEqual(t *testing.T) { @@ -602,6 +604,7 @@ func ptr(i int) *int { } func TestSame(t *testing.T) { + mockT := new(testing.T) if Same(mockT, ptr(1), ptr(1)) { @@ -620,6 +623,7 @@ func TestSame(t *testing.T) { } func TestNotSame(t *testing.T) { + mockT := new(testing.T) if !NotSame(mockT, ptr(1), ptr(1)) { @@ -804,6 +808,7 @@ func TestFormatUnequalValues(t *testing.T) { } func TestNotNil(t *testing.T) { + mockT := new(testing.T) if !NotNil(mockT, new(AssertionTesterConformingObject)) { @@ -815,9 +820,11 @@ func TestNotNil(t *testing.T) { if NotNil(mockT, (*struct{})(nil)) { t.Error("NotNil should return false: object is (*struct{})(nil)") } + } func TestNil(t *testing.T) { + mockT := new(testing.T) if !Nil(mockT, nil) { @@ -829,9 +836,11 @@ func TestNil(t *testing.T) { if Nil(mockT, new(AssertionTesterConformingObject)) { t.Error("Nil should return false: object is not nil") } + } func TestTrue(t *testing.T) { + mockT := new(testing.T) if !True(mockT, true) { @@ -840,9 +849,11 @@ func TestTrue(t *testing.T) { if True(mockT, false) { t.Error("True should return false") } + } func TestFalse(t *testing.T) { + mockT := new(testing.T) if !False(mockT, false) { @@ -851,9 +862,11 @@ func TestFalse(t *testing.T) { if False(mockT, true) { t.Error("False should return false") } + } func TestExactly(t *testing.T) { + mockT := new(testing.T) a := float32(1) @@ -884,6 +897,7 @@ func TestExactly(t *testing.T) { } func TestNotEqual(t *testing.T) { + mockT := new(testing.T) cases := []struct { @@ -966,6 +980,7 @@ func TestNotEqualValues(t *testing.T) { } func TestContainsNotContains(t *testing.T) { + type A struct { Name, Value string } @@ -1145,6 +1160,7 @@ func TestSubsetNotSubset(t *testing.T) { } for _, c := range cases { + t.Run("SubSet: "+c.message, func(t *testing.T) { mockT := new(mockTestingT) res := Subset(mockT, c.list, c.subset) @@ -1192,6 +1208,7 @@ func TestNotSubsetNil(t *testing.T) { } func Test_containsElement(t *testing.T) { + list1 := []string{"Foo", "Bar"} list2 := []int{1, 2} simpleMap := map[interface{}]interface{}{"Foo": "Bar"} @@ -1422,9 +1439,11 @@ func TestCondition(t *testing.T) { if Condition(mockT, func() bool { return false }, "Lie") { t.Error("Condition should return false") } + } func TestDidPanic(t *testing.T) { + const panicMsg = "Panic!" if funcDidPanic, msg, _ := didPanic(func() { @@ -1443,9 +1462,11 @@ func TestDidPanic(t *testing.T) { }); funcDidPanic { t.Error("didPanic should return false") } + } func TestPanics(t *testing.T) { + mockT := new(testing.T) if !Panics(mockT, func() { @@ -1461,6 +1482,7 @@ func TestPanics(t *testing.T) { } func TestPanicsWithValue(t *testing.T) { + mockT := new(testing.T) if !PanicsWithValue(mockT, "Panic!", func() { @@ -1488,6 +1510,7 @@ func TestPanicsWithValue(t *testing.T) { } func TestPanicsWithError(t *testing.T) { + mockT := new(testing.T) if !PanicsWithError(mockT, "panic", func() { @@ -1515,6 +1538,7 @@ func TestPanicsWithError(t *testing.T) { } func TestNotPanics(t *testing.T) { + mockT := new(testing.T) if !NotPanics(mockT, func() { @@ -1527,9 +1551,11 @@ func TestNotPanics(t *testing.T) { }) { t.Error("NotPanics should return false") } + } func TestNoError(t *testing.T) { + mockT := new(testing.T) // start with a nil error @@ -1560,6 +1586,7 @@ type customError struct{} func (*customError) Error() string { return "fail" } func TestError(t *testing.T) { + mockT := new(testing.T) // start with a nil error @@ -1623,6 +1650,7 @@ func TestErrorContains(t *testing.T) { } func Test_isEmpty(t *testing.T) { + chWithValue := make(chan struct{}, 1) chWithValue <- struct{}{} @@ -1649,6 +1677,7 @@ func Test_isEmpty(t *testing.T) { } func TestEmpty(t *testing.T) { + mockT := new(testing.T) chWithValue := make(chan struct{}, 1) chWithValue <- struct{}{} @@ -1693,6 +1722,7 @@ func TestEmpty(t *testing.T) { } func TestNotEmpty(t *testing.T) { + mockT := new(testing.T) chWithValue := make(chan struct{}, 1) chWithValue <- struct{}{} @@ -1806,6 +1836,7 @@ func TestLen(t *testing.T) { } func TestWithinDuration(t *testing.T) { + mockT := new(testing.T) a := time.Now() b := a.Add(10 * time.Second) @@ -1824,6 +1855,7 @@ func TestWithinDuration(t *testing.T) { } func TestWithinRange(t *testing.T) { + mockT := new(testing.T) n := time.Now() s := n.Add(-time.Second) @@ -2046,6 +2078,7 @@ func TestInEpsilonSlice(t *testing.T) { 0.04), "{2.2, 2.0} is not element-wise close to {2.1, 2.1} in epsilon=0.04") False(t, InEpsilonSlice(mockT, "", nil, 1), "Expected non numeral slices to fail") + } func TestRegexp(t *testing.T) { @@ -2778,7 +2811,8 @@ func TestFailNowWithPlainTestingT(t *testing.T) { }, "should panic since mockT is missing FailNow()") } -type mockFailNowTestingT struct{} +type mockFailNowTestingT struct{ +} func (m *mockFailNowTestingT) Errorf(format string, args ...interface{}) {} @@ -2793,7 +2827,7 @@ func TestFailNowWithFullTestingT(t *testing.T) { } func TestBytesEqual(t *testing.T) { - cases := []struct { + var cases = []struct { a, b []byte }{ {make([]byte, 2), make([]byte, 2)}, @@ -3229,6 +3263,7 @@ func Test_validateEqualArgs(t *testing.T) { } func Test_truncatingFormat(t *testing.T) { + original := strings.Repeat("a", bufio.MaxScanTokenSize-102) result := truncatingFormat(original) Equal(t, fmt.Sprintf("%#v", original), result, "string should not be truncated") diff --git a/require/require.go b/require/require.go index bc8468230..e6c22dfaa 100644 --- a/require/require.go +++ b/require/require.go @@ -4,8 +4,8 @@ package require import ( assert "github.com/stretchr/testify/assert" - jsonmatch "github.com/stretchr/testify/assert/jsonmatch" http "net/http" + jsonmatch "github.com/stretchr/testify/assert/jsonmatch" url "net/url" time "time" ) diff --git a/require/require_forward.go b/require/require_forward.go index 554d440cc..b05e10550 100644 --- a/require/require_forward.go +++ b/require/require_forward.go @@ -4,8 +4,8 @@ package require import ( assert "github.com/stretchr/testify/assert" - jsonmatch "github.com/stretchr/testify/assert/jsonmatch" http "net/http" + jsonmatch "github.com/stretchr/testify/assert/jsonmatch" url "net/url" time "time" )