types: keep migrating tests to test suites (#7478)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Alessio Treglia
2020-10-08 11:42:32 +00:00
committed by GitHub
co-authored by mergify[bot]
parent d2b914781b
commit 1f211aad57
4 changed files with 100 additions and 137 deletions
+1 -5
View File
@@ -13,14 +13,10 @@ type decCoinTestSuite struct {
suite.Suite
}
func NewDecCoinTestSuite(t *testing.T) {
func TestDecCoinTestSuite(t *testing.T) {
suite.Run(t, new(decCoinTestSuite))
}
func (s *decCoinTestSuite) SetupSuite() {
s.T().Parallel()
}
func (s *decCoinTestSuite) TestNewDecCoin() {
s.Require().NotPanics(func() {
sdk.NewInt64DecCoin(testDenom1, 5)
+44 -55
View File
@@ -6,10 +6,22 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
func TestABCInfo(t *testing.T) {
type abciTestSuite struct {
suite.Suite
}
func TestABCITestSuite(t *testing.T) {
suite.Run(t, new(abciTestSuite))
}
func (s *abciTestSuite) SetupSuite() {
s.T().Parallel()
}
func (s *abciTestSuite) TestABCInfo() {
cases := map[string]struct {
err error
debug bool
@@ -91,23 +103,14 @@ func TestABCInfo(t *testing.T) {
}
for testName, tc := range cases {
tc := tc
t.Run(testName, func(t *testing.T) {
space, code, log := ABCIInfo(tc.err, tc.debug)
if space != tc.wantSpace {
t.Errorf("want %s space, got %s", tc.wantSpace, space)
}
if code != tc.wantCode {
t.Errorf("want %d code, got %d", tc.wantCode, code)
}
if log != tc.wantLog {
t.Errorf("want %q log, got %q", tc.wantLog, log)
}
})
space, code, log := ABCIInfo(tc.err, tc.debug)
s.Require().Equal(tc.wantSpace, space, testName)
s.Require().Equal(tc.wantCode, code, testName)
s.Require().Equal(tc.wantLog, log, testName)
}
}
func TestABCIInfoStacktrace(t *testing.T) {
func (s *abciTestSuite) TestABCIInfoStacktrace() {
cases := map[string]struct {
err error
debug bool
@@ -140,37 +143,27 @@ func TestABCIInfoStacktrace(t *testing.T) {
},
}
const thisTestSrc = "github.com/cosmos/cosmos-sdk/types/errors.TestABCIInfoStacktrace"
const thisTestSrc = "github.com/cosmos/cosmos-sdk/types/errors.(*abciTestSuite).TestABCIInfoStacktrace"
for testName, tc := range cases {
tc := tc
t.Run(testName, func(t *testing.T) {
_, _, log := ABCIInfo(tc.err, tc.debug)
if tc.wantStacktrace {
if !strings.Contains(log, thisTestSrc) {
t.Errorf("log does not contain this file stack trace: %s", log)
}
_, _, log := ABCIInfo(tc.err, tc.debug)
if !tc.wantStacktrace {
s.Require().Equal(tc.wantErrMsg, log, testName)
continue
}
if !strings.Contains(log, tc.wantErrMsg) {
t.Errorf("log does not contain expected error message: %s", log)
}
} else if log != tc.wantErrMsg {
t.Fatalf("unexpected log message: %s", log)
}
})
s.Require().True(strings.Contains(log, thisTestSrc), testName)
s.Require().True(strings.Contains(log, tc.wantErrMsg), testName)
}
}
func TestABCIInfoHidesStacktrace(t *testing.T) {
func (s *abciTestSuite) TestABCIInfoHidesStacktrace() {
err := Wrap(ErrUnauthorized, "wrapped")
_, _, log := ABCIInfo(err, false)
if log != "wrapped: unauthorized" {
t.Fatalf("unexpected message in non debug mode: %s", log)
}
s.Require().Equal("wrapped: unauthorized", log)
}
func TestRedact(t *testing.T) {
func (s *abciTestSuite) TestRedact() {
cases := map[string]struct {
err error
untouched bool // if true we expect the same error after redact
@@ -196,21 +189,21 @@ func TestRedact(t *testing.T) {
for name, tc := range cases {
spec := tc
t.Run(name, func(t *testing.T) {
redacted := Redact(spec.err)
if spec.untouched {
require.Equal(t, spec.err, redacted)
} else {
// see if we got the expected redact
require.Equal(t, spec.changed, redacted)
// make sure the ABCI code did not change
require.Equal(t, abciCode(spec.err), abciCode(redacted))
}
})
redacted := Redact(spec.err)
if spec.untouched {
s.Require().Equal(spec.err, redacted, name)
continue
}
// see if we got the expected redact
s.Require().Equal(spec.changed, redacted, name)
// make sure the ABCI code did not change
s.Require().Equal(abciCode(spec.err), abciCode(redacted), name)
}
}
func TestABCIInfoSerializeErr(t *testing.T) {
func (s *abciTestSuite) TestABCIInfoSerializeErr() {
var (
// Create errors with stacktrace for equal comparison.
myErrDecode = Wrap(ErrTxDecode, "test")
@@ -250,12 +243,8 @@ func TestABCIInfoSerializeErr(t *testing.T) {
}
for msg, spec := range specs {
spec := spec
t.Run(msg, func(t *testing.T) {
_, _, log := ABCIInfo(spec.src, spec.debug)
if exp, got := spec.exp, log; exp != got {
t.Errorf("expected %v but got %v", exp, got)
}
})
_, _, log := ABCIInfo(spec.src, spec.debug)
s.Require().Equal(spec.exp, log, msg)
}
}
+37 -38
View File
@@ -5,12 +5,23 @@ import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/pkg/errors"
"github.com/stretchr/testify/suite"
)
func TestCause(t *testing.T) {
type errorsTestSuite struct {
suite.Suite
}
func TestErrorsTestSuite(t *testing.T) {
suite.Run(t, new(errorsTestSuite))
}
func (s *errorsTestSuite) SetupSuite() {
s.T().Parallel()
}
func (s *errorsTestSuite) TestCause() {
std := stdlib.New("this is a stdlib error")
cases := map[string]struct {
@@ -32,16 +43,11 @@ func TestCause(t *testing.T) {
}
for testName, tc := range cases {
tc := tc
t.Run(testName, func(t *testing.T) {
if got := errors.Cause(tc.err); got != tc.root {
t.Fatal("unexpected result")
}
})
s.Require().Equal(tc.root, errors.Cause(tc.err), testName)
}
}
func TestErrorIs(t *testing.T) {
func (s *errorsTestSuite) TestErrorIs() {
cases := map[string]struct {
a *Error
b error
@@ -139,12 +145,7 @@ func TestErrorIs(t *testing.T) {
// },
}
for testName, tc := range cases {
tc := tc
t.Run(testName, func(t *testing.T) {
if got := tc.a.Is(tc.b); got != tc.wantIs {
t.Fatalf("unexpected result - got:%v want: %v", got, tc.wantIs)
}
})
s.Require().Equal(tc.wantIs, tc.a.Is(tc.b), testName)
}
}
@@ -155,63 +156,61 @@ func (customError) Error() string {
return "custom error"
}
func TestWrapEmpty(t *testing.T) {
if err := Wrap(nil, "wrapping <nil>"); err != nil {
t.Fatal(err)
}
func (s *errorsTestSuite) TestWrapEmpty() {
s.Require().Nil(Wrap(nil, "wrapping <nil>"))
}
func TestWrappedIs(t *testing.T) {
func (s *errorsTestSuite) TestWrappedIs() {
err := Wrap(ErrTxTooLarge, "context")
require.True(t, stdlib.Is(err, ErrTxTooLarge))
s.Require().True(stdlib.Is(err, ErrTxTooLarge))
err = Wrap(err, "more context")
require.True(t, stdlib.Is(err, ErrTxTooLarge))
s.Require().True(stdlib.Is(err, ErrTxTooLarge))
err = Wrap(err, "even more context")
require.True(t, stdlib.Is(err, ErrTxTooLarge))
s.Require().True(stdlib.Is(err, ErrTxTooLarge))
err = Wrap(ErrInsufficientFee, "...")
require.False(t, stdlib.Is(err, ErrTxTooLarge))
s.Require().False(stdlib.Is(err, ErrTxTooLarge))
}
func TestWrappedIsMultiple(t *testing.T) {
func (s *errorsTestSuite) TestWrappedIsMultiple() {
var errTest = errors.New("test error")
var errTest2 = errors.New("test error 2")
err := Wrap(errTest2, Wrap(errTest, "some random description").Error())
require.True(t, stdlib.Is(err, errTest2))
s.Require().True(stdlib.Is(err, errTest2))
}
func TestWrappedIsFail(t *testing.T) {
func (s *errorsTestSuite) TestWrappedIsFail() {
var errTest = errors.New("test error")
var errTest2 = errors.New("test error 2")
err := Wrap(errTest2, Wrap(errTest, "some random description").Error())
require.False(t, stdlib.Is(err, errTest))
s.Require().False(stdlib.Is(err, errTest))
}
func TestWrappedUnwrap(t *testing.T) {
func (s *errorsTestSuite) TestWrappedUnwrap() {
var errTest = errors.New("test error")
err := Wrap(errTest, "some random description")
require.Equal(t, errTest, stdlib.Unwrap(err))
s.Require().Equal(errTest, stdlib.Unwrap(err))
}
func TestWrappedUnwrapMultiple(t *testing.T) {
func (s *errorsTestSuite) TestWrappedUnwrapMultiple() {
var errTest = errors.New("test error")
var errTest2 = errors.New("test error 2")
err := Wrap(errTest2, Wrap(errTest, "some random description").Error())
require.Equal(t, errTest2, stdlib.Unwrap(err))
s.Require().Equal(errTest2, stdlib.Unwrap(err))
}
func TestWrappedUnwrapFail(t *testing.T) {
func (s *errorsTestSuite) TestWrappedUnwrapFail() {
var errTest = errors.New("test error")
var errTest2 = errors.New("test error 2")
err := Wrap(errTest2, Wrap(errTest, "some random description").Error())
require.NotEqual(t, errTest, stdlib.Unwrap(err))
s.Require().NotEqual(errTest, stdlib.Unwrap(err))
}
func TestABCIError(t *testing.T) {
require.Equal(t, "custom: tx parse error", ABCIError(RootCodespace, 2, "custom").Error())
require.Equal(t, "custom: unknown", ABCIError("unknown", 1, "custom").Error())
func (s *errorsTestSuite) TestABCIError() {
s.Require().Equal("custom: tx parse error", ABCIError(RootCodespace, 2, "custom").Error())
s.Require().Equal("custom: unknown", ABCIError("unknown", 1, "custom").Error())
}
func ExampleWrap() {
+18 -39
View File
@@ -5,10 +5,9 @@ import (
"fmt"
"reflect"
"strings"
"testing"
)
func TestStackTrace(t *testing.T) {
func (s *errorsTestSuite) TestStackTrace() {
cases := map[string]struct {
err error
wantError string
@@ -39,45 +38,25 @@ func TestStackTrace(t *testing.T) {
}
const thisTestSrc = "types/errors/stacktrace_test.go"
for testName, tc := range cases {
tc := tc
t.Run(testName, func(t *testing.T) {
if !reflect.DeepEqual(tc.err.Error(), tc.wantError) {
t.Fatalf("errors not equal, got '%s', want '%s'", tc.err.Error(), tc.wantError)
}
for _, tc := range cases {
s.Require().True(reflect.DeepEqual(tc.err.Error(), tc.wantError))
s.Require().NotNil(stackTrace(tc.err))
fullStack := fmt.Sprintf("%+v", tc.err)
s.Require().True(strings.Contains(fullStack, thisTestSrc))
s.Require().True(strings.Contains(fullStack, tc.wantError))
if stackTrace(tc.err) == nil {
t.Fatal("expected a stack trace to be present")
for _, src := range unwantedSrc {
if strings.Contains(fullStack, src) {
s.T().Logf("Stack trace below\n----%s\n----", fullStack)
s.T().Logf("full stack contains unwanted source file path: %q", src)
}
}
fullStack := fmt.Sprintf("%+v", tc.err)
if !strings.Contains(fullStack, thisTestSrc) {
t.Logf("Stack trace below\n----%s\n----", fullStack)
t.Error("full stack trace should contain this test source code information")
}
if !strings.Contains(fullStack, tc.wantError) {
t.Logf("Stack trace below\n----%s\n----", fullStack)
t.Error("full stack trace should contain the error description")
}
for _, src := range unwantedSrc {
if strings.Contains(fullStack, src) {
t.Logf("Stack trace below\n----%s\n----", fullStack)
t.Logf("full stack contains unwanted source file path: %q", src)
}
}
tinyStack := fmt.Sprintf("%v", tc.err)
if !strings.HasPrefix(tinyStack, tc.wantError) {
t.Fatalf("prefix mimssing: %s", tinyStack)
}
if strings.Contains(tinyStack, "\n") {
t.Fatal("only one stack line is expected")
}
// contains a link to where it was created, which must
// be here, not the Wrap() function
if !strings.Contains(tinyStack, thisTestSrc) {
t.Fatalf("this file missing in stack info:\n %s", tinyStack)
}
})
tinyStack := fmt.Sprintf("%v", tc.err)
s.Require().True(strings.HasPrefix(tinyStack, tc.wantError))
s.Require().False(strings.Contains(tinyStack, "\n"))
// contains a link to where it was created, which must
// be here, not the Wrap() function
s.Require().True(strings.Contains(tinyStack, thisTestSrc))
}
}