Skip to content

Commit

Permalink
test(token): refactoring testcase for token (#1140)
Browse files Browse the repository at this point in the history
* test: refactoring testcase for token

* chore: update CHANGELOG

* Update testutil/encoding.go

Co-authored-by: Jayden Lee <[email protected]>

* chore: accept suggestion and add additional testcase

* chore: add case for panic

---------

Co-authored-by: Jayden Lee <[email protected]>
  • Loading branch information
jaeseung-bae and tkxkd0159 authored Oct 17, 2023
1 parent 08a4586 commit ca32838
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 306 deletions.
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

0 comments on commit ca32838

Please sign in to comment.