Skip to content

Commit

Permalink
Add custom message handler option
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Jan 29, 2021
1 parent 0f46dee commit 5dfb578
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
9 changes: 9 additions & 0 deletions x/wasm/internal/keeper/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
57 changes: 57 additions & 0 deletions x/wasm/internal/keeper/options_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

}

0 comments on commit 5dfb578

Please sign in to comment.