-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_uitl.go
71 lines (58 loc) · 1.49 KB
/
test_uitl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package cliutil
import (
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/codec"
)
func SetViperWithArgs(args []string) {
viper.Reset()
for _, arg := range args {
if !strings.HasPrefix(arg, "--") {
continue
}
idx := strings.Index(arg, "=")
if idx < 0 {
viper.Set(arg[2:], true)
} else {
viper.Set(arg[2:idx], arg[idx+1:])
}
}
}
func TestQueryCmd(t *testing.T, cmdFactory func() *cobra.Command,
args string, expectedPath string, expectedParam interface{}) {
oldCliQuery := CliQuery
defer func() { CliQuery = oldCliQuery }()
executed := false
CliQuery = func(cdc *codec.Codec, path string, param interface{}) error {
executed = true
require.Equal(t, path, expectedPath)
require.Equal(t, param, expectedParam)
return nil
}
cmd := cmdFactory()
cmd.SetArgs(strings.Split(args, " "))
err := cmd.Execute()
require.NoError(t, err)
require.True(t, executed)
}
func TestTxCmd(t *testing.T, cmdFactory func() *cobra.Command,
args string, expectedMsg interface{}) {
oldCliRun := CliRunCommand
defer func() { CliRunCommand = oldCliRun }()
executed := false
CliRunCommand = func(cdc *codec.Codec, msg MsgWithAccAddress) error {
executed = true
require.Equal(t, expectedMsg, msg)
return nil
}
argArr := strings.Split(args, " ")
SetViperWithArgs(argArr)
cmd := cmdFactory()
cmd.SetArgs(argArr)
err := cmd.Execute()
require.NoError(t, err)
require.True(t, executed)
}