Skip to content

Commit

Permalink
test: refactoring testcase for token
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeseung-bae committed Oct 16, 2023
1 parent 08a4586 commit fcaa2d2
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 304 deletions.
8 changes: 7 additions & 1 deletion testutil/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package testutil
import (
"encoding/json"
"fmt"

sdk "github.com/Finschia/finschia-sdk/types"
)

func MustJSONMarshal(v any) []byte {
Expand All @@ -14,6 +16,10 @@ func MustJSONMarshal(v any) []byte {
return b
}

func W(input string) []byte {
type FmtStringer interface {
string | sdk.AccAddress | sdk.Int
}

func W[T FmtStringer](input T) []byte {
return []byte(fmt.Sprintf("\"%s\"", input))
}
42 changes: 13 additions & 29 deletions x/token/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper_test

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/suite"
Expand All @@ -18,8 +17,6 @@ import (
type KeeperTestSuite struct {
suite.Suite

deterministic bool

ctx sdk.Context
goCtx context.Context
keeper keeper.Keeper
Expand All @@ -38,29 +35,21 @@ type KeeperTestSuite struct {
}

func (s *KeeperTestSuite) createRandomAccounts(accNum int) []sdk.AccAddress {
if s.deterministic {
addresses := make([]sdk.AccAddress, accNum)
for i := range addresses {
addresses[i] = sdk.AccAddress(fmt.Sprintf("address%d", i))
}
return addresses
} else {
seenAddresses := make(map[string]bool, accNum)
addresses := make([]sdk.AccAddress, accNum)
for i := range addresses {
var address sdk.AccAddress
for {
pk := secp256k1.GenPrivKey().PubKey()
address = sdk.AccAddress(pk.Address())
if !seenAddresses[address.String()] {
seenAddresses[address.String()] = true
break
}
seenAddresses := make(map[string]bool, accNum)
addresses := make([]sdk.AccAddress, accNum)
for i := range addresses {
var address sdk.AccAddress
for {
pk := secp256k1.GenPrivKey().PubKey()
address = sdk.AccAddress(pk.Address())
if !seenAddresses[address.String()] {
seenAddresses[address.String()] = true
break
}
addresses[i] = address
}
return addresses
addresses[i] = address
}
return addresses
}

func (s *KeeperTestSuite) SetupTest() {
Expand Down Expand Up @@ -132,10 +121,5 @@ func (s *KeeperTestSuite) SetupTest() {
}

func TestKeeperTestSuite(t *testing.T) {
for _, deterministic := range []bool{
false,
true,
} {
suite.Run(t, &KeeperTestSuite{deterministic: deterministic})
}
suite.Run(t, &KeeperTestSuite{})
}
Loading

0 comments on commit fcaa2d2

Please sign in to comment.