Skip to content

Commit 76f6f3d

Browse files
committed
Refactoring.
1 parent 3af3036 commit 76f6f3d

File tree

1 file changed

+36
-44
lines changed

1 file changed

+36
-44
lines changed

x/wasm/keeper/addresses_test.go

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,75 +11,67 @@ import (
1111
sdk "github.com/cosmos/cosmos-sdk/types"
1212
)
1313

14-
func TestBuildContractAddressClassic(t *testing.T) {
14+
func PrepareCleanup(t *testing.T) {
1515
// preserve current Bech32 settings and restore them after test completion
1616
x, y := sdk.GetConfig().GetBech32AccountAddrPrefix(), sdk.GetConfig().GetBech32AccountPubPrefix()
1717
c := sdk.IsAddrCacheEnabled()
1818
t.Cleanup(func() {
1919
sdk.GetConfig().SetBech32PrefixForAccount(x, y)
2020
sdk.SetAddrCacheEnabled(c)
2121
})
22-
2322
// set custom Bech32 settings
2423
sdk.GetConfig().SetBech32PrefixForAccount("purple", "purple")
2524
// disable address cache
2625
// AccAddress -> String conversion is then slower, but does not lead to errors like this:
2726
// runtime error: invalid memory address or nil pointer dereference
2827
sdk.SetAddrCacheEnabled(false)
28+
}
2929

30+
func TestBuildContractAddressClassic(t *testing.T) {
31+
// set cleanup function
32+
PrepareCleanup(t)
33+
// prepare test data
34+
specs := []struct {
35+
codeId uint64
36+
instanceId uint64
37+
expAddress string
38+
}{
39+
{
40+
codeId: 0,
41+
instanceId: 0,
42+
expAddress: "purple1w0w8sasnut0jx0vvsnvlc8nayq0q2ej8xgrpwgel05tn6wy4r57qfplul7",
43+
},
44+
{
45+
codeId: 0,
46+
instanceId: 1,
47+
expAddress: "purple156r47kpk4va938pmtpuee4fh77847gqcw2dmpl2nnpwztwfgz04s5cr8hj",
48+
},
49+
{
50+
codeId: 1,
51+
instanceId: 0,
52+
expAddress: "purple1mzdhwvvh22wrt07w59wxyd58822qavwkx5lcej7aqfkpqqlhaqfs5efvjk",
53+
},
54+
{
55+
codeId: 1,
56+
instanceId: 1,
57+
expAddress: "purple14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9smc2vxm",
58+
},
59+
}
3060
// run tests
31-
for i, spec := range classicContractAddr {
61+
for i, spec := range specs {
3262
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
3363
// when
3464
gotAddr := BuildContractAddressClassic(spec.codeId, spec.instanceId)
3565
// then
36-
require.Equal(t, spec.address, gotAddr.String())
66+
require.Equal(t, spec.expAddress, gotAddr.String())
3767
require.NoError(t, sdk.VerifyAddressFormat(gotAddr))
3868
})
3969
}
4070
}
4171

42-
var classicContractAddr = []struct {
43-
codeId uint64
44-
instanceId uint64
45-
address string
46-
}{
47-
{
48-
codeId: 0,
49-
instanceId: 0,
50-
address: "purple1w0w8sasnut0jx0vvsnvlc8nayq0q2ej8xgrpwgel05tn6wy4r57qfplul7",
51-
},
52-
{
53-
codeId: 0,
54-
instanceId: 1,
55-
address: "purple156r47kpk4va938pmtpuee4fh77847gqcw2dmpl2nnpwztwfgz04s5cr8hj",
56-
},
57-
{
58-
codeId: 1,
59-
instanceId: 0,
60-
address: "purple1mzdhwvvh22wrt07w59wxyd58822qavwkx5lcej7aqfkpqqlhaqfs5efvjk",
61-
},
62-
{
63-
codeId: 1,
64-
instanceId: 1,
65-
address: "purple14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9smc2vxm",
66-
},
67-
}
68-
6972
func TestBuildContractAddressPredictable(t *testing.T) {
70-
x, y := sdk.GetConfig().GetBech32AccountAddrPrefix(), sdk.GetConfig().GetBech32AccountPubPrefix()
71-
c := sdk.IsAddrCacheEnabled()
72-
t.Cleanup(func() {
73-
sdk.GetConfig().SetBech32PrefixForAccount(x, y)
74-
sdk.SetAddrCacheEnabled(c)
75-
})
76-
// set custom Bech32 settings
77-
sdk.GetConfig().SetBech32PrefixForAccount("purple", "purple")
78-
// disable address cache
79-
// AccAddress -> String conversion is then slower, but does not lead to errors like this:
80-
// runtime error: invalid memory address or nil pointer dereference
81-
sdk.SetAddrCacheEnabled(false)
82-
73+
// set cleanup function
74+
PrepareCleanup(t)
8375
// test vectors generated via cosmjs: https://github.com/cosmos/cosmjs/pull/1253/files
8476
type Spec struct {
8577
In struct {

0 commit comments

Comments
 (0)