Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(token): refactoring testcase for token #1140

Merged
merged 5 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/collection) [\#1133](https://github.com/Finschia/finschia-sdk/pull/1133) Refactor unittest for `SendFT`, `OperatorSendFT`, `AuthorizeOperator`, and `RevokeOperator` to check more states
* (x/token) [\#1137](https://github.com/Finschia/finschia-sdk/pull/1137) Add test for event compatibility
* (x/collection) [\#1139](https://github.com/Finschia/finschia-sdk/pull/1139) refactor overall unittests of `x/collection`
* (x/token) [\#1140](https://github.com/Finschia/finschia-sdk/pull/1140) Refactor unittest for `x/token`

### Bug Fixes
* (ledger) [\#1040](https://github.com/Finschia/finschia-sdk/pull/1040) Fix a bug(unable to connect nano S plus ledger on ubuntu)
Expand Down
10 changes: 8 additions & 2 deletions testutil/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ func MustJSONMarshal(v any) []byte {
return b
}

func W(input string) []byte {
return []byte(fmt.Sprintf("\"%s\"", input))
// W wraps input with double quotes if it is a string or fmt.Stringer.
func W(input any) []byte {
switch input.(type) {
case string, fmt.Stringer:
return []byte(fmt.Sprintf("\"%s\"", input))
default:
panic("unsupported type")
}
}
32 changes: 31 additions & 1 deletion testutil/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package testutil_test

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/require"

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

func TestMustJSONMarshal(t *testing.T) {
Expand All @@ -29,5 +31,33 @@ func TestMustJSONMarshal(t *testing.T) {
}

func TestW(t *testing.T) {
require.Equal(t, []byte(`"test"`), testutil.W("test"))
testCases := map[string]struct {
acceptedType any
}{
"string": {
acceptedType: "test",
},
"sdk.AccAddress": {
acceptedType: sdk.AccAddress("address"),
},
"sdk.Coin": {
acceptedType: sdk.NewInt(1),
},
"should panic for unsupported types": {
acceptedType: 1,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
switch v := tc.acceptedType.(type) {
case string, fmt.Stringer:
require.Equal(t, []byte(fmt.Sprintf(`"%s"`, v)), testutil.W(v))
default:
require.Panics(t, func() {
testutil.W(tc.acceptedType)
})
}
})
}
}
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
Loading