github.com/cosmos/cosmos-sdk@v0.50.10/types/handler_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/golang/mock/gomock"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/cosmos/cosmos-sdk/testutil/mock"
    10  	sdk "github.com/cosmos/cosmos-sdk/types"
    11  )
    12  
    13  func TestChainAnteDecorators(t *testing.T) {
    14  	// test panic
    15  	require.Nil(t, sdk.ChainAnteDecorators([]sdk.AnteDecorator{}...))
    16  
    17  	ctx, tx := sdk.Context{}, sdk.Tx(nil)
    18  	mockCtrl := gomock.NewController(t)
    19  	mockAnteDecorator1 := mock.NewMockAnteDecorator(mockCtrl)
    20  	mockAnteDecorator1.EXPECT().AnteHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Any()).Times(1)
    21  	_, err := sdk.ChainAnteDecorators(mockAnteDecorator1)(ctx, tx, true)
    22  	require.NoError(t, err)
    23  
    24  	mockAnteDecorator2 := mock.NewMockAnteDecorator(mockCtrl)
    25  	// NOTE: we can't check that mockAnteDecorator2 is passed as the last argument because
    26  	// ChainAnteDecorators wraps the decorators into closures, so each decorator is
    27  	// receiving a closure.
    28  	mockAnteDecorator1.EXPECT().AnteHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Any()).Times(1)
    29  	mockAnteDecorator2.EXPECT().AnteHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Any()).Times(1)
    30  
    31  	_, err = sdk.ChainAnteDecorators(
    32  		mockAnteDecorator1,
    33  		mockAnteDecorator2)(ctx, tx, true)
    34  	require.NoError(t, err)
    35  }
    36  
    37  func TestChainPostDecorators(t *testing.T) {
    38  	// test panic when passing an empty sclice of PostDecorators
    39  	require.Nil(t, sdk.ChainPostDecorators([]sdk.PostDecorator{}...))
    40  
    41  	// Create empty context as well as transaction
    42  	ctx := sdk.Context{}
    43  	tx := sdk.Tx(nil)
    44  
    45  	// Create mocks
    46  	mockCtrl := gomock.NewController(t)
    47  	mockPostDecorator1 := mock.NewMockPostDecorator(mockCtrl)
    48  	mockPostDecorator2 := mock.NewMockPostDecorator(mockCtrl)
    49  
    50  	// Test chaining only one post decorator
    51  	mockPostDecorator1.EXPECT().PostHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Eq(true), gomock.Any()).Times(1)
    52  	_, err := sdk.ChainPostDecorators(mockPostDecorator1)(ctx, tx, true, true)
    53  	require.NoError(t, err)
    54  
    55  	// Tests chaining multiple post decorators
    56  	mockPostDecorator1.EXPECT().PostHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Eq(true), gomock.Any()).Times(1)
    57  	mockPostDecorator2.EXPECT().PostHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Eq(true), gomock.Any()).Times(1)
    58  	// NOTE: we can't check that mockAnteDecorator2 is passed as the last argument because
    59  	// ChainAnteDecorators wraps the decorators into closures, so each decorator is
    60  	// receiving a closure.
    61  	_, err = sdk.ChainPostDecorators(
    62  		mockPostDecorator1,
    63  		mockPostDecorator2,
    64  	)(ctx, tx, true, true)
    65  	require.NoError(t, err)
    66  }