Skip to content

Commit 0c2b88c

Browse files
test(token): fix lint and quantity test expectations
Signed-off-by: Nishant <nishantbkl3345@gmail.com>
1 parent 10a4e07 commit 0c2b88c

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

token/token/quantity.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,22 @@ func ToQuantity(q string, precision uint64) (Quantity, error) {
8484
}
8585
}
8686

87+
// ToQuantitySum computes the sum of the quantities of the tokens in the iterator.
8788
// ToQuantitySum computes the sum of the quantities of the tokens in the iterator.
8889
func ToQuantitySum(precision uint64) iterators.Reducer[*UnspentToken, Quantity] {
8990
return iterators.NewReducer(NewZeroQuantity(precision), func(sum Quantity, tok *UnspentToken) (res Quantity, err error) {
91+
defer func() {
92+
if r := recover(); r != nil {
93+
err = errors.Errorf("failed to sum quantities: %v", r)
94+
}
95+
}()
96+
9097
q, err := ToQuantity(tok.Quantity, precision)
9198
if err != nil {
9299
return nil, err
93100
}
94101

95-
96102
return sum.Add(q)
97-
98-
return sum.Add(q), nil
99-
e9cc9c08 (fix(token): handle panic recovery correctly in ToQuantitySum and add missing edge case tests (#1348))
100103
})
101104
}
102105

token/token/quantity_test.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ func IntToHex(q int64) string {
413413
return "0x" + strconv.FormatInt(q, 16)
414414
}
415415

416-
417416
// Additional comprehensive tests for improved coverage
418417

419418
func TestTypeMismatchErrors(t *testing.T) {
@@ -811,18 +810,24 @@ func TestUInt64Quantity_ToBigInt(t *testing.T) {
811810
assert.Equal(t, int64(12345), bi.Int64())
812811
}
813812

814-
func TestQuantity_TypeMismatchPanics(t *testing.T) {
815-
big128 := token.NewZeroQuantity(128)
816-
u64 := token.NewZeroQuantity(64)
813+
func TestQuantity_TypeMismatchReturnsError(t *testing.T) {
814+
u64, err := token.UInt64ToQuantity(10, 64)
815+
require.NoError(t, err)
816+
817+
big128, err := token.NewUBigQuantity("10", 128)
818+
require.NoError(t, err)
819+
820+
_, err = big128.Add(u64)
821+
require.Error(t, err)
817822

818-
// BigQuantity methods Add and Sub panic when given UInt64Quantity
819-
assert.Panics(t, func() { big128.(*token.BigQuantity).Add(u64) })
820-
assert.Panics(t, func() { big128.(*token.BigQuantity).Sub(u64) })
823+
_, err = big128.Sub(u64)
824+
require.Error(t, err)
821825

822-
// UInt64Quantity methods Add and Sub panic when given BigQuantity
823-
big128 = token.NewZeroQuantity(128) // fresh instance
824-
assert.Panics(t, func() { u64.(*token.UInt64Quantity).Add(big128) })
825-
assert.Panics(t, func() { u64.(*token.UInt64Quantity).Sub(big128) })
826+
_, err = u64.(*token.UInt64Quantity).Add(big128)
827+
require.Error(t, err)
828+
829+
_, err = u64.(*token.UInt64Quantity).Sub(big128)
830+
require.Error(t, err)
826831
}
827832

828833
func TestUInt64ToQuantity_BigQuantityPath(t *testing.T) {
@@ -878,6 +883,6 @@ func TestToQuantitySum(t *testing.T) {
878883
// Add 1 more to trigger panic
879884
_, err = reducer.Reduce(s, &token.UnspentToken{Quantity: "1"})
880885
require.Error(t, err)
881-
assert.Contains(t, err.Error(), "overflow in quantity sum")
886+
assert.Contains(t, err.Error(), "exceeds uint64")
882887
})
883888
}

0 commit comments

Comments
 (0)