Skip to content

Commit

Permalink
go/staking/api: Add PrettyPrintCommissionScheduleIndexInfixes() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tjanez committed Sep 11, 2020
1 parent e90011e commit 7ce327b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions .changelog/3265.feature.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/staking/api: Add `PrettyPrintCommissionScheduleIndexInfixes()` helper
16 changes: 16 additions & 0 deletions go/staking/api/prettyprint.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package api

import (
"context"
"fmt"
"strings"

"github.com/oasisprotocol/oasis-core/go/common/prettyprint"
"github.com/oasisprotocol/oasis-core/go/common/quantity"
Expand All @@ -20,3 +22,17 @@ func PrettyPrintCommissionRatePercentage(rateNumerator quantity.Quantity) string
denominatorExp := commissionRateDenominatorExponent - 2
return fmt.Sprintf("%s%%", prettyprint.QuantityFrac(rateNumerator, denominatorExp))
}

// PrettyPrintCommissionScheduleIndexInfixes returns two infixes:
// - indexInfix holds the infix to use to pretty print the given commission
// schedule rate (bound) index
// - emptyInfix holds the infix to use to pretty print an empty string of an
// equivalent length
func PrettyPrintCommissionScheduleIndexInfixes(ctx context.Context) (indexInfix, emptyInfix string) {
index, ok := ctx.Value(prettyprint.ContextKeyCommissionScheduleIndex).(int)
if ok {
indexInfix = fmt.Sprintf("(%d) ", index+1)
emptyInfix = strings.Repeat(" ", len(indexInfix))
}
return
}
25 changes: 25 additions & 0 deletions go/staking/api/prettyprint_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package api

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/oasisprotocol/oasis-core/go/common/prettyprint"
"github.com/oasisprotocol/oasis-core/go/common/quantity"
)

Expand All @@ -29,3 +31,26 @@ func TestPrettyPrintCommissionRatePercentage(t *testing.T) {
require.Equal(t.expectedRate, rate, "obtained pretty print didn't match expected value")
}
}

func TestPrettyPrintCommissionScheduleIndexInfixes(t *testing.T) {
require := require.New(t)

for _, t := range []struct {
expectedIndexInfix string
expectedEmptyInfix string
index int
indexPresent bool
}{
{"(1) ", " ", 0, true},
{"(2) ", " ", 1, true},
{"(10) ", " ", 9, true},
{"(123) ", " ", 122, true},
{"(2345678) ", " ", 2345677, true},
} {
require.Equal(len(t.expectedIndexInfix), len(t.expectedEmptyInfix), "expected index and empty infixes should be of equal length")
ctx := context.WithValue(context.Background(), prettyprint.ContextKeyCommissionScheduleIndex, t.index)
indexInfix, emptyInfix := PrettyPrintCommissionScheduleIndexInfixes(ctx)
require.Equal(t.expectedIndexInfix, indexInfix, "obtained index infix didn't match expected value")
require.Equal(t.expectedEmptyInfix, emptyInfix, "obtained empty infix didn't match expected value")
}
}

0 comments on commit 7ce327b

Please sign in to comment.