From 5dfb57863868bcfacf67211699ab016f173acbc2 Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Fri, 29 Jan 2021 13:49:52 +0100 Subject: [PATCH] Add custom message handler option --- x/wasm/internal/keeper/options.go | 9 ++++ x/wasm/internal/keeper/options_test.go | 57 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 x/wasm/internal/keeper/options_test.go diff --git a/x/wasm/internal/keeper/options.go b/x/wasm/internal/keeper/options.go index d7fb6b3acc..5e5d6eb607 100644 --- a/x/wasm/internal/keeper/options.go +++ b/x/wasm/internal/keeper/options.go @@ -8,8 +8,17 @@ func (f optsFn) apply(keeper *Keeper) { f(keeper) } +// WithMessageHandler is an optional constructor parameter to replace the default wasm vm engine with the +// given one. func WithWasmEngine(x types.WasmerEngine) Option { return optsFn(func(k *Keeper) { k.wasmer = x }) } + +// WithMessageHandler is an optional constructor parameter to set a custom message handler. +func WithMessageHandler(n messenger) Option { + return optsFn(func(k *Keeper) { + k.messenger = n + }) +} diff --git a/x/wasm/internal/keeper/options_test.go b/x/wasm/internal/keeper/options_test.go new file mode 100644 index 0000000000..2de206c75e --- /dev/null +++ b/x/wasm/internal/keeper/options_test.go @@ -0,0 +1,57 @@ +package keeper + +import ( + "github.com/CosmWasm/wasmd/x/wasm/internal/keeper/wasmtesting" + "github.com/CosmWasm/wasmd/x/wasm/internal/types" + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestConstructorOptions(t *testing.T) { + specs := map[string]struct { + srcOpt Option + verify func(Keeper) + }{ + "wasm engine": { + srcOpt: WithWasmEngine(&wasmtesting.MockWasmer{}), + verify: func(k Keeper) { + assert.IsType(t, k.wasmer, &wasmtesting.MockWasmer{}) + }, + }, + "message handler": { + srcOpt: WithMessageHandler(&wasmtesting.MockMessageHandler{}), + verify: func(k Keeper) { + assert.IsType(t, k.messenger, &wasmtesting.MockMessageHandler{}) + }, + }, + } + for name, spec := range specs { + t.Run(name, func(t *testing.T) { + k := NewKeeper( + nil, + nil, + paramtypes.NewSubspace(nil, nil, nil, nil, ""), + authkeeper.AccountKeeper{}, + nil, + stakingkeeper.Keeper{}, + distributionkeeper.Keeper{}, + nil, + nil, + nil, + nil, + "tempDir", + types.DefaultWasmConfig(), + "", + nil, + nil, + spec.srcOpt, + ) + spec.verify(k) + }) + } + +}