Skip to content

Commit

Permalink
[1658]: Create stringerJoin to clean up some of the unit test helper …
Browse files Browse the repository at this point in the history
…funcs.
  • Loading branch information
SpicyLemon committed Oct 11, 2023
1 parent d45ba46 commit 56aceff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
39 changes: 5 additions & 34 deletions x/exchange/fulfillment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,7 @@ func distributionString(dist *distribution) string {

// distributionsString is similar to %v except with easier to understand Int entries.
func distributionsString(dists []*distribution) string {
if dists == nil {
return "nil"
}
vals := make([]string, len(dists))
for i, d := range dists {
vals[i] = distributionString(d)
}
return fmt.Sprintf("[%s]", strings.Join(vals, ", "))
return stringerJoin(dists, distributionString, ", ")
}

// orderFulfillmentString is similar to %v except with easier to understand Coin and Int entries.
Expand All @@ -140,15 +133,7 @@ func orderFulfillmentString(f *orderFulfillment) string {

// orderFulfillmentsString is similar to %v except with easier to understand Coin entries.
func orderFulfillmentsString(ofs []*orderFulfillment) string {
if ofs == nil {
return "nil"
}

vals := make([]string, len(ofs))
for i, f := range ofs {
vals[i] = orderFulfillmentString(f)
}
return fmt.Sprintf("[%s]", strings.Join(vals, ", "))
return stringerJoin(ofs, orderFulfillmentString, ", ")
}

// bankInputString is similar to %v except with easier to understand Coin entries.
Expand All @@ -158,14 +143,7 @@ func bankInputString(i banktypes.Input) string {

// bankInputsString returns a string with all the provided inputs.
func bankInputsString(ins []banktypes.Input) string {
if ins == nil {
return "nil"
}
vals := make([]string, len(ins))
for i, input := range ins {
vals[i] = bankInputString(input)
}
return fmt.Sprintf("[%s]", strings.Join(vals, ", "))
return stringerJoin(ins, bankInputString, ", ")
}

// bankOutputString is similar to %v except with easier to understand Coin entries.
Expand All @@ -175,14 +153,7 @@ func bankOutputString(o banktypes.Output) string {

// bankOutputsString returns a string with all the provided outputs.
func bankOutputsString(outs []banktypes.Output) string {
if outs == nil {
return "nil"
}
vals := make([]string, len(outs))
for i, output := range outs {
vals[i] = bankOutputString(output)
}
return fmt.Sprintf("[%s]", strings.Join(vals, ", "))
return stringerJoin(outs, bankOutputString, ", ")
}

// transferString is similar to %v except with easier to understand Coin entries.
Expand Down Expand Up @@ -229,7 +200,7 @@ func indexedAddrAmtsString(i *indexedAddrAmts) string {
for j, amt := range i.amts {
amtsVals[j] = fmt.Sprintf("%q", amt)
}
amts = fmt.Sprintf("[]%T{%s}", i.amts, strings.Join(amtsVals, ", "))
amts = fmt.Sprintf("%T{%s}", i.amts, strings.Join(amtsVals, ", "))
}

indexes := "nil"
Expand Down
12 changes: 12 additions & 0 deletions x/exchange/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ func copySlice[T any](vals []T, copier func(T) T) []T {
return rv
}

// stringerJoin runs the stringer on each of the provided vals and joins them using the provided separator.
func stringerJoin[T any](vals []T, stringer func(T) string, sep 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) + "]"
}

// 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")
Expand Down

0 comments on commit 56aceff

Please sign in to comment.