Merge PR #2392: remove redundant sprintf calls, improve a doc comment, write test

* remove redundant sprintf calls, improve a doc comment, write test

* One more super minor fix
This commit is contained in:
Dev Ojha 2018-09-24 18:42:51 -07:00 committed by Rigel
parent 9dafa3252d
commit 1a5d122c93
4 changed files with 53 additions and 15 deletions

View File

@ -38,7 +38,11 @@ type Keeper struct {
codespace sdk.CodespaceType
}
// NewGovernanceMapper returns a mapper that uses go-codec to (binary) encode and decode gov types.
// NewKeeper returns a governance keeper. It handles:
// - submitting governance proposals
// - depositing funds into proposals, and activating upon sufficient funds being deposited
// - users voting on proposals, with weight proportional to stake in the system
// - and tallying the result of the vote.
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ps params.Setter, ck bank.Keeper, ds sdk.DelegationSet, codespace sdk.CodespaceType) Keeper {
return Keeper{
storeKey: key,
@ -51,11 +55,6 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ps params.Setter, ck bank.Kee
}
}
// Returns the go-codec codec.
func (keeper Keeper) WireCodec() *codec.Codec {
return keeper.cdc
}
// =====================================================
// Proposals

View File

@ -192,8 +192,9 @@ func (pt ProposalKind) String() string {
func (pt ProposalKind) Format(s fmt.State, verb rune) {
switch verb {
case 's':
s.Write([]byte(fmt.Sprintf("%s", pt.String())))
s.Write([]byte(pt.String()))
default:
// TODO: Do this conversion more directly
s.Write([]byte(fmt.Sprintf("%v", byte(pt))))
}
}
@ -295,8 +296,9 @@ func (status ProposalStatus) String() string {
func (status ProposalStatus) Format(s fmt.State, verb rune) {
switch verb {
case 's':
s.Write([]byte(fmt.Sprintf("%s", status.String())))
s.Write([]byte(status.String()))
default:
// TODO: Do this conversion more directly
s.Write([]byte(fmt.Sprintf("%v", byte(status))))
}
}
@ -322,11 +324,8 @@ func EmptyTallyResult() TallyResult {
// checks if two proposals are equal
func (resultA TallyResult) Equals(resultB TallyResult) bool {
if resultA.Yes.Equal(resultB.Yes) &&
return (resultA.Yes.Equal(resultB.Yes) &&
resultA.Abstain.Equal(resultB.Abstain) &&
resultA.No.Equal(resultB.No) &&
resultA.NoWithVeto.Equal(resultB.NoWithVeto) {
return true
}
return false
resultA.NoWithVeto.Equal(resultB.NoWithVeto))
}

40
x/gov/proposals_test.go Normal file
View File

@ -0,0 +1,40 @@
package gov
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestProposalKind_Format(t *testing.T) {
typeText, _ := ProposalTypeFromString("Text")
tests := []struct {
pt ProposalKind
sprintFArgs string
expectedStringOutput string
}{
{typeText, "%s", "Text"},
{typeText, "%v", "1"},
}
for _, tt := range tests {
got := fmt.Sprintf(tt.sprintFArgs, tt.pt)
require.Equal(t, tt.expectedStringOutput, got)
}
}
func TestProposalStatus_Format(t *testing.T) {
statusDepositPeriod, _ := ProposalStatusFromString("DepositPeriod")
tests := []struct {
pt ProposalStatus
sprintFArgs string
expectedStringOutput string
}{
{statusDepositPeriod, "%s", "DepositPeriod"},
{statusDepositPeriod, "%v", "1"},
}
for _, tt := range tests {
got := fmt.Sprintf(tt.sprintFArgs, tt.pt)
require.Equal(t, tt.expectedStringOutput, got)
}
}

View File

@ -205,12 +205,12 @@ func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke
var proposalID int64
err2 := keeper.cdc.UnmarshalJSON(req.Data, proposalID)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
return res, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
}
proposal := keeper.GetProposal(ctx, proposalID)
if proposal == nil {
return []byte{}, ErrUnknownProposal(DefaultCodespace, proposalID)
return res, ErrUnknownProposal(DefaultCodespace, proposalID)
}
var tallyResult TallyResult