diff --git a/x/exchange/fulfillment_test.go b/x/exchange/fulfillment_test.go index c39087f035..bba53eaa3d 100644 --- a/x/exchange/fulfillment_test.go +++ b/x/exchange/fulfillment_test.go @@ -25,18 +25,6 @@ import ( // So when an object has an sdkmath.Int that should have been reduced to zero, you'll need to use this. var ZeroAmtAfterSub = sdkmath.NewInt(1).SubRaw(1) -// copySlice copies a slice using the provided copier for each entry. -func copySlice[T any](vals []T, copier func(T) T) []T { - if vals == nil { - return nil - } - rv := make([]T, len(vals)) - for i, v := range vals { - rv[i] = copier(v) - } - return rv -} - // copyDistribution copies a distribution. func copyDistribution(dist *distribution) *distribution { if dist == nil { diff --git a/x/exchange/helpers_test.go b/x/exchange/helpers_test.go index 0a1f1ec224..69165546c0 100644 --- a/x/exchange/helpers_test.go +++ b/x/exchange/helpers_test.go @@ -1,7 +1,9 @@ package exchange import ( + "fmt" "regexp" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -9,9 +11,11 @@ import ( sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/provenance-io/provenance/testutil/assertions" ) +// amtRx is a regex matching characters that can be removed from an amount string. var amtRx = regexp.MustCompile(`[,_ ]`) // newInt converts the provided string into an Int, stipping out any commas, underscores or spaces first. @@ -22,6 +26,68 @@ func newInt(t *testing.T, amount string) sdkmath.Int { return rv } +// copySlice copies a slice using the provided copier for each entry. +func copySlice[T any](vals []T, copier func(T) T) []T { + if vals == nil { + return nil + } + rv := make([]T, len(vals)) + for i, v := range vals { + rv[i] = copier(v) + } + return rv +} + +// joinErrs joines the provided error strings into a single one to match what errors.Join does. +func joinErrs(errs ...string) string { + return strings.Join(errs, "\n") +} + +// copySDKInt creates a copy of the provided sdkmath.Int +func copySDKInt(i sdkmath.Int) (copy sdkmath.Int) { + defer func() { + if r := recover(); r != nil { + copy = sdkmath.Int{} + } + }() + return i.AddRaw(0) +} + +// copyCoins creates a copy of the provided coins slice with copies of each entry. +func copyCoins(coins sdk.Coins) sdk.Coins { + return copySlice(coins, copyCoin) +} + +// copyCoin returns a copy of the provided coin. +func copyCoin(coin sdk.Coin) sdk.Coin { + return sdk.Coin{Denom: coin.Denom, Amount: copySDKInt(coin.Amount)} +} + +// copyCoinP returns a copy of the provided *coin. +func copyCoinP(coin *sdk.Coin) *sdk.Coin { + if coin == nil { + return nil + } + rv := copyCoin(*coin) + return &rv +} + +// coinPString returns either "nil" or the quoted string version of the provided coins. +func coinPString(coin *sdk.Coin) string { + if coin == nil { + return "nil" + } + return fmt.Sprintf("%q", coin) +} + +// coinsString returns either "nil" or the quoted string version of the provided coins. +func coinsString(coins sdk.Coins) string { + if coins == nil { + return "nil" + } + return fmt.Sprintf("%q", coins) +} + func TestEqualsUint64(t *testing.T) { tests := []struct { name string diff --git a/x/exchange/market_test.go b/x/exchange/market_test.go index e5372085ba..6e34964b0c 100644 --- a/x/exchange/market_test.go +++ b/x/exchange/market_test.go @@ -17,11 +17,6 @@ import ( "github.com/provenance-io/provenance/testutil/assertions" ) -// joinErrs joines the provided error strings into a single one to match what errors.Join does. -func joinErrs(errs ...string) string { - return strings.Join(errs, "\n") -} - func TestMarket_Validate(t *testing.T) { coins := func(coins string) sdk.Coins { rv, err := sdk.ParseCoinsNormalized(coins) @@ -294,7 +289,6 @@ func TestValidateFeeOptions(t *testing.T) { } func TestMarketDetails_Validate(t *testing.T) { - nameErr := func(over int) string { return fmt.Sprintf("name length %d exceeds maximum length of %d", MaxName+over, MaxName) } diff --git a/x/exchange/msg_test.go b/x/exchange/msg_test.go index 1927f1ee3b..220352248b 100644 --- a/x/exchange/msg_test.go +++ b/x/exchange/msg_test.go @@ -10,6 +10,7 @@ import ( sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/provenance-io/provenance/testutil/assertions" ) diff --git a/x/exchange/orders_test.go b/x/exchange/orders_test.go index 157642924a..c375f67102 100644 --- a/x/exchange/orders_test.go +++ b/x/exchange/orders_test.go @@ -70,51 +70,6 @@ func copyBidOrder(bidOrder *BidOrder) *BidOrder { } } -// copySDKInt creates a copy of the provided sdkmath.Int -func copySDKInt(i sdkmath.Int) (copy sdkmath.Int) { - defer func() { - if r := recover(); r != nil { - copy = sdkmath.Int{} - } - }() - return i.AddRaw(0) -} - -// copyCoins creates a copy of the provided coins slice with copies of each entry. -func copyCoins(coins sdk.Coins) sdk.Coins { - return copySlice(coins, copyCoin) -} - -// copyCoin returns a copy of the provided coin. -func copyCoin(coin sdk.Coin) sdk.Coin { - return sdk.Coin{Denom: coin.Denom, Amount: copySDKInt(coin.Amount)} -} - -// copyCoinP returns a copy of the provided *coin. -func copyCoinP(coin *sdk.Coin) *sdk.Coin { - if coin == nil { - return nil - } - rv := copyCoin(*coin) - return &rv -} - -// coinPString returns either "nil" or the quoted string version of the provided coins. -func coinPString(coin *sdk.Coin) string { - if coin == nil { - return "nil" - } - return fmt.Sprintf("%q", coin) -} - -// coinsString returns either "nil" or the quoted string version of the provided coins. -func coinsString(coins sdk.Coins) string { - if coins == nil { - return "nil" - } - return fmt.Sprintf("%q", coins) -} - // orderString is similar to %v except with easier to understand Coin and Int entries. func orderString(order *Order) string { if order == nil { @@ -173,6 +128,40 @@ func bidOrderString(bidOrder *BidOrder) string { return fmt.Sprintf("{%s}", strings.Join(fields, ", ")) } +// unknownOrderType is thing that implements isOrder_Order so it can be +// added to an Order, but isn't a type that there is anything defined for. +type unknownOrderType struct{} + +var _ isOrder_Order = (*unknownOrderType)(nil) + +func (o *unknownOrderType) isOrder_Order() {} +func (o *unknownOrderType) MarshalTo([]byte) (int, error) { + return 0, nil +} +func (o *unknownOrderType) Size() int { + return 0 +} + +// newUnknownOrder returns a new order with the given id and an unknownOrderType. +func newUnknownOrder(orderID uint64) *Order { + return &Order{OrderId: orderID, Order: &unknownOrderType{}} +} + +// badSubTypeErr creates the expected error when a sub-order type is bad. +func badSubTypeErr(orderID uint64, badType string) string { + return fmt.Sprintf("order %d has unknown sub-order type %s: does not implement SubOrderI", orderID, badType) +} + +// nilSubTypeErr creates the expected error when a sub-order type is nil. +func nilSubTypeErr(orderID uint64) string { + return badSubTypeErr(orderID, "") +} + +// unknownSubTypeErr creates the expected error when a sub-order type is the unknownOrderType. +func unknownSubTypeErr(orderID uint64) string { + return badSubTypeErr(orderID, "*exchange.unknownOrderType") +} + func TestOrderTypesAndBytes(t *testing.T) { values := []struct { name string @@ -538,25 +527,6 @@ func TestOrder_WithBid(t *testing.T) { assert.Equal(t, origBid, orderBid, "the bid in the resulting order (actual) vs what the bid was before being provided (expected)") } -// unknownOrderType is thing that implements isOrder_Order so it can be -// added to an Order, but isn't a type that there is anything defined for. -type unknownOrderType struct{} - -var _ isOrder_Order = (*unknownOrderType)(nil) - -func (o *unknownOrderType) isOrder_Order() {} -func (o *unknownOrderType) MarshalTo([]byte) (int, error) { - return 0, nil -} -func (o *unknownOrderType) Size() int { - return 0 -} - -// newUnknownOrder returns a new order with the given id and an unknownOrderType. -func newUnknownOrder(orderID uint64) *Order { - return &Order{OrderId: orderID, Order: &unknownOrderType{}} -} - func TestOrder_IsAskOrder(t *testing.T) { tests := []struct { name string @@ -662,21 +632,6 @@ func TestOrder_GetOrderID(t *testing.T) { } } -// badSubTypeErr creates the expected error when a sub-order type is bad. -func badSubTypeErr(orderID uint64, badType string) string { - return fmt.Sprintf("order %d has unknown sub-order type %s: does not implement SubOrderI", orderID, badType) -} - -// nilSubTypeErr creates the expected error when a sub-order type is nil. -func nilSubTypeErr(orderID uint64) string { - return badSubTypeErr(orderID, "") -} - -// unknownSubTypeErr creates the expected error when a sub-order type is the unknownOrderType. -func unknownSubTypeErr(orderID uint64) string { - return badSubTypeErr(orderID, "*exchange.unknownOrderType") -} - func TestOrder_GetSubOrder(t *testing.T) { askOrder := &AskOrder{ MarketId: 1,