From a107b87e7ced9fce1ae3a449f98fbdcaeb4ac339 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Thu, 15 Sep 2022 16:08:05 +0200 Subject: [PATCH 01/11] refactor: Move FormatCoins to `core` --- core/coins/format.go | 92 +++++++++++++++++++ core/coins/format_test.go | 81 ++++++++++++++++ core/go.mod | 8 ++ core/go.sum | 6 ++ math/dec.go | 32 +++++++ math/dec_test.go | 29 ++++++ math/int.go | 42 +++++++++ math/int_test.go | 30 ++++++ tx/go.mod | 3 + tx/textual/valuerenderer/coins.go | 92 +------------------ tx/textual/valuerenderer/dec.go | 40 +------- tx/textual/valuerenderer/dec_test.go | 52 ++++++----- tx/textual/valuerenderer/int.go | 44 +-------- tx/textual/valuerenderer/int_test.go | 82 +++++++++++------ .../valuerenderer/valuerenderer_test.go | 79 ---------------- 15 files changed, 416 insertions(+), 296 deletions(-) create mode 100644 core/coins/format.go create mode 100644 core/coins/format_test.go diff --git a/core/coins/format.go b/core/coins/format.go new file mode 100644 index 000000000000..b290758bad82 --- /dev/null +++ b/core/coins/format.go @@ -0,0 +1,92 @@ +package coins + +import ( + "fmt" + "sort" + "strings" + + bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1" + basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1" + "cosmossdk.io/math" +) + +// formatCoin formats a sdk.Coin into a value-rendered string, using the +// given metadata about the denom. It returns the formatted coin string, the +// display denom, and an optional error. +func formatCoin(coin *basev1beta1.Coin, metadata *bankv1beta1.Metadata) (string, error) { + coinDenom := coin.Denom + + // Return early if no display denom or display denom is the current coin denom. + if metadata == nil || metadata.Display == "" || coinDenom == metadata.Display { + vr, err := math.FormatDecimal(coin.Amount) + return vr + " " + coin.Denom, err + } + + dispDenom := metadata.Display + + // Find exponents of both denoms. + var coinExp, dispExp uint32 + foundCoinExp, foundDispExp := false, false + for _, unit := range metadata.DenomUnits { + if coinDenom == unit.Denom { + coinExp = unit.Exponent + foundCoinExp = true + } + if dispDenom == unit.Denom { + dispExp = unit.Exponent + foundDispExp = true + } + } + + // If we didn't find either exponent, then we return early. + if !foundCoinExp || !foundDispExp { + vr, err := math.FormatInteger(coin.Amount) + return vr + " " + coin.Denom, err + } + + exponentDiff := int64(coinExp) - int64(dispExp) + + dispAmount, err := math.LegacyNewDecFromStr(coin.Amount) + if err != nil { + return "", err + } + + if exponentDiff > 0 { + dispAmount = dispAmount.Mul(math.LegacyNewDec(10).Power(uint64(exponentDiff))) + } else { + dispAmount = dispAmount.Quo(math.LegacyNewDec(10).Power(uint64(-exponentDiff))) + } + + vr, err := math.FormatDecimal(dispAmount.String()) + return vr + " " + dispDenom, err +} + +// formatCoins formats Coins into a value-rendered string, which uses +// `formatCoin` separated by ", " (a comma and a space), and sorted +// alphabetically by value-rendered denoms. It expects an array of metadata +// (optionally nil), where each metadata at index `i` MUST match the coin denom +// at the same index. +func FormatCoins(coins []*basev1beta1.Coin, metadata []*bankv1beta1.Metadata) (string, error) { + if len(coins) != len(metadata) { + panic(fmt.Errorf("formatCoins expect one metadata for each coin; expected %d, got %d", len(coins), len(metadata))) + } + + formatted := make([]string, len(coins)) + for i, coin := range coins { + var err error + formatted[i], err = formatCoin(coin, metadata[i]) + if err != nil { + return "", err + } + } + + // Sort the formatted coins by display denom. + sort.SliceStable(formatted, func(i, j int) bool { + denomI := strings.Split(formatted[i], " ")[1] + denomJ := strings.Split(formatted[j], " ")[1] + + return denomI < denomJ + }) + + return strings.Join(formatted, ", "), nil +} diff --git a/core/coins/format_test.go b/core/coins/format_test.go new file mode 100644 index 000000000000..21c0f3c65d7a --- /dev/null +++ b/core/coins/format_test.go @@ -0,0 +1,81 @@ +package coins_test + +import ( + "encoding/json" + "os" + "testing" + + bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1" + basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1" + "cosmossdk.io/core/coins" + "github.com/stretchr/testify/require" +) + +func TestFormatCoin(t *testing.T) { + var testcases []coinJsonTest + raw, err := os.ReadFile("../../tx/textual/internal/testdata/coin.json") + require.NoError(t, err) + err = json.Unmarshal(raw, &testcases) + require.NoError(t, err) + + for _, tc := range testcases { + t.Run(tc.Text, func(t *testing.T) { + if tc.Proto != nil { + out, err := coins.FormatCoins([]*basev1beta1.Coin{tc.Proto}, []*bankv1beta1.Metadata{tc.Metadata}) + + if tc.Error { + require.Error(t, err) + return + } + + require.NoError(t, err) + require.Equal(t, tc.Text, out) + } + }) + } +} + +func TestFormatCoins(t *testing.T) { + var testcases []coinsJsonTest + raw, err := os.ReadFile("../../tx/textual/internal/testdata/coins.json") + require.NoError(t, err) + err = json.Unmarshal(raw, &testcases) + require.NoError(t, err) + + for _, tc := range testcases { + t.Run(tc.Text, func(t *testing.T) { + if tc.Proto != nil { + metadata := make([]*bankv1beta1.Metadata, len(tc.Proto)) + for i, coin := range tc.Proto { + metadata[i] = tc.Metadata[coin.Denom] + } + + out, err := coins.FormatCoins(tc.Proto, metadata) + + if tc.Error { + require.Error(t, err) + return + } + + require.NoError(t, err) + require.Equal(t, tc.Text, out) + } + }) + } +} + +// coinsJsonTest is the type of test cases in the coin.json file. +type coinJsonTest struct { + Proto *basev1beta1.Coin + Metadata *bankv1beta1.Metadata + Text string + Error bool +} + +// coinsJsonTest is the type of test cases in the coins.json file. +type coinsJsonTest struct { + Proto []*basev1beta1.Coin + Metadata map[string]*bankv1beta1.Metadata + Text string + Error bool +} diff --git a/core/go.mod b/core/go.mod index 45f451209088..bf0eacabf79b 100644 --- a/core/go.mod +++ b/core/go.mod @@ -5,17 +5,22 @@ go 1.18 require ( cosmossdk.io/api v0.2.1 cosmossdk.io/depinject v1.0.0-alpha.3 + cosmossdk.io/math v1.0.0-beta.3 github.com/cosmos/cosmos-proto v1.0.0-alpha7 + github.com/stretchr/testify v1.8.0 google.golang.org/protobuf v1.28.1 gotest.tools/v3 v3.3.0 sigs.k8s.io/yaml v1.3.0 ) require ( + github.com/cosmos/gogoproto v1.4.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.8 // indirect github.com/kr/text v0.2.0 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/exp v0.0.0-20220827204233-334a2380cb91 // indirect golang.org/x/net v0.0.0-20220726230323-06994584191e // indirect golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect @@ -23,4 +28,7 @@ require ( google.golang.org/genproto v0.0.0-20220725144611-272f38e5d71b // indirect google.golang.org/grpc v1.49.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) + +replace cosmossdk.io/math => ../math diff --git a/core/go.sum b/core/go.sum index ceb01fd00738..9799828b3b93 100644 --- a/core/go.sum +++ b/core/go.sum @@ -6,6 +6,8 @@ github.com/alecthomas/participle/v2 v2.0.0-alpha7 h1:cK4vjj0VSgb3lN1nuKA5F7dw+1s github.com/cockroachdb/apd/v3 v3.1.0 h1:MK3Ow7LH0W8zkd5GMKA1PvS9qG3bWFI95WaVNfyZJ/w= github.com/cosmos/cosmos-proto v1.0.0-alpha7 h1:yqYUOHF2jopwZh4dVQp3xgqwftE5/2hkrwIV6vkUbO0= github.com/cosmos/cosmos-proto v1.0.0-alpha7/go.mod h1:dosO4pSAbJF8zWCzCoTWP7nNsjcvSUBQmniFxDg5daw= +github.com/cosmos/gogoproto v1.4.1 h1:WoyH+0/jbCTzpKNvyav5FL1ZTWsp1im1MxEpJEzKUB8= +github.com/cosmos/gogoproto v1.4.1/go.mod h1:Ac9lzL4vFpBMcptJROQ6dQ4M3pOEK5Z/l0Q9p+LoCr4= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/cucumber/common/gherkin/go/v22 v22.0.0 h1:4K8NqptbvdOrjL9DEea6HFjSpbdT9+Q5kgLpmmsHYl0= github.com/cucumber/common/messages/go/v17 v17.1.1 h1:RNqopvIFyLWnKv0LfATh34SWBhXeoFTJnSrgm9cT/Ts= @@ -31,8 +33,11 @@ github.com/regen-network/gocuke v0.6.2 h1:pHviZ0kKAq2U2hN2q3smKNxct6hS0mGByFMHGn github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -79,6 +84,7 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools/v3 v3.3.0 h1:MfDY1b1/0xN1CyMlQDac0ziEy9zJQd9CXBRRDHw2jJo= gotest.tools/v3 v3.3.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A= pgregory.net/rapid v0.4.7/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU= diff --git a/math/dec.go b/math/dec.go index 6d8a473c063b..cb73d593de73 100644 --- a/math/dec.go +++ b/math/dec.go @@ -894,3 +894,35 @@ func LegacyDecApproxEq(t *testing.T, d1 LegacyDec, d2 LegacyDec, tol LegacyDec) diff := d1.Sub(d2).Abs() return t, diff.LTE(tol), "expected |d1 - d2| <:\t%v\ngot |d1 - d2| = \t\t%v", tol.String(), diff.String() } + +// FormatDecimal formats a decimal into a value-rendered string following +// ADR-050. This function operates with string manipulation (instead of +// manipulating the sdk.Dec object). +func FormatDecimal(v string) (string, error) { + parts := strings.Split(v, ".") + if len(parts) > 2 { + return "", fmt.Errorf("invalid decimal: too many points in %s", v) + } + + intPart, err := FormatInteger(parts[0]) + if err != nil { + return "", err + } + + if len(parts) == 1 { + return intPart, nil + } + + decPart := strings.TrimRight(parts[1], "0") + if len(decPart) == 0 { + return intPart, nil + } + + // Ensure that the decimal part has only digits. + // https://github.com/cosmos/cosmos-sdk/issues/12811 + if !hasOnlyDigits(decPart) { + return "", fmt.Errorf("non-digits detected after decimal point in: %q", decPart) + } + + return intPart + "." + decPart, nil +} diff --git a/math/dec_test.go b/math/dec_test.go index 7bbb8a146697..d4ca9f044b21 100644 --- a/math/dec_test.go +++ b/math/dec_test.go @@ -666,3 +666,32 @@ func BenchmarkLegacyQuoRoundupMut(b *testing.B) { } sink = (interface{})(nil) } + +func TestFormatDecimalNonDigits(t *testing.T) { + badCases := []string{ + "10.a", + "1a.10", + "p1a10.", + "0.10p", + "--10", + "12.😎😎", + "11111111111133333333333333333333333333333a", + "11111111111133333333333333333333333333333 192892", + } + + for _, value := range badCases { + value := value + t.Run(value, func(t *testing.T) { + s, err := math.FormatDecimal(value) + if err == nil { + t.Fatal("Expected an error") + } + if g, w := err.Error(), "non-digits"; !strings.Contains(g, w) { + t.Errorf("Error mismatch\nGot: %q\nWant substring: %q", g, w) + } + if s != "" { + t.Fatalf("Got a non-empty string: %q", s) + } + }) + } +} diff --git a/math/int.go b/math/int.go index 159e2d7d224c..5213478e5999 100644 --- a/math/int.go +++ b/math/int.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "math/big" + "strings" "testing" ) @@ -432,3 +433,44 @@ func (i *Int) UnmarshalAmino(bz []byte) error { return i.Unmarshal(bz) } func IntEq(t *testing.T, exp, got Int) (*testing.T, bool, string, string, string) { return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String() } + +func hasOnlyDigits(s string) bool { + if s == "" { + return false + } + for _, r := range s { + if r < '0' || r > '9' { + return false + } + } + return true +} + +const thousandSeparator string = "'" + +// FormatInteger formats an integer into a value-rendered string following +// ADR-050. This function operates with string manipulation (instead of +// manipulating the int or sdk.Int object). +func FormatInteger(v string) (string, error) { + sign := "" + if v[0] == '-' { + sign = "-" + v = v[1:] + } + if len(v) > 1 { + v = strings.TrimLeft(v, "0") + } + + // Ensure that the string contains only digits at this point. + if !hasOnlyDigits(v) { + return "", fmt.Errorf("expecting only digits 0-9, but got non-digits in %q", v) + } + + startOffset := 3 + for outputIndex := len(v); outputIndex > startOffset; { + outputIndex -= 3 + v = v[:outputIndex] + thousandSeparator + v[outputIndex:] + } + + return sign + v, nil +} diff --git a/math/int_test.go b/math/int_test.go index c39fce0ee51c..60aadd9c58f8 100644 --- a/math/int_test.go +++ b/math/int_test.go @@ -5,6 +5,7 @@ import ( "math/big" "math/rand" "strconv" + "strings" "testing" "github.com/stretchr/testify/suite" @@ -424,3 +425,32 @@ func TestRoundTripMarshalToInt(t *testing.T) { }) } } + +func TestFormatIntegerNonDigits(t *testing.T) { + badCases := []string{ + "a10", + "1a10", + "p1a10", + "10p", + "--10", + "😎😎", + "11111111111133333333333333333333333333333a", + "11111111111133333333333333333333333333333 192892", + } + + for _, value := range badCases { + value := value + t.Run(value, func(t *testing.T) { + s, err := math.FormatInteger(value) + if err == nil { + t.Fatal("Expected an error") + } + if g, w := err.Error(), "but got non-digits in"; !strings.Contains(g, w) { + t.Errorf("Error mismatch\nGot: %q\nWant substring: %q", g, w) + } + if s != "" { + t.Fatalf("Got a non-empty string: %q", s) + } + }) + } +} diff --git a/tx/go.mod b/tx/go.mod index 89517623f115..14bf52998383 100644 --- a/tx/go.mod +++ b/tx/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( cosmossdk.io/api v0.2.1 + cosmossdk.io/core v0.0.0-00010101000000-000000000000 cosmossdk.io/math v1.0.0-beta.3 github.com/cosmos/cosmos-proto v1.0.0-alpha7 github.com/stretchr/testify v1.8.0 @@ -25,3 +26,5 @@ require ( gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +replace cosmossdk.io/core => ../core diff --git a/tx/textual/valuerenderer/coins.go b/tx/textual/valuerenderer/coins.go index 0b3b302dd938..eb43f07286ae 100644 --- a/tx/textual/valuerenderer/coins.go +++ b/tx/textual/valuerenderer/coins.go @@ -4,13 +4,12 @@ import ( "context" "fmt" "io" - "sort" - "strings" + + "google.golang.org/protobuf/reflect/protoreflect" bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1" basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1" - "cosmossdk.io/math" - "google.golang.org/protobuf/reflect/protoreflect" + corecoins "cosmossdk.io/core/coins" ) // NewCoinsValueRenderer returns a ValueRenderer for SDK Coin and Coins. @@ -49,7 +48,7 @@ func (vr coinsValueRenderer) Format(ctx context.Context, v protoreflect.Value, w } } - formatted, err := formatCoins(coins, metadatas) + formatted, err := corecoins.FormatCoins(coins, metadatas) if err != nil { return err } @@ -67,7 +66,7 @@ func (vr coinsValueRenderer) Format(ctx context.Context, v protoreflect.Value, w return err } - formatted, err := formatCoin(coin, metadata) + formatted, err := corecoins.FormatCoins([]*basev1beta1.Coin{coin}, []*bankv1beta1.Metadata{metadata}) if err != nil { return err } @@ -85,84 +84,3 @@ func (vr coinsValueRenderer) Parse(_ context.Context, r io.Reader) (protoreflect // ref: https://github.com/cosmos/cosmos-sdk/issues/13153 panic("implement me, see #13153") } - -// formatCoin formats a sdk.Coin into a value-rendered string, using the -// given metadata about the denom. It returns the formatted coin string, the -// display denom, and an optional error. -func formatCoin(coin *basev1beta1.Coin, metadata *bankv1beta1.Metadata) (string, error) { - coinDenom := coin.Denom - - // Return early if no display denom or display denom is the current coin denom. - if metadata == nil || metadata.Display == "" || coinDenom == metadata.Display { - vr, err := formatDecimal(coin.Amount) - return vr + " " + coin.Denom, err - } - - dispDenom := metadata.Display - - // Find exponents of both denoms. - var coinExp, dispExp uint32 - foundCoinExp, foundDispExp := false, false - for _, unit := range metadata.DenomUnits { - if coinDenom == unit.Denom { - coinExp = unit.Exponent - foundCoinExp = true - } - if dispDenom == unit.Denom { - dispExp = unit.Exponent - foundDispExp = true - } - } - - // If we didn't find either exponent, then we return early. - if !foundCoinExp || !foundDispExp { - vr, err := formatInteger(coin.Amount) - return vr + " " + coin.Denom, err - } - - exponentDiff := int64(coinExp) - int64(dispExp) - - dispAmount, err := math.LegacyNewDecFromStr(coin.Amount) - if err != nil { - return "", err - } - - if exponentDiff > 0 { - dispAmount = dispAmount.Mul(math.LegacyNewDec(10).Power(uint64(exponentDiff))) - } else { - dispAmount = dispAmount.Quo(math.LegacyNewDec(10).Power(uint64(-exponentDiff))) - } - - vr, err := formatDecimal(dispAmount.String()) - return vr + " " + dispDenom, err -} - -// formatCoins formats Coins into a value-rendered string, which uses -// `formatCoin` separated by ", " (a comma and a space), and sorted -// alphabetically by value-rendered denoms. It expects an array of metadata -// (optionally nil), where each metadata at index `i` MUST match the coin denom -// at the same index. -func formatCoins(coins []*basev1beta1.Coin, metadata []*bankv1beta1.Metadata) (string, error) { - if len(coins) != len(metadata) { - panic(fmt.Errorf("formatCoins expect one metadata for each coin; expected %d, got %d", len(coins), len(metadata))) - } - - formatted := make([]string, len(coins)) - for i, coin := range coins { - var err error - formatted[i], err = formatCoin(coin, metadata[i]) - if err != nil { - return "", err - } - } - - // Sort the formatted coins by display denom. - sort.SliceStable(formatted, func(i, j int) bool { - denomI := strings.Split(formatted[i], " ")[1] - denomJ := strings.Split(formatted[j], " ")[1] - - return denomI < denomJ - }) - - return strings.Join(formatted, ", "), nil -} diff --git a/tx/textual/valuerenderer/dec.go b/tx/textual/valuerenderer/dec.go index a3ab675220ea..ff28b0c53ff8 100644 --- a/tx/textual/valuerenderer/dec.go +++ b/tx/textual/valuerenderer/dec.go @@ -2,14 +2,12 @@ package valuerenderer import ( "context" - "fmt" "io" - "strings" "google.golang.org/protobuf/reflect/protoreflect" -) -const thousandSeparator string = "'" + "cosmossdk.io/math" +) // NewDecValueRenderer returns a ValueRenderer for encoding sdk.Dec cosmos // scalars. @@ -22,7 +20,7 @@ type decValueRenderer struct{} var _ ValueRenderer = decValueRenderer{} func (vr decValueRenderer) Format(_ context.Context, v protoreflect.Value, w io.Writer) error { - formatted, err := formatDecimal(v.String()) + formatted, err := math.FormatDecimal(v.String()) if err != nil { return err } @@ -34,35 +32,3 @@ func (vr decValueRenderer) Format(_ context.Context, v protoreflect.Value, w io. func (vr decValueRenderer) Parse(_ context.Context, r io.Reader) (protoreflect.Value, error) { panic("implement me") } - -// formatDecimal formats a decimal into a value-rendered string. This function -// operates with string manipulation (instead of manipulating the sdk.Dec -// object). -func formatDecimal(v string) (string, error) { - parts := strings.Split(v, ".") - if len(parts) > 2 { - return "", fmt.Errorf("invalid decimal: too many points in %s", v) - } - - intPart, err := formatInteger(parts[0]) - if err != nil { - return "", err - } - - if len(parts) == 1 { - return intPart, nil - } - - decPart := strings.TrimRight(parts[1], "0") - if len(decPart) == 0 { - return intPart, nil - } - - // Ensure that the decimal part has only digits. - // https://github.com/cosmos/cosmos-sdk/issues/12811 - if !hasOnlyDigits(decPart) { - return "", fmt.Errorf("non-digits detected after decimal point in: %q", decPart) - } - - return intPart + "." + decPart, nil -} diff --git a/tx/textual/valuerenderer/dec_test.go b/tx/textual/valuerenderer/dec_test.go index 187c6fd55139..28fbada7da0c 100644 --- a/tx/textual/valuerenderer/dec_test.go +++ b/tx/textual/valuerenderer/dec_test.go @@ -1,35 +1,37 @@ -package valuerenderer +package valuerenderer_test import ( + "context" + "encoding/json" + "os" "strings" "testing" + + "cosmossdk.io/tx/textual/valuerenderer" + "github.com/stretchr/testify/require" + "google.golang.org/protobuf/reflect/protoreflect" ) -func TestFormatDecimalNonDigits(t *testing.T) { - badCases := []string{ - "10.a", - "1a.10", - "p1a10.", - "0.10p", - "--10", - "12.😎😎", - "11111111111133333333333333333333333333333a", - "11111111111133333333333333333333333333333 192892", - } +func TestFormatDecimal(t *testing.T) { + type decimalTest []string + var testcases []decimalTest + raw, err := os.ReadFile("../internal/testdata/decimals.json") + require.NoError(t, err) + err = json.Unmarshal(raw, &testcases) + require.NoError(t, err) + + textual := valuerenderer.NewTextual(nil) + + for _, tc := range testcases { + tc := tc + t.Run(tc[0], func(t *testing.T) { + r, err := textual.GetValueRenderer(fieldDescriptorFromName("SDKDEC")) + require.NoError(t, err) + b := new(strings.Builder) + err = r.Format(context.Background(), protoreflect.ValueOf(tc[0]), b) + require.NoError(t, err) - for _, value := range badCases { - value := value - t.Run(value, func(t *testing.T) { - s, err := formatDecimal(value) - if err == nil { - t.Fatal("Expected an error") - } - if g, w := err.Error(), "non-digits"; !strings.Contains(g, w) { - t.Errorf("Error mismatch\nGot: %q\nWant substring: %q", g, w) - } - if s != "" { - t.Fatalf("Got a non-empty string: %q", s) - } + require.Equal(t, tc[1], b.String()) }) } } diff --git a/tx/textual/valuerenderer/int.go b/tx/textual/valuerenderer/int.go index 6197c405e539..6ce146bbc8b4 100644 --- a/tx/textual/valuerenderer/int.go +++ b/tx/textual/valuerenderer/int.go @@ -2,10 +2,9 @@ package valuerenderer import ( "context" - "fmt" "io" - "strings" + "cosmossdk.io/math" "google.golang.org/protobuf/reflect/protoreflect" ) @@ -20,7 +19,7 @@ type intValueRenderer struct{} var _ ValueRenderer = intValueRenderer{} func (vr intValueRenderer) Format(_ context.Context, v protoreflect.Value, w io.Writer) error { - formatted, err := formatInteger(v.String()) + formatted, err := math.FormatInteger(v.String()) if err != nil { return err } @@ -32,42 +31,3 @@ func (vr intValueRenderer) Format(_ context.Context, v protoreflect.Value, w io. func (vr intValueRenderer) Parse(_ context.Context, r io.Reader) (protoreflect.Value, error) { panic("implement me") } - -func hasOnlyDigits(s string) bool { - if s == "" { - return false - } - for _, r := range s { - if r < '0' || r > '9' { - return false - } - } - return true -} - -// formatInteger formats an integer into a value-rendered string. This function -// operates with string manipulation (instead of manipulating the int or sdk.Int -// object). -func formatInteger(v string) (string, error) { - sign := "" - if v[0] == '-' { - sign = "-" - v = v[1:] - } - if len(v) > 1 { - v = strings.TrimLeft(v, "0") - } - - // Ensure that the string contains only digits at this point. - if !hasOnlyDigits(v) { - return "", fmt.Errorf("expecting only digits 0-9, but got non-digits in %q", v) - } - - startOffset := 3 - for outputIndex := len(v); outputIndex > startOffset; { - outputIndex -= 3 - v = v[:outputIndex] + thousandSeparator + v[outputIndex:] - } - - return sign + v, nil -} diff --git a/tx/textual/valuerenderer/int_test.go b/tx/textual/valuerenderer/int_test.go index 4e30599faf17..96c4b740f85d 100644 --- a/tx/textual/valuerenderer/int_test.go +++ b/tx/textual/valuerenderer/int_test.go @@ -1,35 +1,65 @@ -package valuerenderer +package valuerenderer_test import ( + "context" + "encoding/json" + "os" + "strconv" "strings" "testing" + + "github.com/stretchr/testify/require" + "google.golang.org/protobuf/reflect/protoreflect" + + "cosmossdk.io/math" + "cosmossdk.io/tx/textual/valuerenderer" ) -func TestFormatIntegerNonDigits(t *testing.T) { - badCases := []string{ - "a10", - "1a10", - "p1a10", - "10p", - "--10", - "😎😎", - "11111111111133333333333333333333333333333a", - "11111111111133333333333333333333333333333 192892", - } +func TestFormatInteger(t *testing.T) { + type integerTest []string + var testcases []integerTest + raw, err := os.ReadFile("../internal/testdata/integers.json") + require.NoError(t, err) + err = json.Unmarshal(raw, &testcases) + require.NoError(t, err) + + textual := valuerenderer.NewTextual(nil) + + for _, tc := range testcases { + // Parse test case strings as protobuf uint64 + i, err := strconv.ParseUint(tc[0], 10, 64) + if err == nil { + r, err := textual.GetValueRenderer(fieldDescriptorFromName("UINT64")) + require.NoError(t, err) + b := new(strings.Builder) + err = r.Format(context.Background(), protoreflect.ValueOf(i), b) + require.NoError(t, err) + + require.Equal(t, tc[1], b.String()) + } + + // Parse test case strings as protobuf uint32 + i, err = strconv.ParseUint(tc[0], 10, 32) + if err == nil { + r, err := textual.GetValueRenderer(fieldDescriptorFromName("UINT32")) + require.NoError(t, err) + b := new(strings.Builder) + err = r.Format(context.Background(), protoreflect.ValueOf(i), b) + require.NoError(t, err) + + require.Equal(t, tc[1], b.String()) + } + + // Parse test case strings as sdk.Ints + _, ok := math.NewIntFromString(tc[0]) + if ok { + r, err := textual.GetValueRenderer(fieldDescriptorFromName("SDKINT")) + require.NoError(t, err) + b := new(strings.Builder) + err = r.Format(context.Background(), protoreflect.ValueOf(tc[0]), b) + require.NoError(t, err) - for _, value := range badCases { - value := value - t.Run(value, func(t *testing.T) { - s, err := formatInteger(value) - if err == nil { - t.Fatal("Expected an error") - } - if g, w := err.Error(), "but got non-digits in"; !strings.Contains(g, w) { - t.Errorf("Error mismatch\nGot: %q\nWant substring: %q", g, w) - } - if s != "" { - t.Fatalf("Got a non-empty string: %q", s) - } - }) + require.Equal(t, tc[1], b.String()) + } } } diff --git a/tx/textual/valuerenderer/valuerenderer_test.go b/tx/textual/valuerenderer/valuerenderer_test.go index 5db655592893..524b3d9ce49d 100644 --- a/tx/textual/valuerenderer/valuerenderer_test.go +++ b/tx/textual/valuerenderer/valuerenderer_test.go @@ -1,95 +1,16 @@ package valuerenderer_test import ( - "context" - "encoding/json" "fmt" - "os" - "strconv" - "strings" "testing" "github.com/stretchr/testify/require" "google.golang.org/protobuf/reflect/protoreflect" - "cosmossdk.io/math" "cosmossdk.io/tx/textual/internal/testpb" "cosmossdk.io/tx/textual/valuerenderer" ) -func TestFormatInteger(t *testing.T) { - type integerTest []string - var testcases []integerTest - raw, err := os.ReadFile("../internal/testdata/integers.json") - require.NoError(t, err) - err = json.Unmarshal(raw, &testcases) - require.NoError(t, err) - - textual := valuerenderer.NewTextual(nil) - - for _, tc := range testcases { - // Parse test case strings as protobuf uint64 - i, err := strconv.ParseUint(tc[0], 10, 64) - if err == nil { - r, err := textual.GetValueRenderer(fieldDescriptorFromName("UINT64")) - require.NoError(t, err) - b := new(strings.Builder) - err = r.Format(context.Background(), protoreflect.ValueOf(i), b) - require.NoError(t, err) - - require.Equal(t, tc[1], b.String()) - } - - // Parse test case strings as protobuf uint32 - i, err = strconv.ParseUint(tc[0], 10, 32) - if err == nil { - r, err := textual.GetValueRenderer(fieldDescriptorFromName("UINT32")) - require.NoError(t, err) - b := new(strings.Builder) - err = r.Format(context.Background(), protoreflect.ValueOf(i), b) - require.NoError(t, err) - - require.Equal(t, tc[1], b.String()) - } - - // Parse test case strings as sdk.Ints - _, ok := math.NewIntFromString(tc[0]) - if ok { - r, err := textual.GetValueRenderer(fieldDescriptorFromName("SDKINT")) - require.NoError(t, err) - b := new(strings.Builder) - err = r.Format(context.Background(), protoreflect.ValueOf(tc[0]), b) - require.NoError(t, err) - - require.Equal(t, tc[1], b.String()) - } - } -} - -func TestFormatDecimal(t *testing.T) { - type decimalTest []string - var testcases []decimalTest - raw, err := os.ReadFile("../internal/testdata/decimals.json") - require.NoError(t, err) - err = json.Unmarshal(raw, &testcases) - require.NoError(t, err) - - textual := valuerenderer.NewTextual(nil) - - for _, tc := range testcases { - tc := tc - t.Run(tc[0], func(t *testing.T) { - r, err := textual.GetValueRenderer(fieldDescriptorFromName("SDKDEC")) - require.NoError(t, err) - b := new(strings.Builder) - err = r.Format(context.Background(), protoreflect.ValueOf(tc[0]), b) - require.NoError(t, err) - - require.Equal(t, tc[1], b.String()) - }) - } -} - func TestDispatcher(t *testing.T) { testcases := []struct { name string From b2f31037d31bf33ba0fb5a3dda9aa111da62ef7b Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Thu, 15 Sep 2022 16:12:40 +0200 Subject: [PATCH 02/11] Rename to shorter names --- core/coins/format.go | 6 +++--- math/dec.go | 10 +++++----- math/dec_test.go | 4 ++-- math/int.go | 8 ++++---- math/int_test.go | 4 ++-- tx/textual/valuerenderer/dec.go | 2 +- tx/textual/valuerenderer/dec_test.go | 2 +- tx/textual/valuerenderer/int.go | 2 +- tx/textual/valuerenderer/int_test.go | 2 +- 9 files changed, 20 insertions(+), 20 deletions(-) diff --git a/core/coins/format.go b/core/coins/format.go index b290758bad82..1abf98bddecc 100644 --- a/core/coins/format.go +++ b/core/coins/format.go @@ -18,7 +18,7 @@ func formatCoin(coin *basev1beta1.Coin, metadata *bankv1beta1.Metadata) (string, // Return early if no display denom or display denom is the current coin denom. if metadata == nil || metadata.Display == "" || coinDenom == metadata.Display { - vr, err := math.FormatDecimal(coin.Amount) + vr, err := math.FormatDec(coin.Amount) return vr + " " + coin.Denom, err } @@ -40,7 +40,7 @@ func formatCoin(coin *basev1beta1.Coin, metadata *bankv1beta1.Metadata) (string, // If we didn't find either exponent, then we return early. if !foundCoinExp || !foundDispExp { - vr, err := math.FormatInteger(coin.Amount) + vr, err := math.FormatInt(coin.Amount) return vr + " " + coin.Denom, err } @@ -57,7 +57,7 @@ func formatCoin(coin *basev1beta1.Coin, metadata *bankv1beta1.Metadata) (string, dispAmount = dispAmount.Quo(math.LegacyNewDec(10).Power(uint64(-exponentDiff))) } - vr, err := math.FormatDecimal(dispAmount.String()) + vr, err := math.FormatDec(dispAmount.String()) return vr + " " + dispDenom, err } diff --git a/math/dec.go b/math/dec.go index cb73d593de73..1ed82b9ef0c8 100644 --- a/math/dec.go +++ b/math/dec.go @@ -895,16 +895,16 @@ func LegacyDecApproxEq(t *testing.T, d1 LegacyDec, d2 LegacyDec, tol LegacyDec) return t, diff.LTE(tol), "expected |d1 - d2| <:\t%v\ngot |d1 - d2| = \t\t%v", tol.String(), diff.String() } -// FormatDecimal formats a decimal into a value-rendered string following -// ADR-050. This function operates with string manipulation (instead of -// manipulating the sdk.Dec object). -func FormatDecimal(v string) (string, error) { +// FormatDec formats a decimal (as encoded in protobuf) into a value-rendered +// string following ADR-050. This function operates with string manipulation +// (instead of manipulating the sdk.Dec object). +func FormatDec(v string) (string, error) { parts := strings.Split(v, ".") if len(parts) > 2 { return "", fmt.Errorf("invalid decimal: too many points in %s", v) } - intPart, err := FormatInteger(parts[0]) + intPart, err := FormatInt(parts[0]) if err != nil { return "", err } diff --git a/math/dec_test.go b/math/dec_test.go index d4ca9f044b21..aa0508148f9b 100644 --- a/math/dec_test.go +++ b/math/dec_test.go @@ -667,7 +667,7 @@ func BenchmarkLegacyQuoRoundupMut(b *testing.B) { sink = (interface{})(nil) } -func TestFormatDecimalNonDigits(t *testing.T) { +func TestFormatDecNonDigits(t *testing.T) { badCases := []string{ "10.a", "1a.10", @@ -682,7 +682,7 @@ func TestFormatDecimalNonDigits(t *testing.T) { for _, value := range badCases { value := value t.Run(value, func(t *testing.T) { - s, err := math.FormatDecimal(value) + s, err := math.FormatDec(value) if err == nil { t.Fatal("Expected an error") } diff --git a/math/int.go b/math/int.go index 5213478e5999..6811893f7a47 100644 --- a/math/int.go +++ b/math/int.go @@ -448,10 +448,10 @@ func hasOnlyDigits(s string) bool { const thousandSeparator string = "'" -// FormatInteger formats an integer into a value-rendered string following -// ADR-050. This function operates with string manipulation (instead of -// manipulating the int or sdk.Int object). -func FormatInteger(v string) (string, error) { +// FormatInt formats an integer (encoded as in protobuf) into a value-rendered +// string following ADR-050. This function operates with string manipulation +// (instead of manipulating the int or sdk.Int object). +func FormatInt(v string) (string, error) { sign := "" if v[0] == '-' { sign = "-" diff --git a/math/int_test.go b/math/int_test.go index 60aadd9c58f8..6ed381482b35 100644 --- a/math/int_test.go +++ b/math/int_test.go @@ -426,7 +426,7 @@ func TestRoundTripMarshalToInt(t *testing.T) { } } -func TestFormatIntegerNonDigits(t *testing.T) { +func TestFormatIntNonDigits(t *testing.T) { badCases := []string{ "a10", "1a10", @@ -441,7 +441,7 @@ func TestFormatIntegerNonDigits(t *testing.T) { for _, value := range badCases { value := value t.Run(value, func(t *testing.T) { - s, err := math.FormatInteger(value) + s, err := math.FormatInt(value) if err == nil { t.Fatal("Expected an error") } diff --git a/tx/textual/valuerenderer/dec.go b/tx/textual/valuerenderer/dec.go index ff28b0c53ff8..492f5ec0d2a0 100644 --- a/tx/textual/valuerenderer/dec.go +++ b/tx/textual/valuerenderer/dec.go @@ -20,7 +20,7 @@ type decValueRenderer struct{} var _ ValueRenderer = decValueRenderer{} func (vr decValueRenderer) Format(_ context.Context, v protoreflect.Value, w io.Writer) error { - formatted, err := math.FormatDecimal(v.String()) + formatted, err := math.FormatDec(v.String()) if err != nil { return err } diff --git a/tx/textual/valuerenderer/dec_test.go b/tx/textual/valuerenderer/dec_test.go index 28fbada7da0c..d99f705cd5f2 100644 --- a/tx/textual/valuerenderer/dec_test.go +++ b/tx/textual/valuerenderer/dec_test.go @@ -12,7 +12,7 @@ import ( "google.golang.org/protobuf/reflect/protoreflect" ) -func TestFormatDecimal(t *testing.T) { +func TestFormatDec(t *testing.T) { type decimalTest []string var testcases []decimalTest raw, err := os.ReadFile("../internal/testdata/decimals.json") diff --git a/tx/textual/valuerenderer/int.go b/tx/textual/valuerenderer/int.go index 6ce146bbc8b4..9dfc7f9dd785 100644 --- a/tx/textual/valuerenderer/int.go +++ b/tx/textual/valuerenderer/int.go @@ -19,7 +19,7 @@ type intValueRenderer struct{} var _ ValueRenderer = intValueRenderer{} func (vr intValueRenderer) Format(_ context.Context, v protoreflect.Value, w io.Writer) error { - formatted, err := math.FormatInteger(v.String()) + formatted, err := math.FormatInt(v.String()) if err != nil { return err } diff --git a/tx/textual/valuerenderer/int_test.go b/tx/textual/valuerenderer/int_test.go index 96c4b740f85d..b02a613f1447 100644 --- a/tx/textual/valuerenderer/int_test.go +++ b/tx/textual/valuerenderer/int_test.go @@ -15,7 +15,7 @@ import ( "cosmossdk.io/tx/textual/valuerenderer" ) -func TestFormatInteger(t *testing.T) { +func TestFormatInt(t *testing.T) { type integerTest []string var testcases []integerTest raw, err := os.ReadFile("../internal/testdata/integers.json") From da90b5280b1e7ce01465a6d057f1f689035a659e Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Thu, 15 Sep 2022 16:13:10 +0200 Subject: [PATCH 03/11] Add chaneglog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94b7073e8820..987dcf1a5dbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features +* (core) [#13306](https://github.com/cosmos/cosmos-sdk/pull/13306) Add a `FormatCoins` function to in `core/coins` to format sdk Coins following the Value Renderers spec. +* (math) [#13306](https://github.com/cosmos/cosmos-sdk/pull/13306) Add `FormatInt` and `FormatDec` functiosn in `math` to format integers and decimals following the Value Renderers spec. * (cli) [#13207](https://github.com/cosmos/cosmos-sdk/pull/13207) Reduce user's password prompts when calling keyring `List()` function * (x/authz) [#12648](https://github.com/cosmos/cosmos-sdk/pull/12648) Add an allow list, an optional list of addresses allowed to receive bank assets via authz MsgSend grant. * (sdk.Coins) [#12627](https://github.com/cosmos/cosmos-sdk/pull/12627) Make a Denoms method on sdk.Coins. From 0474882b5881c675420b3dbcd520ec2a809142bb Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Thu, 15 Sep 2022 16:18:52 +0200 Subject: [PATCH 04/11] Don't panic --- core/coins/format.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/coins/format.go b/core/coins/format.go index 1abf98bddecc..53a5ae0ff1bd 100644 --- a/core/coins/format.go +++ b/core/coins/format.go @@ -68,7 +68,7 @@ func formatCoin(coin *basev1beta1.Coin, metadata *bankv1beta1.Metadata) (string, // at the same index. func FormatCoins(coins []*basev1beta1.Coin, metadata []*bankv1beta1.Metadata) (string, error) { if len(coins) != len(metadata) { - panic(fmt.Errorf("formatCoins expect one metadata for each coin; expected %d, got %d", len(coins), len(metadata))) + return "", fmt.Errorf("formatCoins expect one metadata for each coin; expected %d, got %d", len(coins), len(metadata)) } formatted := make([]string, len(coins)) From 435e83bd605b9ed0fb496200c2adff0d84968b38 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Mon, 17 Oct 2022 16:27:58 +0200 Subject: [PATCH 05/11] add comments --- core/go.mod | 1 + tx/go.mod | 1 + 2 files changed, 2 insertions(+) diff --git a/core/go.mod b/core/go.mod index bf0eacabf79b..597bb2b7de08 100644 --- a/core/go.mod +++ b/core/go.mod @@ -31,4 +31,5 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) +// temporary until we tag a new go module replace cosmossdk.io/math => ../math diff --git a/tx/go.mod b/tx/go.mod index 96a6d7893200..9b3ab3a532cb 100644 --- a/tx/go.mod +++ b/tx/go.mod @@ -27,4 +27,5 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) +// temporary until we tag a new go module replace cosmossdk.io/core => ../core From 6576291d45d388d2e7c171a49c240634ac7e3b09 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Mon, 17 Oct 2022 16:35:56 +0200 Subject: [PATCH 06/11] go mod tidy --- core/go.sum | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/core/go.sum b/core/go.sum index 9849d62d1449..42200ec4ca74 100644 --- a/core/go.sum +++ b/core/go.sum @@ -4,18 +4,14 @@ cosmossdk.io/depinject v1.0.0-alpha.3 h1:6evFIgj//Y3w09bqOUOzEpFj5tsxBqdc5CfkO7z cosmossdk.io/depinject v1.0.0-alpha.3/go.mod h1:eRbcdQ7MRpIPEM5YUJh8k97nxHpYbc3sMUnEtt8HPWU= github.com/alecthomas/participle/v2 v2.0.0-alpha7 h1:cK4vjj0VSgb3lN1nuKA5F7dw+1s1pWBe5bx7nNCnN+c= github.com/cockroachdb/apd/v3 v3.1.0 h1:MK3Ow7LH0W8zkd5GMKA1PvS9qG3bWFI95WaVNfyZJ/w= -<<<<<<< HEAD -github.com/cosmos/cosmos-proto v1.0.0-alpha7 h1:yqYUOHF2jopwZh4dVQp3xgqwftE5/2hkrwIV6vkUbO0= -github.com/cosmos/cosmos-proto v1.0.0-alpha7/go.mod h1:dosO4pSAbJF8zWCzCoTWP7nNsjcvSUBQmniFxDg5daw= -github.com/cosmos/gogoproto v1.4.1 h1:WoyH+0/jbCTzpKNvyav5FL1ZTWsp1im1MxEpJEzKUB8= -github.com/cosmos/gogoproto v1.4.1/go.mod h1:Ac9lzL4vFpBMcptJROQ6dQ4M3pOEK5Z/l0Q9p+LoCr4= -======= github.com/cosmos/cosmos-proto v1.0.0-alpha8 h1:d3pCRuMYYvGA5bM0ZbbjKn+AoQD4A7dyNG2wzwWalUw= github.com/cosmos/cosmos-proto v1.0.0-alpha8/go.mod h1:6/p+Bc4O8JKeZqe0VqUGTX31eoYqemTT4C1hLCWsO7I= ->>>>>>> a34d5b213d1d0e6ffbba2e9d4841eb894f933527 +github.com/cosmos/gogoproto v1.4.1 h1:WoyH+0/jbCTzpKNvyav5FL1ZTWsp1im1MxEpJEzKUB8= +github.com/cosmos/gogoproto v1.4.1/go.mod h1:Ac9lzL4vFpBMcptJROQ6dQ4M3pOEK5Z/l0Q9p+LoCr4= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/cucumber/common/gherkin/go/v22 v22.0.0 h1:4K8NqptbvdOrjL9DEea6HFjSpbdT9+Q5kgLpmmsHYl0= github.com/cucumber/common/messages/go/v17 v17.1.1 h1:RNqopvIFyLWnKv0LfATh34SWBhXeoFTJnSrgm9cT/Ts= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0= @@ -31,16 +27,12 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/regen-network/gocuke v0.6.2 h1:pHviZ0kKAq2U2hN2q3smKNxct6hS0mGByFMHGnWA97M= github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg= -<<<<<<< HEAD -github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -======= ->>>>>>> a34d5b213d1d0e6ffbba2e9d4841eb894f933527 github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -86,17 +78,11 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -<<<<<<< HEAD gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gotest.tools/v3 v3.3.0 h1:MfDY1b1/0xN1CyMlQDac0ziEy9zJQd9CXBRRDHw2jJo= -gotest.tools/v3 v3.3.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A= -pgregory.net/rapid v0.4.7/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU= -pgregory.net/rapid v0.5.2 h1:zC+jmuzcz5yJvG/igG06aLx8kcGmZY435NcuyhblKjY= -======= gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o= gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g= pgregory.net/rapid v0.5.3 h1:163N50IHFqr1phZens4FQOdPgfJscR7a562mjQqeo4M= ->>>>>>> a34d5b213d1d0e6ffbba2e9d4841eb894f933527 sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= From 00aefd5042921de0bb4df4a320845791c15a9c46 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Mon, 17 Oct 2022 16:38:51 +0200 Subject: [PATCH 07/11] fix changelog --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbb03ad57801..c6577dffa378 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,7 +41,6 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (core) [#13306](https://github.com/cosmos/cosmos-sdk/pull/13306) Add a `FormatCoins` function to in `core/coins` to format sdk Coins following the Value Renderers spec. * (math) [#13306](https://github.com/cosmos/cosmos-sdk/pull/13306) Add `FormatInt` and `FormatDec` functiosn in `math` to format integers and decimals following the Value Renderers spec. -* (cli) [#13207](https://github.com/cosmos/cosmos-sdk/pull/13207) Reduce user's password prompts when calling keyring `List()` function * (grpc) [#13485](https://github.com/cosmos/cosmos-sdk/pull/13485) Implement a new gRPC query, `/cosmos/base/node/v1beta1/config`, which provides operator configuration. * (x/staking) [#13122](https://github.com/cosmos/cosmos-sdk/pull/13122) Add `UnbondingCanComplete` and `PutUnbondingOnHold` to `x/staking` module. * [#13437](https://github.com/cosmos/cosmos-sdk/pull/13437) Add new flag `--modules-to-export` in `simd export` command to export only selected modules. From 802e2d7f7e6a8f55b1786176da54511ffdf95051 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 18 Oct 2022 17:28:43 +0200 Subject: [PATCH 08/11] move structs to top --- core/coins/format_test.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/core/coins/format_test.go b/core/coins/format_test.go index 21c0f3c65d7a..a2d5bff7bc21 100644 --- a/core/coins/format_test.go +++ b/core/coins/format_test.go @@ -11,6 +11,22 @@ import ( "github.com/stretchr/testify/require" ) +// coinsJsonTest is the type of test cases in the coin.json file. +type coinJsonTest struct { + Proto *basev1beta1.Coin + Metadata *bankv1beta1.Metadata + Text string + Error bool +} + +// coinsJsonTest is the type of test cases in the coins.json file. +type coinsJsonTest struct { + Proto []*basev1beta1.Coin + Metadata map[string]*bankv1beta1.Metadata + Text string + Error bool +} + func TestFormatCoin(t *testing.T) { var testcases []coinJsonTest raw, err := os.ReadFile("../../tx/textual/internal/testdata/coin.json") @@ -63,19 +79,3 @@ func TestFormatCoins(t *testing.T) { }) } } - -// coinsJsonTest is the type of test cases in the coin.json file. -type coinJsonTest struct { - Proto *basev1beta1.Coin - Metadata *bankv1beta1.Metadata - Text string - Error bool -} - -// coinsJsonTest is the type of test cases in the coins.json file. -type coinsJsonTest struct { - Proto []*basev1beta1.Coin - Metadata map[string]*bankv1beta1.Metadata - Text string - Error bool -} From 4f9736286666121f22cfcb06818ae8782e49b1df Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 18 Oct 2022 17:57:21 +0200 Subject: [PATCH 09/11] Fix test --- tx/go.mod | 8 ++++---- tx/go.sum | 15 --------------- tx/textual/valuerenderer/dec_test.go | 7 +------ tx/textual/valuerenderer/int_test.go | 28 +++++++++++++++------------- 4 files changed, 20 insertions(+), 38 deletions(-) diff --git a/tx/go.mod b/tx/go.mod index 25ade2524d18..74f79d563cc6 100644 --- a/tx/go.mod +++ b/tx/go.mod @@ -15,17 +15,17 @@ require ( github.com/cosmos/gogoproto v1.4.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/protobuf v1.5.2 // indirect - github.com/kr/pretty v0.3.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rogpeppe/go-internal v1.8.1 // indirect golang.org/x/net v0.0.0-20221004154528-8021a29435af // indirect golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect golang.org/x/text v0.3.7 // indirect google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91 // indirect google.golang.org/grpc v1.50.0 // indirect - gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) // temporary until we tag a new go module -replace cosmossdk.io/core => ../core +replace ( + cosmossdk.io/core => ../core + cosmossdk.io/math => ../math +) diff --git a/tx/go.sum b/tx/go.sum index c1eb7a2221e2..51babe5dd63f 100644 --- a/tx/go.sum +++ b/tx/go.sum @@ -1,12 +1,9 @@ cosmossdk.io/api v0.2.1 h1:4m6vIHKJygrixSIfOsD3Mhij9vZlQC/+BTeb+Un9os0= cosmossdk.io/api v0.2.1/go.mod h1:kNpfY0UY7Cz4ZuLJ4hm9auUGfmj23UFpOQ/Bo8IKCFw= -cosmossdk.io/math v1.0.0-beta.3 h1:TbZxSopz2LqjJ7aXYfn7nJSb8vNaBklW6BLpcei1qwM= -cosmossdk.io/math v1.0.0-beta.3/go.mod h1:3LYasri3Zna4XpbrTNdKsWmD5fHHkaNAod/mNT9XdE4= github.com/cosmos/cosmos-proto v1.0.0-alpha8 h1:d3pCRuMYYvGA5bM0ZbbjKn+AoQD4A7dyNG2wzwWalUw= github.com/cosmos/cosmos-proto v1.0.0-alpha8/go.mod h1:6/p+Bc4O8JKeZqe0VqUGTX31eoYqemTT4C1hLCWsO7I= github.com/cosmos/gogoproto v1.4.2 h1:UeGRcmFW41l0G0MiefWhkPEVEwvu78SZsHBvI78dAYw= github.com/cosmos/gogoproto v1.4.2/go.mod h1:cLxOsn1ljAHSV527CHOtaIP91kK6cCrZETRBrkzItWU= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -15,20 +12,11 @@ github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= -github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg= -github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -50,10 +38,7 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/tx/textual/valuerenderer/dec_test.go b/tx/textual/valuerenderer/dec_test.go index d99f705cd5f2..02d4362009b7 100644 --- a/tx/textual/valuerenderer/dec_test.go +++ b/tx/textual/valuerenderer/dec_test.go @@ -1,10 +1,8 @@ package valuerenderer_test import ( - "context" "encoding/json" "os" - "strings" "testing" "cosmossdk.io/tx/textual/valuerenderer" @@ -27,11 +25,8 @@ func TestFormatDec(t *testing.T) { t.Run(tc[0], func(t *testing.T) { r, err := textual.GetValueRenderer(fieldDescriptorFromName("SDKDEC")) require.NoError(t, err) - b := new(strings.Builder) - err = r.Format(context.Background(), protoreflect.ValueOf(tc[0]), b) - require.NoError(t, err) - require.Equal(t, tc[1], b.String()) + checkNumberTest(t, r, protoreflect.ValueOf(tc[0]), tc[1]) }) } } diff --git a/tx/textual/valuerenderer/int_test.go b/tx/textual/valuerenderer/int_test.go index b02a613f1447..047e938671fb 100644 --- a/tx/textual/valuerenderer/int_test.go +++ b/tx/textual/valuerenderer/int_test.go @@ -5,7 +5,6 @@ import ( "encoding/json" "os" "strconv" - "strings" "testing" "github.com/stretchr/testify/require" @@ -31,11 +30,8 @@ func TestFormatInt(t *testing.T) { if err == nil { r, err := textual.GetValueRenderer(fieldDescriptorFromName("UINT64")) require.NoError(t, err) - b := new(strings.Builder) - err = r.Format(context.Background(), protoreflect.ValueOf(i), b) - require.NoError(t, err) - require.Equal(t, tc[1], b.String()) + checkNumberTest(t, r, protoreflect.ValueOf(i), tc[1]) } // Parse test case strings as protobuf uint32 @@ -43,11 +39,8 @@ func TestFormatInt(t *testing.T) { if err == nil { r, err := textual.GetValueRenderer(fieldDescriptorFromName("UINT32")) require.NoError(t, err) - b := new(strings.Builder) - err = r.Format(context.Background(), protoreflect.ValueOf(i), b) - require.NoError(t, err) - require.Equal(t, tc[1], b.String()) + checkNumberTest(t, r, protoreflect.ValueOf(i), tc[1]) } // Parse test case strings as sdk.Ints @@ -55,11 +48,20 @@ func TestFormatInt(t *testing.T) { if ok { r, err := textual.GetValueRenderer(fieldDescriptorFromName("SDKINT")) require.NoError(t, err) - b := new(strings.Builder) - err = r.Format(context.Background(), protoreflect.ValueOf(tc[0]), b) - require.NoError(t, err) - require.Equal(t, tc[1], b.String()) + checkNumberTest(t, r, protoreflect.ValueOf(tc[0]), tc[1]) } } } + +// checkNumberTest checks that the output of a number value renderer +// matches the expected string. Only use it to test numbers. +func checkNumberTest(t *testing.T, r valuerenderer.ValueRenderer, pv protoreflect.Value, expected string) { + screens, err := r.Format(context.Background(), pv) + require.NoError(t, err) + require.Len(t, screens, 1) + require.Equal(t, 0, screens[0].Indent) + require.Equal(t, false, screens[0].Expert) + + require.Equal(t, expected, screens[0].Text) +} From 3e03d626b282c5ad6fa227624cbb325f30dbd137 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 18 Oct 2022 17:58:15 +0200 Subject: [PATCH 10/11] go mod tidy --- tests/go.mod | 2 +- tx/go.mod | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/go.mod b/tests/go.mod index f49f32273991..1590f260878d 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -4,6 +4,7 @@ go 1.19 require ( cosmossdk.io/api v0.2.1 + cosmossdk.io/depinject v1.0.0-alpha.2 cosmossdk.io/math v1.0.0-beta.3 cosmossdk.io/simapp v0.0.0-20220908203654-84d4bf5accad github.com/cosmos/cosmos-sdk v0.0.0-00010101000000-000000000000 @@ -23,7 +24,6 @@ require ( cloud.google.com/go/iam v0.3.0 // indirect cloud.google.com/go/storage v1.14.0 // indirect cosmossdk.io/core v0.2.0 // indirect - cosmossdk.io/depinject v1.0.0-alpha.2 // indirect cosmossdk.io/errors v1.0.0-beta.7 // indirect filippo.io/edwards25519 v1.0.0-rc.1 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect diff --git a/tx/go.mod b/tx/go.mod index ce21e564bfdb..51ac5b162356 100644 --- a/tx/go.mod +++ b/tx/go.mod @@ -16,13 +16,11 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rogpeppe/go-internal v1.8.1 // indirect golang.org/x/net v0.0.0-20221017152216-f25eb7ecb193 // indirect golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect golang.org/x/text v0.3.8 // indirect google.golang.org/genproto v0.0.0-20221014213838-99cd37c6964a // indirect google.golang.org/grpc v1.50.1 // indirect - gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) From be1b0afa259c6cb5dd393219fbd2096772830ad9 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Wed, 19 Oct 2022 11:31:56 +0200 Subject: [PATCH 11/11] Refactor tests --- math/dec_test.go | 19 +++++++++++++++++++ math/int_test.go | 18 ++++++++++++++++++ tx/textual/valuerenderer/dec_test.go | 2 +- tx/textual/valuerenderer/int_test.go | 2 +- 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/math/dec_test.go b/math/dec_test.go index aa0508148f9b..45daeee77a13 100644 --- a/math/dec_test.go +++ b/math/dec_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "math/big" + "os" "strings" "testing" @@ -667,6 +668,24 @@ func BenchmarkLegacyQuoRoundupMut(b *testing.B) { sink = (interface{})(nil) } +func TestFormatDec(t *testing.T) { + type decimalTest []string + var testcases []decimalTest + raw, err := os.ReadFile("../tx/textual/internal/testdata/decimals.json") + require.NoError(t, err) + err = json.Unmarshal(raw, &testcases) + require.NoError(t, err) + + for _, tc := range testcases { + tc := tc + t.Run(tc[0], func(t *testing.T) { + out, err := math.FormatDec(tc[0]) + require.NoError(t, err) + require.Equal(t, tc[1], out) + }) + } +} + func TestFormatDecNonDigits(t *testing.T) { badCases := []string{ "10.a", diff --git a/math/int_test.go b/math/int_test.go index 6ed381482b35..5af450cb6c19 100644 --- a/math/int_test.go +++ b/math/int_test.go @@ -1,13 +1,16 @@ package math_test import ( + "encoding/json" "fmt" "math/big" "math/rand" + "os" "strconv" "strings" "testing" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "cosmossdk.io/math" @@ -426,6 +429,21 @@ func TestRoundTripMarshalToInt(t *testing.T) { } } +func TestFormatInt(t *testing.T) { + type integerTest []string + var testcases []integerTest + raw, err := os.ReadFile("../tx/textual/internal/testdata/integers.json") + require.NoError(t, err) + err = json.Unmarshal(raw, &testcases) + require.NoError(t, err) + + for _, tc := range testcases { + out, err := math.FormatInt(tc[0]) + require.NoError(t, err) + require.Equal(t, tc[1], out) + } +} + func TestFormatIntNonDigits(t *testing.T) { badCases := []string{ "a10", diff --git a/tx/textual/valuerenderer/dec_test.go b/tx/textual/valuerenderer/dec_test.go index 02d4362009b7..46fafac22ea4 100644 --- a/tx/textual/valuerenderer/dec_test.go +++ b/tx/textual/valuerenderer/dec_test.go @@ -10,7 +10,7 @@ import ( "google.golang.org/protobuf/reflect/protoreflect" ) -func TestFormatDec(t *testing.T) { +func TestDecJsonTestcases(t *testing.T) { type decimalTest []string var testcases []decimalTest raw, err := os.ReadFile("../internal/testdata/decimals.json") diff --git a/tx/textual/valuerenderer/int_test.go b/tx/textual/valuerenderer/int_test.go index 047e938671fb..37fd90fc3618 100644 --- a/tx/textual/valuerenderer/int_test.go +++ b/tx/textual/valuerenderer/int_test.go @@ -14,7 +14,7 @@ import ( "cosmossdk.io/tx/textual/valuerenderer" ) -func TestFormatInt(t *testing.T) { +func TestIntJsonTestcases(t *testing.T) { type integerTest []string var testcases []integerTest raw, err := os.ReadFile("../internal/testdata/integers.json")