Skip to content

Commit

Permalink
chore: cleanup gno files (#3527)
Browse files Browse the repository at this point in the history
This PR mostly removes some unused variables and type assertions that
should have been identified by `golang` as error at compile time. These
errors were flagged by `gnopls`, and I'm unsure why they are not
currently detected as error by our current `gno` tools suite.

---------

Signed-off-by: gfanton <[email protected]>
Co-authored-by: Leon Hudak <[email protected]>
Co-authored-by: Morgan Bazalgette <[email protected]>
  • Loading branch information
3 people authored Jan 20, 2025
1 parent 5448754 commit a52b8b2
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 94 deletions.
134 changes: 68 additions & 66 deletions examples/gno.land/p/demo/btree/btree_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -138,32 +138,32 @@ func TestHas(t *testing.T) {
}

func TestMin(t *testing.T) {
min := Content(genericSeeding(New(WithDegree(10)), 53).Min())
min := genericSeeding(New(WithDegree(10)), 53).Min().(Content)

if min.Key != 0 {
t.Errorf("Minimum should have been 0, but it was reported as %d.", min)
}
}

func TestMax(t *testing.T) {
max := Content(genericSeeding(New(WithDegree(10)), 53).Min())
max := genericSeeding(New(WithDegree(10)), 53).Max().(Content)

if max.Key != 0 {
t.Errorf("Minimum should have been 0, but it was reported as %d.", max)
if max.Key != 52 {
t.Errorf("Maximum should have been 52, but it was reported as %d.", max)
}
}

func TestGet(t *testing.T) {
tree := genericSeeding(New(WithDegree(10)), 40)

if Content(tree.Get(Content{Key: 7})).Value != "Value_7" {
t.Errorf("Get(7) should have returned 'Value_7', but it returned %v.", tree.Get(Content{Key: 7}))
if val := tree.Get(Content{Key: 7}); val != nil && val.(Content).Value != "Value_7" {
t.Errorf("Get(7) should have returned 'Value_7', but it returned %v.", val)
}
if Content(tree.Get(Content{Key: 39})).Value != "Value_39" {
t.Errorf("Get(40) should have returnd 'Value_39', but it returned %v.", tree.Get(Content{Key: 39}))
if val := tree.Get(Content{Key: 39}); val != nil && val.(Content).Value != "Value_39" {
t.Errorf("Get(39) should have returned 'Value_39', but it returned %v.", val)
}
if tree.Get(Content{Key: 1111}) != nil {
t.Errorf("Get(1111) returned %v, but it should be nil.", Content(tree.Get(Content{Key: 1111})))
if val := tree.Get(Content{Key: 1111}); val != nil {
t.Errorf("Get(1111) returned %v, but it should be nil.", val)
}
}

Expand All @@ -174,8 +174,8 @@ func TestDescend(t *testing.T) {
found := []int{}

tree.Descend(func(_record Record) bool {
record := Content(_record)
found = append(found, int(record.Key))
record := _record.(Content)
found = append(found, record.Key.(int))
return true
})

Expand All @@ -191,8 +191,8 @@ func TestDescendGreaterThan(t *testing.T) {
found := []int{}

tree.DescendGreaterThan(Content{Key: 4}, func(_record Record) bool {
record := Content(_record)
found = append(found, int(record.Key))
record := _record.(Content)
found = append(found, record.Key.(int))
return true
})

Expand All @@ -208,8 +208,8 @@ func TestDescendLessOrEqual(t *testing.T) {
found := []int{}

tree.DescendLessOrEqual(Content{Key: 4}, func(_record Record) bool {
record := Content(_record)
found = append(found, int(record.Key))
record := _record.(Content)
found = append(found, record.Key.(int))
return true
})

Expand All @@ -225,8 +225,8 @@ func TestDescendRange(t *testing.T) {
found := []int{}

tree.DescendRange(Content{Key: 6}, Content{Key: 1}, func(_record Record) bool {
record := Content(_record)
found = append(found, int(record.Key))
record := _record.(Content)
found = append(found, record.Key.(int))
return true
})

Expand All @@ -242,8 +242,8 @@ func TestAscend(t *testing.T) {
found := []int{}

tree.Ascend(func(_record Record) bool {
record := Content(_record)
found = append(found, int(record.Key))
record := _record.(Content)
found = append(found, record.Key.(int))
return true
})

Expand All @@ -259,8 +259,8 @@ func TestAscendGreaterOrEqual(t *testing.T) {
found := []int{}

tree.AscendGreaterOrEqual(Content{Key: 5}, func(_record Record) bool {
record := Content(_record)
found = append(found, int(record.Key))
record := _record.(Content)
found = append(found, record.Key.(int))
return true
})

Expand All @@ -276,13 +276,13 @@ func TestAscendLessThan(t *testing.T) {
found := []int{}

tree.AscendLessThan(Content{Key: 5}, func(_record Record) bool {
record := Content(_record)
found = append(found, int(record.Key))
record := _record.(Content)
found = append(found, record.Key.(int))
return true
})

if intSlicesCompare(expected, found) != 0 {
t.Errorf("DescendLessOrEqual returned the wrong sequence. Expected %v, but got %v.", expected, found)
t.Errorf("AscendLessThan returned the wrong sequence. Expected %v, but got %v.", expected, found)
}
}

Expand All @@ -293,13 +293,13 @@ func TestAscendRange(t *testing.T) {
found := []int{}

tree.AscendRange(Content{Key: 2}, Content{Key: 7}, func(_record Record) bool {
record := Content(_record)
found = append(found, int(record.Key))
record := _record.(Content)
found = append(found, record.Key.(int))
return true
})

if intSlicesCompare(expected, found) != 0 {
t.Errorf("DescendRange returned the wrong sequence. Expected %v, but got %v.", expected, found)
t.Errorf("AscendRange returned the wrong sequence. Expected %v, but got %v.", expected, found)
}
}

Expand All @@ -309,11 +309,11 @@ func TestDeleteMin(t *testing.T) {
expected := []int{0, 1, 2, 3, 4}
found := []int{}

found = append(found, int(Content(tree.DeleteMin()).Key))
found = append(found, int(Content(tree.DeleteMin()).Key))
found = append(found, int(Content(tree.DeleteMin()).Key))
found = append(found, int(Content(tree.DeleteMin()).Key))
found = append(found, int(Content(tree.DeleteMin()).Key))
found = append(found, tree.DeleteMin().(Content).Key.(int))
found = append(found, tree.DeleteMin().(Content).Key.(int))
found = append(found, tree.DeleteMin().(Content).Key.(int))
found = append(found, tree.DeleteMin().(Content).Key.(int))
found = append(found, tree.DeleteMin().(Content).Key.(int))

if intSlicesCompare(expected, found) != 0 {
t.Errorf("5 rounds of DeleteMin returned the wrong elements. Expected %v, but got %v.", expected, found)
Expand All @@ -326,11 +326,11 @@ func TestShift(t *testing.T) {
expected := []int{0, 1, 2, 3, 4}
found := []int{}

found = append(found, int(Content(tree.Shift()).Key))
found = append(found, int(Content(tree.Shift()).Key))
found = append(found, int(Content(tree.Shift()).Key))
found = append(found, int(Content(tree.Shift()).Key))
found = append(found, int(Content(tree.Shift()).Key))
found = append(found, tree.Shift().(Content).Key.(int))
found = append(found, tree.Shift().(Content).Key.(int))
found = append(found, tree.Shift().(Content).Key.(int))
found = append(found, tree.Shift().(Content).Key.(int))
found = append(found, tree.Shift().(Content).Key.(int))

if intSlicesCompare(expected, found) != 0 {
t.Errorf("5 rounds of Shift returned the wrong elements. Expected %v, but got %v.", expected, found)
Expand All @@ -343,14 +343,14 @@ func TestDeleteMax(t *testing.T) {
expected := []int{99, 98, 97, 96, 95}
found := []int{}

found = append(found, int(Content(tree.DeleteMax()).Key))
found = append(found, int(Content(tree.DeleteMax()).Key))
found = append(found, int(Content(tree.DeleteMax()).Key))
found = append(found, int(Content(tree.DeleteMax()).Key))
found = append(found, int(Content(tree.DeleteMax()).Key))
found = append(found, tree.DeleteMax().(Content).Key.(int))
found = append(found, tree.DeleteMax().(Content).Key.(int))
found = append(found, tree.DeleteMax().(Content).Key.(int))
found = append(found, tree.DeleteMax().(Content).Key.(int))
found = append(found, tree.DeleteMax().(Content).Key.(int))

if intSlicesCompare(expected, found) != 0 {
t.Errorf("5 rounds of DeleteMin returned the wrong elements. Expected %v, but got %v.", expected, found)
t.Errorf("5 rounds of DeleteMax returned the wrong elements. Expected %v, but got %v.", expected, found)
}
}

Expand All @@ -360,14 +360,14 @@ func TestPop(t *testing.T) {
expected := []int{99, 98, 97, 96, 95}
found := []int{}

found = append(found, int(Content(tree.Pop()).Key))
found = append(found, int(Content(tree.Pop()).Key))
found = append(found, int(Content(tree.Pop()).Key))
found = append(found, int(Content(tree.Pop()).Key))
found = append(found, int(Content(tree.Pop()).Key))
found = append(found, tree.Pop().(Content).Key.(int))
found = append(found, tree.Pop().(Content).Key.(int))
found = append(found, tree.Pop().(Content).Key.(int))
found = append(found, tree.Pop().(Content).Key.(int))
found = append(found, tree.Pop().(Content).Key.(int))

if intSlicesCompare(expected, found) != 0 {
t.Errorf("5 rounds of DeleteMin returned the wrong elements. Expected %v, but got %v.", expected, found)
t.Errorf("5 rounds of Pop returned the wrong elements. Expected %v, but got %v.", expected, found)
}
}

Expand All @@ -383,13 +383,15 @@ func TestInsertGet(t *testing.T) {
}

for count := 0; count < 20; count++ {
if tree.Get(Content{Key: count}) != expected[count] {
t.Errorf("Insert/Get doesn't appear to be working. Expected to retrieve %v with key %d, but got %v.", expected[count], count, tree.Get(Content{Key: count}))
val := tree.Get(Content{Key: count})
if val == nil || val.(Content) != expected[count] {
t.Errorf("Insert/Get doesn't appear to be working. Expected to retrieve %v with key %d, but got %v.", expected[count], count, val)
}
}
}

func TestClone(t *testing.T) {
// Implement the clone test
}

// ***** The following tests are functional or stress testing type tests.
Expand Down Expand Up @@ -440,18 +442,18 @@ func TestBTree(t *testing.T) {
val := tree.Get(test.test)

if test.expected {
if val != nil && Content(val).Value == test.test.Value {
if val != nil && val.(Content).Value == test.test.Value {
t.Logf("Found expected key:value %v:%v", test.test.Key, test.test.Value)
} else {
if val == nil {
t.Logf("Didn't find %v, but expected", test.test.Key)
} else {
t.Errorf("Expected key %v:%v, but found %v:%v.", test.test.Key, test.test.Value, Content(val).Key, Content(val).Value)
t.Errorf("Expected key %v:%v, but found %v:%v.", test.test.Key, test.test.Value, val.(Content).Key, val.(Content).Value)
}
}
} else {
if val != nil {
t.Errorf("Did not expect key %v, but found key:value %v:%v", test.test.Key, Content(val).Key, Content(val).Value)
t.Errorf("Did not expect key %v, but found key:value %v:%v", test.test.Key, val.(Content).Key, val.(Content).Value)
} else {
t.Logf("Didn't find %v, but wasn't expected", test.test.Key)
}
Expand All @@ -474,7 +476,7 @@ func TestBTree(t *testing.T) {
t.Logf("Tree Length: %d", tree.Len())

tree.Ascend(func(_record Record) bool {
record := Content(_record)
record := _record.(Content)
t.Logf("Key:Value == %v:%v", record.Key, record.Value)
if record.Key != sortedInsertData[pos].Key {
t.Errorf("Out of order! Expected %v, but got %v", sortedInsertData[pos].Key, record.Key)
Expand All @@ -487,7 +489,7 @@ func TestBTree(t *testing.T) {
pos = len(sortedInsertData) - 1

tree.Descend(func(_record Record) bool {
record := Content(_record)
record := _record.(Content)
t.Logf("Key:Value == %v:%v", record.Key, record.Value)
if record.Key != sortedInsertData[pos].Key {
t.Errorf("Out of order! Expected %v, but got %v", sortedInsertData[pos].Key, record.Key)
Expand All @@ -497,10 +499,10 @@ func TestBTree(t *testing.T) {
})

deleteTests := []Content{
Content{Key: 10, Value: "Value_10"},
Content{Key: 15, Value: ""},
Content{Key: "banana", Value: "Fruit_banana"},
Content{Key: "kiwi", Value: ""},
{Key: 10, Value: "Value_10"},
{Key: 15, Value: ""},
{Key: "banana", Value: "Fruit_banana"},
{Key: "kiwi", Value: ""},
}
for _, test := range deleteTests {
fmt.Printf("\nDeleting %+v\n", test)
Expand All @@ -514,7 +516,7 @@ func TestBTree(t *testing.T) {
for _, test := range deleteTests {
val := tree.Get(test)
if val != nil {
t.Errorf("Did not expect key %v, but found key:value %v:%v", test.Key, Content(val).Key, Content(val).Value)
t.Errorf("Did not expect key %v, but found key:value %v:%v", test.Key, val.(Content).Key, val.(Content).Value)
} else {
t.Logf("Didn't find %v, but wasn't expected", test.Key)
}
Expand Down Expand Up @@ -558,7 +560,7 @@ func TestStress(t *testing.T) {
val := tree.Get(content)
if i%2 == 0 {
if val != nil {
t.Errorf("Didn't expect key %v, but found key:value %v:%v", content.Key, Content(val).Key, Content(val).Value)
t.Errorf("Didn't expect key %v, but found key:value %v:%v", content.Key, val.(Content).Key, val.(Content).Value)
}
} else {
if val == nil {
Expand Down Expand Up @@ -643,7 +645,7 @@ func TestBTreeCloneIsolation(t *testing.T) {

if i%3 == 0 || i%2 == 0 {
if val != nil {
t.Errorf("Didn't expect key %v, but found key:value %v:%v", content.Key, Content(val).Key, Content(val).Value)
t.Errorf("Didn't expect key %v, but found key:value %v:%v", content.Key, val.(Content).Key, val.(Content).Value)
}
} else {
if val == nil {
Expand All @@ -654,7 +656,7 @@ func TestBTreeCloneIsolation(t *testing.T) {
val = clone.Get(content)
if i%2 != 0 || i%3 == 0 {
if val != nil {
t.Errorf("Didn't expect key %v, but found key:value %v:%v", content.Key, Content(val).Key, Content(val).Value)
t.Errorf("Didn't expect key %v, but found key:value %v:%v", content.Key, val.(Content).Key, val.(Content).Value)
}
} else {
if val == nil {
Expand All @@ -665,7 +667,7 @@ func TestBTreeCloneIsolation(t *testing.T) {
val = clone2.Get(content)
if i%2 != 0 || (i-2)%3 == 0 {
if val != nil {
t.Errorf("Didn't expect key %v, but found key:value %v:%v", content.Key, Content(val).Key, Content(val).Value)
t.Errorf("Didn't expect key %v, but found key:value %v:%v", content.Key, val.(Content).Key, val.(Content).Value)
}
} else {
if val == nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/p/demo/context/context_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func TestContextExample(t *testing.T) {
ctx := WithValue(Empty(), k, "Gno")

if v := ctx.Value(k); v != nil {
if string(v) != "Gno" {
if v.(string) != "Gno" {
t.Errorf("language value should be Gno, but is %s", v)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestBalanceOf(t *testing.T) {
tid1 := TokenID("1")
tid2 := TokenID("2")

balanceZeroAddressOfToken1, err := dummy.BalanceOf(zeroAddress, tid1)
_, err := dummy.BalanceOf(zeroAddress, tid1)
uassert.Error(t, err, "should result in error")

balanceAddr1OfToken1, err := dummy.BalanceOf(addr1, tid1)
Expand Down
4 changes: 2 additions & 2 deletions examples/gno.land/p/demo/grc/grc721/basic_nft_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestGetApproved(t *testing.T) {
dummy := NewBasicNFT(dummyNFTName, dummyNFTSymbol)
uassert.True(t, dummy != nil, "should not be nil")

approvedAddr, err := dummy.GetApproved(TokenID("invalid"))
_, err := dummy.GetApproved(TokenID("invalid"))
uassert.Error(t, err, "should result in error")
}

Expand Down Expand Up @@ -247,7 +247,7 @@ func TestBurn(t *testing.T) {
uassert.NoError(t, err, "should not result in error")

// Check Owner of Token id
owner, err := dummy.OwnerOf(TokenID("1"))
_, err = dummy.OwnerOf(TokenID("1"))
uassert.Error(t, err, "should result in error")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestSetTokenRoyalty(t *testing.T) {
uassert.NoError(t, derr, "Should not result in error")

// Test case: Invalid token ID
err := dummy.SetTokenRoyalty(TokenID("3"), RoyaltyInfo{
_ = dummy.SetTokenRoyalty(TokenID("3"), RoyaltyInfo{
PaymentAddress: paymentAddress,
Percentage: percentage,
})
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/p/demo/grc/grc777/dummy_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type dummyImpl struct{}
var _ IGRC777 = (*dummyImpl)(nil)

func TestInterface(t *testing.T) {
var dummy IGRC777 = &dummyImpl{}
var _ IGRC777 = &dummyImpl{}
}

func (impl *dummyImpl) GetName() string { panic("not implemented") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestRecurringSubscription(t *testing.T) {
err = rs.HasValidSubscription(std.PrevRealm().Addr())
uassert.NoError(t, err, "Expected Alice to have access")

expiration, err := rs.GetExpiration(std.PrevRealm().Addr())
_, err = rs.GetExpiration(std.PrevRealm().Addr())
uassert.NoError(t, err, "Expected to get expiration for Alice")
}

Expand Down
Loading

0 comments on commit a52b8b2

Please sign in to comment.