2018-03-17 18:03:25 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2018-10-19 09:55:20 -07:00
|
|
|
"fmt"
|
2018-03-17 18:03:25 -07:00
|
|
|
"testing"
|
|
|
|
|
2018-06-29 18:10:15 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-17 18:03:25 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var codeTypes = []CodeType{
|
|
|
|
CodeInternal,
|
2018-03-20 18:40:58 -07:00
|
|
|
CodeTxDecode,
|
2018-03-17 18:03:25 -07:00
|
|
|
CodeInvalidSequence,
|
|
|
|
CodeUnauthorized,
|
|
|
|
CodeInsufficientFunds,
|
|
|
|
CodeUnknownRequest,
|
2018-08-06 12:00:49 -07:00
|
|
|
CodeInvalidAddress,
|
2018-03-17 18:03:25 -07:00
|
|
|
CodeInvalidPubKey,
|
2018-08-06 12:00:49 -07:00
|
|
|
CodeUnknownAddress,
|
|
|
|
CodeInsufficientCoins,
|
|
|
|
CodeInvalidCoins,
|
|
|
|
CodeOutOfGas,
|
|
|
|
CodeMemoTooLarge,
|
2018-03-17 18:03:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type errFn func(msg string) Error
|
|
|
|
|
|
|
|
var errFns = []errFn{
|
|
|
|
ErrInternal,
|
2018-03-20 18:40:58 -07:00
|
|
|
ErrTxDecode,
|
2018-03-17 18:03:25 -07:00
|
|
|
ErrInvalidSequence,
|
|
|
|
ErrUnauthorized,
|
|
|
|
ErrInsufficientFunds,
|
|
|
|
ErrUnknownRequest,
|
2018-08-06 12:00:49 -07:00
|
|
|
ErrInvalidAddress,
|
2018-03-17 18:03:25 -07:00
|
|
|
ErrInvalidPubKey,
|
2018-08-06 12:00:49 -07:00
|
|
|
ErrUnknownAddress,
|
|
|
|
ErrInsufficientCoins,
|
|
|
|
ErrInvalidCoins,
|
|
|
|
ErrOutOfGas,
|
|
|
|
ErrMemoTooLarge,
|
2018-03-17 18:03:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCodeType(t *testing.T) {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, ABCICodeOK.IsOK())
|
2018-03-17 18:03:25 -07:00
|
|
|
|
2018-08-06 12:00:49 -07:00
|
|
|
for tcnum, c := range codeTypes {
|
2018-03-17 18:03:25 -07:00
|
|
|
msg := CodeToDefaultMsg(c)
|
2018-08-06 12:00:49 -07:00
|
|
|
require.NotEqual(t, unknownCodeMsg(c), msg, "Code expected to be known. tc #%d, code %d, msg %s", tcnum, c, msg)
|
2018-03-17 18:03:25 -07:00
|
|
|
}
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
msg := CodeToDefaultMsg(CodeOK)
|
|
|
|
require.Equal(t, unknownCodeMsg(CodeOK), msg)
|
2018-03-17 18:03:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrFn(t *testing.T) {
|
|
|
|
for i, errFn := range errFns {
|
|
|
|
err := errFn("")
|
|
|
|
codeType := codeTypes[i]
|
2018-08-06 12:00:49 -07:00
|
|
|
require.Equal(t, err.Code(), codeType, "Err function expected to return proper code. tc #%d", i)
|
2018-08-22 07:15:45 -07:00
|
|
|
require.Equal(t, err.Codespace(), CodespaceRoot, "Err function expected to return proper codespace. tc #%d", i)
|
2018-08-06 12:00:49 -07:00
|
|
|
require.Equal(t, err.Result().Code, ToABCICode(CodespaceRoot, codeType), "Err function expected to return proper ABCICode. tc #%d")
|
2018-08-22 07:15:45 -07:00
|
|
|
require.Equal(t, err.QueryResult().Code, uint32(err.ABCICode()), "Err function expected to return proper ABCICode from QueryResult. tc #%d")
|
|
|
|
require.Equal(t, err.QueryResult().Log, err.ABCILog(), "Err function expected to return proper ABCILog from QueryResult. tc #%d")
|
2018-03-17 18:03:25 -07:00
|
|
|
}
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
require.Equal(t, ABCICodeOK, ToABCICode(CodespaceRoot, CodeOK))
|
2018-03-17 18:03:25 -07:00
|
|
|
}
|
2018-10-19 09:55:20 -07:00
|
|
|
|
|
|
|
func TestAppendMsgToErr(t *testing.T) {
|
|
|
|
for i, errFn := range errFns {
|
|
|
|
err := errFn("")
|
|
|
|
errMsg := err.Stacktrace().Error()
|
|
|
|
abciLog := err.ABCILog()
|
|
|
|
|
|
|
|
// plain msg error
|
|
|
|
msg := AppendMsgToErr("something unexpected happened", errMsg)
|
|
|
|
require.Equal(t, fmt.Sprintf("something unexpected happened; %s",
|
|
|
|
errMsg),
|
|
|
|
msg,
|
|
|
|
fmt.Sprintf("Should have formatted the error message of ABCI Log. tc #%d", i))
|
|
|
|
|
|
|
|
// ABCI Log msg error
|
|
|
|
msg = AppendMsgToErr("something unexpected happened", abciLog)
|
|
|
|
msgIdx := mustGetMsgIndex(abciLog)
|
|
|
|
require.Equal(t, fmt.Sprintf("%s%s; %s}",
|
|
|
|
abciLog[:msgIdx],
|
|
|
|
"something unexpected happened",
|
|
|
|
abciLog[msgIdx:len(abciLog)-1]),
|
|
|
|
msg,
|
|
|
|
fmt.Sprintf("Should have formatted the error message of ABCI Log. tc #%d", i))
|
|
|
|
}
|
|
|
|
}
|