github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/keeper/options_test.go (about)

     1  package keeper
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	authkeeper "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/keeper"
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/params"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/fibonacci-chain/fbc/x/wasm/keeper/wasmtesting"
    13  	"github.com/fibonacci-chain/fbc/x/wasm/types"
    14  )
    15  
    16  func TestConstructorOptions(t *testing.T) {
    17  	cfg := MakeEncodingConfig(t)
    18  	specs := map[string]struct {
    19  		srcOpt Option
    20  		verify func(*testing.T, Keeper)
    21  	}{
    22  		"wasm engine": {
    23  			srcOpt: WithWasmEngine(&wasmtesting.MockWasmer{}),
    24  			verify: func(t *testing.T, k Keeper) {
    25  				assert.IsType(t, &wasmtesting.MockWasmer{}, k.wasmVM)
    26  			},
    27  		},
    28  		"message handler": {
    29  			srcOpt: WithMessageHandler(&wasmtesting.MockMessageHandler{}),
    30  			verify: func(t *testing.T, k Keeper) {
    31  				assert.IsType(t, &wasmtesting.MockMessageHandler{}, k.messenger)
    32  			},
    33  		},
    34  		"query plugins": {
    35  			srcOpt: WithQueryHandler(&wasmtesting.MockQueryHandler{}),
    36  			verify: func(t *testing.T, k Keeper) {
    37  				assert.IsType(t, &wasmtesting.MockQueryHandler{}, k.wasmVMQueryHandler)
    38  			},
    39  		},
    40  		"message handler decorator": {
    41  			srcOpt: WithMessageHandlerDecorator(func(old Messenger) Messenger {
    42  				require.IsType(t, &MessageHandlerChain{}, old)
    43  				return &wasmtesting.MockMessageHandler{}
    44  			}),
    45  			verify: func(t *testing.T, k Keeper) {
    46  				assert.IsType(t, &wasmtesting.MockMessageHandler{}, k.messenger)
    47  			},
    48  		},
    49  		"query plugins decorator": {
    50  			srcOpt: WithQueryHandlerDecorator(func(old WasmVMQueryHandler) WasmVMQueryHandler {
    51  				require.IsType(t, QueryPlugins{}, old)
    52  				return &wasmtesting.MockQueryHandler{}
    53  			}),
    54  			verify: func(t *testing.T, k Keeper) {
    55  				assert.IsType(t, &wasmtesting.MockQueryHandler{}, k.wasmVMQueryHandler)
    56  			},
    57  		},
    58  		"coin transferrer": {
    59  			srcOpt: WithCoinTransferrer(&wasmtesting.MockCoinTransferrer{}),
    60  			verify: func(t *testing.T, k Keeper) {
    61  				assert.IsType(t, &wasmtesting.MockCoinTransferrer{}, k.bank)
    62  			},
    63  		},
    64  		"costs": {
    65  			srcOpt: WithGasRegister(&wasmtesting.MockGasRegister{}),
    66  			verify: func(t *testing.T, k Keeper) {
    67  				assert.IsType(t, &wasmtesting.MockGasRegister{}, k.gasRegister)
    68  			},
    69  		},
    70  		"api costs": {
    71  			srcOpt: WithAPICosts(1, 2),
    72  			verify: func(t *testing.T, k Keeper) {
    73  				t.Cleanup(setApiDefaults)
    74  				assert.Equal(t, uint64(1), costHumanize)
    75  				assert.Equal(t, uint64(2), costCanonical)
    76  			},
    77  		},
    78  		"max recursion query limit": {
    79  			srcOpt: WithMaxQueryStackSize(1),
    80  			verify: func(t *testing.T, k Keeper) {
    81  				assert.IsType(t, uint32(1), k.maxQueryStackSize)
    82  			},
    83  		},
    84  	}
    85  	for name, spec := range specs {
    86  		t.Run(name, func(t *testing.T) {
    87  			tempDir := t.TempDir()
    88  			t.Cleanup(func() {
    89  				os.RemoveAll(tempDir)
    90  			})
    91  			k := NewKeeper(&cfg.Marshaler, nil, params.NewSubspace(nil, nil, nil, ""), &authkeeper.AccountKeeper{}, nil, nil, nil, nil, nil, nil, nil, tempDir, types.DefaultWasmConfig(), SupportedFeatures, spec.srcOpt)
    92  			spec.verify(t, k)
    93  		})
    94  	}
    95  }
    96  
    97  func setApiDefaults() {
    98  	costHumanize = DefaultGasCostHumanAddress * DefaultGasMultiplier
    99  	costCanonical = DefaultGasCostCanonicalAddress * DefaultGasMultiplier
   100  }