Skip to content

Commit 0eb61fb

Browse files
authored
Ban usage of require.Equal when testing for length (#1497)
1 parent d146232 commit 0eb61fb

File tree

15 files changed

+65
-47
lines changed

15 files changed

+65
-47
lines changed

codec/test_codec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ func TestSliceWithEmptySerialization(codec GeneralCodec, t testing.TB) {
690690
version, err := manager.Unmarshal(expected, &unmarshaled)
691691
require.NoError(err)
692692
require.Zero(version)
693-
require.Equal(1000, len(unmarshaled.Arr))
693+
require.Len(unmarshaled.Arr, 1000)
694694
}
695695

696696
func TestSliceWithEmptySerializationOutOfMemory(codec GeneralCodec, t testing.TB) {

database/manager/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func TestNewSortsDatabases(t *testing.T) {
181181
require.Zero(cmp, "incorrect version on previous database")
182182

183183
dbs := manager.GetDatabases()
184-
require.Equal(len(vers), len(dbs))
184+
require.Len(dbs, len(vers))
185185

186186
for i, db := range dbs {
187187
cmp = db.Version.Compare(vers[i])

scripts/lint.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fi
2121
# by default, "./scripts/lint.sh" runs all lint tests
2222
# to run only "license_header" test
2323
# TESTS='license_header' ./scripts/lint.sh
24-
TESTS=${TESTS:-"golangci_lint license_header require_error_is_no_funcs_as_params single_import interface_compliance_nil require_equal_zero require_len_zero"}
24+
TESTS=${TESTS:-"golangci_lint license_header require_error_is_no_funcs_as_params single_import interface_compliance_nil require_equal_zero require_len_zero require_equal_len"}
2525

2626
function test_golangci_lint {
2727
go install -v github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.2
@@ -84,6 +84,24 @@ function test_require_len_zero {
8484
fi
8585
}
8686

87+
function test_require_equal_len {
88+
# This should only flag if len(foo) is the *actual* val, not the expected val.
89+
#
90+
# These should *not* match:
91+
# - require.Equal(len(foo), 2)
92+
# - require.Equal(t, len(foo), 2)
93+
#
94+
# These should match:
95+
# - require.Equal(2, len(foo))
96+
# - require.Equal(t, 2, len(foo))
97+
if grep -R -o -P --exclude-dir='scripts' 'require\.Equal\((t, )?.*, len\([^,]*$' .; then
98+
echo ""
99+
echo "Use require.Len instead of require.Equal when testing for length."
100+
echo ""
101+
return 1
102+
fi
103+
}
104+
87105
# Ref: https://go.dev/doc/effective_go#blank_implements
88106
function test_interface_compliance_nil {
89107
if grep -R -o -P '_ .+? = &.+?\{\}' .; then

snow/engine/common/queue/jobs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ func TestHandleJobWithMissingDependencyOnRunnableStack(t *testing.T) {
411411
}
412412

413413
missingIDs := jobs.MissingIDs()
414-
require.Equal(1, len(missingIDs))
414+
require.Len(missingIDs, 1)
415415

416416
require.Equal(missingIDs[0], job0.ID())
417417

utils/buffer/unbounded_deque_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func TestUnboundedSliceDequePushLeftPopLeft(t *testing.T) {
236236
require.IsType(&unboundedSliceDeque[int]{}, bIntf)
237237
b := bIntf.(*unboundedSliceDeque[int])
238238
require.Zero(bIntf.Len())
239-
require.Equal(2, len(b.data))
239+
require.Len(b.data, 2)
240240
require.Zero(b.left)
241241
require.Equal(1, b.right)
242242
require.Empty(b.List())
@@ -251,7 +251,7 @@ func TestUnboundedSliceDequePushLeftPopLeft(t *testing.T) {
251251

252252
b.PushLeft(1) // slice is [1,EMPTY]
253253
require.Equal(1, b.Len())
254-
require.Equal(2, len(b.data))
254+
require.Len(b.data, 2)
255255
require.Equal(1, b.left)
256256
require.Equal(1, b.right)
257257
require.Equal([]int{1}, b.List())
@@ -267,7 +267,7 @@ func TestUnboundedSliceDequePushLeftPopLeft(t *testing.T) {
267267
// This causes a resize
268268
b.PushLeft(2) // slice is [2,1,EMPTY,EMPTY]
269269
require.Equal(2, b.Len())
270-
require.Equal(4, len(b.data))
270+
require.Len(b.data, 4)
271271
require.Equal(3, b.left)
272272
require.Equal(2, b.right)
273273
require.Equal([]int{2, 1}, b.List())
@@ -289,7 +289,7 @@ func TestUnboundedSliceDequePushLeftPopLeft(t *testing.T) {
289289
// Tests left moving left with no wrap around.
290290
b.PushLeft(3) // slice is [2,1,EMPTY,3]
291291
require.Equal(3, b.Len())
292-
require.Equal(4, len(b.data))
292+
require.Len(b.data, 4)
293293
require.Equal(2, b.left)
294294
require.Equal(2, b.right)
295295
require.Equal([]int{3, 2, 1}, b.List())
@@ -318,7 +318,7 @@ func TestUnboundedSliceDequePushLeftPopLeft(t *testing.T) {
318318
require.True(ok)
319319
require.Equal(3, got)
320320
require.Equal(2, b.Len())
321-
require.Equal(4, len(b.data))
321+
require.Len(b.data, 4)
322322
require.Equal(3, b.left)
323323
require.Equal(2, b.right)
324324
require.Equal([]int{2, 1}, b.List())
@@ -342,7 +342,7 @@ func TestUnboundedSliceDequePushLeftPopLeft(t *testing.T) {
342342
require.True(ok)
343343
require.Equal(2, got)
344344
require.Equal(1, b.Len())
345-
require.Equal(4, len(b.data))
345+
require.Len(b.data, 4)
346346
require.Zero(b.left)
347347
require.Equal(2, b.right)
348348
require.Equal([]int{1}, b.List())
@@ -361,7 +361,7 @@ func TestUnboundedSliceDequePushLeftPopLeft(t *testing.T) {
361361
// Test left wrapping around to the right side.
362362
b.PushLeft(2) // slice is [2,1,EMPTY,EMPTY]
363363
require.Equal(2, b.Len())
364-
require.Equal(4, len(b.data))
364+
require.Len(b.data, 4)
365365
require.Equal(3, b.left)
366366
require.Equal(2, b.right)
367367
require.Equal([]int{2, 1}, b.List())
@@ -384,7 +384,7 @@ func TestUnboundedSliceDequePushLeftPopLeft(t *testing.T) {
384384
require.True(ok)
385385
require.Equal(2, got)
386386
require.Equal(1, b.Len())
387-
require.Equal(4, len(b.data))
387+
require.Len(b.data, 4)
388388
require.Zero(b.left)
389389
require.Equal(2, b.right)
390390
require.Equal([]int{1}, b.List())
@@ -396,7 +396,7 @@ func TestUnboundedSliceDequePushLeftPopLeft(t *testing.T) {
396396
require.True(ok)
397397
require.Equal(1, got)
398398
require.Zero(b.Len())
399-
require.Equal(4, len(b.data))
399+
require.Len(b.data, 4)
400400
require.Equal(1, b.left)
401401
require.Equal(2, b.right)
402402
require.Empty(b.List())
@@ -419,7 +419,7 @@ func TestUnboundedSliceDequePushRightPopRight(t *testing.T) {
419419
require.IsType(&unboundedSliceDeque[int]{}, bIntf)
420420
b := bIntf.(*unboundedSliceDeque[int])
421421
require.Zero(bIntf.Len())
422-
require.Equal(2, len(b.data))
422+
require.Len(b.data, 2)
423423
require.Zero(b.left)
424424
require.Equal(1, b.right)
425425
require.Empty(b.List())
@@ -434,7 +434,7 @@ func TestUnboundedSliceDequePushRightPopRight(t *testing.T) {
434434

435435
b.PushRight(1) // slice is [1,EMPTY]
436436
require.Equal(1, b.Len())
437-
require.Equal(2, len(b.data))
437+
require.Len(b.data, 2)
438438
require.Zero(b.left)
439439
require.Zero(b.right)
440440
require.Equal([]int{1}, b.List())
@@ -453,7 +453,7 @@ func TestUnboundedSliceDequePushRightPopRight(t *testing.T) {
453453
// This causes a resize
454454
b.PushRight(2) // slice is [1,2,EMPTY,EMPTY]
455455
require.Equal(2, b.Len())
456-
require.Equal(4, len(b.data))
456+
require.Len(b.data, 4)
457457
require.Equal(3, b.left)
458458
require.Equal(2, b.right)
459459
require.Equal([]int{1, 2}, b.List())
@@ -475,7 +475,7 @@ func TestUnboundedSliceDequePushRightPopRight(t *testing.T) {
475475
// Tests right moving right with no wrap around
476476
b.PushRight(3) // slice is [1,2,3,EMPTY]
477477
require.Equal(3, b.Len())
478-
require.Equal(4, len(b.data))
478+
require.Len(b.data, 4)
479479
require.Equal(3, b.left)
480480
require.Equal(3, b.right)
481481
require.Equal([]int{1, 2, 3}, b.List())
@@ -502,7 +502,7 @@ func TestUnboundedSliceDequePushRightPopRight(t *testing.T) {
502502
require.True(ok)
503503
require.Equal(3, got)
504504
require.Equal(2, b.Len())
505-
require.Equal(4, len(b.data))
505+
require.Len(b.data, 4)
506506
require.Equal(3, b.left)
507507
require.Equal(2, b.right)
508508
require.Equal([]int{1, 2}, b.List())
@@ -527,7 +527,7 @@ func TestUnboundedSliceDequePushRightPopRight(t *testing.T) {
527527
require.True(ok)
528528
require.Equal(2, got)
529529
require.Equal(1, b.Len())
530-
require.Equal(4, len(b.data))
530+
require.Len(b.data, 4)
531531
require.Equal(3, b.left)
532532
require.Equal(1, b.right)
533533
require.Equal([]int{1}, b.List())
@@ -549,7 +549,7 @@ func TestUnboundedSliceDequePushRightPopRight(t *testing.T) {
549549
require.True(ok)
550550
require.Equal(1, got)
551551
require.Zero(b.Len())
552-
require.Equal(4, len(b.data))
552+
require.Len(b.data, 4)
553553
require.Equal(3, b.left)
554554
require.Zero(b.right)
555555
require.Empty(b.List())
@@ -566,7 +566,7 @@ func TestUnboundedSliceDequePushRightPopRight(t *testing.T) {
566566

567567
b.PushLeft(1) // slice is [EMPTY,EMPTY,EMPTY,1]
568568
require.Equal(1, b.Len())
569-
require.Equal(4, len(b.data))
569+
require.Len(b.data, 4)
570570
require.Equal(2, b.left)
571571
require.Zero(b.right)
572572
require.Equal([]int{1}, b.List())
@@ -587,7 +587,7 @@ func TestUnboundedSliceDequePushRightPopRight(t *testing.T) {
587587
require.True(ok)
588588
require.Equal(1, got)
589589
require.Zero(b.Len())
590-
require.Equal(4, len(b.data))
590+
require.Len(b.data, 4)
591591
require.Equal(2, b.left)
592592
require.Equal(3, b.right)
593593
require.Empty(b.List())
@@ -604,7 +604,7 @@ func TestUnboundedSliceDequePushRightPopRight(t *testing.T) {
604604
// Tests right wrapping around to the left
605605
b.PushRight(2) // slice is [EMPTY,EMPTY,EMPTY,2]
606606
require.Equal(1, b.Len())
607-
require.Equal(4, len(b.data))
607+
require.Len(b.data, 4)
608608
require.Equal(2, b.left)
609609
require.Zero(b.right)
610610
require.Equal([]int{2}, b.List())
@@ -648,7 +648,7 @@ func FuzzUnboundedSliceDeque(f *testing.F) {
648648
}
649649

650650
list := b.List()
651-
require.Equal(len(input), len(list))
651+
require.Len(list, len(input))
652652
for i, n := range input {
653653
require.Equal(n, list[i])
654654
}

utils/sampler/rand_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,6 @@ func FuzzRNG(f *testing.F) {
205205
mathRNG := rand.New(stdSource) //#nosec G404
206206
stdVal := mathRNG.Int63n(int64(max + 1))
207207
require.Equal(val, uint64(stdVal))
208-
require.Equal(len(source.nums), len(stdSource.nums))
208+
require.Len(stdSource.nums, len(source.nums))
209209
})
210210
}

utils/set/set_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func TestSet(t *testing.T) {
2626
s.Add(id1)
2727
require.True(s.Contains(id1))
2828
require.Len(s.List(), 1)
29-
require.Equal(len(s.List()), 1)
3029
require.Equal(id1, s.List()[0])
3130

3231
s.Clear()

vms/platformvm/service_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ func TestGetCurrentValidators(t *testing.T) {
600600

601601
err := service.GetCurrentValidators(nil, &args, &response)
602602
require.NoError(err)
603-
require.Equal(len(genesis.Validators), len(response.Validators))
603+
require.Len(response.Validators, len(genesis.Validators))
604604

605605
for _, vdr := range genesis.Validators {
606606
found := false
@@ -650,7 +650,7 @@ func TestGetCurrentValidators(t *testing.T) {
650650
args = GetCurrentValidatorsArgs{SubnetID: constants.PrimaryNetworkID}
651651
err = service.GetCurrentValidators(nil, &args, &response)
652652
require.NoError(err)
653-
require.Equal(len(genesis.Validators), len(response.Validators))
653+
require.Len(response.Validators, len(genesis.Validators))
654654

655655
// Make sure the delegator is there
656656
found := false
@@ -676,7 +676,7 @@ func TestGetCurrentValidators(t *testing.T) {
676676
require.Equal(vdr.NodeID, innerVdr.NodeID)
677677

678678
require.NotNil(innerVdr.Delegators)
679-
require.Equal(1, len(*innerVdr.Delegators))
679+
require.Len(*innerVdr.Delegators, 1)
680680
delegator := (*innerVdr.Delegators)[0]
681681
require.Equal(delegator.NodeID, innerVdr.NodeID)
682682
require.Equal(uint64(delegator.StartTime), delegatorStartTime)
@@ -696,14 +696,14 @@ func TestGetCurrentValidators(t *testing.T) {
696696
// Call getValidators
697697
response = GetCurrentValidatorsReply{}
698698
require.NoError(service.GetCurrentValidators(nil, &args, &response))
699-
require.Equal(len(genesis.Validators), len(response.Validators))
699+
require.Len(response.Validators, len(genesis.Validators))
700700

701-
for i := 0; i < len(response.Validators); i++ {
702-
vdr := response.Validators[i].(pchainapi.PermissionlessValidator)
703-
if vdr.NodeID != validatorNodeID {
701+
for _, vdr := range response.Validators {
702+
castVdr := vdr.(pchainapi.PermissionlessValidator)
703+
if castVdr.NodeID != validatorNodeID {
704704
continue
705705
}
706-
require.Equal(uint64(100000), uint64(*vdr.AccruedDelegateeReward))
706+
require.Equal(uint64(100000), uint64(*castVdr.AccruedDelegateeReward))
707707
}
708708
}
709709

vms/platformvm/txs/executor/import_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ func TestNewImportTx(t *testing.T) {
168168

169169
unsignedTx := tx.Unsigned.(*txs.ImportTx)
170170
require.NotEmpty(unsignedTx.ImportedInputs)
171-
require.Equal(len(tx.Creds), len(unsignedTx.Ins)+len(unsignedTx.ImportedInputs), "should have the same number of credentials as inputs")
171+
numInputs := len(unsignedTx.Ins) + len(unsignedTx.ImportedInputs)
172+
require.Equal(len(tx.Creds), numInputs, "should have the same number of credentials as inputs")
172173

173174
totalIn := uint64(0)
174175
for _, in := range unsignedTx.Ins {

vms/platformvm/vm_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ func TestGenesis(t *testing.T) {
549549
require.True(ok)
550550

551551
currentValidators := vdrSet.List()
552-
require.Equal(len(currentValidators), len(genesisState.Validators))
552+
require.Len(genesisState.Validators, len(currentValidators))
553553

554554
for _, key := range keys {
555555
nodeID := ids.NodeID(key.PublicKey().Address())
@@ -2776,7 +2776,7 @@ func TestVM_GetValidatorSet(t *testing.T) {
27762776
if tt.expectedErr != nil {
27772777
return
27782778
}
2779-
require.Equal(len(tt.expectedVdrSet), len(gotVdrSet))
2779+
require.Len(gotVdrSet, len(tt.expectedVdrSet))
27802780
for nodeID, vdr := range tt.expectedVdrSet {
27812781
otherVdr, ok := gotVdrSet[nodeID]
27822782
require.True(ok)

0 commit comments

Comments
 (0)