Skip to content

Commit

Permalink
[1658]: Create stringerLines and use it to clean up some more unit te…
Browse files Browse the repository at this point in the history
…st stuff.
  • Loading branch information
SpicyLemon committed Oct 11, 2023
1 parent 86d4d7b commit 7fa39e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
47 changes: 14 additions & 33 deletions x/exchange/fulfillment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,7 @@ func transferString(t *Transfer) string {

// transfersStringsLines creates a string for each transfer.
func transfersStringsLines(ts []*Transfer) []string {
if ts == nil {
return nil
}
rv := make([]string, len(ts))
for i, t := range ts {
rv[i] = transferString(t)
}
return rv
return stringerLines(ts, transferString)
}

// String converts a indexedAddrAmtsString to a string.
Expand Down Expand Up @@ -272,28 +265,19 @@ func assertEqualOrderFulfillmentSlices(t *testing.T, expected, actual []*orderFu
}

// Check the order ids (and lengths) since that's gonna be a huge clue to a problem
expIDs := make([]string, len(expected))
for i, exp := range expected {
expIDs[i] = fmt.Sprintf("%d", exp.GetOrderID())
}
actIDs := make([]string, len(actual))
for i, act := range actual {
actIDs[i] = fmt.Sprintf("%d", act.GetOrderID())
idStringer := func(of *orderFulfillment) string {
return fmt.Sprintf("%d", of.GetOrderID())
}
expIDs := stringerLines(expected, idStringer)
actIDs := stringerLines(actual, idStringer)
if !assert.Equalf(t, expIDs, actIDs, msg("OrderIDs"), args...) {
// Wooo, should have actionable info in the failure, so we can be done.
return false
}

// Try the comparisons as strings, one per line because that's easier with ints and coins.
expStrs := make([]string, len(expected))
for i, exp := range expected {
expStrs[i] = orderFulfillmentString(exp)
}
actStrs := make([]string, len(actual))
for i, act := range actual {
actStrs[i] = orderFulfillmentString(act)
}
expStrs := stringerLines(expected, orderFulfillmentString)
actStrs := stringerLines(actual, orderFulfillmentString)
if !assert.Equalf(t, expStrs, actStrs, msg("orderFulfillment strings"), args...) {
// Wooo, should have actionable info in the failure, so we can be done.
return false
Expand Down Expand Up @@ -1010,23 +994,20 @@ func TestBuildSettlement(t *testing.T) {
assertions.RequireErrorValue(t, err, tc.expErr, "BuildSettlement error")
if !assert.Equal(t, tc.expSettlement, settlement, "BuildSettlement result") {
// Doing each field on its own now to try to help pinpoint the differences.
expTrans := transfersStringsLines(tc.expSettlement.Transfers)
actTrans := transfersStringsLines(settlement.Transfers)
expTrans := stringerLines(tc.expSettlement.Transfers, transferString)
actTrans := stringerLines(settlement.Transfers, transferString)
assert.Equal(t, expTrans, actTrans, "Transfers (as strings)")

expFeeInputs := bankInputsString(tc.expSettlement.FeeInputs)
actFeeInputs := bankInputsString(settlement.FeeInputs)
assert.Equal(t, expFeeInputs, actFeeInputs, "FeeInputs (as strings)")

expFilledIds := make([]uint64, len(tc.expSettlement.FullyFilledOrders))
for i, fo := range tc.expSettlement.FullyFilledOrders {
expFilledIds[i] = fo.GetOrderID()
}
actFilledIds := make([]uint64, len(settlement.FullyFilledOrders))
for i, fo := range settlement.FullyFilledOrders {
actFilledIds[i] = fo.GetOrderID()
idStringer := func(of *FilledOrder) string {
return fmt.Sprintf("%d", of.GetOrderID())
}
if assert.Equal(t, expFilledIds, actFilledIds, "FullyFilledOrders ids") {
expFilledIDs := stringerLines(tc.expSettlement.FullyFilledOrders, idStringer)
actFilledIDs := stringerLines(settlement.FullyFilledOrders, idStringer)
if assert.Equal(t, expFilledIDs, actFilledIDs, "FullyFilledOrders ids") {
// If they're the same ids, compare each individually.
for i := range tc.expSettlement.FullyFilledOrders {
assert.Equal(t, tc.expSettlement.FullyFilledOrders[i], settlement.FullyFilledOrders[i], "FullyFilledOrders[%d]", i)
Expand Down
10 changes: 9 additions & 1 deletion x/exchange/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@ func stringerJoin[T any](vals []T, stringer func(T) string, sep string) string {
if vals == nil {
return "nil"
}
return "[" + strings.Join(stringerLines(vals, stringer), sep) + "]"
}

// stringerLines returns a slice where each of the vals has been converted using the given stringer.
func stringerLines[T any](vals []T, stringer func(T) string) []string {
if vals == nil {
return nil
}
strs := make([]string, len(vals))
for i, val := range vals {
strs[i] = stringer(val)
}
return "[" + strings.Join(strs, sep) + "]"
return strs
}

// joinErrs joines the provided error strings into a single one to match what errors.Join does.
Expand Down

0 comments on commit 7fa39e2

Please sign in to comment.