Skip to content
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

ban require.Equal when testing for 0 #1495

Merged
merged 6 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
46 changes: 23 additions & 23 deletions codec/test_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestStruct(codec GeneralCodec, t testing.TB) {
version, err := manager.Unmarshal(myStructBytes, myStructUnmarshaled)
require.NoError(err)

require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(myStructInstance, *myStructUnmarshaled)
}

Expand Down Expand Up @@ -180,7 +180,7 @@ func TestUInt32(codec GeneralCodec, t testing.TB) {
var numberUnmarshaled uint32
version, err := manager.Unmarshal(bytes, &numberUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(number, numberUnmarshaled)
}

Expand Down Expand Up @@ -215,7 +215,7 @@ func TestSlice(codec GeneralCodec, t testing.TB) {
var sliceUnmarshaled []bool
version, err := manager.Unmarshal(bytes, &sliceUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(mySlice, sliceUnmarshaled)
}

Expand All @@ -240,7 +240,7 @@ func TestMaxSizeSlice(codec GeneralCodec, t testing.TB) {
var sliceUnmarshaled []string
version, err := manager.Unmarshal(bytes, &sliceUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(mySlice, sliceUnmarshaled)
}

Expand All @@ -263,7 +263,7 @@ func TestBool(codec GeneralCodec, t testing.TB) {
var boolUnmarshaled bool
version, err := manager.Unmarshal(bytes, &boolUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(myBool, boolUnmarshaled)
}

Expand All @@ -286,7 +286,7 @@ func TestArray(codec GeneralCodec, t testing.TB) {
var myArrUnmarshaled [5]uint64
version, err := manager.Unmarshal(bytes, &myArrUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(myArr, myArrUnmarshaled)
}

Expand All @@ -309,7 +309,7 @@ func TestBigArray(codec GeneralCodec, t testing.TB) {
var myArrUnmarshaled [30000]uint64
version, err := manager.Unmarshal(bytes, &myArrUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(myArr, myArrUnmarshaled)
}

Expand All @@ -332,7 +332,7 @@ func TestPointerToStruct(codec GeneralCodec, t testing.TB) {
var myPtrUnmarshaled *MyInnerStruct
version, err := manager.Unmarshal(bytes, &myPtrUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(myPtr, myPtrUnmarshaled)
}

Expand Down Expand Up @@ -369,7 +369,7 @@ func TestSliceOfStruct(codec GeneralCodec, t testing.TB) {
var mySliceUnmarshaled []MyInnerStruct3
version, err := manager.Unmarshal(bytes, &mySliceUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(mySlice, mySliceUnmarshaled)
}

Expand All @@ -395,7 +395,7 @@ func TestInterface(codec GeneralCodec, t testing.TB) {
var unmarshaledFoo Foo
version, err := manager.Unmarshal(bytes, &unmarshaledFoo)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(f, unmarshaledFoo)
}

Expand Down Expand Up @@ -428,7 +428,7 @@ func TestSliceOfInterface(codec GeneralCodec, t testing.TB) {
var mySliceUnmarshaled []Foo
version, err := manager.Unmarshal(bytes, &mySliceUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(mySlice, mySliceUnmarshaled)
}

Expand Down Expand Up @@ -461,7 +461,7 @@ func TestArrayOfInterface(codec GeneralCodec, t testing.TB) {
var myArrayUnmarshaled [2]Foo
version, err := manager.Unmarshal(bytes, &myArrayUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(myArray, myArrayUnmarshaled)
}

Expand Down Expand Up @@ -489,7 +489,7 @@ func TestPointerToInterface(codec GeneralCodec, t testing.TB) {
var myPtrUnmarshaled *Foo
version, err := manager.Unmarshal(bytes, &myPtrUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(myPtr, myPtrUnmarshaled)
}

Expand All @@ -512,7 +512,7 @@ func TestString(codec GeneralCodec, t testing.TB) {
var stringUnmarshaled string
version, err := manager.Unmarshal(bytes, &stringUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(myString, stringUnmarshaled)
}

Expand All @@ -539,8 +539,8 @@ func TestNilSlice(codec GeneralCodec, t testing.TB) {
var structUnmarshaled structWithSlice
version, err := manager.Unmarshal(bytes, &structUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Equal(0, len(structUnmarshaled.Slice))
require.Zero(version)
require.Empty(structUnmarshaled.Slice)
}

// Ensure that trying to serialize a struct with an unexported member
Expand Down Expand Up @@ -596,7 +596,7 @@ func TestSerializeOfNoSerializeField(codec GeneralCodec, t testing.TB) {
unmarshalled := s{}
version, err := manager.Unmarshal(marshalled, &unmarshalled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)

expectedUnmarshalled := s{SerializedField: "Serialize me"}
require.Equal(expectedUnmarshalled, unmarshalled)
Expand Down Expand Up @@ -627,8 +627,8 @@ func TestNilSliceSerialization(codec GeneralCodec, t testing.TB) {
valUnmarshaled := &simpleSliceStruct{}
version, err := manager.Unmarshal(result, &valUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Equal(0, len(valUnmarshaled.Arr))
require.Zero(version)
require.Empty(valUnmarshaled.Arr)
}

// Test marshaling a slice that has 0 elements (but isn't nil)
Expand Down Expand Up @@ -656,7 +656,7 @@ func TestEmptySliceSerialization(codec GeneralCodec, t testing.TB) {
valUnmarshaled := &simpleSliceStruct{}
version, err := manager.Unmarshal(result, &valUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(val, valUnmarshaled)
}

Expand Down Expand Up @@ -689,7 +689,7 @@ func TestSliceWithEmptySerialization(codec GeneralCodec, t testing.TB) {
unmarshaled := nestedSliceStruct{}
version, err := manager.Unmarshal(expected, &unmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(1000, len(unmarshaled.Arr))
}

Expand Down Expand Up @@ -756,7 +756,7 @@ func TestNegativeNumbers(codec GeneralCodec, t testing.TB) {
mySUnmarshaled := s{}
version, err := manager.Unmarshal(bytes, &mySUnmarshaled)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
require.Equal(myS, mySUnmarshaled)
}

Expand Down Expand Up @@ -808,7 +808,7 @@ func TestUnmarshalInvalidInterface(codec GeneralCodec, t testing.TB) {
s := outer{}
version, err := manager.Unmarshal(bytes, &s)
require.NoError(err)
require.Equal(uint16(0), version)
require.Zero(version)
}
{
bytes := []byte{0, 0, 0, 0, 0, 1}
Expand Down
12 changes: 6 additions & 6 deletions database/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestNewSingleLevelDB(t *testing.T) {

semDB := manager.Current()
cmp := semDB.Version.Compare(v1)
require.Equal(0, cmp, "incorrect version on current database")
require.Zero(cmp, "incorrect version on current database")

_, exists := manager.Previous()
require.False(exists, "there should be no previous database")
Expand All @@ -63,7 +63,7 @@ func TestNewCreatesSingleDB(t *testing.T) {

semDB := manager.Current()
cmp := semDB.Version.Compare(v1)
require.Equal(0, cmp, "incorrect version on current database")
require.Zero(cmp, "incorrect version on current database")

_, exists := manager.Previous()
require.False(exists, "there should be no previous database")
Expand Down Expand Up @@ -173,19 +173,19 @@ func TestNewSortsDatabases(t *testing.T) {

semDB := manager.Current()
cmp := semDB.Version.Compare(vers[0])
require.Equal(0, cmp, "incorrect version on current database")
require.Zero(cmp, "incorrect version on current database")

prev, exists := manager.Previous()
require.True(exists, "expected to find a previous database")
cmp = prev.Version.Compare(vers[1])
require.Equal(0, cmp, "incorrect version on previous database")
require.Zero(cmp, "incorrect version on previous database")

dbs := manager.GetDatabases()
require.Equal(len(vers), len(dbs))

for i, db := range dbs {
cmp = db.Version.Compare(vers[i])
require.Equal(0, cmp, "expected to find database version %s, but found %s", vers[i], db.Version.String())
require.Zero(cmp, "expected to find database version %s, but found %s", vers[i], db.Version.String())
}
}

Expand Down Expand Up @@ -401,7 +401,7 @@ func TestNewManagerFromDBs(t *testing.T) {
dbs := m.GetDatabases()
require.Len(dbs, len(versions))
for i, db := range dbs {
require.Equal(0, db.Version.Compare(versions[i]))
require.Zero(db.Version.Compare(versions[i]))
}
}

Expand Down
2 changes: 1 addition & 1 deletion database/test_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ func TestClear(t *testing.T, db Database) {

count, err = Count(db)
require.NoError(err)
require.Equal(0, count)
require.Zero(count)

require.NoError(db.Close())
}
Expand Down
20 changes: 19 additions & 1 deletion scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fi
# by default, "./scripts/lint.sh" runs all lint tests
# to run only "license_header" test
# TESTS='license_header' ./scripts/lint.sh
TESTS=${TESTS:-"golangci_lint license_header require_error_is_no_funcs_as_params single_import interface_compliance_nil"}
TESTS=${TESTS:-"golangci_lint license_header require_error_is_no_funcs_as_params single_import interface_compliance_nil require_equal_zero"}

function test_golangci_lint {
go install -v github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.2
Expand Down Expand Up @@ -57,6 +57,24 @@ function test_require_error_is_no_funcs_as_params {
fi
}

function test_require_equal_zero {
# check if the first arg, other than t, is 0
if grep -R -o -P 'require\.Equal\((t, )?(u?int\d+\(0\)|0)' .; then
echo ""
echo "Use require.Zero instead of require.Equal when testing for 0."
echo ""
return 1
fi

# check if the last arg is 0
if grep -R -zo -P 'require\.Equal\(.+?, (u?int\d+\(0\)|0)\)\n' .; then
echo ""
echo "Use require.Zero instead of require.Equal when testing for 0."
echo ""
return 1
fi
}

# Ref: https://go.dev/doc/effective_go#blank_implements
function test_interface_compliance_nil {
if grep -R -o -P '_ .+? = &.+?\{\}' .; then
Expand Down
8 changes: 4 additions & 4 deletions snow/engine/common/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestRequests(t *testing.T) {
req := Requests{}

length := req.Len()
require.Equal(t, 0, length, "should have had no outstanding requests")
require.Zero(t, length, "should have had no outstanding requests")

_, removed := req.Remove(ids.EmptyNodeID, 0)
require.False(t, removed, "shouldn't have removed the request")
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestRequests(t *testing.T) {
require.True(t, removed, "should have removed the request")

length = req.Len()
require.Equal(t, 0, length, "should have had no outstanding requests")
require.Zero(t, length, "should have had no outstanding requests")

req.Add(ids.EmptyNodeID, 0, ids.Empty)

Expand All @@ -80,11 +80,11 @@ func TestRequests(t *testing.T) {
require.True(t, removed, "should have removed the request")

length = req.Len()
require.Equal(t, 0, length, "should have had no outstanding requests")
require.Zero(t, length, "should have had no outstanding requests")

removed = req.RemoveAny(ids.Empty)
require.False(t, removed, "shouldn't have removed the request")

length = req.Len()
require.Equal(t, 0, length, "should have had no outstanding requests")
require.Zero(t, length, "should have had no outstanding requests")
}
12 changes: 6 additions & 6 deletions snow/networking/benchlist/benchlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func TestBenchlistAdd(t *testing.T) {
require.False(t, b.isBenched(vdrID3))
require.False(t, b.isBenched(vdrID4))
require.Len(t, b.failureStreaks, 0)
require.Equal(t, b.benchedQueue.Len(), 0)
require.Equal(t, b.benchlistSet.Len(), 0)
require.Zero(t, b.benchedQueue.Len())
require.Zero(t, b.benchlistSet.Len())
b.lock.Unlock()

// Register [threshold - 1] failures in a row for vdr0
Expand All @@ -84,8 +84,8 @@ func TestBenchlistAdd(t *testing.T) {

// Still shouldn't be benched due to not enough consecutive failure
require.False(t, b.isBenched(vdrID0))
require.Equal(t, b.benchedQueue.Len(), 0)
require.Equal(t, b.benchlistSet.Len(), 0)
require.Zero(t, b.benchedQueue.Len())
require.Zero(t, b.benchlistSet.Len())
require.Len(t, b.failureStreaks, 1)
fs := b.failureStreaks[vdrID0]
require.Equal(t, threshold-1, fs.consecutive)
Expand All @@ -98,8 +98,8 @@ func TestBenchlistAdd(t *testing.T) {
// has passed since the first failure
b.lock.Lock()
require.False(t, b.isBenched(vdrID0))
require.Equal(t, b.benchedQueue.Len(), 0)
require.Equal(t, b.benchlistSet.Len(), 0)
require.Zero(t, b.benchedQueue.Len())
require.Zero(t, b.benchlistSet.Len())
b.lock.Unlock()

// Move the time up
Expand Down
4 changes: 2 additions & 2 deletions snow/networking/router/chain_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ func TestRouterHonorsRequestedEngine(t *testing.T) {
chainRouter.HandleInbound(context.Background(), msg)
}

require.Equal(0, chainRouter.timedRequests.Len())
require.Zero(chainRouter.timedRequests.Len())
}

func TestRouterClearTimeouts(t *testing.T) {
Expand Down Expand Up @@ -1087,7 +1087,7 @@ func TestRouterClearTimeouts(t *testing.T) {
chainRouter.HandleInbound(context.Background(), msg)
}

require.Equal(t, 0, chainRouter.timedRequests.Len())
require.Zero(t, chainRouter.timedRequests.Len())
}

func TestValidatorOnlyMessageDrops(t *testing.T) {
Expand Down
Loading